Skip to content

Commit

Permalink
Merge pull request #21797 from storybookjs/docs/styling-and-css
Browse files Browse the repository at this point in the history
Update styling & CSS doc to mention styling addon
  • Loading branch information
jonniebigodes authored Apr 19, 2023
2 parents ac64c1d + 7316204 commit a90af29
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 53 deletions.
8 changes: 8 additions & 0 deletions docs/configure/import-css/import-css.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
```js
// .storybook/preview.js
import '../src/styles/global.css';

export default {
parameters: {},
};
```
14 changes: 14 additions & 0 deletions docs/configure/import-css/import-css.ts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```ts
// .storybook/preview.ts

// Replace your-framework with the framework you are using (e.g., react, vue3)
import { Preview } from '@storybook/your-framework';

import '../src/styles/global.css';

const preview: Preview = {
parameters: {},
};

export default preview;
```
22 changes: 15 additions & 7 deletions docs/configure/styling-and-css.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@
title: 'Styling and CSS'
---

There are many ways to include CSS in a web application, and correspondingly there are many ways to include CSS in Storybook. Usually, it is best to try and replicate what your application does with styling in Storybook’s configuration.
There are many ways to include CSS in a web application, and correspondingly there are many ways to include CSS in Storybook. Usually, it is best to try and replicate what your application does with styling in Storybook’s configuration. To make this easier, we recommend using [`@storybook/addon-styling`](https://github.com/storybookjs/addon-styling).

### CSS-in-JS
## Importing CSS files

CSS-in-JS libraries are designed to use basic JavaScript, and they often work in Storybook without any extra configuration. Some libraries expect components to render in a specific rendering “context” (for example, to provide themes), and you may need to add a [global decorator](../writing-stories/decorators.md#global-decorators) to supply it.
Storybook is pre-configured to recognize imports for CSS files. To add global CSS for all your stories, import it in [`.storybook/preview.js`](./overview.md#configure-story-rendering).

### Importing CSS files
<FeatureSnippets paths={['configure/import-css/import-css.js.mdx', 'configure/import-css/import-css.ts.mdx']} />

If your component files import their CSS, Storybook's webpack configuration will work out of the box. The noticeable exception to this is if you're using a CSS precompiler. In this case, you can either install and configure a Storybook addon (e.g., [`@storybook/addon-styling`](https://github.com/storybookjs/addon-styling#storybookaddon-styling)), or customize [Storybook's webpack configuration](../builders/webpack.md#extending-storybooks-webpack-config) and include the appropriate loader.
If your component files import their CSS files, this will work too. The noticeable exception to this is if you're using CSS processor tools like Sass or Postcss.

<FeatureSnippets paths={['configure/css-troubleshooting/angular.mdx']} />

To use your CSS in all stories, you import it in [`.storybook/preview.js`](./overview.md#configure-story-rendering)
## CSS processors

### Adding webfonts
If you're using Vite as your builder, you're covered! Vite supports Sass and PostCSS out-of-the-box 🎉

However, if you're using Webpack and want to use Sass and PostCss, you'll need some extra configuration. We recommend installing [`@storybook/addon-styling`](https://github.com/storybookjs/addon-styling#storybookaddon-styling) to help you configure these tools. Or if you'd prefer, you can customize [Storybook's webpack configuration yourself](../builders/webpack.md#override-the-default-configuration) to include the appropriate loader(s).

## CSS-in-JS

CSS-in-JS libraries are designed to use basic JavaScript, and they often work in Storybook without any extra configuration. Some libraries expect components to render in a specific rendering “context” (for example, to provide themes), which can be accomplished with `@storybook/addon-styling`'s [`withThemeFromJSXProvider` decorator](https://github.com/storybookjs/addon-styling/blob/next/docs/api.md#withthemefromjsxprovider).

## Adding webfonts

If you need webfonts to be available, you may need to add some code to the [`.storybook/preview-head.html`](./story-rendering.md#adding-to-head) file. We recommend including any assets with your Storybook if possible, in which case you likely want to configure the [static file location](./images-and-assets.md#serving-static-files-via-storybook-configuration).
57 changes: 16 additions & 41 deletions docs/get-started/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ Go to your Storybook to view the rendered component. It’s OK if it looks a bit

Depending on your technology stack, you also might need to configure the Storybook environment further.

## Render component styles

Storybook isn’t opinionated about how you generate or load CSS. It renders whatever DOM elements you provide. But sometimes, things won’t “look right” out of the box.

You may have to configure your CSS tooling for Storybook’s rendering environment. Here are some setup guides for popular tools in the community.

- [Tailwind](https://storybook.js.org/recipes/tailwindcss/)
- [Material UI](https://storybook.js.org/recipes/@mui/material/)
- [Vuetify](https://storybook.js.org/recipes/vuetify/)
- [Styled Components](https://storybook.js.org/recipes/styled-components/)
- [Emotion](https://storybook.js.org/recipes/@emotion/styled/)
- [Sass](https://storybook.js.org/recipes/sass/)
- [Bootstrap](https://storybook.js.org/recipes/bootstrap/)

Don't see the tool that you're looking for? Check out the [styling and css](../configure/styling-and-css.md) page for more details.

## Configure Storybook for your stack

Storybook comes with a permissive [default configuration](../configure/overview.md). It attempts to customize itself to fit your setup. But it’s not foolproof.
Expand Down Expand Up @@ -91,47 +107,6 @@ Use [decorators](../writing-stories/decorators.md) to “wrap” every story in

</details>

## Render component styles

Storybook isn’t opinionated about how you generate or load CSS. It renders whatever DOM elements you provide. But sometimes, things won’t “look right” out of the box.

You may have to configure your CSS tooling for Storybook’s rendering environment. Here are some tips on what could help:

<details>
<summary>CSS-in-JS like styled-components and Emotion</summary>

If you are using CSS-in-JS, chances are your styles are working because they’re generated in JavaScript and served alongside each component. Theme users may need to add a decorator to `.storybook/preview.js`, [see above](#component-context).

</details>

<details>
<summary>@import CSS into components</summary>

Storybook allows you to import CSS files in your components directly. But in some cases you may need to [tweak its Webpack configuration](../builders/webpack.md#override-the-default-configuration). Angular components require [a special import](../configure/styling-and-css.md#importing-css-files).

</details>

<details>
<summary>Global imported styles</summary>

If you have global imported styles, create a file called [`.storybook/preview.js`](../configure/overview.md#configure-story-rendering) and import the styles there. They will be added by Storybook automatically for all stories.

</details>

<details>
<summary>Add external CSS or webfonts in the &lt;head&gt;</summary>

Alternatively, if you want to inject a CSS link tag to the `<head>` directly (or some other resource like a webfont link), you can use [`.storybook/preview-head.html`](../configure/story-rendering.md#adding-to-&#60head&#62) to add arbitrary HTML.

</details>

<details>
<summary>Load fonts or images from a local directory</summary>

If you're referencing fonts or images from a local directory, you'll need to configure the Storybook script to [serve the static files](../configure/images-and-assets.md).

</details>

## Load assets and resources

If you want to [link to static files](../configure/images-and-assets.md) in your project or stories (e.g., `/fonts/XYZ.woff`), use the `-s path/to/folder` flag to specify a static folder to serve from when you start up Storybook. To do so, edit the `storybook` and `build-storybook` scripts in `package.json`.
Expand Down
10 changes: 5 additions & 5 deletions docs/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ module.exports = {
title: 'Overview',
type: 'link',
},
{
pathSegment: 'styling-and-css',
title: 'Styling and CSS',
type: 'link',
},
{
pathSegment: 'upgrading',
title: 'Upgrading',
Expand Down Expand Up @@ -356,11 +361,6 @@ module.exports = {
title: 'TypeScript',
type: 'link',
},
{
pathSegment: 'styling-and-css',
title: 'Styling and CSS',
type: 'link',
},
{
pathSegment: 'images-and-assets',
title: 'Images and assets',
Expand Down

0 comments on commit a90af29

Please sign in to comment.