Skip to content

Commit a80a5af

Browse files
Sidnioulzstorybook-bot
authored andcommitted
Merge pull request #34985 from TheSeydiCharyyev/fix/issue-34951-partial-globals
CSF: Allow partial globals overrides in story and meta annotations (cherry picked from commit 17ae360)
1 parent 5b929ca commit a80a5af

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

‎code/core/src/csf/csf-factories.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@ test('addon parameters are inferred', () => {
4747
});
4848
});
4949

50+
interface GlobalsAddonTypes {
51+
globals: { theme: 'light' | 'dark'; locale: 'en' | 'fr' };
52+
}
53+
54+
const globalsAddon = definePreviewAddon<GlobalsAddonTypes>({});
55+
const globalsPreview = definePreview({ addons: [globalsAddon], renderToCanvas: () => {} });
56+
const globalsMeta = globalsPreview.type<{ args: { label: string } }>().meta({
57+
args: { label: 'foo' },
58+
render: ({ label }) => 'hello' + label,
59+
});
60+
61+
test('globals overrides may be partial, mirroring args', () => {
62+
// A story may override a subset of the typed globals (here `theme` without `locale`).
63+
globalsMeta.story({ globals: { theme: 'dark' } });
64+
65+
// The same applies to component-level (meta) overrides.
66+
globalsPreview.type<{ args: { label: string } }>().meta({
67+
args: { label: 'foo' },
68+
render: ({ label }) => 'hello' + label,
69+
globals: { locale: 'fr' },
70+
});
71+
72+
// Only the keys become optional — values stay type-checked.
73+
globalsPreview.type<{ args: { label: string } }>().meta({
74+
args: { label: 'foo' },
75+
render: ({ label }) => 'hello' + label,
76+
// @ts-expect-error theme must be 'light' | 'dark'
77+
globals: { theme: 'blue' },
78+
});
79+
});
80+
5081
describe('test function', () => {
5182
test('without overrides', async () => {
5283
const MyStory = meta.story({ args: { label: 'foo' } });

‎code/core/src/csf/story.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,10 @@ export interface ComponentAnnotations<
536536
play?: PlayFunction<TRenderer, TArgs>;
537537

538538
/** Override the globals values for all stories in this component */
539-
globals?: Globals &
540-
(TRenderer['csf4'] extends true ? CoreTypes['globals'] & TRenderer['globals'] : unknown);
539+
globals?: Partial<
540+
Globals &
541+
(TRenderer['csf4'] extends true ? CoreTypes['globals'] & TRenderer['globals'] : unknown)
542+
>;
541543
}
542544

543545
export type StoryAnnotations<
@@ -555,8 +557,10 @@ export type StoryAnnotations<
555557
play?: PlayFunction<TRenderer, TArgs>;
556558

557559
/** Override the globals values for this story */
558-
globals?: Globals &
559-
(TRenderer['csf4'] extends true ? CoreTypes['globals'] & TRenderer['globals'] : unknown);
560+
globals?: Partial<
561+
Globals &
562+
(TRenderer['csf4'] extends true ? CoreTypes['globals'] & TRenderer['globals'] : unknown)
563+
>;
560564

561565
/** @deprecated */
562566
story?: Omit<StoryAnnotations<TRenderer, TArgs>, 'story'>;

0 commit comments

Comments
 (0)