Skip to content

Commit c8a172b

Browse files
MannXocixzhang
andauthored
docs(useOverlay): add hook docs and a product quick view example block (#9908)
* docs(useOverlay): add hook docs and a product quick view example block Fixes #9559 * Merge main into the useOverlay product quick view example * docs(useOverlay): show Quick view in a bottom strip so the image link stays clickable A revealed fill scrim takes pointer input over the whole image, so a mouse click on the image never reached its link. The hook doc now says so. * Merge main into docs/9559-useoverlay-example Resolves the generated route lists (the union of both sides' entries, in generator format) and refreshes any generated goldens; no other change. Co-authored-by: Cindy Zhang <cindyxz@meta.com>
1 parent 9e7567c commit c8a172b

8 files changed

Lines changed: 366 additions & 1 deletion

File tree

‎.changeset/hook-doc-use-overlay.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@astryxdesign/core': patch
3+
'@astryxdesign/cli': patch
4+
---
5+
6+
[docs] Hook docs for `useOverlay`, so `astryx hook useOverlay` and `astryx search` return it, and a `useOverlay — Product Quick View` example block: a product Card spreads the hook's `containerRef` and `containerProps` and renders the returned scrim once over its image. Hovering the card, or moving keyboard focus into it, reveals a Quick view button, and the image link before it lets Tab reach the button. Copy it with `astryx template useOverlayHookUsage <path>`.
7+
8+
@MannXo

‎apps/sandbox/scripts/next-export-routes.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@
807807
"/templates/useMergedRefsHookUsage/",
808808
"/templates/useOutlineFromMarkdownHookUsage/",
809809
"/templates/useOverflowHookUsage/",
810+
"/templates/useOverlayHookUsage/",
810811
"/templates/usePressFeedbackHookUsage/",
811812
"/templates/useScrollOverflowHookUsage/",
812813
"/templates/useScrollableAreaHookUsage/",

‎internal/shadcn-registry/routes.lock.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3776,6 +3776,11 @@
37763776
"path": "examples/use-overflow/editor-toolbar",
37773777
"aliases": []
37783778
},
3779+
{
3780+
"name": "example-use-overlay-product-quick-view",
3781+
"path": "examples/use-overlay/product-quick-view",
3782+
"aliases": []
3783+
},
37793784
{
37803785
"name": "example-use-pan-gesture-a-draggable-box",
37813786
"path": "examples/use-pan-gesture/a-draggable-box",
@@ -4096,6 +4101,11 @@
40964101
"path": "hooks/use-overflow",
40974102
"aliases": []
40984103
},
4104+
{
4105+
"name": "hook-use-overlay",
4106+
"path": "hooks/use-overlay",
4107+
"aliases": []
4108+
},
40994109
{
41004110
"name": "hook-use-pan-gesture",
41014111
"path": "hooks/use-pan-gesture",

‎packages/cli/api/component/__fixtures__/resolutions/modules.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ useMenuHover useMenuHover hooks/useAnimatedLayout.doc.mjs hooks/useMenuHover.ts
279279
useMenuOverflow useMenuOverflow DropdownMenu/DropdownMenu.doc.mjs DropdownMenu/useMenuOverflow.ts
280280
useMobileKeyboard ERR_UNKNOWN_COMPONENT - keyboard/useMobileKeyboard.ts
281281
useOutlineFromDOM useOutlineFromDOM Outline/Outline.doc.mjs Outline/useOutlineFromDOM.ts
282-
useOverlay useOverlay Overlay/Overlay.doc.mjs Overlay/useOverlay.tsx
282+
useOverlay useOverlay Overlay/useOverlay.doc.mjs Overlay/useOverlay.tsx
283283
useOwnScrollGesture useOwnScrollGesture DateInput/DateInput.doc.mjs DateInput/useOwnScrollGesture.ts
284284
usePager usePager hooks/useAnimatedLayout.doc.mjs hooks/usePager.ts
285285
usePointerDragScroll usePointerDragScroll DateInput/DateInput.doc.mjs DateInput/usePointerDragScroll.ts
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
// @vitest-environment jsdom
3+
4+
/**
5+
* The useOverlay example spreads the hook's container ref and props on a
6+
* product Card and renders the returned scrim once over the image. Hover and
7+
* focus reveal are CSS, which jsdom does not apply, so this checks the wiring
8+
* and the tab order that keyboard users depend on.
9+
*/
10+
11+
import {act} from 'react';
12+
import {createRoot, type Root} from 'react-dom/client';
13+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
14+
import {useOverlay} from '@astryxdesign/core/Overlay';
15+
import UseOverlayHookUsage from '../../assets/templates/blocks/components/Overlay/useOverlayHookUsage';
16+
17+
(globalThis as {IS_REACT_ACT_ENVIRONMENT?: boolean}).IS_REACT_ACT_ENVIRONMENT =
18+
true;
19+
20+
let root: Root | null = null;
21+
let container: HTMLElement;
22+
23+
function overlayScopeClasses(): string[] {
24+
let className = '';
25+
function Probe() {
26+
className = useOverlay().containerProps.className ?? '';
27+
return null;
28+
}
29+
const probeRoot = createRoot(document.createElement('div'));
30+
act(() => probeRoot.render(<Probe />));
31+
act(() => probeRoot.unmount());
32+
return className.split(' ').filter(Boolean);
33+
}
34+
35+
function quickView(): HTMLButtonElement {
36+
const button = Array.from(container.querySelectorAll('button')).find(
37+
candidate => candidate.textContent === 'Quick view',
38+
);
39+
if (button == null) {
40+
throw new Error('no Quick view button rendered');
41+
}
42+
return button;
43+
}
44+
45+
beforeEach(() => {
46+
// A device with a mouse, so hover reveal is CSS-driven.
47+
vi.stubGlobal('matchMedia', (query: string) => ({
48+
matches: false,
49+
media: query,
50+
addEventListener: () => {},
51+
removeEventListener: () => {},
52+
}));
53+
container = document.createElement('div');
54+
document.body.appendChild(container);
55+
act(() => {
56+
root = createRoot(container);
57+
root.render(<UseOverlayHookUsage />);
58+
});
59+
});
60+
61+
afterEach(() => {
62+
act(() => root?.unmount());
63+
root = null;
64+
container.remove();
65+
vi.unstubAllGlobals();
66+
});
67+
68+
describe('useOverlay example', () => {
69+
it('renders one Quick view scrim inside the card that carries the container props', () => {
70+
const card = container.firstElementChild as HTMLElement;
71+
const scope = overlayScopeClasses();
72+
expect(scope.length).toBeGreaterThan(0);
73+
for (const name of scope) {
74+
expect(card.classList.contains(name)).toBe(true);
75+
}
76+
expect(card.contains(quickView())).toBe(true);
77+
expect(
78+
Array.from(container.querySelectorAll('button')).filter(
79+
button => button.textContent === 'Quick view',
80+
),
81+
).toHaveLength(1);
82+
});
83+
84+
it('shows Quick view in a bottom strip, which leaves the image link above it clickable', () => {
85+
const scrim = quickView().closest('.astryx-overlay-scrim');
86+
expect(scrim?.getAttribute('data-position')).toBe('bottom');
87+
});
88+
89+
it('puts the image link before Quick view in tab order, so focus can reveal it', () => {
90+
const link = container.querySelector('a');
91+
if (link == null) {
92+
throw new Error('no image link rendered');
93+
}
94+
expect(link.getAttribute('href')).toBe('#ceramic-mug');
95+
expect(
96+
link.compareDocumentPosition(quickView()) &
97+
Node.DOCUMENT_POSITION_FOLLOWING,
98+
).toBeTruthy();
99+
});
100+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
4+
export const doc = {
5+
type: 'block',
6+
exampleFor: 'useOverlay',
7+
name: 'useOverlay — Product Quick View',
8+
displayName: 'useOverlay — Product Quick View',
9+
description:
10+
'A product card that is its own overlay container: useOverlay returns the ref and props to spread on the Card and a bottom strip to render once over the image. Hovering the card, or moving keyboard focus into it, slides in a Quick view button along the bottom of the image, and leaving hides it again. The image is a link that the strip leaves clickable above it, and Tab lands on it first so the next Tab reaches Quick view.',
11+
isReady: true,
12+
aspectRatio: 4 / 3,
13+
componentsUsed: [
14+
'Overlay',
15+
'Card',
16+
'AspectRatio',
17+
'Button',
18+
'Layout',
19+
'Text',
20+
],
21+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
'use client';
4+
5+
import type {RefObject} from 'react';
6+
import {AspectRatio} from '@astryxdesign/core/AspectRatio';
7+
import {Button} from '@astryxdesign/core/Button';
8+
import {Card} from '@astryxdesign/core/Card';
9+
import {VStack} from '@astryxdesign/core/Layout';
10+
import {useOverlay} from '@astryxdesign/core/Overlay';
11+
import {Text} from '@astryxdesign/core/Text';
12+
13+
export default function UseOverlayHookUsage() {
14+
const overlay = useOverlay({
15+
showOn: 'hover',
16+
position: 'bottom',
17+
align: 'center',
18+
content: <Button label="Quick view" variant="secondary" size="sm" />,
19+
});
20+
21+
return (
22+
<Card
23+
width={280}
24+
padding={0}
25+
ref={overlay.containerRef as RefObject<HTMLDivElement | null>}
26+
{...overlay.containerProps}>
27+
<div style={{position: 'relative'}}>
28+
<a href="#ceramic-mug">
29+
<AspectRatio ratio={1}>
30+
<img
31+
src="/template-assets/light-product-1.png"
32+
alt="Ceramic mug"
33+
style={{width: '100%', height: '100%', objectFit: 'cover'}}
34+
/>
35+
</AspectRatio>
36+
</a>
37+
{overlay.element}
38+
</div>
39+
<VStack gap={1} padding={3}>
40+
<Text type="body" weight="bold">
41+
Ceramic mug
42+
</Text>
43+
<Text type="supporting" color="secondary">
44+
$24
45+
</Text>
46+
</VStack>
47+
</Card>
48+
);
49+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/** @type {import('@astryxdesign/cli/authoring').HookDoc} */
4+
export const docs = {
5+
name: 'useOverlay',
6+
displayName: 'useOverlay',
7+
group: 'Overlay',
8+
keywords: [
9+
'overlay',
10+
'scrim',
11+
'hover reveal',
12+
'focus-within',
13+
'quick view',
14+
'card actions',
15+
'media overlay',
16+
],
17+
params: [
18+
{
19+
name: 'options',
20+
type: 'UseOverlayOptions',
21+
description: 'Overlay configuration. Every field is optional.',
22+
required: false,
23+
},
24+
{
25+
name: 'options.content',
26+
type: 'ReactNode',
27+
description:
28+
'Content to render inside the scrim. When set, the hook returns it pre-rendered as element.',
29+
required: false,
30+
},
31+
{
32+
name: 'options.showOn',
33+
type: "'hover' | 'always' | 'focus' | 'hover-or-focus'",
34+
description:
35+
"Visibility trigger, read with CSS from the container. 'hover' also reveals on focus within the container for keyboard users; 'hover-or-focus' is an alias for it. On a touch device (hover: none), a hover overlay toggles on a tap on the container instead.",
36+
default: "'always'",
37+
required: false,
38+
},
39+
{
40+
name: 'options.isOpen',
41+
type: 'boolean',
42+
description:
43+
'Controlled visibility. When set, it takes precedence over showOn and the touch toggle.',
44+
required: false,
45+
},
46+
{
47+
name: 'options.scrim',
48+
type: "'dark' | 'light' | false",
49+
description:
50+
'Scrim background. false renders the content with no scrim background.',
51+
default: "'dark'",
52+
required: false,
53+
},
54+
{
55+
name: 'options.position',
56+
type: "'fill' | 'bottom' | 'top'",
57+
description:
58+
'Where the scrim sits: over the whole positioned area, or as a strip along its bottom or top edge.',
59+
default: "'fill'",
60+
required: false,
61+
},
62+
{
63+
name: 'options.align',
64+
type: "'start' | 'center' | 'end'",
65+
description: 'Alignment of the content within the scrim.',
66+
default: "'end'",
67+
required: false,
68+
},
69+
],
70+
returns: [
71+
{
72+
name: 'containerRef',
73+
type: 'RefObject<HTMLElement | null>',
74+
description: 'Attach to the container element.',
75+
},
76+
{
77+
name: 'containerProps',
78+
type: 'OverlayContainerProps',
79+
description:
80+
'Spread on the same container: className and style mark it as the overlay scope (positioned, clipped), and on a touch device with a hover overlay onClick and onMouseUp toggle it on a tap.',
81+
},
82+
{
83+
name: 'element',
84+
type: 'ReactElement | null',
85+
description:
86+
'The scrim with options.content, ready to render once inside the container. null when content is not set.',
87+
},
88+
{
89+
name: 'renderOverlay',
90+
type: '(children: ReactNode) => ReactElement',
91+
description:
92+
'Renders the scrim around the children you pass, for content that changes or a scrim you mount yourself. Use it or element, not both.',
93+
},
94+
],
95+
usage: {
96+
description:
97+
"Adds overlay behavior to a container you already render, such as a Card or a custom media frame, without the wrapper Overlay adds. Spread containerRef and containerProps on the container, then render element (or renderOverlay(children)) once inside it. The scrim is absolutely positioned, so it covers the nearest positioned ancestor: the container itself, or a positioned section inside it such as an image area. Visibility is CSS-driven from the container's :hover and :focus-within, so with showOn 'hover' a keyboard user reveals the content by moving focus into the container. Hidden content is not focusable, so the container needs a focusable element before the overlay content in tab order. A revealed scrim takes pointer input over the area it covers, so put that element outside the scrim, or use a top or bottom strip and keep it in the part the strip leaves uncovered.",
98+
bestPractices: [
99+
{
100+
guidance: true,
101+
description:
102+
"Use showOn 'hover' for actions on media cards: it also reveals on keyboard focus within the container.",
103+
},
104+
{
105+
guidance: true,
106+
description:
107+
'Put a focusable element (a link or the card title) before the overlay content in the container, so Tab reveals the content and the next Tab reaches it.',
108+
},
109+
{
110+
guidance: true,
111+
description:
112+
'Use the Overlay component when you do not already have a container element to attach to.',
113+
},
114+
{
115+
guidance: false,
116+
description:
117+
'Put the only way to reach an action behind hover with nothing focusable before it; keyboard users cannot focus content that is hidden.',
118+
},
119+
],
120+
},
121+
relatedComponents: ['Overlay', 'Card', 'AspectRatio'],
122+
relatedHooks: ['useClickableContainer'],
123+
importPath: '@astryxdesign/core/Overlay',
124+
category: 'interaction',
125+
};
126+
127+
/** @type {import('@astryxdesign/cli/authoring').HookTranslationDoc} */
128+
export const docsDense = {
129+
description:
130+
"Overlay behavior on a container you already render (Card, media frame) w/o Overlay's wrapper. Spread containerRef + containerProps on it, render element / renderOverlay(children) once inside. CSS-driven via container :hover/:focus-within; hover mode also reveals on keyboard focus. Hidden content is not focusable, so put a focusable element before it, outside the area a revealed scrim covers (the scrim takes pointer input).",
131+
paramDescriptions: {
132+
options: 'overlay config; all fields optional.',
133+
'options.content':
134+
'content inside the scrim; returned pre-rendered as element.',
135+
'options.showOn':
136+
"CSS visibility trigger. 'hover' also reveals on focus-within; 'hover-or-focus' = alias. Touch (hover: none): hover overlays toggle on tap.",
137+
'options.isOpen': 'controlled visibility; wins over showOn + touch toggle.',
138+
'options.scrim': 'scrim background; false = no background.',
139+
'options.position': "scrim placement: 'fill', or a 'bottom' / 'top' strip.",
140+
'options.align': 'content alignment in the scrim.',
141+
},
142+
returnDescriptions: {
143+
containerRef: 'attach to the container.',
144+
containerProps:
145+
'spread on the container: overlay scope className/style (positioned, clipped) + tap toggle handlers for hover overlays on touch devices.',
146+
element:
147+
'scrim with content, render once inside the container; null w/o content.',
148+
renderOverlay:
149+
'(children) => scrim; for dynamic content or self-mounted scrim. Use it or element.',
150+
},
151+
usage: {
152+
description:
153+
'Overlay behavior on an existing container: spread containerRef + containerProps, render element once inside. Scrim covers nearest positioned ancestor. CSS :hover/:focus-within; hover mode reveals on keyboard focus too. Put a focusable element before hidden content.',
154+
bestPractices: [
155+
{
156+
guidance: true,
157+
description:
158+
"showOn 'hover' for media card actions; also reveals on keyboard focus.",
159+
},
160+
{
161+
guidance: true,
162+
description:
163+
'Put a focusable element before the overlay content so Tab reveals it, next Tab reaches it.',
164+
},
165+
{
166+
guidance: true,
167+
description: 'Use Overlay when there is no container to attach to.',
168+
},
169+
{
170+
guidance: false,
171+
description:
172+
'Hide the only route to an action behind hover with nothing focusable before it.',
173+
},
174+
],
175+
},
176+
};

0 commit comments

Comments
 (0)