Skip to content

Instantly share code, notes, and snippets.

View isocroft's full-sized avatar
😜
Seriously fooling around!

Ifeora Okechukwu Patrick isocroft

😜
Seriously fooling around!
View GitHub Profile
@isocroft
isocroft / List.js
Last active January 20, 2025 11:06
A List data structure for JavaScript
/**
* No-op function.
* @private
*/
function noop () {}
const CONCEPTS = {
noop
};
@isocroft
isocroft / useMediaQuery.ts
Created October 29, 2024 12:20
Basic ReactJS useMediaQuery() hook
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);
@isocroft
isocroft / useNextJSBackNavigationReload.ts
Created October 29, 2024 12:38
Refresh page on a back button navigation in the browser or by calling `router.back()` in NextJS v11+ page router
import { useRouter } from "next/router";
import { useEffect } from "react";
export const useNextJSBackNavigationReload = () => {
const { reload } = useRouter();
useEffect(() => {
const handlePopState = () => {
reload();
};
@isocroft
isocroft / useNextJSBackNavigationRefresh.ts
Created October 29, 2024 12:38
Refresh page on a back button navigation in the browser or by calling `router.back()` in NextJS v13+ app router
import { useRouter } from "next/navigation";
import { useEffect } from "react";
export const useNextJSBackNavigationRefresh = () => {
const { refresh } = useRouter();
useEffect(() => {
const handlePopState = () => {
refresh();
};
@isocroft
isocroft / useScrollPosition.ts
Last active November 12, 2025 19:43
Get the `x` & `y` coordinate position of the scrollbar on the body or target DOM element.
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;
@isocroft
isocroft / useNextJSUnsavedChangesStatusAction.ts
Last active November 1, 2025 14:17
Display dialog for notifying user of unsaved changes in Nextjs v11+ page router software
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();
@isocroft
isocroft / useNextJSAppRouteChange.ts
Created October 29, 2024 13:31
Detecting route changes for Nextjs v13+ app router software
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);
@isocroft
isocroft / useNextJSPageRouteChange.ts
Last active October 30, 2024 17:29
Detecting route changes for Nextjs v11+ page router software
import { useRouter } from "next/router";
import { useRef, useEffect } from "react";
const useNexJSPreviousRoute = () => {
const router = useRouter();
const ref = useRef<string>("");
useEffect(() => {
if (ref.current === "") {
@isocroft
isocroft / useTrustedTypes.ts
Created October 29, 2024 19:11
Setup for trusted types 'default' policy using a ReactJS hook
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") {
@isocroft
isocroft / useNextJSPagePreserveScroll.ts
Last active January 9, 2025 11:40
Preserve the value of the last scroll position on any given page for scroll restoration in NextJs v11+ page router software
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 = () => {