Skip to content

Instantly share code, notes, and snippets.

function useGetterState<T>(value: T) {
const [state, setState] = React.useState<T>(value)
const stateRef = React.useRef<T>(state)
React.useEffect(() => {
stateRef.current = state
}, [state])
const getter = React.useCallback(function (newValue?: React.SetStateAction<T>): T {
// use arguments.length because it's the only way to
import * as React from 'react'
import { SpringValue, easings, useSpring } from 'react-spring'
/**
* Hook that animates height when args.animationKey changes
*
* Ex:
* const animatedBlock = useAnimatedHeight({
* animationKey: key,
* })
@tvler
tvler / getLinkifiedString.tsx
Last active October 29, 2022 01:04
Turn a string into JSX with autolinks
type LinkifiedString = Array<string | JSX.Element> | string | null | undefined
/**
* Turn a string into JSX with autolinks
*/
export function getLinkifiedString(str: string | null | undefined): LinkifiedString {
try {
// Group by whitespace (wrapping regex in parens keeps matched results in the array)
const substrings = str?.split(/(\s+)/g)
@tvler
tvler / machine.js
Created December 1, 2022 19:55
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@tvler
tvler / getChainFromId.ts
Created May 5, 2023 18:50
Fast way to get a wagmi chain from a chainId
import * as chains from '@wagmi/chains'
import type { Chain } from 'wagmi'
const chainValues = Object.values(chains)
const chainMap = new Map<number, Chain>()
for (const chain of chainValues) {
chainMap.set(chain.id, chain)
}
@tvler
tvler / useSystemTheme.ts
Last active October 12, 2023 16:23
useSystemTheme.ts
import * as React from "react";
function getMediaQuery(): MediaQueryList {
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
return mediaQuery;
}
function subscribe(onStoreChange: () => void): () => void {
const mediaQuery = getMediaQuery();
mediaQuery.addEventListener("change", onStoreChange);