From 5b674b46841155762a6e5f9ebd566a0110279384 Mon Sep 17 00:00:00 2001 From: castastrophe Date: Mon, 22 Jul 2024 12:16:54 -0400 Subject: [PATCH] feat: use globals to enable VRT modes in chromatic --- projects/story-decorator/decorator.ts | 61 ++++++++++++++---- storybook/preview.js | 91 +++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 12 deletions(-) diff --git a/projects/story-decorator/decorator.ts b/projects/story-decorator/decorator.ts index b79f4b50ea..2a18cbc307 100644 --- a/projects/story-decorator/decorator.ts +++ b/projects/story-decorator/decorator.ts @@ -28,16 +28,53 @@ export const themeStyles = html` `; -export const swcThemeDecorator = (story: () => TemplateResult) => { - requestAnimationFrame(() => { - const decorator = document.querySelector( - 'sp-story-decorator' - ) as HTMLElement; - render(story(), decorator); - }); +export const swcThemeDecoratorWithConfig = + ({ bundled } = { bundled: true }) => + ( + story: () => TemplateResult, + context: import('@storybook/csf').StoryContext + ) => { + if (!bundled) { + requestAnimationFrame(() => { + document.documentElement.setAttribute('lang', 'en'); + const decorator = document.querySelector( + 'sp-story-decorator' + ) as HTMLElement; + render(story(), decorator); + }); + } + + let hideNavStyles; + // If the global settings exist, hide the bottom toolbar + if ( + context?.globals?.system || + context?.globals?.color || + context?.globals?.scale || + context?.globals?.textDirection || + context?.globals?.reduceMotion + ) { + hideNavStyles = html` + + `; + } + + return html` + ${themeStyles} ${hideNavStyles} + + ${bundled ? story() : html``} + + `; + }; - return html` - ${themeStyles} - - `; -}; +export const swcThemeDecorator = swcThemeDecoratorWithConfig(); diff --git a/storybook/preview.js b/storybook/preview.js index b8bdf197a2..fa68943b8e 100644 --- a/storybook/preview.js +++ b/storybook/preview.js @@ -15,6 +15,82 @@ import cem from './custom-elements.json'; setCustomElementsManifest(cem); +export const globalTypes = { + system: { + title: 'Design context', + description: 'The variation of Spectrum to use in the component', + defaultValue: 'spectrum', + type: 'string', + showName: true, + toolbar: { + items: [ + { value: 'spectrum', title: 'Spectrum 2', right: 'default' }, + { value: 'legacy', title: 'Spectrum 1', right: 'legacy' }, + { value: 'express', title: 'Express' }, + ], + dynamicTitle: true, + }, + }, + color: { + title: 'Color', + description: 'Controls the color context of the component', + defaultValue: 'light', + icon: 'paintbrush', + type: 'string', + toolbar: { + items: [ + { value: 'light', title: 'Light', right: 'default' }, + { value: 'dark', title: 'Dark' }, + ], + dynamicTitle: true, + }, + }, + scale: { + title: 'Platform scale', + description: 'Controls the platform scale of the component', + defaultValue: 'medium', + type: 'string', + toolbar: { + items: [ + { + value: 'medium', + title: 'Medium', + right: 'default', + icon: 'browser', + }, + { value: 'large', title: 'Large', icon: 'mobile' }, + ], + dynamicTitle: true, + }, + }, + textDirection: { + title: 'Text direction', + description: 'Direction of the content flow', + defaultValue: 'ltr', + type: 'string', + toolbar: { + items: [ + { value: 'ltr', title: 'Left to right' }, + { value: 'rtl', title: 'Right to left' }, + ], + dynamicTitle: true, + }, + }, + reducedMotion: { + title: 'Reduce motion', + description: 'Reduce animation and transitions', + defaultValue: false, + type: 'boolean', + toolbar: { + items: [ + { value: false, title: 'Default', icon: 'play' }, + { value: true, title: 'Reduced motion', icon: 'stop' }, + ], + dynamicTitle: true, + }, + }, +}; + export const parameters = { docs: { hidden: true }, controls: { expanded: true }, @@ -33,6 +109,21 @@ export const parameters = { forcedColors: 'none', prefersReducedMotion: 'no-preference', pauseAnimationAtEnd: true, + modes: { + 'Context: Spectrum 1': { + scale: 'medium', + color: 'light', + textDirection: 'ltr', + context: 'spectrum1', + }, + 'Context: Express': { + context: 'express', + }, + 'Dark | RTL': { + color: 'dark', + textDirection: 'rtl', + }, + }, }, };