Skip to content

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

TypeScript

docs(useInteractiveRole): add a four-roles example block (#9972)

Sleep8h1f9ac7198536
diff --git a/.changeset/hook-example-use-interactive-role.md b/.changeset/hook-example-use-interactive-role.md
new file mode 100644
index 0000000000..dcdbdf4947
--- /dev/null
+++ b/.changeset/hook-example-use-interactive-role.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[docs] A `useInteractiveRole — One Tile, Four Roles` example block for the existing hook docs: a polymorphic Tile rendered four ways — href link, onClick button, Popover-trigger context button, inert content — showing the resolution order in one place.
+
+@LetMeSleep8h
diff --git a/apps/sandbox/scripts/next-export-routes.json b/apps/sandbox/scripts/next-export-routes.json
index 90fd10e0c3..40989e4d0b 100644
--- a/apps/sandbox/scripts/next-export-routes.json
+++ b/apps/sandbox/scripts/next-export-routes.json
@@ -815,6 +815,7 @@
"/templates/useImageModeHookUsage/",
"/templates/useInputContainerHookUsage/",
"/templates/useInputStatusIconHookUsage/",
+ "/templates/useInteractiveRoleHookUsage/",
"/templates/useKeyboardHintHookUsage/",
"/templates/useLinkComponentHookUsage/",
"/templates/useListFocusHookUsage/",
diff --git a/internal/shadcn-registry/routes.lock.json b/internal/shadcn-registry/routes.lock.json
index 5361edffcc..f780c10a63 100644
--- a/internal/shadcn-registry/routes.lock.json
+++ b/internal/shadcn-registry/routes.lock.json
@@ -3811,6 +3811,11 @@
"path": "examples/use-input-status-icon/coupon-code-status",
"aliases": []
},
+ {
+ "name": "example-use-interactive-role-one-tile-four-roles",
+ "path": "examples/use-interactive-role/one-tile-four-roles",
+ "aliases": []
+ },
{
"name": "example-use-keyboard-hint-arrow-key-hint",
"path": "examples/use-keyboard-hint/arrow-key-hint",
diff --git a/packages/cli/api/template/use-interactive-role-hook-usage.test.tsx b/packages/cli/api/template/use-interactive-role-hook-usage.test.tsx
new file mode 100644
index 0000000000..af9424e8d1
--- /dev/null
+++ b/packages/cli/api/template/use-interactive-role-hook-usage.test.tsx
@@ -0,0 +1,76 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+// @vitest-environment jsdom
+
+/**
+ * The useInteractiveRole example renders one Tile component four ways and the
+ * hook alone decides the element: href → an anchor, onClick → a button, no
+ * props inside Popover → a button through the trigger context, and no props
+ * anywhere → inert content with no interactive element.
+ */
+
+import {act} from 'react';
+import {createRoot, type Root} from 'react-dom/client';
+import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
+import UseInteractiveRoleHookUsage from '../../assets/templates/blocks/components/Hooks/useInteractiveRoleHookUsage';
+
+(globalThis as {IS_REACT_ACT_ENVIRONMENT?: boolean}).IS_REACT_ACT_ENVIRONMENT =
+ true;
+
+let root: Root | null = null;
+let container: HTMLElement;
+
+beforeEach(() => {
+ vi.stubGlobal(
+ 'matchMedia',
+ (query: string) =>
+ ({
+ matches: false,
+ media: query,
+ onchange: null,
+ addListener: () => {},
+ removeListener: () => {},
+ addEventListener: () => {},
+ removeEventListener: () => {},
+ dispatchEvent: () => false,
+ }) as unknown as MediaQueryList,
+ );
+ container = document.createElement('div');
+ document.body.appendChild(container);
+});
+
+afterEach(() => {
+ act(() => root?.unmount());
+ root = null;
+ container.remove();
+ vi.unstubAllGlobals();
+});
+
+describe('useInteractiveRoleHookUsage example', () => {
+ it('resolves the element from the inputs: link, button, context button, inert', () => {
+ root = createRoot(container);
+ act(() => root!.render(<UseInteractiveRoleHookUsage />));
+
+ const anchors = Array.from(container.querySelectorAll('a'));
+ const buttons = Array.from(container.querySelectorAll('button'));
+
+ // href → an anchor to the docs URL.
+ expect(
+ anchors.some(a => a.getAttribute('href') === 'https://example.com/docs'),
+ ).toBe(true);
+
+ // onClick → a real button; Popover context → a real button. Exactly two.
+ const tileButtons = buttons.filter(b =>
+ ['Renders as a button', 'Trigger via context'].includes(
+ b.textContent ?? '',
+ ),
+ );
+ expect(tileButtons).toHaveLength(2);
+
+ // The plain tile renders no interactive element around its card.
+ const plain = Array.from(container.querySelectorAll('*')).find(
+ el => el.textContent === 'Plain content',
+ );
+ expect(plain).toBeDefined();
+ expect(plain!.closest('a, button')).toBeNull();
+ });
+});
diff --git a/packages/cli/assets/templates/blocks/components/Hooks/useInteractiveRoleHookUsage.doc.mjs b/packages/cli/assets/templates/blocks/components/Hooks/useInteractiveRoleHookUsage.doc.mjs
new file mode 100644
index 0000000000..7281f5eace
--- /dev/null
+++ b/packages/cli/assets/templates/blocks/components/Hooks/useInteractiveRoleHookUsage.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: 'useInteractiveRole',
+ name: 'useInteractiveRole — One Tile, Four Roles',
+ displayName: 'useInteractiveRole — One Tile, Four Roles',
+ description:
+ 'A polymorphic Tile used four ways: with an href it renders as a link, with an onClick as a button, with neither as plain content, and placed as a Popover trigger it becomes the trigger button through context — the resolution order in one place.',
+ isReady: true,
+ aspectRatio: 4 / 3,
+ componentsUsed: ['Popover', 'Card', 'Text', 'Layout', 'Link'],
+};
diff --git a/packages/cli/assets/templates/blocks/components/Hooks/useInteractiveRoleHookUsage.tsx b/packages/cli/assets/templates/blocks/components/Hooks/useInteractiveRoleHookUsage.tsx
new file mode 100644
index 0000000000..7d09565c79
--- /dev/null
+++ b/packages/cli/assets/templates/blocks/components/Hooks/useInteractiveRoleHookUsage.tsx
@@ -0,0 +1,56 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use client';
+
+import {useInteractiveRole} from '@astryxdesign/core/hooks';
+import {Link} from '@astryxdesign/core/Link';
+import {Popover} from '@astryxdesign/core/Popover';
+import {Card} from '@astryxdesign/core/Card';
+import {Text} from '@astryxdesign/core/Text';
+import {VStack} from '@astryxdesign/core/Layout';
+
+// A polymorphic Tile: the hook alone decides what it renders as — href wins,
+// then onClick, then a trigger context (Popover), then inert.
+function Tile({
+ href,
+ onClick,
+ children,
+}: {
+ href?: string;
+ onClick?: () => void;
+ children: React.ReactNode;
+}) {
+ const role = useInteractiveRole({href, onClick});
+ const body = (
+ <Card padding={3} width={200}>
+ <Text type="body">{children}</Text>
+ </Card>
+ );
+ switch (role) {
+ case 'link':
+ return <Link href={href}>{body}</Link>;
+ case 'button':
+ return <button onClick={onClick}>{body}</button>;
+ default:
+ return body;
+ }
+}
+
+export default function UseInteractiveRoleHookUsage() {
+ return (
+ <VStack gap={3} hAlign="center" style={{width: '100%'}}>
+ <Tile href="https://example.com/docs">Renders as a link</Tile>
+ <Tile onClick={() => {}}>Renders as a button</Tile>
+ <Popover
+ label="Tile details"
+ content={
+ <Card padding={3}>
+ <Text type="body">Opened through the tile's context role</Text>
+ </Card>
+ }>
+ <Tile>Trigger via context</Tile>
+ </Popover>
+ <Tile>Plain content</Tile>
+ </VStack>
+ );
+}