Skip to content

Style isolation through Shadow DOM #22

Closed
@rauchg

Description

@rauchg

For the vast number of users who with very good reasons reject CSS-in-JS, we could support something like this:

import Head from 'next/head'
import shadow from 'next-shadow'
export shadow(() => (
  <footer>
    <div>hi <span>world</span></div>
    <style>{`
      div span {
        /* this won't conflict with any other element anywhere,
            including spans from nested components */
        color: red
      }
    `}</style>
  </footer>
))

In the future, if we find a way of making this work with server-rendering, we could make every single component a custom element with built-in isolation capabilities out of the box, no shadow() wrapping needed. If you want global effects, you can use <Head>, otherwise everything is isolated.

This would put CSS-in-JS and full CSS support on equal ground, both supported, both with pros-and-cons and great performance.

The problems:

  • Limited browser support for now
  • Is this actually doable?
    • Perhaps base it on react-shadow
    • What's the server-rendering story of custom elements? Do they require JS execution for the elements registration? Could be slow and impractical
  • Are there performance drawbacks?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions