Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/use-merged-refs-example.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions apps/sandbox/scripts/next-export-routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@
"/templates/useKeyboardHintHookUsage/",
"/templates/useListFocusHookUsage/",
"/templates/useLocaleHookUsage/",
"/templates/useMergedRefsHookUsage/",
"/templates/useOutlineFromMarkdownHookUsage/",
"/templates/useOverflowHookUsage/",
"/templates/usePressFeedbackHookUsage/",
Expand Down
5 changes: 5 additions & 0 deletions internal/shadcn-registry/routes.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -3721,6 +3721,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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
export const doc = {
type: 'block',
exampleFor: 'useMergedRefs',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block adds the shadcn item example-use-merged-refs-auto-grow-text-area (examples/use-merged-refs/auto-grow-text-area). It is not in internal/shadcn-registry/routes.lock.json, so apps/docsite/scripts/generate-data.mjs stops with "Generated ShadCN names or routes changed" and the docs build fails. Run UPDATE_SHADCN_ROUTE_LOCK=1 node apps/docsite/scripts/generate-data.mjs and commit the one entry it adds.

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'],
};
Original file line number Diff line number Diff line change
@@ -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(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a note that names what this Effect syncs and why it runs after commit (react-component-runtime INV3), as the other hook examples do. For example:

// Syncs the textarea's inline height with the committed value.
// scrollHeight is right only after React writes the new text.

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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: the textarea has no label, so its accessible name is this placeholder, which never shows because the box starts filled. An aria-label that names the field fixes that for readers who copy the pattern.

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>
);
}
Loading