Skip to content

Commit

Permalink
docs: storybook styling revisited
Browse files Browse the repository at this point in the history
  • Loading branch information
meza committed Mar 9, 2023
1 parent cba0932 commit f352328
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 64 deletions.
4 changes: 2 additions & 2 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import { DefinePlugin } from 'webpack';
import { StorybookConfig } from '@storybook/react-webpack5';

const storybookConfig: StorybookConfig = {
stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
'@storybook/addon-a11y',
'@storybook/addon-links',
'@meza/storybook-react-i18next',
'@storybook/addon-essentials',
'@storybook/addon-interactions',
{
Expand Down
6 changes: 2 additions & 4 deletions .storybook/manager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { addons } from '@storybook/manager-api';
import { theme } from './theme';
import theme from './theme';

addons.setConfig({
theme: theme,
theme: theme
});

export default theme;
37 changes: 0 additions & 37 deletions .storybook/preview.ts

This file was deleted.

88 changes: 88 additions & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import i18n from '../testUtils/i18nextForStorybook';
import React, { useEffect } from 'react';
import { ColorMode } from '~/components/ColorModeSwitcher';
import { StoryContext, StoryFn } from '@storybook/react';
import '../src/styles/app.css';
import './storybook.css';
import { I18nextProvider } from 'react-i18next';
import theme from './theme';

const withAllTheThings = (Story: StoryFn, context: StoryContext) => {
const { colorMode } = context.globals;

const setMode = (mode: ColorMode) => {
const cl = document.firstElementChild?.classList;
document.body.classList.remove(ColorMode.LIGHT, ColorMode.DARK);
if (cl) {
cl.remove(ColorMode.LIGHT, ColorMode.DARK);
cl.add(mode);
}
};

useEffect(() => {
setMode(colorMode);
}, [colorMode]);

return (
<I18nextProvider i18n={i18n}>
<Story/>
</I18nextProvider>
);
};

export const decorators = [
withAllTheThings
];

/** @type { import('@storybook/react').Preview } */
export const parameters = {
docs: {
theme: theme,
canvas: {
background: false,
className: 'storybook-canvas'
},
story: {
inline: true
}
},
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/
}
},
dependencies: {
// display only dependencies/dependents that have a story in storybook
// by default this is false
withStoriesOnly: true,

// completely hide a dependency/dependents block if it has no elements
// by default this is false
hideEmpty: true
},
backgrounds: { disable: true },
locale: 'en',
locales: {
en: 'English'
}
};

export const globalTypes = {
colorMode: {
name: 'ColorMode',
description: 'Color Mode',
defaultValue: 'dark',
toolbar: {
items: [
{ value: 'dark', title: 'Dark', icon: 'moon' },
{ value: 'light', title: 'Light', icon: 'sun' }
],
showName: true,
dynamicTitle: true
}
}
};


5 changes: 5 additions & 0 deletions .storybook/storybook.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** This is necessary to override the default canvas styles */
.storybook-canvas {
background-color: var(--color-secondary) !important;
color: var(--color-primary) !important;
}
7 changes: 2 additions & 5 deletions .storybook/theme.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { create } from '@storybook/theming/create';

export const theme = create({
export default create({
base: 'dark',
brandTitle: 'Storybook',
brandUrl: 'https://storybook.com',
brandImage: 'https://placekitten.com/350/150',
brandTarget: '_self',
colorPrimary: '#ED117D',
brandTarget: '_self'
});

export default theme;
34 changes: 18 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,30 @@
"@jscpd/badge-reporter": "^3.4.5",
"@jscpd/html-reporter": "^3.5.3",
"@meza/adr-tools": "^1.0.10",
"@meza/storybook-react-i18next": "latest",
"@playwright/test": "^1.30.0",
"@remix-run/dev": "^1.13.0",
"@remix-run/eslint-config": "^1.13.0",
"@remix-run/serve": "^1.13.0",
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/github": "^8.0.7",
"@semantic-release/release-notes-generator": "^10.0.3",
"@storybook/addon-a11y": "^7.0.0-beta.62",
"@storybook/addon-actions": "^7.0.0-beta.62",
"@storybook/addon-docs": "^7.0.0-beta.62",
"@storybook/addon-essentials": "^7.0.0-beta.62",
"@storybook/addon-interactions": "^7.0.0-beta.62",
"@storybook/addon-links": "^7.0.0-beta.62",
"@storybook/addons": "^7.0.0-beta.62",
"@storybook/api": "^7.0.0-beta.62",
"@storybook/blocks": "^7.0.0-beta.62",
"@storybook/components": "^7.0.0-beta.62",
"@storybook/core-events": "^7.0.0-beta.62",
"@storybook/react": "^7.0.0-beta.62",
"@storybook/react-webpack5": "^7.0.0-beta.62",
"@storybook/addon-a11y": "^7.0.0-rc.0",
"@storybook/addon-actions": "^7.0.0-rc.0",
"@storybook/addon-controls": "^7.0.0-rc.0",
"@storybook/addon-docs": "^7.0.0-rc.0",
"@storybook/addon-essentials": "^7.0.0-rc.0",
"@storybook/addon-interactions": "^7.0.0-rc.0",
"@storybook/addon-links": "^7.0.0-rc.0",
"@storybook/addons": "^7.0.0-rc.0",
"@storybook/api": "^7.0.0-rc.0",
"@storybook/blocks": "^7.0.0-rc.0",
"@storybook/components": "^7.0.0-rc.0",
"@storybook/core-events": "^7.0.0-rc.0",
"@storybook/manager-api": "^7.0.0-rc.0",
"@storybook/react": "^7.0.0-rc.0",
"@storybook/react-webpack5": "^7.0.0-rc.0",
"@storybook/testing-library": "^0.0.14-next.1",
"@storybook/theming": "^7.0.0-beta.62",
"@storybook/theming": "^7.0.0-rc.0",
"@testing-library/dom": "^9.0.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
Expand Down Expand Up @@ -182,8 +183,9 @@
"rimraf": "^4.3.0",
"semantic-release": "^20.1.0",
"semantic-release-export-data": "^1.0.1",
"storybook": "^7.0.0-beta.62",
"storybook": "^7.0.0-rc.0",
"storybook-addon-themes": "^6.1.0",
"storybook-dark-mode-v7": "^3.0.0-alpha.0",
"stylelint": "^15.1.0",
"stylelint-config-standard": "^30.0.1",
"stylelint-order": "^6.0.2",
Expand Down
23 changes: 23 additions & 0 deletions testUtils/i18nextForStorybook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import en from '../public/locales/en/translation.json';

i18n
.use(initReactI18next)

.init({
lng: 'en',
fallbackLng: 'en',
ns: ['translation'],
defaultNS: 'translation',
interpolation: {
escapeValue: false
},
resources: {
en: {
translation: en // no translations mean we can test and snapshot the translation keys used
}
}
});

export default i18n;

0 comments on commit f352328

Please sign in to comment.