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

Migrate @storybook/addon-viewbook to strict ts #22339

Merged
merged 2 commits into from
May 3, 2023
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
39 changes: 15 additions & 24 deletions code/addons/viewport/src/Tool.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-fallthrough */
import type { ReactNode, FC } from 'react';
import React, { useState, Fragment, useEffect, useRef, memo } from 'react';
import memoize from 'memoizerific';
Expand Down Expand Up @@ -37,25 +36,16 @@ const baseViewports: ViewportItem[] = [responsiveViewport];

const toLinks = memoize(50)((list: ViewportItem[], active: LinkBase, set, state, close): Link[] => {
return list
.filter((i) => i.id !== responsiveViewport.id || active.id !== i.id)
.map((i) => {
switch (i.id) {
case responsiveViewport.id: {
if (active.id === i.id) {
return null;
}
}
default: {
return {
...i,
onClick: () => {
set({ ...state, selected: i.id });
close();
},
};
}
}
})
.filter(Boolean);
return {
...i,
onClick: () => {
set({ ...state, selected: i.id });
close();
},
};
});
1234tgk marked this conversation as resolved.
Show resolved Hide resolved
});

const wrapperId = 'storybook-preview-wrapper';
Expand Down Expand Up @@ -112,12 +102,12 @@ interface ViewportToolState {
}

const getStyles = (
prevStyles: ViewportStyles,
prevStyles: ViewportStyles | undefined,
styles: Styles,
isRotated: boolean
): ViewportStyles => {
if (styles === null) {
return null;
): ViewportStyles | undefined => {
if (!styles || !prevStyles) {
return undefined;
}
const result = typeof styles === 'function' ? styles(prevStyles) : styles;
return isRotated ? flip(result) : result;
Expand Down Expand Up @@ -154,7 +144,8 @@ export const ViewportTool: FC = memo(
useEffect(() => {
setState({
selected:
defaultViewport || (viewports[state.selected] ? state.selected : responsiveViewport.id),
defaultViewport ||
(state.selected && viewports[state.selected] ? state.selected : responsiveViewport.id),
isRotated: defaultOrientation === 'landscape',
});
}, [defaultOrientation, defaultViewport]);
Expand Down
2 changes: 1 addition & 1 deletion code/addons/viewport/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
}
}