Skip to content

Instantly share code, notes, and snippets.

View KristofferEriksson's full-sized avatar

Kristoffer Eriksson KristofferEriksson

View GitHub Profile
@KristofferEriksson
KristofferEriksson / useKeypress.ts
Last active May 8, 2024 02:31
A React custom hook for handling keyboard events.
import { useEffect } from "react";
const Keys = {
Backspace: "Backspace",
Tab: "Tab",
Enter: "Enter",
Shift: "Shift",
Control: "Control",
Alt: "Alt",
Pause: "Pause",
@KristofferEriksson
KristofferEriksson / useGesture.ts
Created February 7, 2024 10:27
A custom React Typescript hook for advanced touch gestures in UI
import { useEffect, useRef } from "react";
type GestureType =
| "swipeUp"
| "swipeDown"
| "swipeLeft"
| "swipeRight"
| "tap"
| "pinch"
| "zoom";
@KristofferEriksson
KristofferEriksson / useNetwork.ts
Created February 13, 2024 22:04
A React Typescript hook to get real-time network insights
import { useEffect, useState } from "react";
interface NetworkInformation extends EventTarget {
downlink?: number;
effectiveType?: "slow-2g" | "2g" | "3g" | "4g";
rtt?: number;
saveData?: boolean;
onchange?: EventListener;
}
@KristofferEriksson
KristofferEriksson / useIdle.ts
Created January 25, 2024 10:26
React Hook to detect user inactivity based on specified events like mouse movements, touch events or key presses
import { useEffect, useState } from "react";
const defaultEvents = [
"mousemove",
"mousedown",
"touchstart",
"keydown",
"wheel",
"resize",
];
@KristofferEriksson
KristofferEriksson / useDynamicTextareaSize.ts
Last active April 9, 2024 10:49
A simple React hook to dynamically adjust the height of a textarea based on its content
/**
* Custom hook for dynamically resizing a textarea to fit its content.
* @param {React.RefObject<HTMLTextAreaElement>} textareaRef - Reference to the textarea element.
* @param {string} textContent - Current text content of the textarea.
* @param {number} maxHeight - Optional: maxHeight of the textarea in pixels.
*/
import { useEffect } from "react";
const useDynamicTextareaSize = (
textareaRef: React.RefObject<HTMLTextAreaElement>,
@KristofferEriksson
KristofferEriksson / use-copy-to-clipboard.ts
Created January 22, 2024 11:32
Custom React hook for effortless text copying to clipboard! It handles copy status with a customizable timer and error management.
import { useCallback, useState } from "react";
// Custom hook to copy text to clipboard
const useCopyToClipboard = (timeoutDuration: number = 1000) => {
const [copied, setCopied] = useState(false);
const [error, setError] = useState<Error | null>(null);
const copyToClipboard = useCallback(
async (text: string) => {
try {
@KristofferEriksson
KristofferEriksson / useUndo.ts
Created January 31, 2024 11:34
A React hook that enhances your components with powerful undo/redo functionality
import { useCallback, useEffect, useRef, useState } from "react";
interface UseUndoHook<T> {
value: T;
onChange: (newValue: T) => void;
undo: () => void;
redo: () => void;
clear: () => void;
canUndo: boolean;
canRedo: boolean;
@KristofferEriksson
KristofferEriksson / useCookie.ts
Created January 29, 2024 09:16
A hook to easily read and update browser cookies. Plus, it auto-updates your component when cookie values change
import { useEffect, useState } from "react";
type UseCookieReturnType = {
cookie: string | undefined;
setCookie: (value: string, days?: number) => void;
};
const useCookie = (cookieName: string): UseCookieReturnType => {
const getCookie = (name: string): string | undefined => {
const value = `; ${document.cookie}`;
@KristofferEriksson
KristofferEriksson / useLocalStorage.ts
Last active March 21, 2024 20:31
An easy-to-use API for storing and retrieving data from Local Storage in React, with built-in real-time synchronization
import { useEffect, useState } from "react";
function useLocalStorage() {
const [loadingStates, setLoadingStates] = useState<Map<string, boolean>>(
new Map()
);
const setStorageValue = <T>(key: string, value: T) => {
try {
window.localStorage.setItem(key, JSON.stringify(value));
@KristofferEriksson
KristofferEriksson / useTailwindBreakpoint.ts
Created February 2, 2024 10:55
React hook for triggering effects when layout changes due to Tailwind breakpoints
import { useEffect, useState } from "react";
import resolveConfig from "tailwindcss/resolveConfig";
// Update the path to your Tailwind config file
import tailwindConfig from "tailwind.config";
const useTailwindBreakpoint = ({
onBreakpointChange,
}: {
// eslint-disable-next-line no-unused-vars