Skip to content

tailwindcss and shadow dom not working well (--spacing) #15799

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

Closed
vdegenne opened this issue Jan 24, 2025 · 8 comments · Fixed by #15975
Closed

tailwindcss and shadow dom not working well (--spacing) #15799

vdegenne opened this issue Jan 24, 2025 · 8 comments · Fixed by #15975

Comments

@vdegenne
Copy link

What version of Tailwind CSS are you using?

For example: v4

What build tool (or framework if it abstracts the build tool) are you using?

For example: vite

What version of Node.js are you using?

For example: v23

What browser are you using?

For example: Chrome

What operating system are you using?

For example: Linux

Describe your issue

I mainly using tailwindcss to structure my layout (utilities), I've successfuly migrated from v3 to v4.
But there is a small issue, using @import 'tailwindcss'; works but --spacing is missing a default.

Image

which makes all properties relying on this variable broken.

@wongjn
Copy link
Collaborator

wongjn commented Jan 24, 2025

Could you provide a reproducible example please?

@vdegenne vdegenne reopened this Jan 24, 2025
@vdegenne
Copy link
Author

Never mind, the problem occured because of shadow doms... I was binding the stylesheet transformed by tailwindcss to a custom element, but for some reasons --spacing was not accessible (is it because @layer are not working in custom elements?) but adding the same stylesheet to document.adoptedStyleSheets fixed the issue.

@vdegenne
Copy link
Author

vdegenne commented Jan 24, 2025

I still think that's a problem because the css variables should be available in the custom elements.

I think the issue is because it's using :root only and not :host (I may be wrong).

For those who wants to investigate/pin point the issue, I made a repository with minimal example: https://github.com/vdegenne/tailwindcss-4-vite-and-shadow-doms

(note: do not mind the repository name, the issue is not about vite)

@vdegenne vdegenne changed the title --spacing is missing from default theme. tailwindcss and shadow dom not working well Jan 24, 2025
@vdegenne vdegenne changed the title tailwindcss and shadow dom not working well tailwindcss and shadow dom not working well (--spacing) Jan 24, 2025
@vdegenne
Copy link
Author

Also, on my custom element the base layer is correctly applied:

Image

why not the theme layer?

@vdegenne
Copy link
Author

vdegenne commented Jan 24, 2025

Ok so my first intuition turned out to be correct, the problem is that theme layer css variables are only available in :root scope. If I manually add:

@import "tailwindcss";

@layer theme {
  :host {
    --spacing: 0.25rem;
  }
}

Then it works, so :host should be added for tailwindcss defaults to also work in custom elements. It's a huge deal.

@vdegenne
Copy link
Author

vdegenne commented Jan 24, 2025

For now I'll use the snippet above, but if one of you devs see this thread I really think that this needs to be fixed.

@vincaslt
Copy link

There is also an issue that @property properties won't work from inside of shadowDOM - they get created on the document (I think) and don't propagate back to shadow dom. For instance, try adding border - it won't work out of the box, event though it should.

@hugo-vrijswijk
Copy link
Contributor

hugo-vrijswijk commented Jan 27, 2025

It should be noted that this is not exclusive to --spacing. Any tailwind variable is inaccessible to the shadow dom at the moment. Meaning, all of tailwind v4 is broken inside web components/shadow dom.

hugo-vrijswijk added a commit to hugo-vrijswijk/tailwindcss that referenced this issue Jan 28, 2025
Fixes tailwindlabs#15799

This replaces the `:root` selector with `:host` in the `@theme` layer. This is necessary for the `@theme` layer to work correctly in shadow DOM.

Also updates the snapshots for the tests that were affected by this change (in a separate commit)
hugo-vrijswijk added a commit to hugo-vrijswijk/tailwindcss that referenced this issue Jan 28, 2025
Fixes tailwindlabs#15799

Adds a `:host` selector for the `@theme` layer. This is necessary for the `@theme` layer to work correctly in shadow DOM.

Also updates the snapshots for the tests that were affected by this change.
hugo-vrijswijk added a commit to hugo-vrijswijk/tailwindcss that referenced this issue Jan 28, 2025
Fixes tailwindlabs#15799

Adds a `:host` selector for the `@theme` layer. This is necessary for the `@theme` layer to work correctly in shadow DOM.

Also updates the snapshots for the tests that were affected by this change.
hugo-vrijswijk added a commit to hugo-vrijswijk/tailwindcss that referenced this issue Jan 28, 2025
Fixes tailwindlabs#15799

Adds a `:host` selector for the `@theme` layer. This is necessary for the `@theme` layer to work correctly in shadow DOM.

Also updates the snapshots for the tests that were affected by this change.
hugo-vrijswijk added a commit to hugo-vrijswijk/tailwindcss that referenced this issue Jan 29, 2025
Fixes tailwindlabs#15799

Adds a `:host` selector for the `@theme` layer. This is necessary for the `@theme` layer to work correctly in shadow DOM.

Also updates the snapshots for the tests that were affected by this change.
philipp-spiess added a commit that referenced this issue Jan 29, 2025
Resolves #15799
Resolves #14478
Part-of #15005

Adds a `:host` selector for the `@theme` layer. This is necessary for
the `@theme` layer to work correctly in shadow DOM.

Also updates the snapshots for the tests that were affected by this
change (in a separate commit).

## Test plan

Tested via the Vite playground:

<img width="1121" alt="Screenshot 2025-01-29 at 15 06 49"
src="https://github.com/user-attachments/assets/a7908135-5ff8-472f-a053-d2c6d5c81e1b"
/>

Additionally made sure that `@property` defaults also work across
Firefox, Chrome, and Safari (the `@property` definition from the root is
pulled in) and added a UI spec.

---------

Co-authored-by: Philipp Spiess <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants