|
| 1 | +import { applicationConfig, Preview } from '@storybook/angular'; |
| 2 | +import { setCompodocJson } from '@storybook/addon-docs/angular'; |
| 3 | + |
| 4 | +import docJson from '../documentation.json'; |
| 5 | + |
| 6 | +import '!style-loader!css-loader!postcss-loader!sass-loader!../src/styles.scss'; |
| 7 | + |
| 8 | +import { provideAnimations } from '@angular/platform-browser/animations'; |
| 9 | + |
| 10 | +setCompodocJson(docJson); |
| 11 | + |
| 12 | +const THEMES = { |
| 13 | + 'ek5-light-primary': 'assets/themes/ek5-light-primary/theme.css', |
| 14 | + 'ek5-dark-primary': 'assets/themes/ek5-dark-primary/theme.css', |
| 15 | + 'ek5-light-secondary': 'assets/themes/ek5-light-secondary/theme.css', |
| 16 | + 'ek5-dark-secondary': 'assets/themes/ek5-dark-secondary/theme.css', |
| 17 | + 'brand-light-primary': 'assets/themes/brand-light-primary/theme.css', |
| 18 | + 'brand-dark-primary': 'assets/themes/brand-dark-primary/theme.css', |
| 19 | + 'brand-light-secondary': 'assets/themes/brand-light-secondary/theme.css', |
| 20 | + 'brand-dark-secondary': 'assets/themes/brand-dark-secondary/theme.css', |
| 21 | +}; |
| 22 | + |
| 23 | +// функция переключения темы |
| 24 | +function setTheme(theme: keyof typeof THEMES) { |
| 25 | + const id = 'storybook-theme'; |
| 26 | + let linkTag = document.getElementById(id) as HTMLLinkElement | null; |
| 27 | + |
| 28 | + if (!linkTag) { |
| 29 | + linkTag = document.createElement('link'); |
| 30 | + linkTag.id = id; |
| 31 | + linkTag.rel = 'stylesheet'; |
| 32 | + document.head.appendChild(linkTag); |
| 33 | + } |
| 34 | + |
| 35 | + linkTag.href = THEMES[theme]; |
| 36 | +} |
| 37 | + |
| 38 | +const preview: Preview = { |
| 39 | + decorators: [ |
| 40 | + applicationConfig({ |
| 41 | + providers: [provideAnimations()], |
| 42 | + }), |
| 43 | + (storyFn, context) => { |
| 44 | + const theme = context.globals?.['theme'] || 'ek5-light-primary'; |
| 45 | + |
| 46 | + // Логика подмены <link> |
| 47 | + setTheme(theme as keyof typeof THEMES); |
| 48 | + |
| 49 | + return storyFn(); |
| 50 | + }, |
| 51 | + ], |
| 52 | + parameters: { |
| 53 | + controls: { |
| 54 | + matchers: { |
| 55 | + color: /(background|color)$/i, |
| 56 | + date: /Date$/i, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + globalTypes: { |
| 61 | + theme: { |
| 62 | + name: 'Theme', |
| 63 | + description: 'Global theme for components', |
| 64 | + defaultValue: 'ek5-light-primary', |
| 65 | + toolbar: { |
| 66 | + icon: 'paintbrush', |
| 67 | + dynamicTitle: true, |
| 68 | + items: [ |
| 69 | + { value: 'ek5-light-primary', title: 'ek5-light-primary' }, |
| 70 | + { value: 'ek5-dark-primary', title: 'ek5-dark-primary' }, |
| 71 | + { value: 'ek5-light-secondary', title: 'ek5-light-secondary' }, |
| 72 | + { value: 'ek5-dark-secondary', title: 'ek5-dark-secondary' }, |
| 73 | + { value: 'brand-light-primary', title: 'brand-light-primary' }, |
| 74 | + { value: 'brand-dark-primary', title: 'brand-dark-primary' }, |
| 75 | + { value: 'brand-light-secondary', title: 'brand-light-secondary' }, |
| 76 | + { value: 'brand-dark-secondary', title: 'brand-dark-secondary' }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | +}; |
| 82 | + |
| 83 | +export default preview; |
0 commit comments