Skip to content

Commit 0a8cb77

Browse files
authored
Merge pull request #29025 from storybookjs/jeppe/fix-default-globals
Backgrounds/Viewports: Make defaults overridable in `StoryGlobals`-mode
2 parents e2aac62 + ec394c0 commit 0a8cb77

File tree

7 files changed

+22
-31
lines changed

7 files changed

+22
-31
lines changed

code/addons/backgrounds/src/components/Tool.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ import { useGlobals, useParameter } from 'storybook/internal/manager-api';
66
import { CircleIcon, GridIcon, PhotoIcon, RefreshIcon } from '@storybook/icons';
77

88
import { PARAM_KEY as KEY } from '../constants';
9+
import { DEFAULT_BACKGROUNDS } from '../defaults';
910
import type { Background, BackgroundMap, Config, GlobalStateUpdate } from '../types';
1011

1112
type Link = Parameters<typeof TooltipLinkList>['0']['links'][0];
1213

13-
const emptyBackgroundMap: BackgroundMap = {};
14-
1514
export const BackgroundTool = memo(function BackgroundSelector() {
1615
const config = useParameter<Config>(KEY);
1716
const [globals, updateGlobals, storyGlobals] = useGlobals();
1817
const [isTooltipVisible, setIsTooltipVisible] = useState(false);
1918

20-
const { options = emptyBackgroundMap, disable = true } = config || {};
19+
const { options = DEFAULT_BACKGROUNDS, disable = true } = config || {};
2120
if (disable) {
2221
return null;
2322
}

code/addons/backgrounds/src/decorator.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
} from 'storybook/internal/types';
77

88
import { PARAM_KEY as KEY } from './constants';
9+
import { DEFAULT_BACKGROUNDS } from './defaults';
910
import type { Config, GridConfig } from './types';
1011
import { addBackgroundStyle, addGridStyle, clearStyles, isReduceMotionEnabled } from './utils';
1112

@@ -25,7 +26,11 @@ export const withBackgroundAndGrid = (
2526
context: StoryContext<Renderer>
2627
) => {
2728
const { globals, parameters, viewMode, id } = context;
28-
const { options = {}, disable, grid = defaultGrid } = (parameters[KEY] || {}) as Config;
29+
const {
30+
options = DEFAULT_BACKGROUNDS,
31+
disable,
32+
grid = defaultGrid,
33+
} = (parameters[KEY] || {}) as Config;
2934
const data = globals[KEY] || {};
3035
const backgroundName: string | undefined = data.value;
3136

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { BackgroundMap } from './types';
2+
3+
export const DEFAULT_BACKGROUNDS: BackgroundMap = {
4+
light: { name: 'light', value: '#F8F8F8' },
5+
dark: { name: 'dark', value: '#333' },
6+
};

code/addons/backgrounds/src/preview.ts

+5-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Addon_DecoratorFunction } from 'storybook/internal/types';
22

33
import { PARAM_KEY as KEY } from './constants';
44
import { withBackgroundAndGrid } from './decorator';
5+
import { DEFAULT_BACKGROUNDS } from './defaults';
56
import { withBackground } from './legacy/withBackgroundLegacy';
67
import { withGrid } from './legacy/withGridLegacy';
78
import type { Config, GlobalState } from './types';
@@ -18,20 +19,10 @@ export const parameters = {
1819
cellAmount: 5,
1920
},
2021
disable: false,
21-
...(FEATURES?.backgroundsStoryGlobals
22-
? {
23-
options: {
24-
light: { name: 'light', value: '#F8F8F8' },
25-
dark: { name: 'dark', value: '#333' },
26-
},
27-
}
28-
: {
29-
// TODO: remove in 9.0
30-
values: [
31-
{ name: 'light', value: '#F8F8F8' },
32-
{ name: 'dark', value: '#333333' },
33-
],
34-
}),
22+
// TODO: remove in 9.0
23+
...(!FEATURES?.backgroundsStoryGlobals && {
24+
values: Object.values(DEFAULT_BACKGROUNDS),
25+
}),
3526
} satisfies Partial<Config>,
3627
};
3728

code/addons/viewport/src/components/Tool.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { Global } from 'storybook/internal/theming';
77
import { GrowIcon, RefreshIcon, TransferIcon } from '@storybook/icons';
88

99
import { PARAM_KEY as KEY } from '../constants';
10+
import { MINIMAL_VIEWPORTS } from '../defaults';
1011
import { responsiveViewport } from '../responsiveViewport';
1112
import { registerShortcuts } from '../shortcuts';
12-
import type { Config, GlobalState, GlobalStateUpdate, Viewport, ViewportMap } from '../types';
13+
import type { Config, GlobalStateUpdate, Viewport, ViewportMap } from '../types';
1314
import {
1415
ActiveViewportLabel,
1516
ActiveViewportSize,
1617
IconButtonLabel,
1718
IconButtonWithLabel,
18-
emptyViewportMap,
1919
iconsMap,
2020
} from '../utils';
2121

@@ -39,7 +39,7 @@ export const ViewportTool: FC<{ api: API }> = ({ api }) => {
3939
const [globals, updateGlobals, storyGlobals] = useGlobals();
4040
const [isTooltipVisible, setIsTooltipVisible] = useState(false);
4141

42-
const { options = emptyViewportMap, disable } = config || {};
42+
const { options = MINIMAL_VIEWPORTS, disable } = config || {};
4343
const data = globals?.[KEY] || {};
4444
const viewportName: string = data.value;
4545
const isRotated: boolean = data.isRotated;

code/addons/viewport/src/preview.ts

-8
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,3 @@ const modern: Record<string, GlobalState> = {
1010
const legacy = { viewport: 'reset', viewportRotated: false };
1111

1212
export const initialGlobals = FEATURES?.viewportStoryGlobals ? modern : legacy;
13-
14-
export const parameters = FEATURES?.viewportStoryGlobals
15-
? {
16-
[KEY]: {
17-
options: MINIMAL_VIEWPORTS,
18-
},
19-
}
20-
: {};

code/addons/viewport/src/utils.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,3 @@ export const iconsMap: Record<Viewport['type'], React.ReactNode> = {
4242
tablet: <TabletIcon />,
4343
other: <Fragment />,
4444
};
45-
46-
export const emptyViewportMap: ViewportMap = {};

0 commit comments

Comments
 (0)