Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon-measure: Migrate to strict TS #22402

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion code/addons/measure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"@storybook/global": "^5.0.0",
"@storybook/manager-api": "7.1.0-alpha.29",
"@storybook/preview-api": "7.1.0-alpha.29",
"@storybook/types": "7.1.0-alpha.29"
"@storybook/types": "7.1.0-alpha.29",
"tiny-invariant": "^1.3.1"
},
"devDependencies": {
"typescript": "~4.9.3"
Expand Down
10 changes: 7 additions & 3 deletions code/addons/measure/src/box-model/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-param-reassign */
import { global } from '@storybook/global';
import invariant from 'tiny-invariant';

interface Size {
width: number;
Expand All @@ -25,6 +26,7 @@ function createCanvas(): CanvasState {
const canvas = global.document.createElement('canvas');
canvas.id = 'storybook-addon-measure';
const context = canvas.getContext('2d');
invariant(context != null);
// Set canvas width & height
const { width, height } = getDocumentWidthAndHeight();
setCanvasWidthAndHeight(canvas, context, { width, height });
Expand Down Expand Up @@ -67,16 +69,18 @@ export function init() {

export function clear() {
if (state.context) {
state.context.clearRect(0, 0, state.width, state.height);
state.context.clearRect(0, 0, state.width ?? 0, state.height ?? 0);
}
}

export function draw(callback: (context: CanvasRenderingContext2D) => void) {
export function draw(callback: (context?: CanvasRenderingContext2D) => void) {
clear();
callback(state.context);
}

export function rescale() {
invariant(state.canvas, 'Canvas should exist in the state.');
invariant(state.context, 'Context should exist in the state.');
// First reset so that the canvas size doesn't impact the container size
setCanvasWidthAndHeight(state.canvas, state.context, { width: 0, height: 0 });

Expand All @@ -91,7 +95,7 @@ export function rescale() {
export function destroy() {
if (state.canvas) {
clear();
state.canvas.parentNode.removeChild(state.canvas);
state.canvas.parentNode?.removeChild(state.canvas);
state = {};
}
}
2 changes: 1 addition & 1 deletion code/addons/measure/src/box-model/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export function labelStacks(
acc[l.position] = [];
}

acc[l.position].push(l);
acc[l.position]?.push(l);

return acc;
}, {});
Expand Down
2 changes: 1 addition & 1 deletion code/addons/measure/src/box-model/visualizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function drawContent(
}

function drawBoxModel(element: HTMLElement) {
return (context: CanvasRenderingContext2D) => {
return (context?: CanvasRenderingContext2D) => {
if (element && context) {
const measurements = measureElement(element);

Expand Down
2 changes: 1 addition & 1 deletion code/addons/measure/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"extends": "../../tsconfig.json",
"include": ["src/**/*"],
"compilerOptions": {
"strict": false
"strict": true
}
}
1 change: 1 addition & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5555,6 +5555,7 @@ __metadata:
"@storybook/manager-api": 7.1.0-alpha.29
"@storybook/preview-api": 7.1.0-alpha.29
"@storybook/types": 7.1.0-alpha.29
tiny-invariant: ^1.3.1
typescript: ~4.9.3
peerDependencies:
react: ^16.8.0 || ^17.0.0 || ^18.0.0
Expand Down