Skip to content

Commit

Permalink
Create utils file and refactor window handling
Browse files Browse the repository at this point in the history
  • Loading branch information
msoham123 committed Jan 5, 2023
1 parent 1c34de9 commit 7bd1de7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
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
4 changes: 2 additions & 2 deletions src/libs/configureDarkMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/


const colorSchemeMedia = "(prefers-color-scheme: dark)";
const isBrowser = () => typeof window !== "undefined"
import { isBrowser } from "./utils";
import { colorSchemeMedia } from "./utils";

const configureDarkMode = () => {

Expand Down
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 "";
}

}

0 comments on commit 7bd1de7

Please sign in to comment.