#Fresh best way to include script tag in html

15 messages · Page 1 of 1 (latest)

static yacht
#

I'm working on a darkmode toggle on my fresh site and I want to export a small script tag as part of the rendered html (to avoid the "flash" of light mode that can happen before the .js files are loaded)

This is how I am doing it in my island:

return (
      <>
        <Head>
          <script>
            let dark = localStorage.getItem('darkmode');

            document.querySelector(':root')?.classList.toggle('dark', dark === 'true');
          </script>
        </Head>
      </>

I'm wondering if there is a more elegant way to do this?

alpine fulcrum
#

A workaround would be to set darkmode as a cookie and assign the dark class server side

static yacht
#

Would that work with a toggle button on the page? Sending a request to the server to set/unset a cookie seems like a pain

alpine fulcrum
#

I think you can set cookies client side as well if a request costs too much in this economy

static yacht
#

makes sense. How to you assign the class to the document root server side though?

alpine fulcrum
#

The second link has a class={theme} thing, but idk about fresh. Look into app.tsx

static yacht
alpine fulcrum
#

Set on div then

weak thicket
broken verge
#

@weak thicket this thread was dead, and I accidentally revived it today (while searching for Troll's messages for something), then deleted my message

#

sorry for the noise, everyone

halcyon slate
broken verge