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
| /** | |
| * No-op function. | |
| * @private | |
| */ | |
| function noop () {} | |
| const CONCEPTS = { | |
| noop | |
| }; |
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 { useState, useEffect } from "react"; | |
| export const useMediaQuery = (query: string) => { | |
| const [matches, setMatches] = useState<boolean>( | |
| () => { | |
| if (query.includes('max-width')) { | |
| const [ matchPoint ] = query.match(/\d+(?=\s*px)/) || [ '0' ] | |
| return typeof window === "undefined" | |
| ? false | |
| : window.innerWidth <= parseInt(matchPoint); |
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 React, { useState, useEffect } from "react"; | |
| export const useScrollPosition = <T extends HTMLElement>( | |
| ref?: React.MutableRefObject<T> | |
| ): [number, number] => { | |
| const [scrollPosition, setScrollPosition] = useState<[number, number]>([0, 0]); | |
| useEffect(() => { | |
| if (!ref || !ref.current) { | |
| return; |
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 { useRouter } from "next/router"; | |
| import { useState, useEffect } from "react"; | |
| export const useNextJSUnsavedChangesStatusAction = ({ | |
| promptText = "You have unsaved changes - are you sure you wish to leave this page?", | |
| useBrowserPrompt = true, | |
| unsavedChangesStatus = false | |
| }: { promptText: string, useBrowserPrompt?: boolean, unsavedChangesStatus?: boolean }) => { | |
| const router = useRouter(); |
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 { usePathname, useSearchParams } from "next/navigation"; | |
| import { useRef, useEffect } from "react"; | |
| export const useNexJSAppRouteChange = ( | |
| cb: (url: URL, updateTag: 'pathname' | 'query', options: { shallow: boolean }) => void | |
| ) => { | |
| const pathname = usePathname(); | |
| const searchParams = useSearchParams(); | |
| const previousRouteRef = useRef<string | null>(null); |
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 { useRouter } from "next/router"; | |
| import { useRef, useEffect } from "react"; | |
| const useNexJSPreviousRoute = () => { | |
| const router = useRouter(); | |
| const ref = useRef<string>(""); | |
| useEffect(() => { | |
| if (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
| import { useEffect } from "react"; | |
| import { trustedTypes } from "trusted-types"; | |
| import URISanity from "urisanity"; | |
| import DOMPurify from "dompurify"; | |
| export const useTrustedTypes = (policyName: string, callback?: (e: Event) => void):void => { | |
| useEffect(() => { | |
| const violationCallback = (e: Event) => { | |
| if (typeof callback === "function") { |
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 { useRef, useEffect } from "react"; | |
| import { useRouter } from "next/router"; | |
| /** | |
| * | |
| * @author: Jakob Chill | |
| * @see: https://jak-ch-ll.medium.com/next-js-preserve-scroll-history-334cf699802a | |
| */ | |
| export const usePreserveScroll = () => { |