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

Feature Request: Append content to body tag attributes via Helmet #365

Closed
jeffredodd opened this issue Mar 23, 2018 · 13 comments
Closed

Feature Request: Append content to body tag attributes via Helmet #365

jeffredodd opened this issue Mar 23, 2018 · 13 comments

Comments

@jeffredodd
Copy link

jeffredodd commented Mar 23, 2018

When using react Helmet, I would like a way to append content into my attributes onto my <body> tag.

For example this code will set my body class ...
JSX

<Helmet>
  <body class='first-class' />
</Helmet>

Output
<body class='first-class'>

And when I have a component deeper in my application call helmet like such the body class is overwritten...

JSX

<Helmet>
  <body class='second-class' />
</Helmet>

Output
<body class='second-class'>

Desired Output
<body class='first-class second-class'>

Currently the fix for this in my application is to call the following which is not sustainable as we now have to maintain many body classes throughout our application...
JSX

<Helmet>
  <body class='first-class second-class' />
</Helmet>

Potential Solution
In order to set this up correctly you cannot call class but maybe using something like the following would be an appropriate way to append an item to helmet as calling class directly should still overwrite class so that a breaking change is not introduced to helmet?

JSX

<Helmet>
  <body append-class='second-class' />
</Helmet>
@andrewbusch7
Copy link

Isn't this creeping beyond the scope of react-helmet? Helmet i.e. head?

@sneridagh
Copy link

+1

@panzerdp
Copy link

While react-helmet does not support appending classes, you can simply add new attributes to your body tag. Then use those attributes as selectors.

E.g.:

<App>
  <Helmet>
    <body data-first="true" />
  </Helmet>
  <Helmet>
    <body data-second="true" />
  </Helmet>
</App>

The output will be:

<body data-first="true" data-second="true" />

Then use attribute selectors:

body[data-first] {
  color: blue;
}

body[data-second] {
  color: red;
}

@Noyabronok
Copy link

We have a 3rd party script adding classes to the body tag, but Helmet is overwriting them . Why is appending classes not supported?

@tmbtech
Copy link
Contributor

tmbtech commented Nov 9, 2018

We have a 3rd party script adding classes to the body tag, but Helmet is overwriting them . Why is appending classes not supported?

The typical and recommended approach is to not wrap the entire body but to use helmet as a sibling to the body.

ie

<div className="application">
    <Helmet>
        <meta charSet="utf-8" />
    </Helmet>
    <body>.....</body>
</div>

https://github.com/nfl/react-helmet#as-react-components

I could be misunderstanding the issue, let me know if that helps.

@Noyabronok
Copy link

@tmbtech We're using react strictly client side, and Helmet to add some data attributes and update className='...' to an existing body tag.
If I understand correctly, your example applies to server side render, which is a different use case.

@tmbtech
Copy link
Contributor

tmbtech commented Nov 9, 2018

@Noyabronok can you provide an example via https://codesandbox.io

@bittu
Copy link

bittu commented Oct 15, 2019

bump!!!
@Noyabronok example please.
@jeffredodd @tmbtech were you able to achieve this?

Edit:
Figured it...
Helmet.peek() on clients-side gives the Helmet props

  const { bodyAttributes } = Helmet.peek()

// render...
  <Helmet>
      <body className={cn(bodyAttributes.class, styles.bodySlide)} />
  </Helmet>

@jeffredodd
Copy link
Author

jeffredodd commented Oct 15, 2019

@bittu This task was for a former project but I heard they made quite a bit of progress with this in our UI platform by using https://github.com/staylor/react-helmet-async .

@bittu
Copy link

bittu commented Oct 16, 2019

@jeffredodd Ah ok... https://github.com/staylor/react-helmet-async is this package for SSR only or client-side rendering as well?

@javierguzman
Copy link

Hello all, I am looking for placing a noscript snippet within body. Is there a way to achieve this with react-helmet? Thank you in advance

@IPRIT
Copy link

IPRIT commented Jan 13, 2022

Any updates? Still actual.

@jeffredodd
Copy link
Author

Closing as React has changed a ton in 4 years and this is not a good proposed solution. Plus this repo hasn't received updates in a while.

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

No branches or pull requests

9 participants