-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat: add Storybook starter #2381
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fec8004
Add Storybook starter
DustinJSilk e53e6e0
Update Storybook docs with preview command
DustinJSilk edf8ec6
Copy the project vite plugins
DustinJSilk 7e3ba9f
Use storybook v7 and html-vite
DustinJSilk 4a204ba
Fix tsconfig options
DustinJSilk db79b81
Fix storybook.build
DustinJSilk 7aab865
Add required react dependency
DustinJSilk 2b8caf6
fix to working storybook version and remove QWIK_LOADER eval
DustinJSilk feba151
Update integration to include 3rd party library
DustinJSilk 75a130c
Include tips on using storybook with tailwind
DustinJSilk 433ab1f
Add documentation for running with i18n
DustinJSilk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/docs/src/routes/integrations/integration/storybook/index.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
--- | ||
title: Qwik City and Storybook | ||
keywords: storybook, components, preview, testing, docs | ||
contributors: | ||
- DustinJSilk | ||
--- | ||
|
||
# Storybook | ||
|
||
Storybook is a frontend tool for building UI components and pages in isolation. | ||
It helps you develop hard-to-reach states and edge cases without needing to run | ||
the whole app. You can test and preview components in a local Storybook UI or | ||
deploy a preview of Storybook to a server to share with your team. | ||
|
||
## Usage | ||
|
||
You can add Storybook easily by using the following Qwik starter script: | ||
|
||
```shell | ||
npm run qwik add storybook | ||
``` | ||
|
||
> The default configuration for Storybook requires Nodejs v17 or higher | ||
|
||
The previous command will add a folder under `.storybook` that holds the | ||
Storybook configuration. Essential dependencies and scripts are added to your | ||
package.json, and a new component is added to `src/components/story-example` | ||
with a Story to get you started. | ||
|
||
You can now run a preview server with: | ||
|
||
```shell | ||
npm run storybook | ||
``` | ||
|
||
You can add stories to your components by adding a `component.stories.tsx` which | ||
Storybook will pick up on automatically. | ||
|
||
```tsx | ||
import { Meta, StoryObj } from 'storybook-framework-qwik' | ||
import { StoryExample, StoryExampleProps } from './story-example'; | ||
|
||
export default { | ||
title: 'Story Example', | ||
component: StoryExample, | ||
args: { | ||
color: 'red', | ||
}, | ||
argTypes: { | ||
color: { | ||
options: ['red', 'green', 'blue'], | ||
control: { | ||
type: 'select', | ||
}, | ||
}, | ||
}, | ||
} as Meta<StoryExampleProps>; | ||
|
||
export const Default: StoryObj<StoryExampleProps> = {} | ||
``` | ||
|
||
## With tailwindcss | ||
|
||
When using tailwindcss with storybook, avoid adding uneccessary bloat to your | ||
production builds by ignoring tailwind classes that are only used in stories. | ||
|
||
Update your `tailwind.config.js` file to ignore stories when building for | ||
production. | ||
|
||
```JavaScript | ||
module.exports = { | ||
content: [ | ||
'./src/**/*.{js,ts,jsx,tsx,mdx}', | ||
process.env.NODE_ENV === 'production' && '!./src/**/*.stories.{js,ts,jsx,tsx,mdx}', | ||
], | ||
... | ||
} | ||
``` | ||
|
||
If you are building a static version of your storybook, you'll want to keep | ||
any tailwindcss classes found in your `.stories` files. Try adding an | ||
environment variable to your build script and check if it exists when including | ||
your files. | ||
|
||
```JavaScript | ||
process.env.NODE_ENV === 'production' && | ||
!process.env.STORYBOOK_BUILD && | ||
'!./src/**/*.stories.{js,ts,jsx,tsx,mdx}' | ||
``` | ||
|
||
## With i18n | ||
|
||
When using multiple locales with `$localize` from [qwik-i18n](https://github.com/mhevery/qwik-i18n), | ||
you will need to initiate your translations in your `.storybook/preview.ts` file: | ||
|
||
```tsx | ||
import { initTranslations } from '../src/i18n' | ||
|
||
initTranslations() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { StorybookConfig } from '@storybook/builder-vite'; | ||
|
||
const config: StorybookConfig = { | ||
addons: ['@storybook/addon-essentials'], | ||
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'], | ||
framework: { | ||
name: 'storybook-framework-qwik', | ||
}, | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { addons } from '@storybook/addons'; | ||
import { themes } from '@storybook/theming'; | ||
|
||
addons.setConfig({ | ||
theme: themes.dark, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Decorator, Parameters } from 'storybook-framework-qwik'; | ||
import { qwikCityDecorator } from 'storybook-framework-qwik/qwik-city-decorator'; | ||
|
||
import '../src/global.css'; | ||
|
||
export const parameters: Parameters = { | ||
actions: { argTypesRegex: '^on[A-Z].*' }, | ||
controls: { | ||
matchers: { | ||
color: /(background|color)$/i, | ||
date: /Date$/, | ||
}, | ||
}, | ||
options: { | ||
storySort: { | ||
method: 'alphabetical', | ||
}, | ||
}, | ||
}; | ||
|
||
export const decorators: Decorator[] = [qwikCityDecorator]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"rootDir": "../" | ||
}, | ||
"exclude": ["../**/*.spec.ts", "../**/*.test.ts", "../**/*.spec.tsx", "../**/*.test.tsx"], | ||
"include": [ | ||
"../src/**/*.stories.ts", | ||
"../src/**/*.stories.tsx", | ||
"../src/**/*.stories.mdx", | ||
"**/*.ts", | ||
"**/*.tsx", | ||
"../vite.config.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"description": "Use Storybook with your Qwik app", | ||
"devDependencies": { | ||
"@storybook/addon-a11y": "^7.0.0-beta.24", | ||
"@storybook/addon-actions": "^7.0.0-beta.24", | ||
"@storybook/addon-essentials": "^7.0.0-beta.24", | ||
"@storybook/addon-interactions": "^7.0.0-beta.24", | ||
"@storybook/addon-links": "^7.0.0-beta.24", | ||
"@storybook/builder-vite": "^7.0.0-beta.24", | ||
"@storybook/cli": "^7.0.0-beta.24", | ||
"@storybook/html": "^7.0.0-beta.24", | ||
"@storybook/html-vite": "^7.0.0-beta.24", | ||
"@storybook/node-logger": "^7.0.0-beta.24", | ||
"@storybook/testing-library": "^0.0.14-next.1", | ||
"@storybook/theming": "^7.0.0-beta.24", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"storybook": "^7.0.0-beta.24", | ||
"storybook-framework-qwik": "^0.0.8" | ||
}, | ||
"scripts": { | ||
"storybook": "storybook dev -p 6006 -s public", | ||
"storybook.build": "storybook build -s public" | ||
}, | ||
"__qwik__": { | ||
"displayName": "Integration: Storybook", | ||
"priority": -10, | ||
"viteConfig": {}, | ||
"docs": [ | ||
"https://storybook.js.org/docs/react/get-started/introduction", | ||
"https://qwik.builder.io/qwikcity/integrations/storybook/", | ||
"https://github.com/literalpie/storybook-framework-qwik" | ||
] | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
starters/features/storybook/src/components/story-example/story-example.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Meta, StoryObj } from 'storybook-framework-qwik'; | ||
import { StoryExample, StoryExampleProps } from './story-example'; | ||
|
||
export default { | ||
title: 'Story Example', | ||
component: StoryExample, | ||
args: { | ||
color: 'red', | ||
}, | ||
argTypes: { | ||
color: { | ||
options: ['red', 'green', 'blue'], | ||
control: { | ||
type: 'select', | ||
}, | ||
}, | ||
}, | ||
} as Meta<StoryExampleProps>; | ||
|
||
export const Default: StoryObj<StoryExampleProps> = {}; |
26 changes: 26 additions & 0 deletions
26
starters/features/storybook/src/components/story-example/story-example.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { component$, Slot, useStylesScoped$ } from '@builder.io/qwik'; | ||
|
||
export interface StoryExampleProps { | ||
color: 'red' | 'green' | 'blue'; | ||
} | ||
|
||
export const StoryExample = component$((props: StoryExampleProps) => { | ||
useStylesScoped$(` | ||
div { | ||
align-items: center; | ||
border-radius: 50%; | ||
display: flex; | ||
height: 200px; | ||
justify-content: center; | ||
width: 200px; | ||
} | ||
`); | ||
|
||
return ( | ||
<div style={`background-color: ${props.color}`}> | ||
<span> | ||
<Slot /> | ||
</span> | ||
</div> | ||
); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The storybook cli scaffolds
*.mdx
for all frameworks (not*.stories.mdx
). I'm guessing this is becausestories.mdx
is a special pattern that will make the file declare stories rather than just be documentation.Picking up all mdx files can cause conflicts with Qwik-city mdx though, so having some explicit indicator that mdx is for storybook, not qwik city is important (because Qwik-city mdx will only work with Qwik component, and SB mdx will only work with react components).
I think a "storybook" prefix gets the explicit marking, without triggering the special behavior of
stories.mdx
.But, because of some limitations, I'm planning on just doing
*.mdx
in the storybook CLI qwik integration like other frameworks. I'll need to add documentation to warn about the Qwik-city conflict potential. You could do that if you want to match how the sb cli will eventually work (based on my current guess).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
stories.mdx
is the recommended approach to defining stories in mdx files. So this should keep it consistent with what storybook users expect and avoids adding Qwik mdx files as stories. There is of course still the issue of any mdx files in the routes folder being turned into a page but I think thats fine for now as most stories will be on a component level.What is the special behaviour from a
.stories.mdx
file that we're trying to avoid by using.storybook.mdx
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in a
stories.mdx
file, any story you reference will get added to the navigation. If you already declared the story in astories.tsx
file, this will cause duplicates.with any other pattern, you can reference the stories in the mdx without adding them to the side bar.
I suppose it could go either way, but I defer to the default in storybook CLI