Skip to content

Instantly share code, notes, and snippets.

View KristofferEriksson's full-sized avatar

Kristoffer Eriksson KristofferEriksson

View GitHub Profile
@KristofferEriksson
KristofferEriksson / pocketbase-docker-compose.yaml
Last active January 24, 2024 12:04
PocketBase Docker Compose File
version: "3.7"
services:
pocketbase:
image: ghcr.io/muchobien/pocketbase:latest
container_name: pocketbase
restart: unless-stopped
command:
- --encryptionEnv
- ENCRYPTION
environment:
@KristofferEriksson
KristofferEriksson / useLongPress.ts
Created January 24, 2024 11:05
A simple useLongPress hook for React
import { useCallback, useEffect, useRef } from "react";
export function useLongPress({
delay,
onLongPress,
onClick,
onCancel,
onFinish,
onStart,
}: {
@KristofferEriksson
KristofferEriksson / useMeasure.ts
Created January 27, 2024 16:52
A generic React TypeScript hook for measuring element dimensions in real-time
import { useEffect, useRef, useState } from "react";
interface MeasureResult<T extends Element> {
ref: React.RefObject<T>;
bounds: DOMRectReadOnly;
}
const useMeasure = <T extends Element = Element>(): MeasureResult<T> => {
const ref = useRef<T>(null);
const [bounds, setBounds] = useState<DOMRectReadOnly>(new DOMRectReadOnly());
@KristofferEriksson
KristofferEriksson / useShare.ts
Created February 7, 2024 22:19
A React Typescript hook that let users share your content directly via native share dialogs
import { useCallback, useEffect, useState } from "react";
// Types for the useShare hook parameters
interface UseShareParams {
onShare?: (content: ShareParams) => void;
onSuccess?: (content: ShareParams) => void;
onError?: (error: any) => void;
fallback?: () => void;
successTimeout?: number;
}
@KristofferEriksson
KristofferEriksson / useDeviceOrientation.ts
Created February 13, 2024 15:10
A React Typescript hook that utilizes the DeviceOrientation API to provide real-time orientation data of a device in 3D space
import { useCallback, useEffect, useState } from "react";
interface DeviceOrientationState {
alpha: number | null;
beta: number | null;
gamma: number | null;
absolute: boolean;
}
// Define an extended interface for DeviceOrientationEvent including requestPermission
@KristofferEriksson
KristofferEriksson / useSpeechToText.ts
Last active February 20, 2024 05:33
An experimental React hook for real-time speech-to-text using the Web Speech API
import { useCallback, useEffect, useState } from "react";
// Define custom types for SpeechRecognition and SpeechRecognitionEvent
interface ISpeechRecognitionEvent extends Event {
results: SpeechRecognitionResultList;
resultIndex: number;
}
interface ISpeechRecognition extends EventTarget {
lang: string;
@KristofferEriksson
KristofferEriksson / useBroadcastChannel.ts
Created February 5, 2024 09:20
A React hook that allows you to send and receive messages between browser tabs or windows
import { useCallback, useEffect, useRef, useState } from "react";
interface UseBroadcastChannelOptions {
name: string;
onMessage?: (event: MessageEvent) => void;
onMessageError?: (event: MessageEvent) => void;
}
interface UseBroadcastChannelReturn<D, P> {
isSupported: boolean;
@KristofferEriksson
KristofferEriksson / useConfirmation.ts
Created January 23, 2024 10:11
Custom React hook for double-click confirmation for critical actions
import { useCallback, useEffect, useRef, useState } from "react";
/**
* Custom React hook for double-click confirmation for critical actions.
*
* @param action - The action to be executed on the second click.
* @param timeout - Time in milliseconds to reset the unlocked state.
* @returns The current unlocked state and the trigger function.
*/
const useConfirmation = (action: () => void, timeout: number = 5000) => {
@KristofferEriksson
KristofferEriksson / useLocation.ts
Created February 11, 2024 16:44
A React Typescript hook that provides real-time geolocation data, complete with heading and speed metrics
import { useEffect, useState } from "react";
interface LocationOptions {
enableHighAccuracy?: boolean;
timeout?: number;
maximumAge?: number;
}
interface LocationState {
coords: {
@KristofferEriksson
KristofferEriksson / useTextSelection.ts
Last active February 24, 2024 22:22
A React Typescript hook that tracks user text selections & their screen positions
import { useEffect, useState } from "react";
type UseTextSelectionReturn = {
text: string;
rects: DOMRect[];
ranges: Range[];
selection: Selection | null;
};
const getRangesFromSelection = (selection: Selection): Range[] => {