Skip to content

fix(ui): scope widget css so it cannot restyle the host page #91

Closed

Description

@Ardnived

Summary

The widget injects its stylesheet into the host document (light DOM, vite-plugin-css-injected-by-js — no shadow boundary), and that stylesheet currently carries a full unscoped CSS reset, globally-named utility classes, :root-level custom properties, whole third-party library stylesheets, and a runtime Google Fonts @import. Appended after the host's own stylesheets, it wins ties and visibly restyles the embedding page — a WordPress theme's links, headings, lists and forms change the moment the widget script loads. This violates the repo's own rule (.claude/rules/components.md: the injected stylesheet "lands in the HOST document, so every selector in it must be scoped to our own DOM") — the previously hand-removed main {}/a {} leaks were cosmetic; the real leak is upstream in Preflight and the config. Ship-blocker #1 of the release-readiness review.

Bundled into this ticket: self-host the Raleway font. src/styles/globals.css:1 runtime-@imports Google Fonts from inside the injected style — a GDPR exposure for EU hosts (German court precedent on Google Fonts disclosing visitor IPs, LG München I 3 O 17493/20) and it forces every host CSP to allow fonts.googleapis.com + fonts.gstatic.com. Bundling the font files removes both problems at once.

Evidence (verified against the working tree, 2026-08-06)

  • Tailwind Preflight ships unscoped: @tailwind base at src/styles/globals.css:19; tailwind.config.js has no corePlugins.preflight disable and no prefix/important scoping strategy. The built bundle contains the full reset — a{color:inherit;text-decoration:inherit}, zeroed h1–h6/list/blockquote margins, border:0 on the universal selector, form-control resets — applied to every element of the host page.
  • Generated utilities land globally, including .container (a guaranteed Bootstrap/theme collision), .hidden, .sr-only, etc.
  • Hand-written unscoped selectors in src/styles/globals.css:
    • :root-level palette custom properties: :root, .light, .light-theme + .dark, .dark-theme blocks (lines 30–117), plus the Radix Colors imports (lines 14–17), whose own files scope to :root, .light / .dark.
    • .dark / .dark-theme — extremely common host theme classes; a host using .dark for its own dark mode receives our palette variables.
    • .flex-center / .flex-center-x / .flex-center-y utilities (lines 198–208).
    • .colored-links with !important (lines 215–221).
    • Top-level [data-vaul-drawer] media-query rule (lines 124–128).
    • Top-level @import of mapbox-gl/dist/mapbox-gl.css, three swiper/css* sheets, and the vendored ./vaul.css (lines 1–7) — these restyle a host page running its own Swiper or Mapbox GL instance.
  • Runtime Google Fonts @import at src/styles/globals.css:1 (verified in the shipped bundle).

Proposed changes

Implementation options — pick per sub-problem; don't feel bound to a single mechanism:

  1. Preflight: corePlugins: { preflight: false } plus a minimal hand-rolled reset scoped under a widget-owned class (the theme-root wrapper div in src/Widget.tsx:83–88 already wraps the whole tree, and portals are retargeted to it, so a scope class there covers everything) — and/or keep Preflight but scope all Tailwind output with the important: '<scope-selector>' strategy and/or a prefix. Whatever the mechanism, the invariant is: no Tailwind-emitted rule can select a host element.
  2. Utilities: the same scoping strategy must cover generated utilities (.container et al.) and the hand-written @layer utilities / @layer components entries.
  3. Palette variables: rescope from :root/bare .dark to the widget theme root (the wrapper div already carries the light/dark class); the Radix Colors imports need the same treatment (vendor, or rescope via a PostCSS pass — e.g. postcss-prefix-selector).
  4. Third-party sheets: scope the mapbox-gl / swiper / vaul imports under widget-owned wrappers (the map container, .sy-carousel, the drawer scope) — a PostCSS prefix pass over the imports, or vendoring, both work.
  5. Font: self-host Raleway (e.g. @fontsource/raleway or checked-in woff2 subsets + @font-face), drop the Google Fonts @import.
  6. Build assertion: a small script (e.g. scripts/assert-css-scoped.mjs) that parses the emitted CSS after pnpm build and fails when any top-level selector falls outside the allowlisted widget-owned scopes — wired so CI catches a re-leak.

Acceptance criteria

  • After pnpm build, the emitted CSS contains no top-level selectors outside widget-owned scopes, asserted by a script/unit check runnable in CI (allowlist of widget-owned scope prefixes; the check fails when an unscoped selector reappears).
  • Host elements outside the widget render identically with and without the widget script on a test page with representative host markup (links, headings, lists, blockquote, form controls, a .container element, a host .dark class, a host Swiper instance) — no Preflight reset, no utility collision, no palette-variable injection, no third-party-sheet bleed.
  • The widget still themes correctly from its own theme root: brand palette override, light/dark toggle, and the Radix gray/red ramps all resolve inside the widget.
  • Raleway is self-hosted: the widget makes no request to fonts.googleapis.com / fonts.gstatic.com, and text renders in Raleway.
  • Visual verification (Ladle + demo.html, Playwright MCP acceptable): drawers + peek strips, calendar view, image carousel (pagination bullets), event-pin popover, and .colored-links prose all render correctly after rescoping.
  • The CSP table in the integrator docs (README) is updated: fonts.googleapis.com (style-src) and fonts.gstatic.com (font-src) removed as required origins.
  • pnpm lint + pnpm typecheck + pnpm test:run + pnpm build pass.
  • Write an implementation report to .claude/reports/91-scope-widget-css.md — in the MAIN checkout at /Users/devindra/Documents/Projects/SahajAtlasWeb even when implementing from a worktree (the folder is gitignored) — covering what changed, what was deliberately NOT changed, and which readiness-report findings are resolved.

Files affected

Owned by this ticket:

  • tailwind.config.js
  • src/styles/globals.css
  • src/styles/vaul.css
  • Font assets (new — e.g. @fontsource/raleway dependency or checked-in woff2 + @font-face)
  • A small build-assertion script (new, e.g. scripts/assert-css-scoped.mjs) + its package.json/CI wiring

Parallel implementation: this ticket OWNS the files listed above; sibling release-hardening tickets are implemented concurrently, so changes outside the owned files must be avoided or kept strictly additive. In particular src/Widget.tsx is owned by the mount-path hardening ticket — if a scope class must be added to its theme-root wrapper, keep that change strictly additive (add a class, touch nothing else).

References

  • .claude/reports/RELEASE_READINESS.md §3.1, §3.5, Appendix B (local, gitignored — for repo-local implementers; all load-bearing evidence is inlined above)
  • .claude/rules/components.md — the scoping rule this violates, and the record of the two prior leaks (main {}/a {}, .swiper bullets → .sy-carousel)
  • src/styles/globals.css:176–181 — comment documenting the earlier hand-removed leaks
  • LG München I, 3 O 17493/20 (2022) — Google Fonts / visitor-IP disclosure ruling
  • Sibling ticket (same batch): fix(widget): survive foreign hashes and harden the mount path
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

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions