Skip to content

Instantly share code, notes, and snippets.

View srph's full-sized avatar

Kier Borromeo srph

View GitHub Profile
@srph
srph / readme.md
Last active July 22, 2026 18:38
Next: useUnsavedChanges - A super basic boilerplate wrapping Next.js route abort events

what

A super basic boilerplate wrapping Next.js route abort events

usage

useUnsavedChangesWarning(form.isDirty === true && status !== 'pending')
@srph
srph / readme.md
Last active July 22, 2026 18:38
React: useWiggle - Wiggle relevant elements when the user does the wrong thing twice

what

Wiggle relevant elements when the user does the wrong thing twice (e.g. submitting a form with validation errors twice)

usage

const { scope: wiggleRef, wiggle } = useWiggle();

const handleSubmit = () => {
  if (errors) {
 wiggle();
@srph
srph / layer-card.tsx
Last active July 22, 2026 18:38
layer-card - modern duo-tone cards
import type { HTMLAttributes } from "react";
import { cn } from "@/lib/cn";
// Example: <LayerCard><LayerCardSecondary>Title</LayerCardSecondary><LayerCardContent>Content</LayerCardContent></LayerCard>
export function LayerCard({ className, ...props }: HTMLAttributes<HTMLDivElement>) {
return (
<div
className={cn(
"rounded-lg border border-neutral-200 bg-neutral-100",
className,
@srph
srph / readme.md
Last active July 22, 2026 18:38
React: useEphemeralState - Latches a boolean flag for a short duration, then resets to `false`

what

Latches a boolean flag for a short duration, then resets to false

Useful for when an action succeeds, then you want to reset back to normal after a short while

User completes swapping X to Y: temporarily show "Success" then allow user to interact with the form again

usage

const { mutate, status } = useMutation(...)
@srph
srph / readme.md
Last active July 22, 2026 18:37
React: useLatestMutationState - An ergonomic wrapper for react-query's useMutationState

what

An ergonomic wrapper for react-query's useMutationState

usage

export function useCreatePostMutation(...) {
  return useMutation({ mutationKey: ['create-post'], ... })
}

export function useCreatePostMutationState(...) {
@srph
srph / readme.md
Created July 22, 2026 18:36
React: useAnimationText - Rotating text for long-running pending buttons

what

Rotating text for long-running pending buttons

usage

const STOP_TEXTS = [
  "Closing Positions",
  "Canceling Orders",
  "Finalizing Trades",
 "Updating Balances",
@srph
srph / poll.ts
Last active December 4, 2025 09:48
JS: Poll until condition is true (with max attempts and manual abort)
import { delay } from '../time'
type PollValueCallback<T> = () => Promise<T>
type PollConditionCallback<T> = (t: T) => boolean
interface PollOptions {
ms?: number
max?: number
signal?: AbortSignal
@srph
srph / readme.md
Last active January 9, 2025 06:12
React: Flash success state before reverting back to normal state

Usage

Here's an example of using this hook on top of react-query.

When your react-query mutation succeeds, we'll only flash the success state for 3 seconds until it's reverted back to normal

const isSuccess = useEphemeralState(
  mutation.isSuccess,
  3000,
);
@srph
srph / readme.md
Created October 7, 2024 05:51
Vercel Deploy Script

Vercel Deploy Script

This takes advantage of deployment hooks on Vercel. This simply removes the deploy file if it exists. Otherwise, it creates it. Then pushes it to the repository.

Usage

Add to your package.json

{
@srph
srph / throttle.manual.ts
Last active August 28, 2024 08:27
throttle: Guarantee an async functions only resolves after X ms
import { delay } from '../time'
import { throttle } from './throttle'
// @TODO: In the future, let's write some tests. It's proving to be challenging
// because vitest has no means of flushing promises, and it causes some tests
// to fail (ergo, we expect callback to resolve faster, but it doesn't).
//
// Manual test for throttle function. To run:
// $ npm i -g ts-node && ts-node throttle.manual.ts
const tests = {