Skip to content
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

Remove react hook and use javascript to detect theme state #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/components/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Link } from "gatsby";
import AnchorLink from 'react-anchor-link-smooth-scroll-v2'
import rippleFn from "../libs/ripple";
import classNames from "classnames";
import { getPathName } from "../libs/utils";

const ButtonInner = ({ icon, text, className }) => (
<div role="button" tabIndex={0} className={classNames("button relative overflow-hidden", className)} onMouseDown={rippleFn} onTouchStart={rippleFn}>
Expand All @@ -17,15 +18,9 @@ const ButtonInner = ({ icon, text, className }) => (
</div>
);

const GetPathName = () => {
if (typeof window !== "undefined")
return window.location.pathname;
return "";
}

const Button = ({ href="", icon, text, onClick, className }) => {
if (href.startsWith(GetPathName() + "#"))
href = href.substring(GetPathName().length);
if (href.startsWith(getPathName() + "#"))
href = href.substring(getPathName().length);

return href.startsWith("http") ? (
<a href={href} onClick={onClick} rel="noreferrer" target="_blank" tabIndex={-1}>
Expand Down
11 changes: 3 additions & 8 deletions src/components/CustomLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ import { Link } from "gatsby";
import AnchorLink from 'react-anchor-link-smooth-scroll-v2'
import rippleFn from "../libs/ripple";
import classnames from "classnames";

const GetPathName = () => {
if (typeof window !== "undefined")
return window.location.pathname;
return "";
}
import { getPathName } from "../libs/utils";

const CustomLink = ({ style, href, children, className, ripple }) => {
if (href.startsWith(GetPathName() + "#"))
href = href.substring(GetPathName().length);
if (href.startsWith(getPathName() + "#"))
href = href.substring(getPathName().length);

return href.startsWith("http") ? (
<a
Expand Down
6 changes: 2 additions & 4 deletions src/components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import Footer from "../components/Footer";
import { Helmet } from "react-helmet";
import Button from "./Button";
import Cookies from "js-cookie";
import useDarkModeState from "../libs/useDarkModeState";

const addAnalyticsScripts = () => {
console.log('Analytics Enabled');
Expand Down Expand Up @@ -64,15 +63,14 @@ const handleDeclineCookie = () => {
cookiesDeclined = true;
};

const Layout = ({ children, title }) => {
const isDark = useDarkModeState();
const Layout = ({ children, title}) => {

Copy link
Member

Choose a reason for hiding this comment

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

Extra space.

const hasConsent = getCookieConsent();
if (hasConsent)
initializeCookies();

return (
<main className={isDark ? "dark" : "light"}>
<main>
<Helmet htmlAttributes={{ lang: "en" }}>
<title>{title}</title>
</Helmet>
Expand Down
23 changes: 23 additions & 0 deletions src/libs/configureDarkMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @license
* Copyright © 2022 Skyline Team and Contributors (https://github.com/skyline-emu/)
* SPDX-License-Identifier: BSD-3-Clause
*/


import { isBrowser } from "./utils";
import { colorSchemeMedia } from "./utils";

const configureDarkMode = () => {

Copy link
Member

Choose a reason for hiding this comment

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

Extra space.

if (isBrowser()) {
if (window.matchMedia(colorSchemeMedia).matches) {
document.documentElement.classList.add('dark')
} else {
document.documentElement.classList.add('light')
}
}

};

export default configureDarkMode;
31 changes: 0 additions & 31 deletions src/libs/useDarkModeState.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/libs/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @license
* Copyright © 2022 Skyline Team and Contributors (https://github.com/skyline-emu/)
* SPDX-License-Identifier: BSD-3-Clause
*/

export const colorSchemeMedia = "(prefers-color-scheme: dark)";
export const isBrowser = () => typeof window !== "undefined"
export const getPathName = () => {

if (isBrowser()) {
return window.location.pathname;
} else {
return "";
}

}
4 changes: 4 additions & 0 deletions src/pages/404.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import * as React from "react";
import Layout from "../components/Layout";
import Button from "../components/Button";
import { FaHome } from "react-icons/fa";
import configureDarkMode from "../libs/configureDarkMode";

// markup
const NotFoundPage = () => {

configureDarkMode();

return (
<Layout title="404 - Not Found">
<div className="w-full min-h-screen dark:text-white flex flex-col items-center justify-center">
Expand Down
5 changes: 5 additions & 0 deletions src/pages/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ import useSWR from "swr";
import config from "../config";
import { Helmet } from "react-helmet";
import Spinner from "../components/Spinner";
import configureDarkMode from "../libs/configureDarkMode";


const IndexPage = () => {

configureDarkMode();

const { data } = useSWR(config.builds_api_url, (url) => axios.get(url).then((r) => r.data));

return (
Expand Down
4 changes: 4 additions & 0 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import Landing from "../sections/Landing";
import About from "../sections/About";
import Team from "../sections/Team";
import Layout from "../components/Layout";
import configureDarkMode from "../libs/configureDarkMode";

const IndexPage = () => {

Copy link
Member

Choose a reason for hiding this comment

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

Extra space.

configureDarkMode();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm just thinking not tried but is it working when we go to another page? It looks like it is working on index page but what happens if we go to download page? Is it still works because it looks like you are just adding dark class on index page

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, I didn't even think about that. It seems to maintain state when navigating to the download page, which makes sense, but then upon reload defaults to light. I added the method to all pages and now it works fine, no flash and respects system theme, for all of them (even upon changing in system settings and navigating to the page.)


return (
<Layout title="Skyline - Nintendo Switch Emulator">
<Landing />
Expand Down
4 changes: 4 additions & 0 deletions src/pages/privacy-policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import * as React from "react";
import Layout from "../components/Layout";
import CustomLink from "../components/CustomLink";
import configureDarkMode from "../libs/configureDarkMode";

const PrivacyPolicyPage = () => {

configureDarkMode();

return (
<Layout title="Privacy Policy">
<div className="mx-16 text-gray-800 dark:text-white">
Expand Down
2 changes: 1 addition & 1 deletion tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx}"],
darkMode: "class",
darkMode: ['class'],
i18n: {
locales: ["en-US"],
defaultLocale: "en-US",
Expand Down