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

update so classes get correctly applied with Styled JSX #24

Closed
dylanjha opened this issue Mar 18, 2021 · 6 comments · Fixed by #85
Closed

update so classes get correctly applied with Styled JSX #24

dylanjha opened this issue Mar 18, 2021 · 6 comments · Fixed by #85

Comments

@dylanjha
Copy link
Collaborator

dylanjha commented Mar 18, 2021

styled-jsx is a popular method for css-in-js in the React ecosystem.

The way it works is like this:

function Home() {
  return (
    <div className="container">
      <h1>Hello Next.js</h1>
      <p>Let's explore different ways to style Next.js apps</p>
      <style jsx>{`
        .container {
          margin: 50px;
        }
        p {
          color: blue;
        }
      `}</style>
    </div>
  )
}

The p tag in this style only gets applied to this component. Internally, the library creates a unique class, and it applies that class to the HTML elements in the component AND the selectors in the CSS. So the DOM and CSS ends up looking like this:

<div class="container jsx-1234">
  <h1 class="jsx-1234">Hello Next.js</h1>
  <p class="jsx-1234">Let's explore different ways to style Next.js apps</p>
</div>

The css looks like:

<style>
  .container.jsx-1234 {
    margin: 50px;
  }
  p.jsx-1234 {
    color: blue;
  }
</style>

When I attempted to do this with media-container I got the following:

return (
  <div className="video-container">
    <media-container>
    </media-container>
    <styled jsx>{`
       .video-container {
          // some styles here
       }
       media-container {
          // these styles don't get applied
       }
    `}
    </styled>
  </div>
)

media-container-classname_2021-03-18_15-36-51

media-container classname="jsx-1234" should be media-container class="jsx-1234". It's almost as if styled-jsx things media-container is a react component and it's passing in className down and it ends up being an attribute on the web component.

There is a way to workaround this by using .video-container :global(media-container), but ideally the above code should work.

I don't know if the fix would be on the media-chrome side or the styled-jsx side to make this compatible. This issue might be relevant

@heff
Copy link
Collaborator

heff commented Mar 22, 2021

I'm gonna guess something in the styled-jsx/react stack is whitelisting native elements, which would make it incompatible with web components. We're not doing anything with class/classname in media-chrome that would cause this.

The answer for a lot of react ecosystem things is probably react wrappers for each component, at least until they support custom elements more cleanly.

@heff
Copy link
Collaborator

heff commented Mar 23, 2021

Looks like there's an answer to this, just not quite merged in yet.

@dylanjha
Copy link
Collaborator Author

@heff good find! that's exactly it 👌

@heff
Copy link
Collaborator

heff commented Jun 17, 2021

If anyone else is running into this, please go thumbs-up the PR.

@heff
Copy link
Collaborator

heff commented Feb 4, 2022

@dylanjha is there anything else we can do here besides wait? If not let's close this issue.

@dylanjha
Copy link
Collaborator Author

dylanjha commented Feb 4, 2022

@heff actually #85 would resolve this on our end (react folk start using the react components) -- I'm in favor of getting #85 shipped

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

Successfully merging a pull request may close this issue.

2 participants