Skip to content
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

How to set <body> classes? #1672

Closed
code-by opened this issue Aug 17, 2017 · 9 comments
Closed

How to set <body> classes? #1672

code-by opened this issue Aug 17, 2017 · 9 comments

Comments

@code-by
Copy link

code-by commented Aug 17, 2017

Hello!
Is there way to set <body> (inside <iframe>) class(es)? (except using finddomnode)
Thank you

@code-by code-by changed the title How to set <body> classes? How to set <body> classes? Aug 17, 2017
@danielduan
Copy link
Member

danielduan commented Aug 17, 2017

What are you trying to insert? You should generally be using stories to insert components and add decorators to them if you want the components to be wrapped in something.

Extremely hacky workaround: you can target the nodes onload with a script in the header
https://storybook.js.org/configurations/add-custom-head-tags/

@code-by
Copy link
Author

code-by commented Aug 18, 2017

I have add body tag with class attribute in the manager-head.html but it sets same for all stories while I need for different stories different body class(es).

@ndelangen
Copy link
Member

Write a wrapper component / decorator that sets the classes for you.

Alternatively you could write an addon so you can change the class from a panel for example.

@matthewharwood
Copy link

@ndelangen How do you set wrapper decorators with angular story books... I'm totally unable to do so.

@LiranBri
Copy link

you can add file config.js to .storybook dir with something likes this:

import { configure } from '@storybook/react'

function loadStories () {
  document.body.className += ' ' + 'hasHover'
}

configure(loadStories, module)

@ndelangen
Copy link
Member

That will work, mind that config.js CAN be MHR, and so the code may execute multiple times.
Another way wuld be to do this in an addon, which rejects HMR or in preview-head.html.

@ndelangen
Copy link
Member

storiesOf('name', module)
  .addDecorator(story => {
    document.body.classList.add('hasHover');
    return story();
  })
  .add('other name', () => {
    // ..
  });

Something like that might work, as a decorator.
But please consider that this will add a class to the body every time the story is rendered, and never cleans-up.. pretty bad.

Decorators are to change the story before they render, wrap them, extract data etc.

For permanent things side effects you're way better off using preview-head.html right now.

@vidal7
Copy link

vidal7 commented Jan 18, 2019

Creating a file named preview-head.html in your config directory containing this seems to have worked out for me.

<script>setTimeout(function () { document.body.classList.add("yourClass"); }, 0);</script>

Check the documentation here : https://storybook.js.org/configurations/add-custom-head-tags/

@iksent
Copy link

iksent commented Apr 24, 2023

Also you can try:

.storybook/preview-body.html:

<html class="custom-class">
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants