Skip to content

Instantly share code, notes, and snippets.

View carloitaben's full-sized avatar

Carlo carloitaben

View GitHub Profile
@carloitaben
carloitaben / cookies.ts
Last active January 16, 2025 13:32
TanStack Start isomorphic cookies
import * as v from "valibot"
import { AnyJSONString, JSONStringify } from "@artists-together/core/schemas"
import { createIsomorphicFn } from "@tanstack/start"
import { deleteCookie, getCookie, setCookie } from "vinxi/http"
import type { CookieSerializeOptions } from "cookie-es"
import { parse, serialize } from "cookie-es"
export function getDocumentCookie(name: string): string | undefined {
return parse(document.cookie)[name]
}
@carloitaben
carloitaben / merge-refs.ts
Created December 9, 2024 21:26
React 19 merge refs
import type { Ref } from "react"
function mergeRefs<T>(...refs: (Ref<T> | undefined)[]): Ref<T> {
return (value) => {
const cleanups = refs.reduce<VoidFunction[]>((accumulator, ref) => {
if (typeof ref === "function") {
const cleanup = ref(value)
if (typeof cleanup === "function") {
accumulator.push(cleanup)
}
@carloitaben
carloitaben / TransitionRouter.tsx
Created October 29, 2024 10:39
React 19 page transitions
import type { ReactNode } from "react"
import { useEffect, useState, useTransition } from "react"
import { createSafeContext } from "@/lib/context"
type TransitionRouterStage = "entering" | "leaving" | undefined
type TransitionRouterStartFunction = (
callback: () => void | Promise<void>
) => void