Open f0o opened 7 months ago
Worth to note;
Manually including
<svelte:head>{@html '<script>(' + setInitialClassState.toString() + ')();</script>'}</svelte:head>
in +layout.svelte
solves the issue at the expense of having the JS included twice (once with weird script tag and once correctly here)
//EDIT: Scratch that, it's non-deterministic - sometimes it works sometimes it doesnt.
//EDIT2:
Placing (function Gn(){const e=document.documentElement.classList,t=localStorage.getItem("modeUserPrefers")==="false",n=!("modeUserPrefers"in localStorage),r=window.matchMedia("(prefers-color-scheme: dark)").matches;t||n&&r?e.add("dark"):e.remove("dark")})();
into the OnMount will actually solve it.
Seems like a race-condition somewhere?
@f0o can you share a few more details about your project config. Specifically:
selector
(aka class
) or media (aka "OS") strategy for your app?For example, under normal circumstances when using the default auto-adapter you should not need to insert the extra script tag in the page header.
We also provide this note when using the Lightswitch with the class-based strategy approach:
...Please note the Lightswitch component must be present on page load in order to operate. For best results, insert this in a fixed element that appears on every page.
So if the ilghtswitch is not part of the normal DOM tree on page load, then it can cause behavior similar to what you're reporting. For the Skeleton documentation site for example, this is part of the header. Even though it is "hidden" within a popup menu, but that menu is just visibly hidden, not removed from the DOM.
I can't speak for f0o but I'm experiencing the same issue. I'm using the adapter-static
package but experience the same issue with adapter-auto
. I'm running locally with pnpm run dev
, using the class
strategy.
I've copied the AppBar code straight from the Skeleton site so the LightSwitch should be in the DOM.
I can see the <script>
tag in the head
but it doesn't appear to be running - if I manually execute the code then it works as expected.
I've had success removing the svelte:head
tags and moving the call to setInitialClassState()
into the onMount
block on line 66
I've entirely forgotten about this bugreport; terribly sorry about it.
I'm also using adapter-static but without SSR and the class selector.
That being said; I've been running it with my OnMount workaround since I opened the bugreport and although hacky it seems to be stable across all browsers.
Current Behavior
After Toggling Lightswitch and reloading the page, the page will return to the default state instead of the one selected by Lightswitch.
The code https://github.com/skeletonlabs/skeleton/blob/master/packages/skeleton/src/lib/utilities/LightSwitch/LightSwitch.svelte#L77 seems to be the culprit.
Copy-pasting the code itself into the JS Console will set the correct state into HTML but that oddly formed "fake" script tag will not trigger execution (at least on current Google Chrome).
Expected Behavior
https://github.com/skeletonlabs/skeleton/blob/master/packages/skeleton/src/lib/utilities/LightSwitch/LightSwitch.svelte#L77
Should execute correctly and set the class into html element
Steps To Reproduce
Link to Reproduction / Stackblitz
No response
More Information
Google Chrome: Version 123.0.6312.106 (Official Build) (64-bit)