Skip to content

Commit d524f13

Browse files
fixes
1 parent 6ff6afa commit d524f13

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

apps/portal/src/content/docs/components/dialog.mdx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,57 @@ The Body component automatically handles scrolling for long content:
352352
</div>`}
353353
/>
354354

355+
### Keep focus on the custom trigger element
356+
357+
If a dialog is opened without the `Trigger` component (e.g., a dropdown item), focus will not return to the trigger element when the dialog is closed. To address this use `useCustomDialogTrigger` hook to register your custom trigger element.
358+
359+
<DocsPage.ComponentExample
360+
client:only
361+
code={`() => {
362+
const [open, setOpen] = React.useState(false);
363+
const { triggerRef, registerTrigger } = useCustomDialogTrigger();
364+
365+
const handleOpenDialog = () => {
366+
registerTrigger();
367+
setOpen(true);
368+
};
369+
370+
return (
371+
372+
<>
373+
<DropdownMenu.Root>
374+
<DropdownMenu.Trigger aria-label="options menu" ref={triggerRef} asChild>
375+
<Button>Open Menu</Button>
376+
</DropdownMenu.Trigger>
377+
378+
<DropdownMenu.Content>
379+
<DropdownMenu.Item title="Open dialog" onSelect={handleOpenDialog} />
380+
</DropdownMenu.Content>
381+
382+
</DropdownMenu.Root>
383+
384+
<Dialog.Root open={open} onOpenChange={setOpen}>
385+
<Dialog.Content>
386+
<Dialog.Header icon="plus-circle">
387+
<Dialog.Title>Content</Dialog.Title>
388+
<Dialog.Description>Dialog opened from the custom trigger</Dialog.Description>
389+
</Dialog.Header>
390+
<Dialog.Body>
391+
<p className="font-body-normal">
392+
Lorem ipsum dolor sit amet, consectetur adipiscing elit
393+
</p>
394+
</Dialog.Body>
395+
<Dialog.Footer>
396+
<Dialog.Close>Close</Dialog.Close>
397+
</Dialog.Footer>
398+
</Dialog.Content>
399+
400+
</Dialog.Root>
401+
402+
</>
403+
)}`}
404+
/>
405+
355406
## `Anatomy`
356407

357408
All parts of the `Dialog` component can be imported and composed as required.

packages/ui/src/components/dialog.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,13 @@ Close.displayName = 'Dialog.Close'
332332
* It registers the trigger element with the dialog focus manager.
333333
*/
334334
const Trigger = forwardRef<HTMLButtonElement, HTMLAttributes<HTMLDivElement> & { children: ReactNode }>(
335-
({ onClick, id, children: _children, ...props }, ref) => {
335+
({ onClick, id, children, ...props }, ref) => {
336336
const triggerId = useTriggerId(id)
337337
const focusManager = useDialogFocusManager()
338338

339339
const dialogContext = useContext(DialogOpenContext)
340340
const isInsideDialog = dialogContext !== undefined
341-
const childrenArray = Children.toArray(_children)
342-
const childrenCount = childrenArray.length
343-
let children = _children
341+
const childrenCount = Children.count(children)
344342

345343
if (childrenCount > 1) {
346344
console.warn('Dialog.Trigger: Only one child is allowed')

0 commit comments

Comments
 (0)