Skip to content

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

TypeScript

docs(useClipboard): add a share-link example block (#9507)

Parman Mohammadalizadeh1027ce2063bb
diff --git a/.changeset/use-clipboard-share-link-example.md b/.changeset/use-clipboard-share-link-example.md
new file mode 100644
index 0000000000..cc763a968c
--- /dev/null
+++ b/.changeset/use-clipboard-share-link-example.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[docs] A new `useClipboard — Share Link` example block shows the hook on a share card: Copy writes the invite link, the button reads "Copied" while `isCopied` is true and resets on the hook's timer, and a screen reader hears "Link copied". Copy it with `astryx template useClipboardHookUsage <path>`.
+
+@MannXo
diff --git a/apps/sandbox/scripts/next-export-routes.json b/apps/sandbox/scripts/next-export-routes.json
index 31429a43af..7660143cab 100644
--- a/apps/sandbox/scripts/next-export-routes.json
+++ b/apps/sandbox/scripts/next-export-routes.json
@@ -784,6 +784,7 @@
"/templates/table/",
"/templates/theme-showcase/",
"/templates/useAnnounceHookUsage/",
+ "/templates/useClipboardHookUsage/",
"/templates/useCollatorHookUsage/",
"/templates/useComboboxHookUsage/",
"/templates/useContainerRevealHookUsage/",
diff --git a/internal/shadcn-registry/routes.lock.json b/internal/shadcn-registry/routes.lock.json
index de79a4c914..22178c1ee5 100644
--- a/internal/shadcn-registry/routes.lock.json
+++ b/internal/shadcn-registry/routes.lock.json
@@ -3651,6 +3651,11 @@
"path": "examples/use-app-shell-mobile/custom-mobile-trigger",
"aliases": []
},
+ {
+ "name": "example-use-clipboard-share-link",
+ "path": "examples/use-clipboard/share-link",
+ "aliases": []
+ },
{
"name": "example-use-collapsible-custom-disclosure",
"path": "examples/use-collapsible/custom-disclosure",
diff --git a/packages/cli/api/template/use-clipboard-hook-usage.test.tsx b/packages/cli/api/template/use-clipboard-hook-usage.test.tsx
new file mode 100644
index 0000000000..f2c8941f88
--- /dev/null
+++ b/packages/cli/api/template/use-clipboard-hook-usage.test.tsx
@@ -0,0 +1,92 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+// @vitest-environment jsdom
+
+/**
+ * The useClipboard example copies its invite link through the hook: the
+ * button label follows isCopied, the confirmation is announced politely, and
+ * the label resets when the hook's timer runs out.
+ */
+
+import {act} from 'react';
+import {createRoot, type Root} from 'react-dom/client';
+import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
+import UseClipboardHookUsage from '../../assets/templates/blocks/components/Hooks/useClipboardHookUsage';
+
+const INVITE_LINK = 'https://example.com/invite/q7x2k9';
+
+(globalThis as {IS_REACT_ACT_ENVIRONMENT?: boolean}).IS_REACT_ACT_ENVIRONMENT =
+ true;
+
+let root: Root | null = null;
+let container: HTMLElement;
+
+function renderExample(): void {
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ act(() => {
+ root = createRoot(container);
+ root.render(<UseClipboardHookUsage />);
+ });
+}
+
+function copyButton(): HTMLButtonElement {
+ const button = container.querySelector('button');
+ if (button == null) {
+ throw new Error('no button rendered');
+ }
+ return button;
+}
+
+async function pressCopy(): Promise<void> {
+ await act(async () => {
+ copyButton().click();
+ });
+}
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ // jsdom does not implement the async Clipboard API.
+ Object.assign(navigator, {
+ clipboard: {writeText: vi.fn().mockResolvedValue(undefined)},
+ });
+});
+
+afterEach(() => {
+ act(() => root?.unmount());
+ root = null;
+ container.remove();
+ vi.useRealTimers();
+});
+
+describe('useClipboard example', () => {
+ it('copies the invite link, confirms it and announces it', async () => {
+ renderExample();
+ expect(copyButton().textContent).toBe('Copy');
+ await pressCopy();
+ expect(navigator.clipboard.writeText).toHaveBeenCalledWith(INVITE_LINK);
+ expect(copyButton().textContent).toBe('Copied');
+ act(() => {
+ vi.advanceTimersToNextFrame();
+ });
+ expect(
+ document.querySelector('[data-astryx-live-region="polite"]')?.textContent,
+ ).toBe('Link copied');
+ });
+
+ it('resets the label after the hook timer, restarting on a second copy', async () => {
+ renderExample();
+ await pressCopy();
+ act(() => {
+ vi.advanceTimersByTime(1500);
+ });
+ await pressCopy();
+ act(() => {
+ vi.advanceTimersByTime(1500);
+ });
+ expect(copyButton().textContent).toBe('Copied');
+ act(() => {
+ vi.advanceTimersByTime(500);
+ });
+ expect(copyButton().textContent).toBe('Copy');
+ });
+});
diff --git a/packages/cli/assets/templates/blocks/components/Hooks/useClipboardHookUsage.doc.mjs b/packages/cli/assets/templates/blocks/components/Hooks/useClipboardHookUsage.doc.mjs
new file mode 100644
index 0000000000..958c9102bb
--- /dev/null
+++ b/packages/cli/assets/templates/blocks/components/Hooks/useClipboardHookUsage.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: 'useClipboard',
+ name: 'useClipboard — Share Link',
+ displayName: 'useClipboard — Share Link',
+ description:
+ 'A share card that copies its invite link with useClipboard: the Copy button reads "Copied" while isCopied is true, a screen reader hears "Link copied", and a second press restarts the reset timer.',
+ isReady: true,
+ aspectRatio: 4 / 3,
+ componentsUsed: ['Card', 'Button', 'TextInput', 'Text', 'Layout'],
+};
diff --git a/packages/cli/assets/templates/blocks/components/Hooks/useClipboardHookUsage.tsx b/packages/cli/assets/templates/blocks/components/Hooks/useClipboardHookUsage.tsx
new file mode 100644
index 0000000000..6002fc066a
--- /dev/null
+++ b/packages/cli/assets/templates/blocks/components/Hooks/useClipboardHookUsage.tsx
@@ -0,0 +1,35 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use client';
+
+import {Button} from '@astryxdesign/core/Button';
+import {Card} from '@astryxdesign/core/Card';
+import {useClipboard} from '@astryxdesign/core/hooks';
+import {HStack, StackItem, VStack} from '@astryxdesign/core/Layout';
+import {Text} from '@astryxdesign/core/Text';
+import {TextInput} from '@astryxdesign/core/TextInput';
+
+const INVITE_LINK = 'https://example.com/invite/q7x2k9';
+
+export default function UseClipboardHookUsage() {
+ const {copy, isCopied} = useClipboard({announce: 'Link copied'});
+
+ return (
+ <Card width={420} padding={4}>
+ <VStack gap={3}>
+ <Text type="body" weight="bold">
+ Share
+ </Text>
+ <HStack gap={2} vAlign="end">
+ <StackItem size="fill">
+ <TextInput label="Invite link" value={INVITE_LINK} isReadOnly />
+ </StackItem>
+ <Button
+ label={isCopied ? 'Copied' : 'Copy'}
+ onClick={() => void copy(INVITE_LINK)}
+ />
+ </HStack>
+ </VStack>
+ </Card>
+ );
+}