-
-
Notifications
You must be signed in to change notification settings - Fork 319
chore: Component library + Storybook #3327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: storybook
Are you sure you want to change the base?
Changes from all commits
bfa1238
458b9b9
7ba21ec
cf6f72a
03ed4dd
518516e
61f8ec3
266e8bf
294911f
73aa921
2466a32
3811ed7
2402964
07a45c7
b784526
43ef846
b30ddb9
d8de18f
4e416c3
57e38be
d0237bc
ebaab6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| node webapp/scripts/updateBranchInfo.mjs | ||
| node webapp/scripts/updateBranchInfo.js | ||
zomp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| node webapp/scripts/updateBranchInfo.mjs | ||
| node webapp/scripts/updateBranchInfo.js |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # copy file to .env.local and fill in the API key to enable Tolgee in Storybook | ||
| VITE_APP_TOLGEE_API_URL=https://app.tolgee.io | ||
| VITE_APP_TOLGEE_API_KEY= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| dist/ | ||
| node_modules/ | ||
| *.local | ||
|
|
||
| *storybook.log | ||
| storybook-static/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /dist/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { StorybookConfig } from '@storybook/react-vite'; | ||
|
|
||
| export default { | ||
| stories: ['../src/**/stories.@(js|jsx|ts|tsx)', '../src/**/*.@(md|mdx)'], | ||
| addons: [ | ||
| '@storybook/addon-docs', | ||
| '@storybook/addon-a11y', | ||
| '@storybook/addon-themes', | ||
| '@tolgee/storybook-addon', | ||
| ], | ||
| framework: { | ||
| name: '@storybook/react-vite', | ||
| options: {}, | ||
| }, | ||
| typescript: { | ||
| check: true, | ||
| reactDocgen: 'react-docgen-typescript', | ||
| }, | ||
| } satisfies StorybookConfig; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { CssBaseline, ThemeProvider } from '@mui/material'; | ||
| import type { Preview } from '@storybook/react-vite'; | ||
| import { withThemeFromJSXProvider } from '@storybook/addon-themes'; | ||
| import { configure } from 'storybook/test'; | ||
| import { withTolgeeProvider } from '@tolgee/storybook-addon'; | ||
| import { MuiLocalizationProvider } from 'lib.components/MuiLocalizationProvider'; | ||
| import { locales } from 'lib.constants/locales'; | ||
|
|
||
| import { getTheme } from '../../webapp/src/ThemeProvider'; // TODO migrate https://github.com/tolgee/tolgee-platform/issues/3326 | ||
| import { branchName } from '../../webapp/src/branch.json'; | ||
|
|
||
| const LANGUAGE = 'en'; | ||
| const FEATURE_TAG = `draft: ${branchName.split('/').pop()}`; | ||
|
|
||
| configure({ testIdAttribute: 'data-cy' }); // instead of data-testid in findByTestId, getAllByTestId... | ||
|
|
||
| const preview: Preview = { | ||
| parameters: { | ||
| controls: { | ||
| matchers: { | ||
| color: /(background|color)$/i, | ||
| date: /Date$/i, | ||
| }, | ||
| }, | ||
| }, | ||
| decorators: [ | ||
| withTolgeeProvider({ | ||
| messageFormat: 'icu', | ||
| locales, | ||
| LocalizationProvider: MuiLocalizationProvider, | ||
| tolgee: { | ||
| language: LANGUAGE, | ||
| fallbackLanguage: LANGUAGE, | ||
| apiUrl: import.meta.env.VITE_APP_TOLGEE_API_URL, | ||
| apiKey: import.meta.env.VITE_APP_TOLGEE_API_KEY, | ||
| staticData: Object.fromEntries( | ||
| Object.entries(locales).map(([k, v]) => [k, v.translations]), | ||
| ), | ||
| tagNewKeys: [FEATURE_TAG], | ||
| }, | ||
| }), | ||
| withThemeFromJSXProvider({ | ||
| GlobalStyles: CssBaseline, | ||
| Provider: ThemeProvider, | ||
| themes: { | ||
| Light: getTheme('light'), | ||
| Dark: getTheme('dark'), | ||
| }, | ||
| defaultTheme: 'Light', | ||
| }), | ||
| ], | ||
| } satisfies Preview; | ||
|
|
||
| export default preview; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # Component/Hook/... Library | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd probably give it a better title
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any suggestion please? 🐱 If there were no Hooks and such things, I would call it Component Library. Design System also is not the thing. Tolgee Library maybe? |
||
|
|
||
| This project is a library of components/hooks/... which are reused across other | ||
| Tolgee applications. This is not an application by itself, but there is a | ||
| Storybook instance integrated to display and let you try the components | ||
| individually or in preset combinations. | ||
|
|
||
| ## Storybook | ||
|
|
||
| [Storybook](https://storybook.js.org/) is a catalog of components. This | ||
| particular instance hosts those of the Tolgee library. | ||
|
|
||
| ### Setup Storybook | ||
|
|
||
| One time Storybook setup. | ||
|
|
||
| Install dependencies of Storybook and the library itself: | ||
|
|
||
| ```shell | ||
| cd library | ||
| npm ci | ||
| ``` | ||
|
|
||
| Duplicate [.env](./.env) as [.env.local](./.env.local) and configure your | ||
| Tolgee API KEY. | ||
|
|
||
| Addon for Tolgee is preset, but its dependencies have to be also installed and | ||
| the addon built to work properly: | ||
|
|
||
| ```shell | ||
| cd storybook-tolgee-addon | ||
| npm ci | ||
| npm run build | ||
| ``` | ||
|
|
||
| ### Open Storybook | ||
|
|
||
| Every time you want to open Storybook run: | ||
|
|
||
| ```shell | ||
| cd library | ||
| npm run storybook | ||
| ``` | ||
|
|
||
| Storybook should open in your browser, otherwise open <http://localhost:6006/>. | ||
|
|
||
| ## Component Migration | ||
|
|
||
| - Apply the boy scout rule – improve names, aliases, subpaths, dependencies... | ||
| - Move the component/hook/... from `webapp/src/component/` directory under `library/src/components/` (note the plural – we are unifying it to plurals everywhere). If the component/hook/... is placed under a subpath, persist it. | ||
| - Keep one component per file. Constants reused elsewhere can be placed under `constants.ts`. Helper functions under `utils.ts`. | ||
| - Up until now, each component was a single file (e.g. `MfaBadge.tsx`), this is changing (not for hooks/...). Please create a directory named like the component (i.e. `MfaBadge`), move the component inside and name it `index.tsx`. This way the component can still be referenced the same path (omit `index.tsx` in the path). | ||
| - Remove the original component/hook/... | ||
| - Update dependencies. Within `webapp` use `@tginternal/library/` prefix (e.g. `@tginternal/library/hooks/useCurrentLanguage`). Within `library` use `lib.` prefix (e.g. `lib.hooks/useCurrentLanguage`). Referencing `webapp` from `library` might not be safe. At the moment, only a few prefixes are predefined in `library`'s [tsconfig.json](./tsconfig.json), [package.json](./package.json) and [vite.config.ts](./vite.config.ts), add more as you need them. | ||
| - Remember the boy scout rule. | ||
|
|
||
| ## Component Documentation | ||
|
|
||
| - Read [writing stories](https://storybook.js.org/docs/writing-stories) Storybook docs. | ||
| - CSF docs: | ||
| - In the component directory, create `stories.ts` file. | ||
| - The component (object of `Meta` type) and its stories (objects of `Story` type) are declared and exported here. | ||
| - Many properties can be set here, but Storybook can derive a lot by itself, so check the defaults at first. Storybook inspects the component's code for prop names, comments... so rather spend time on the component than on the stories (TSDoc comments will bring the value also elsewhere). | ||
| - Many properties can be set on the component level for all stories or on the individual story level. | ||
| - For inspiration have a look on [MfaBadge/stories.ts](./src/components/MfaBadge/stories.ts). | ||
| - For details have a look on [CSF docs](https://storybook.js.org/docs/api/csf). | ||
| - MDX docs: | ||
| - If the autogenerated CSF documentation is not enough (e.g. additional formatted texts are needed), create `stories.mdx` file next to `stories.ts`. | ||
| - You have a free hand in writing arbitrary Markdown texts here and inserting stories from `stories.ts` to them. Please follow CSF structure and other MDX files. | ||
| - For inspiration have a look on [FlagImage/stories.mdx](./src/components/languages/FlagImage/stories.mdx). | ||
| - For details have a look on [MDX docs](https://storybook.js.org/docs/writing-docs/mdx). | ||
Uh oh!
There was an error while loading. Please reload this page.