Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove default Storybook Canvas styles?

Storybook applies default styles to the the story canvas iframe. This prevents my stories from looking the way they should. How can I get rid of Storybook's default styles?

For example, here is the default style for an h2 element (via Storybook's page.css):

enter image description here

The source of that page.css is webpack://src/stories/page.css.

If I add styles in preview-head.html, Storybook will apply my custom styles AND the default Storybook styles, with the default Storybook styles taking precedence (unless my custom style has a strong specificity).

like image 517
Johnny Oshika Avatar asked Dec 19 '20 00:12

Johnny Oshika


People also ask

How do I turn off canvas in storybook?

To hide the "Canvas" tab: export default { title: 'YourTitle', parameters: { previewTabs: { canvas: { hidden: true}, }, viewMode: 'docs', } }; To hide the "Docs" tab: export default { title: 'YourTitle', parameters: { previewTabs: { 'storybook/docs/panel': { hidden: true } }, viewMode: 'canvas', } };

What is storybook canvas?

Storybook's Canvas Doc Block is a wrapper featuring a toolbar that allows you to interact with its content while automatically providing the required Source snippets.

How do I add my own CSS to Storybook?

The noticeable exception to this is if you're using a CSS precompiler. In this case, you can either install and configure a Storybook preset (e.g., SCSS preset ), or customize Storybook's webpack configuration and include the appropriate loader. To use your CSS in all stories, you import it in .storybook/preview.js

How to style the body of a storybook web application?

The Storybook web application reads .storybook/preview-body.html if it exists, which directly controls the <body> element in the rendered HTML. As we have said, the styling should be set in head, i.e. preview-head.html, instead of preview-body.html.

How do I Center a component in storybook?

In the example above, Storybook will center all stories in the UI. layout accepts these options: 1 centered: center the component horizontally and vertically in the Canvas 2 fullscreen: allow the component to expand to the full width and height of the Canvas 3 padded: (default) Add extra padding around the component More ...

How do I add webfonts to my Storybook?

If you need webfonts to be available, you may need to add some code to the .storybook/preview-head.html file. We recommend including any assets with your Storybook if possible, in which case you likely want to configure the static file location. ✍️ Edit on GitHub – PRs welcome!


1 Answers

By default when installing Storybook via npx sb init for React, Storybook adds a Page.tsx (.js) file under src/stories. In that file they import './page.css'; simply remove that import and you'll be set.

Hope this is your fix... It may be different depending on what framework you're using.

like image 170
aedwards22 Avatar answered Oct 27 '22 23:10

aedwards22