slipHQ / run-wasm

Run WASM based code executions in the browser easily
https://www.runwasm.com
Apache License 2.0
468 stars 47 forks source link

fix: screen flash on changing themes #87

Closed Deep-Codes closed 2 years ago

Deep-Codes commented 2 years ago

Attempts to fix #75

There are 2 approaches for this issue

  1. Run a script in the <head> tag

This works well when was have theme data attribute in the HTML element So when the page loads we can get the theme based on preference and localStorage and assign it before the <body> starts executing.

const docHtml = document.documentElement.dataset;
docHtml.theme =getTheme();

and then we add it to _document.tsx

import Document, { Html, Head, Main, NextScript } from 'next/document';
import React from 'react';

class MyDocument extends Document {
    render() {
        return (
            <Html lang="en">
                <Head>
                    {/* To Avoid Flickering */}
                    <script type="text/javascript" src="/static/theme.js" />
                </Head>
                <body>
                    <Main />
                    <NextScript />
                </body>
            </Html>
        );
    }
}

export default MyDocument;

Since we aren't using the data theme attribute here instead we are assigning the theme based on className to the body element.

  1. Using next-themes

Things I have done in this PR:

Some implementations

https://github.com/leerob/leerob.io/blob/7bd1fdfdb0f5bf7ad0945b14f7a0da26eaa68c11/pages/_app.tsx#L13

vercel[bot] commented 2 years ago

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/sliphq/run-wasm/H6QpxwjrpBAeeQTsHQQUEZ9DDYvg
✅ Preview: https://run-wasm-git-fork-deep-codes-fix-dark-mode-sliphq.vercel.app

kennethcassel commented 2 years ago

Thanks for the PR and detailed info! Can you resolve the merge conflicts? We merged a few PR earlier today (dark mode toggle seems to be missing on your preview deploy)

Deep-Codes commented 2 years ago

Thanks for the PR and detailed info! Can you resolve the merge conflicts? We merged a few PR earlier today (dark mode toggle seems to be missing on your preview deploy)

Yeah was on it, doing

kennethcassel commented 2 years ago

Ah one small thing is the toggle icon isn't showing up on mobile safari

image

Deep-Codes commented 2 years ago

Ah one small thing is the toggle icon isn't showing up on mobile safari

image

Simulator Screen Shot - iPhone 12 Pro Max - 2021-10-02 at 06 24 32

Adding width and height should solve it commiting

Deep-Codes commented 2 years ago

Verified on iPhone Safari let me know if anything else 👍🏽

kennethcassel commented 2 years ago

It's great thank you!