From 7bd1de70114db49e4c2252538de0044731f279dc Mon Sep 17 00:00:00 2001 From: msoham123 Date: Thu, 5 Jan 2023 12:52:51 -0800 Subject: [PATCH] Create utils file and refactor window handling --- src/components/Button.js | 11 +++-------- src/components/CustomLink.js | 11 +++-------- src/libs/configureDarkMode.js | 4 ++-- src/libs/utils.js | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 src/libs/utils.js diff --git a/src/components/Button.js b/src/components/Button.js index f235ce5..414762d 100644 --- a/src/components/Button.js +++ b/src/components/Button.js @@ -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 }) => (
@@ -17,15 +18,9 @@ const ButtonInner = ({ icon, text, className }) => (
); -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") ? ( diff --git a/src/components/CustomLink.js b/src/components/CustomLink.js index 1e419d3..44e3231 100644 --- a/src/components/CustomLink.js +++ b/src/components/CustomLink.js @@ -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") ? ( typeof window !== "undefined" +import { isBrowser } from "./utils"; +import { colorSchemeMedia } from "./utils"; const configureDarkMode = () => { diff --git a/src/libs/utils.js b/src/libs/utils.js new file mode 100644 index 0000000..1da5485 --- /dev/null +++ b/src/libs/utils.js @@ -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 ""; + } + +}