diff --git a/docs/src/app/(public)/(content)/react/components/alert-dialog/page.mdx b/docs/src/app/(public)/(content)/react/components/alert-dialog/page.mdx index 25f84cfb0e..242ad9df4f 100644 --- a/docs/src/app/(public)/(content)/react/components/alert-dialog/page.mdx +++ b/docs/src/app/(public)/(content)/react/components/alert-dialog/page.mdx @@ -35,6 +35,52 @@ import { AlertDialog } from '@base-ui-components/react/alert-dialog'; ## Examples +### Open from a menu + +In order to open a dialog using a menu, control the dialog state and open it imperatively using the `onClick` handler on the menu item. + +Make sure to also use the dialog’s `finalFocus` prop to return focus back to the menu trigger. + +```tsx {12-13,17-18,24-25,28-29} title="Connecting a dialog to a menu" +import * as React from 'react'; +import { AlertDialog } from '@base-ui/components/alert-dialog'; +import { Menu } from '@base-ui/components/menu'; + +function ExampleMenu() { + const menuTriggerRef = React.useRef(null); + const [dialogOpen, setDialogOpen] = React.useState(false); + + return ( + + + {/* Set the trigger ref */} + Open menu + + + + {/* Open the dialog when the menu item is clicked */} + setDialogOpen(true)}>Open dialog + + + + + + {/* Control the dialog state */} + + + + {/* Return focus to the menu trigger when the dialog is closed */} + + {/* prettier-ignore */} + {/* Rest of the dialog */} + + + + + ); +} +``` + ### Close confirmation This example shows a nested confirmation dialog that opens if the text entered in the parent dialog is going to be discarded. diff --git a/docs/src/app/(public)/(content)/react/components/dialog/page.mdx b/docs/src/app/(public)/(content)/react/components/dialog/page.mdx index 20b160a433..8c4d8d0da9 100644 --- a/docs/src/app/(public)/(content)/react/components/dialog/page.mdx +++ b/docs/src/app/(public)/(content)/react/components/dialog/page.mdx @@ -92,6 +92,52 @@ It’s also common to use `onOpenChange` if your app needs to do something when > ``` +### Open from a menu + +In order to open a dialog using a menu, control the dialog state and open it imperatively using the `onClick` handler on the menu item. + +Make sure to also use the dialog’s `finalFocus` prop to return focus back to the menu trigger. + +```tsx {12-13,17-18,24-25,28-29} title="Connecting a dialog to a menu" +import * as React from 'react'; +import { Dialog } from '@base-ui/components/dialog'; +import { Menu } from '@base-ui/components/menu'; + +function ExampleMenu() { + const menuTriggerRef = React.useRef(null); + const [dialogOpen, setDialogOpen] = React.useState(false); + + return ( + + + {/* Set the trigger ref */} + Open menu + + + + {/* Open the dialog when the menu item is clicked */} + setDialogOpen(true)}>Open dialog + + + + + + {/* Control the dialog state */} + + + + {/* Return focus to the menu trigger when the dialog is closed */} + + {/* prettier-ignore */} + {/* Rest of the dialog */} + + + + + ); +} +``` + ### Nested dialogs You can nest dialogs within one another normally. diff --git a/docs/src/app/(public)/(content)/react/components/menu/page.mdx b/docs/src/app/(public)/(content)/react/components/menu/page.mdx index ec997b9f24..5607b5592f 100644 --- a/docs/src/app/(public)/(content)/react/components/menu/page.mdx +++ b/docs/src/app/(public)/(content)/react/components/menu/page.mdx @@ -121,3 +121,49 @@ Use the `render` prop to compose a menu item with an anchor element. ```jsx title="A menu item that opens a link" Go to Projects} /> ``` + +### Open a dialog + +In order to open a dialog using a menu, control the dialog state and open it imperatively using the `onClick` handler on the menu item. + +Make sure to also use the dialog’s `finalFocus` prop to return focus back to the menu trigger. + +```tsx {12-13,17-18,24-25,28-29} title="Connecting a dialog to a menu" +import * as React from 'react'; +import { Dialog } from '@base-ui/components/dialog'; +import { Menu } from '@base-ui/components/menu'; + +function ExampleMenu() { + const menuTriggerRef = React.useRef(null); + const [dialogOpen, setDialogOpen] = React.useState(false); + + return ( + + + {/* Set the trigger ref */} + Open menu + + + + {/* Open the dialog when the menu item is clicked */} + setDialogOpen(true)}>Open dialog + + + + + + {/* Control the dialog state */} + + + + {/* Return focus to the menu trigger when the dialog is closed */} + + {/* prettier-ignore */} + {/* Rest of the dialog */} + + + + + ); +} +``` diff --git a/docs/src/components/Code.css b/docs/src/components/Code.css index 500bdfbf9c..3711dad5da 100644 --- a/docs/src/components/Code.css +++ b/docs/src/components/Code.css @@ -17,17 +17,8 @@ padding-inline: 0.75rem; } - & [data-highlighted-line] { - background-color: var(--color-line-highlight); - } - - & [data-highlighted-line-id='strong'] { - background-color: var(--color-line-highlight-strong); - } - & mark { - color: var(--syntax-default); - background-color: var(--color-inline-highlight); + display: inline-block; } } } diff --git a/docs/src/syntax-highlighting/index.css b/docs/src/syntax-highlighting/index.css index 9a5b25528d..d13c014c3c 100644 --- a/docs/src/syntax-highlighting/index.css +++ b/docs/src/syntax-highlighting/index.css @@ -40,3 +40,16 @@ --syntax-string: var(--color-navy); --syntax-nullish: var(--color-gray-500); } + +[data-highlighted-line] { + background-color: var(--color-line-highlight); +} + +[data-highlighted-line-id='strong'] { + background-color: var(--color-line-highlight-strong); +} + +[data-highlighted-chars] { + color: var(--syntax-default); + background-color: var(--color-inline-highlight); +}