Skip to content

fix(ui): host CSS without !important still restyles widget text #236

Closed

Description

@Ardnived

Summary

A host's ordinary, non-!important CSS still reaches into the widget. On https://shrimataji.org/test/kn/find-a-class/ the compact card's heading renders in the theme's NotoSerif Regular at rgb(184, 131, 131), not the widget's typeface and foreground. docs/embedding.md ("The reverse direction is now defended, with one documented exception") and CHANGELOG.md promise that only !important gets through.

This is not a Mesmerize quirk. The plugin's browser lane measured the production widget across 18 booting cells × 4 viewports: the typeface was lost on 61 of 72 page checks, on every theme tried, by element a 48 / button 32 / h2 23 / input 2.

The reset in src/styles/globals.css:94 (:where(.sy-atlas) :where(*:not(svg, svg *)) { all: revert }) has zero specificity. The baseline that restates font-family and color sits on .sy-atlas itself (globals.css:113) and reaches descendants only by inheritance. So any host rule that targets a descendant directly, at any specificity above zero, wins wherever no Tailwind utility of ours sets that property. Here it is Mesmerize's body h2 { font-family; color } (0,0,2) against <h2 class="text-sm font-semibold"> (src/views/CompactEmbedView/CompactEmbedView.tsx:73), which sets size and weight but not family or colour.

This ticket is now the shadow-DOM revisitation that #157 deferred and closed as not planned. #157's own bar for reopening was "a host we actually need to support ships CSS that breaks the widget, and cannot or will not change it". The fleet measurement clears that bar — and clears it for non-!important CSS, which #157 believed was already fully blocked.

Does shadow DOM definitely fix it?

For this failure class, yes. A host selector cannot match an element inside a shadow root at all — !important or not, at any specificity. Every leak measured above is selector matching (a, button, h2, input rules), so all of it stops.

Two things still cross, and both must be stated or the fix will be mis-scoped:

  • Inherited properties cross the boundary from the host element <sahaj-atlas>, exactly as they cross into any element. So the baseline at globals.css:113 is not replaced by shadow DOM — it is what makes the widget the root of its own world, and it must survive the move. Only the all: revert reset above it retires.
  • A host rule aimed at sahaj-atlas itself still sets that element's own non-inherited properties — its box, its display, its margin. That is the host's slot and legitimately theirs.

So the honest claim is: shadow DOM closes host-selector reach completely, and leaves inheritance, which we already defend and would keep defending.

What it would cost

The mount itself is one line. @r2wc/[email protected] takes shadow?: "open" | "closed", so r2wc(Widget) at src/Widget.tsx:431 becomes r2wc(Widget, { shadow: 'open' }). Everything below is the actual price.

  • The two style-tag ids are a documented host contract. docs/embedding.md:953-961 publishes sahaj-atlas-style and sahaj-atlas-fonts as "kept stable across releases precisely because host sites key off them". A shadow root moves the stylesheet out of the host's <head> and breaks that for any host who did. This is a breaking change for docs/embedding.md and CHANGELOG.md, not a detail.
  • The CSS injector has no shadow target. vite-plugin-css-injected-by-js (vite.config.ts:228) appends to document.head. It must be replaced, or post-processed into a stylesheet the root adopts.
  • @font-face has to stay in the document and is already correct there (src/styles/fonts.ts:136). Faces registered inside a shadow root are not applied. So the stylesheet splits into two permanent homes, and the 'Atlas Rethink Sans' family-name hack stays for the same reason it exists now.
  • document.activeElement returns the host element across the boundary, and two of our own call sites read it: src/config/store.ts:241 (the report modal's return-focus capture — breaking it drops a keyboard user at the top of the host page, the exact failure that comment exists to prevent) and src/views/CompactEmbedView/CompactEmbedView.tsx:214. Both need getRootNode().
  • Radix's dismissible-layer and focus machinery is document-level, as is vaul's. Click-outside, Esc, and focus trapping all route through it, and none of it is ours to patch.
  • Mapbox GL has known document.elementFromPoint trouble in a shadow root, and @mapbox/[email protected] is itself a shadow-DOM custom element, so it would nest.
  • Turnstile injects its script into document.head (src/hooks/use-turnstile.ts:77) and renders its challenge into our container. It gates the report-issue form, which delivers real email since fix(event): deliver report-issue submissions instead of alerting #103, so a silent failure there is not cosmetic.

What it would NOT cost — checked, so nobody re-derives it

  • Portals are already shadow-shaped. Everything targets the theme root through overlayContainer() (src/lib/overlay.ts:110), never document.body (src/lib/scope.ts:17-18). This is the single largest obstacle in chore(ui): evaluate shadow DOM now that the CSS reset has a measured gap #157's list and it has since been built.
  • There is no slotted content. No <slot> exists anywhere in src/views/ or src/components/, and the widget renders no host children — so there is no slotted subtree where host CSS would walk straight back in.
  • position: fixed is unaffected. A shadow root creates no containing block, so the fixed layer, MapFrame, and the measured contain: layout / transform table in src/components/AGENTS.md all stand.
  • Custom properties inherit through the boundary, so the brand palette keeps arriving exactly as it does now.
  • The readiness marker is on document.documentElement (src/lib/readiness.ts:77), so the smoke lane is unaffected.

What gets simpler

  • The PostCSS scoping pass and its gate become unnecessary inside a shadow root: scripts/postcss-scope-widget.mjs (513 lines), scripts/assert-css-scoped.mjs (281) and scripts/postcss-scope-widget.test.ts (352) — 1,146 lines — retire or re-aim, and selectors go back to being plain.
  • The reset's measured cost goes: a forced style recalc over 3,200 scoped nodes currently runs about 2× one outside the scope (41ms vs 20ms, globals.css:76-80).
  • The SVG-exclusion trap (globals.css:82-93) disappears with the reset that caused it.

Recommendation

Adopt it, with Phase 1 item 1 as the go/no-go. Neither fallback is a real alternative:

  • Raising the reset's specificity cannot hold. The reset is (0,0,0) deliberately (globals.css:45-52): it must stay under our own Tailwind utilities (0,1,0), so any raise is bounded above by our rules while a host's selector is bounded by nothing. A host adds one class and leads again. That route lowers 61/72 and never reaches 0.
  • Restating the baseline per element buys a green lane over an unchanged failure class. The measured losses are font-family and color, both inherited, so it would score 0/72 here while every non-inherited leak — text-decoration, background, border-radius — stands exactly where it is.

Shadow DOM is the only one of the three that is a boundary rather than a bet, and it retires 1,146 lines and the 2× recalc with it.

The condition: Radix's dismissible layer and vaul are document-level and not ours to patch, so that is the one cost that can kill the approach outright. If Phase 1 item 1 fails, take the per-element baseline plus an honest docs/embedding.md — not the specificity route.

Answer the style-id question before the spike. It is the only breaking cost in the list, and a grep of the known hosts settles it in minutes.

Approach

Two phases, one ticket, one PR.

Phase 1 — the spike, in a browser. Flip src/Widget.tsx:431 to { shadow: 'open' } on a branch and answer only the things no document can settle, each by observation in current Chrome and Safari:

  1. Radix dismiss / Esc / focus trap across the boundary, and the two document.activeElement reads above. This one is the go/no-go — settle it before paying for anything below.
  2. Mapbox GL pointer hit-testing, and the geocoder nesting its own shadow root.
  3. Turnstile rendering and solving inside the root.
  4. The stylesheet's new home, and whether display: contents on the theme root (src/Widget.tsx:81) still behaves.
  5. That @font-face must stay in document.head — confirm against the engines, not the folklore.

Phase 2 — adopt, or decline with the fallbacks. If adopted: migrate the injector, keep the globals.css:113 baseline, drop the all: revert reset, retire or re-aim the scoping pass and assert:css, and write the style-id break into docs/embedding.md and CHANGELOG.md. If declined: restate the inherited baseline on every scoped element rather than the root, and docs/embedding.md must stop promising that non-!important CSS is blocked, because it is not.

Acceptance criteria

  • A host rule such as body h2 { font-family: serif; color: red }, without !important, does not change any widget text.
  • A host rule carrying !important and targeting a widget descendant does not change it either, or the decision records why that was not bought.
  • Tailwind utilities inside the widget still win over whatever reset remains.
  • Focus return from the report modal still lands on the opener, not the top of the host page.
  • The report-issue form still renders, solves and submits its Turnstile challenge.
  • docs/embedding.md describes exactly what a host's CSS can and cannot reach, and docs/embedding.md + CHANGELOG.md record any change to the two published style-tag ids.

Verification checklist

  • pnpm build && pnpm review:embed; open /__review/hostile-css.html with added body h2 { font-family: serif; color: red } and body a { color: red !important } rules; the compact card heading and every link compute the widget's font family and foreground colour.
  • In the same page: open the report modal from the settings menu, close it, and confirm focus returns to the opener.
  • In the same page: open a drawer and dismiss it by click-outside and by Esc; drag the map and open the geocoder.
  • pnpm lint && pnpm typecheck && pnpm test:run, then pnpm build && pnpm size.
  • Re-run the plugin's browser lane in sydevs/SahajAtlasWordpress (see test(browser): run the real widget in real themes, in a browser SahajAtlasWordpress#38) against a deploy of the branch; the 61/72 typeface losses go to 0/72. ⚠ There is no pnpm test:browser in this repo — that lane is the plugin's.
  • The same check on https://shrimataji.org/test/kn/find-a-class/ at 1280×720 after deploy.

Downstream impact

  • sydevs/SahajAtlasWordpress — owns the browser lane that measured this and is the acceptance evidence for the fix.
  • Embedding hosts — a shadow root changes where the stylesheet lives, which docs/embedding.md:953-961 currently publishes as stable.

Open questions

  • Which of the three candidate fixes? Shadow DOM is the one under evaluation, per the thread. The other two stay as Phase 2 fallbacks.
  • Open or closed root? open. It keeps the widget inspectable and keeps our own getRootNode() reads simple, and closed buys nothing a host cannot already defeat.
  • Do any current hosts actually key off sahaj-atlas-style or sahaj-atlas-fonts, or is the published contract unused and cheap to retire? Settle this first — it is the only breaking cost.
  • If Radix or Turnstile fails the spike, is the fallback a partial shadow root, or the per-element baseline?

Notes

Found while fixing #235. hostile-css.html uses only !important and single-element selectors, which is why the gap stayed unseen.

Supersedes #157, which closed as not planned on the premise this ticket disproves — that non-!important host CSS was fully blocked. #157's obstacle list is still the right list; this body records which entries have since been built (portals), which stand (Radix, Mapbox, fonts, the injector), and which are new (the published style-tag ids, Turnstile, our two document.activeElement reads).

⚠ Shadow DOM does not fix vaul's window-height arithmetic. That is a separate problem and must not be used to justify this one.

Activity

  1. added theissue type on Sep 30, 2026
  2. Ardnived commented on Oct 1, 2026

    @Ardnived
    ContributorAuthor

    Measured across the fleet's free themes by the plugin's new browser lane (sydevs/SahajAtlasWordpress#38): production widget, WordPress 6.7, 18 booting cells × 4 viewports.

    The widget's typeface was lost on 61 of 72 page checks. By element: a 48, button 32, h2 23, input 2. Every theme leaked at least one: Astra, OceanWP, Mesmerize, Esotera, Fluida, PopularFX, Seva Lite, Enigma, Twenty Twenty/Four/Five, and Astra under Elementor and Beaver Builder.

    So this is not a Mesmerize quirk. Any theme rule on a, button or h2 with specificity above zero beats the (0,0,0) reset. pnpm test:browser re-measures a fix; screenshots land in tests/screenshots/.

    🤖 Written by the sydevs autonomous loop — see what this is

  3. Ardnived commented on Oct 1, 2026

    @Ardnived
    ContributorAuthor

    @sydevs-bot Revise this ticket as a revisitation of the shadow dom approach. What are the pros and cons? What would we lose by implementing the shadow DOM and would it definitely fix the issue raised in this ticket?

  4. added
    bot:working
    and removed on Oct 1, 2026
  5. sydevs-bot commented on Oct 1, 2026

    @sydevs-bot
    Contributor

    🤖 no session running

    recent dispatches
    • 21:39Z implement · attempt 1 · session · done
    • 21:39Z implement · attempt 1 · session
    • 21:09Z implement · attempt 1 · session · done
    • 21:09Z implement · attempt 1 · session
    • 21:01Z answer · attempt 1 · session · done

    Written by the sydevs dispatcher. Sessions never edit this comment.

  6. sydevs-bot commented on Oct 1, 2026

    @sydevs-bot
    Contributor

    Revised as a shadow-DOM revisitation, per your ask. The body now leads with the three answers.

    Would it fix this? Yes, completely, for host selectors — they cannot match inside a shadow root, !important or not. Two things still cross: inherited properties from <sahaj-atlas>, and rules aimed at that element itself. So the baseline at globals.css:113 stays; only the all: revert reset retires.

    The mount is one line — @r2wc/[email protected] takes shadow: 'open'.

    What we would lose, the part worth your eye: docs/embedding.md:953-961 publishes two style-tag ids as stable "because host sites key off them". A shadow root breaks that. Also the CSS injector, Turnstile, and two of our own document.activeElement reads.

    What #157 feared and we already built: portals all target the theme root, not document.body.

    Also raised Effort to Hard, and added sydevs/SahajAtlasWordpress under Downstream impact. Title unchanged.

    @sydevs-bot implement starts the work.

    🤖 Written by the sydevs autonomous loop — see what this is


    Generated by Claude Code

  7. added and removed
    bot:working
    on Oct 1, 2026
  8. Ardnived commented on Oct 1, 2026

    @Ardnived
    ContributorAuthor

    @sydevs-bot Do you recommend implement the shadow DOM?

  9. 7 remaining items

  10. moved this from Revising to Approved in SYDevelopers Technical Roadmapon Oct 1, 2026
  11. added
    bot:working
    and removed
    bot:working
    on Oct 1, 2026
  12. removed
    bot:working
    on Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    Priority

    Medium

    Hold Until

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions