Skip to content

fix: changed how the browsers theme is detected - #31

Open
nathinho89 wants to merge 1 commit into
mainfrom
fix/mnt-429-fix-theme-provide-in-field-plugins
Open

fix: changed how the browsers theme is detected#31
nathinho89 wants to merge 1 commit into
mainfrom
fix/mnt-429-fix-theme-provide-in-field-plugins

Conversation

@nathinho89

@nathinho89 nathinho89 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Jira Ticket:
https://storyblok.atlassian.net/browse/MNT-429#icft=MNT-429

Description

Follow-up to #27. The Tag field plugin's background was reverting to white in production because MUI's <CssBaseline /> hardcodes body's background to the (non-theme-aware) lightTheme.palette.background.default (#fff). Since #app is transparent, that white body showed through — visible only in the built app because the injection order between our own styles and CssBaseline's emotion-injected global styles flips between dev and build.

While digging into why the dark theme wasn't applying either, found the actual root cause of MNT-429: the theme-detection added in #27 reads theme from window.location.search, which only ever reflects the iframe's own URL, not the parent Storyblok dashboard's theme. The container doesn't pass a theme param (confirmed via @storyblok/field-plugin's message/URL-param types — no such field exists), so the plugin was always stuck on theme="default" regardless of the shell's actual theme.

Cross-origin restrictions mean the iframe can't read the parent document directly, so there's no way to know the Storyblok app's own theme toggle today. The closest available signal is the browser/OS-level prefers-color-scheme, which this PR switches to.

Changes

  • App.tsx: replaced the ?theme= URL-param read with window.matchMedia('(prefers-color-scheme: dark)'), including a change listener so the field updates live if the OS scheme flips while it's open.
  • style.scss: body background set to transparent !important, so it reliably overrides CssBaseline's hardcoded background regardless of style-injection order.

Known limitation

This tracks the OS/browser color-scheme preference, not an independent in-app Storyblok theme toggle. If a user's OS is light but they've manually set Storyblok to dark (or vice versa), the field will follow the OS setting instead. Fixing that fully would require the field-plugin container to forward its own theme state into the iframe (via URL param or postMessage), which isn't part of the SDK today.

Screenshot

Screenshot 2026-07-30 at 11 03 56

Comment thread pnpm-workspace.yaml
Comment on lines +9 to +13
allowBuilds:
'@parcel/watcher': true
core-js: true
esbuild: true
nx: true

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @nathinho89 🙌

Was it intentional or some testing?

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 this pull request may close these issues.

2 participants