Skip to content

Commit

Permalink
Merge pull request #29025 from storybookjs/jeppe/fix-default-globals
Browse files Browse the repository at this point in the history
Backgrounds/Viewports: Make defaults overridable in `StoryGlobals`-mode
  • Loading branch information
JReinhold authored Sep 2, 2024
2 parents e2aac62 + ec394c0 commit 0a8cb77
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 31 deletions.
5 changes: 2 additions & 3 deletions code/addons/backgrounds/src/components/Tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import { useGlobals, useParameter } from 'storybook/internal/manager-api';
import { CircleIcon, GridIcon, PhotoIcon, RefreshIcon } from '@storybook/icons';

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

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

const emptyBackgroundMap: BackgroundMap = {};

export const BackgroundTool = memo(function BackgroundSelector() {
const config = useParameter<Config>(KEY);
const [globals, updateGlobals, storyGlobals] = useGlobals();
const [isTooltipVisible, setIsTooltipVisible] = useState(false);

const { options = emptyBackgroundMap, disable = true } = config || {};
const { options = DEFAULT_BACKGROUNDS, disable = true } = config || {};
if (disable) {
return null;
}
Expand Down
7 changes: 6 additions & 1 deletion code/addons/backgrounds/src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
} from 'storybook/internal/types';

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

Expand All @@ -25,7 +26,11 @@ export const withBackgroundAndGrid = (
context: StoryContext<Renderer>
) => {
const { globals, parameters, viewMode, id } = context;
const { options = {}, disable, grid = defaultGrid } = (parameters[KEY] || {}) as Config;
const {
options = DEFAULT_BACKGROUNDS,
disable,
grid = defaultGrid,
} = (parameters[KEY] || {}) as Config;
const data = globals[KEY] || {};
const backgroundName: string | undefined = data.value;

Expand Down
6 changes: 6 additions & 0 deletions code/addons/backgrounds/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { BackgroundMap } from './types';

export const DEFAULT_BACKGROUNDS: BackgroundMap = {
light: { name: 'light', value: '#F8F8F8' },
dark: { name: 'dark', value: '#333' },
};
19 changes: 5 additions & 14 deletions code/addons/backgrounds/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Addon_DecoratorFunction } from 'storybook/internal/types';

import { PARAM_KEY as KEY } from './constants';
import { withBackgroundAndGrid } from './decorator';
import { DEFAULT_BACKGROUNDS } from './defaults';
import { withBackground } from './legacy/withBackgroundLegacy';
import { withGrid } from './legacy/withGridLegacy';
import type { Config, GlobalState } from './types';
Expand All @@ -18,20 +19,10 @@ export const parameters = {
cellAmount: 5,
},
disable: false,
...(FEATURES?.backgroundsStoryGlobals
? {
options: {
light: { name: 'light', value: '#F8F8F8' },
dark: { name: 'dark', value: '#333' },
},
}
: {
// TODO: remove in 9.0
values: [
{ name: 'light', value: '#F8F8F8' },
{ name: 'dark', value: '#333333' },
],
}),
// TODO: remove in 9.0
...(!FEATURES?.backgroundsStoryGlobals && {
values: Object.values(DEFAULT_BACKGROUNDS),
}),
} satisfies Partial<Config>,
};

Expand Down
6 changes: 3 additions & 3 deletions code/addons/viewport/src/components/Tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { Global } from 'storybook/internal/theming';
import { GrowIcon, RefreshIcon, TransferIcon } from '@storybook/icons';

import { PARAM_KEY as KEY } from '../constants';
import { MINIMAL_VIEWPORTS } from '../defaults';
import { responsiveViewport } from '../responsiveViewport';
import { registerShortcuts } from '../shortcuts';
import type { Config, GlobalState, GlobalStateUpdate, Viewport, ViewportMap } from '../types';
import type { Config, GlobalStateUpdate, Viewport, ViewportMap } from '../types';
import {
ActiveViewportLabel,
ActiveViewportSize,
IconButtonLabel,
IconButtonWithLabel,
emptyViewportMap,
iconsMap,
} from '../utils';

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

const { options = emptyViewportMap, disable } = config || {};
const { options = MINIMAL_VIEWPORTS, disable } = config || {};
const data = globals?.[KEY] || {};
const viewportName: string = data.value;
const isRotated: boolean = data.isRotated;
Expand Down
8 changes: 0 additions & 8 deletions code/addons/viewport/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,3 @@ const modern: Record<string, GlobalState> = {
const legacy = { viewport: 'reset', viewportRotated: false };

export const initialGlobals = FEATURES?.viewportStoryGlobals ? modern : legacy;

export const parameters = FEATURES?.viewportStoryGlobals
? {
[KEY]: {
options: MINIMAL_VIEWPORTS,
},
}
: {};
2 changes: 0 additions & 2 deletions code/addons/viewport/src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,3 @@ export const iconsMap: Record<Viewport['type'], React.ReactNode> = {
tablet: <TabletIcon />,
other: <Fragment />,
};

export const emptyViewportMap: ViewportMap = {};

0 comments on commit 0a8cb77

Please sign in to comment.