Skip to content

Instantly share code, notes, and snippets.

View codemile's full-sized avatar

Nick Foscarini codemile

View GitHub Profile
@codemile
codemile / ScaffoldEmotionCachProvider.tsx
Created August 20, 2023 14:05
Emotion provider for MUI that works in layout.tsx files of the app directory in NextJs 13
'use client';
import createCache from '@emotion/cache';
import {CacheProvider, EmotionCache} from '@emotion/react';
import {
SerializedStyles,
StyleSheet
} from '@emotion/utils/dist/declarations/types';
import {useServerInsertedHTML} from 'next/navigation';
import {FC, PropsWithChildren, useCallback, useMemo} from 'react';
@codemile
codemile / useBounceValue.tsx
Created July 17, 2022 16:21
React hook for bouncing a value back to it's default after a delay.
import {useEffect, useState} from 'react';
export const useBounceValue = <TValue,>(
newValue: TValue,
defaultValue: TValue,
delay: number
) => {
const [value, setValue] = useState<TValue>(defaultValue);
useEffect(() => {
@codemile
codemile / g7_currencies.json
Created June 8, 2022 21:04
A list of the G7 currencies.
{
"AED": "United Arab Emirates Dirham",
"AFN": "Afghan Afghani",
"ALL": "Albanian Lek",
"AMD": "Armenian Dram",
"ANG": "Netherlands Antillean Guilder",
"AOA": "Angolan Kwanza",
"ARS": "Argentine Peso",
"AUD": "Australian Dollar",
"AWG": "Aruban Florin",
@codemile
codemile / useResizeObserver.tsx
Created September 29, 2021 12:07
Hook that listens for bounding box changes on a DOM element.
import {MutableRefObject, useEffect, useState} from 'react';
const toRect = (rect: DOMRect | undefined) => {
const width = rect?.width || 0,
height = rect?.height || 0,
x = rect?.x || 0,
y = rect?.y || 0;
return {width, height, x, y};
};
@codemile
codemile / useToggle.tsx
Last active December 30, 2021 20:04
Hook that toggles a boolean state.
import {useCallback, useState} from 'react';
const useToggle = (init: boolean = false): [boolean, (v: unknown) => void] => {
const [toggle, setToggle] = useState(init);
const callback = useCallback((value) => {
setToggle((prev) => (typeof value === 'boolean' ? value : !prev));
}, []);
return [toggle, callback];
};
@codemile
codemile / usePrevious.tsx
Created August 24, 2021 11:11
Hook that returns the previous value.
import {useEffect, useRef} from 'react';
export function usePrevious(value) {
const ref = useRef(value);
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
@codemile
codemile / useGotQuote.tsx
Created August 13, 2021 15:15
Hook that fetches a random Games Of Thrones quote.
import {useEffect, useState} from 'react';
const useGotQuote = () => {
const [quote, setQuote] = useState('');
useEffect(() => {
fetch('https://game-of-thrones-quotes.herokuapp.com/v1/random')
.then((response) => response.json())
.then((data) => setQuote(data?.sentence));
}, []);
return quote;
@codemile
codemile / useCurrency.tsx
Last active December 30, 2021 20:10
Hook that formats a number as a currency using the browser's locale.
import {useMemo } from 'react';
const CODE: Record<string, string> = {
IN: 'INR',
SE: 'SEK',
GB: 'GBP',
IE: 'EUR',
CN: 'CNY',
JP: 'JPY',
US: 'USD',
@codemile
codemile / useGeoLocation.tsx
Created July 27, 2021 10:54
Hook for watching the users current location as geo coordinates.
import {useEffect, useState} from 'react';
const useGeoLocation = (options?: PositionOptions) => {
const [geo, setGeo] = useState<GeolocationPosition>();
const [err, setError] = useState<GeolocationPositionError>();
useEffect(() => {
const id = navigator.geolocation.watchPosition(
(pos) => {
setGeo(pos);
err && setError(undefined);
@codemile
codemile / useAudio.tsx
Created July 20, 2021 12:26
Hook that plays an audio file from a source URL.
import {useEffect, useMemo} from 'react';
export const useAudio = (
src: string,
play: boolean,
volume: number,
loop: boolean,
onDone?: () => void
) => {
const audio = useMemo(() => {