|
| 1 | +import type { Preview } from '@storybook-vue/nuxt' |
| 2 | +import { withThemeByDataAttribute } from '@storybook/addon-themes' |
| 3 | +import { currentLocales } from '../config/i18n' |
| 4 | +import { fn } from 'storybook/test' |
| 5 | +import { ACCENT_COLORS } from '../shared/utils/constants' |
| 6 | + |
| 7 | +// related: https://github.com/npmx-dev/npmx.dev/blob/1431d24be555bca5e1ae6264434d49ca15173c43/test/nuxt/setup.ts#L12-L26 |
| 8 | +// Stub Nuxt specific globals |
| 9 | +// @ts-expect-error - dynamic global name |
| 10 | +globalThis['__NUXT_COLOR_MODE__'] ??= { |
| 11 | + preference: 'system', |
| 12 | + value: 'dark', |
| 13 | + getColorScheme: fn(() => 'dark'), |
| 14 | + addColorScheme: fn(), |
| 15 | + removeColorScheme: fn(), |
| 16 | +} |
| 17 | +// @ts-expect-error - dynamic global name |
| 18 | +globalThis.defineOgImageComponent = fn() |
| 19 | + |
| 20 | +const preview: Preview = { |
| 21 | + parameters: { |
| 22 | + controls: { |
| 23 | + matchers: { |
| 24 | + color: /(background|color)$/i, |
| 25 | + date: /Date$/i, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + // Provides toolbars to switch things like theming and language |
| 30 | + globalTypes: { |
| 31 | + locale: { |
| 32 | + name: 'Locale', |
| 33 | + description: 'UI language', |
| 34 | + defaultValue: 'en-US', |
| 35 | + toolbar: { |
| 36 | + icon: 'globe', |
| 37 | + dynamicTitle: true, |
| 38 | + items: [ |
| 39 | + // English is at the top so it's easier to reset to it |
| 40 | + { value: 'en-US', title: 'English (US)' }, |
| 41 | + ...currentLocales |
| 42 | + .filter(locale => locale.code !== 'en-US') |
| 43 | + .map(locale => ({ value: locale.code, title: locale.name })), |
| 44 | + ], |
| 45 | + }, |
| 46 | + }, |
| 47 | + accentColor: { |
| 48 | + name: 'Accent Color', |
| 49 | + description: 'Accent color', |
| 50 | + toolbar: { |
| 51 | + icon: 'paintbrush', |
| 52 | + dynamicTitle: true, |
| 53 | + items: [ |
| 54 | + ...Object.keys(ACCENT_COLORS.light).map(color => ({ |
| 55 | + value: color, |
| 56 | + title: color.charAt(0).toUpperCase() + color.slice(1), |
| 57 | + })), |
| 58 | + { value: undefined, title: 'No Accent' }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + decorators: [ |
| 64 | + withThemeByDataAttribute({ |
| 65 | + themes: { |
| 66 | + Light: 'light', |
| 67 | + Dark: 'dark', |
| 68 | + }, |
| 69 | + defaultTheme: 'Dark', |
| 70 | + attributeName: 'data-theme', |
| 71 | + }), |
| 72 | + (story, context) => { |
| 73 | + const { locale, accentColor } = context.globals as { |
| 74 | + locale: string |
| 75 | + accentColor?: string |
| 76 | + } |
| 77 | + |
| 78 | + // Set accent color from globals |
| 79 | + if (accentColor) { |
| 80 | + document.documentElement.style.setProperty('--accent-color', `var(--swatch-${accentColor})`) |
| 81 | + } else { |
| 82 | + document.documentElement.style.removeProperty('--accent-color') |
| 83 | + } |
| 84 | + |
| 85 | + return { |
| 86 | + template: '<story />', |
| 87 | + // Set locale from globals |
| 88 | + created() { |
| 89 | + if (this.$i18n) { |
| 90 | + this.$i18n.setLocale(locale) |
| 91 | + } |
| 92 | + }, |
| 93 | + updated() { |
| 94 | + if (this.$i18n) { |
| 95 | + this.$i18n.setLocale(locale) |
| 96 | + } |
| 97 | + }, |
| 98 | + } |
| 99 | + }, |
| 100 | + ], |
| 101 | +} |
| 102 | + |
| 103 | +export default preview |
0 commit comments