Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

docs(hooks): add useMergedRefs AutoGrowTextArea example block (#9938)

B Sainath Reddyb2659fea5de0
diff --git a/.changeset/use-merged-refs-example.md b/.changeset/use-merged-refs-example.md
new file mode 100644
index 0000000000..e85c4099b5
--- /dev/null
+++ b/.changeset/use-merged-refs-example.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[docs] A `useMergedRefs — AutoGrowTextArea` example block: an AutoGrowTextArea forwards the caller's ref and keeps its own ref to measure its height, and the merged ref attaches both to the same textarea, so it resizes itself while a parent button focuses it through the forwarded ref. Copy it with `astryx template useMergedRefsHookUsage <path>`.
+
+@Sainath-Reddy7
diff --git a/apps/sandbox/scripts/next-export-routes.json b/apps/sandbox/scripts/next-export-routes.json
index 1914120a28..015c01b26e 100644
--- a/apps/sandbox/scripts/next-export-routes.json
+++ b/apps/sandbox/scripts/next-export-routes.json
@@ -804,6 +804,7 @@
"/templates/useLinkComponentHookUsage/",
"/templates/useListFocusHookUsage/",
"/templates/useLocaleHookUsage/",
+ "/templates/useMergedRefsHookUsage/",
"/templates/useOutlineFromMarkdownHookUsage/",
"/templates/useOverflowHookUsage/",
"/templates/usePressFeedbackHookUsage/",
diff --git a/internal/shadcn-registry/routes.lock.json b/internal/shadcn-registry/routes.lock.json
index 7c60cf109f..b8da3bf073 100644
--- a/internal/shadcn-registry/routes.lock.json
+++ b/internal/shadcn-registry/routes.lock.json
@@ -3766,6 +3766,11 @@
"path": "examples/use-locale/invoice-summary",
"aliases": []
},
+ {
+ "name": "example-use-merged-refs-auto-grow-text-area",
+ "path": "examples/use-merged-refs/auto-grow-text-area",
+ "aliases": []
+ },
{
"name": "example-use-outline-from-markdown-docs-article",
"path": "examples/use-outline-from-markdown/docs-article",
diff --git a/packages/cli/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.doc.mjs b/packages/cli/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.doc.mjs
new file mode 100644
index 0000000000..878a42c735
--- /dev/null
+++ b/packages/cli/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.doc.mjs
@@ -0,0 +1,14 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
+export const doc = {
+ type: 'block',
+ exampleFor: 'useMergedRefs',
+ name: 'useMergedRefs — AutoGrowTextArea',
+ displayName: 'useMergedRefs — AutoGrowTextArea',
+ description:
+ "An AutoGrowTextArea that forwards the caller's ref and also keeps its own ref to measure scrollHeight. The merged ref attaches both to the same textarea: it resizes itself while a parent button focuses it through the forwarded ref.",
+ isReady: true,
+ aspectRatio: 4 / 3,
+ componentsUsed: ['Card', 'Button', 'Layout'],
+};
diff --git a/packages/cli/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.tsx b/packages/cli/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.tsx
new file mode 100644
index 0000000000..e5e78fff3a
--- /dev/null
+++ b/packages/cli/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.tsx
@@ -0,0 +1,78 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use client';
+
+import {forwardRef, useEffect, useRef, useState} from 'react';
+import {Button} from '@astryxdesign/core/Button';
+import {Card} from '@astryxdesign/core/Card';
+import {useMergedRefs} from '@astryxdesign/core/hooks';
+import {VStack} from '@astryxdesign/core/Layout';
+
+type AutoGrowTextAreaProps = {
+ value: string;
+ onChange: (value: string) => void;
+};
+
+// Forwards the caller's ref and also keeps its own ref to measure
+// scrollHeight; useMergedRefs attaches both to the same textarea.
+const AutoGrowTextArea = forwardRef<HTMLTextAreaElement, AutoGrowTextAreaProps>(
+ function AutoGrowTextArea({value, onChange}, forwardedRef) {
+ const keptRef = useRef<HTMLTextAreaElement>(null);
+ const mergedRef = useMergedRefs(forwardedRef, keptRef);
+
+ // Syncs the textarea's inline height with the committed value.
+ // scrollHeight is right only after React writes the new text.
+ useEffect(() => {
+ const el = keptRef.current;
+ if (!el) {
+ return;
+ }
+ el.style.height = 'auto';
+ el.style.height = `${el.scrollHeight}px`;
+ }, [value]);
+
+ return (
+ <textarea
+ ref={mergedRef}
+ value={value}
+ onChange={event => onChange(event.target.value)}
+ rows={1}
+ aria-label="Auto-growing message"
+ placeholder="Type — the box grows itself"
+ style={{
+ width: '100%',
+ padding: 8,
+ borderRadius: 8,
+ border: '1px solid',
+ resize: 'none',
+ overflow: 'hidden',
+ fontFamily: 'inherit',
+ fontSize: 14,
+ lineHeight: '20px',
+ color: 'inherit',
+ background: 'transparent',
+ }}
+ />
+ );
+ },
+);
+
+export default function UseMergedRefsHookUsage() {
+ const areaRef = useRef<HTMLTextAreaElement>(null);
+ const [value, setValue] = useState(
+ 'This box keeps its own ref to measure and grow. The button focuses it through the forwarded ref.',
+ );
+
+ return (
+ <Card width={360} padding={4}>
+ <VStack gap={3}>
+ <AutoGrowTextArea ref={areaRef} value={value} onChange={setValue} />
+ <Button
+ label="Focus via forwarded ref"
+ size="sm"
+ onClick={() => areaRef.current?.focus()}
+ />
+ </VStack>
+ </Card>
+ );
+}