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