This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ConditionalWrapperProps = { | |
/** | |
* Inner component to be wrapped. | |
*/ | |
children: React.ReactElement; | |
/** | |
* Condition to be met for the wrapper to be applied. | |
*/ | |
condition: boolean; | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useOnlineStatus = () => { | |
const [online, setOnline] = useState(navigator.onLine); | |
useEventListener("online", () => setOnline(navigator.onLine)); | |
useEventListener("offline", () => setOnline(navigator.onLine)); | |
return online; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const usePrevious = <T>(value: T): T => { | |
// The ref object is a generic container whose current property is mutable ... | |
// ... and can hold any value, similar to an instance property on a class | |
const ref: any = useRef<T>(); | |
// Store current value in ref | |
useEffect(() => { | |
ref.current = value; | |
}, [value]); // Only re-run if value changes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useLockBodyScroll = (): void => { | |
// useLaoutEffect callback return type is "() => void" type | |
useLayoutEffect((): (() => void) => { | |
// Get original body overflow | |
const originalStyle: string = window.getComputedStyle( | |
document.body | |
).overflow; | |
// Prevent scrolling on mount | |
document.body.style.overflow = "hidden"; | |
// Re-enable scrolling when component unmounts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { RefObject, useEffect, useState } from "react"; | |
const useIntersectionObserver = ( | |
ref: RefObject<Element>, | |
rootMargin = "0px" | |
): boolean => { | |
const [isVisible, setIsVisible] = useState(false); | |
useEffect(() => { | |
const current = ref.current; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Lazy load a module with a named export | |
* | |
* @param {string} path - path for the import | |
* @param {string} namedExport - named exported member of the module | |
*/ | |
const lazyLoad = (path: string, namedExport: string) => | |
lazy(() => | |
namedExport | |
? import(path).then((module) => ({ default: module[namedExport] })) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from: https://usehooks.com/useLocalStorage/ | |
/** | |
* Get and set a value in browser local storage | |
* | |
* @param {string} key - key for the data | |
* @param {T} initialValue - initial data value | |
*/ | |
const useLocalStorage = <T>(key: string, initialValue: T) => { | |
// State to store our value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from "react"; | |
/** | |
* Set the document meta attributes | |
* | |
* @param {string} title - meta title | |
* @param {string} description - meta description | |
*/ | |
const useDocumentMeta = ( | |
title?: string, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const x = { | |
...( condition && { key: value }) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new URLSearchParams({ | |
key: "value" | |
}) |
NewerOlder