-
Notifications
You must be signed in to change notification settings - Fork 10k
[Starlight] Auto-set theme from param #26412
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
base: production
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| import Default from "@astrojs/starlight/components/ThemeProvider.astro"; | ||
| --- | ||
|
|
||
| <script is:inline> | ||
| const params = new URL(location.href).searchParams; | ||
| const theme = params.get("preferred-color-scheme"); | ||
| if (theme === "dark" || theme === "light") { | ||
| document.documentElement.dataset.theme = theme; | ||
| localStorage.setItem("starlight-theme", theme); | ||
| } | ||
| </script> | ||
|
|
||
| <Default><slot /></Default> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this play with Starlight's version? Which one gets loaded first? You might want to not use the const urlTheme = new URL(location.href).searchParams.get("preferred-color-scheme");
const storedTheme =
typeof localStorage !== 'undefined' && localStorage.getItem('starlight-theme');
const theme =
urlTheme || storedTheme ||
(window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark');
// do the same logic as Starlight's from here on outI'm guessing the standard way to override Starlight comps won't allow that? And thus we need to replace it instead of adding to it?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think the following use case could happen:
Not sure we care about that though. It in theory could break copy/pasting of links depending on how Astro handle relative links when there's a query param. (I assume it ignores it.) If this case matters, then something in ThemeSelect likely needs to be overridden as well.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think so, as far as I understand Starlight's overrides. You can append logic to the component via the Default pattern, but once you start touching the internals you have to rewrite a lot. |
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line doesn't appear to do anything based on testing. Keeping it for the moment, but probably superceded by the local storage setting line in 10.
Ideally, I'd prefer to keep this behavior (9 over 10) -- and just load this time in this theme -- instead of setting the local variable to avoid someone getting docs theming set unexpectedly