useMergedRef
Merges several refs into one callback ref.
useMergedRef(a, b) returns one ref to put on an element when two parts of the code each need that element: a component that keeps its own ref and also takes the caller's ref prop, or a measuring callback beside a ref that moves focus. It takes object refs, ref callbacks (their cleanup function is kept) and undefined, in any number.
Two refs on one field
"use client"
import { useRef, type ComponentProps } from "react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { useMergedRef } from "@/hooks/use-merged-ref"
/** A field that needs the input itself, to select its text on focus, and still takes the caller's ref. */
function SelectAllInput({ ref, ...props }: ComponentProps<typeof Input>) {
const own = useRef<HTMLInputElement>(null)
const merged = useMergedRef(own, ref)
return <Input ref={merged} onFocus={() => own.current?.select()} {...props} />
}
export default function UseMergedRefTwoRefsOnOneField() {
const field = useRef<HTMLInputElement>(null)
return (
<div className="flex w-full max-w-xs items-end gap-2">
<div className="flex min-w-0 flex-1 flex-col gap-1.5">
<Label htmlFor="merged-ref-project-key">Project key</Label>
<SelectAllInput id="merged-ref-project-key" ref={field} defaultValue="ATL" />
</div>
<Button variant="outline" onClick={() => field.current?.focus()}>
Edit the key
</Button>
</div>
)
}
A ref callback with cleanup
Measuring
"use client"
import { useCallback, useRef, useState } from "react"
import { Button } from "@/components/ui/button"
import { Label } from "@/components/ui/label"
import { Textarea } from "@/components/ui/textarea"
import { useMergedRef } from "@/hooks/use-merged-ref"
export default function UseMergedRefARefCallbackWithCleanup() {
const [height, setHeight] = useState<number | null>(null)
// A ref callback that watches the node and returns its own cleanup.
const measure = useCallback((node: HTMLTextAreaElement | null) => {
if (!node) return
const observer = new ResizeObserver(() => setHeight(Math.round(node.getBoundingClientRect().height)))
observer.observe(node)
return () => observer.disconnect()
}, [])
const note = useRef<HTMLTextAreaElement>(null)
const ref = useMergedRef(measure, note)
return (
<div className="flex w-full max-w-xs flex-col gap-1.5">
<Label htmlFor="merged-ref-note">Release note</Label>
<Textarea
id="merged-ref-note"
ref={ref}
defaultValue="Offline drafts are ready. Write more lines and the field grows."
/>
<div className="flex items-center justify-between gap-2">
<span className="text-sm text-muted-foreground tabular-nums">
{height === null ? "Measuring" : `${height}px tall`}
</span>
<Button variant="outline" onClick={() => note.current?.focus()}>
Write the note
</Button>
</div>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/use-merged-refbunx --bun shadcn@latest add @khaeli/use-merged-refInstalls with
Nothing else. This item needs no other Registry item and no npm package.
Accessible name
- The hook adds nothing to the page. A button that moves focus through a merged ref says where focus goes: Edit the key, not Focus.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/use-merged-ref.json