Nest overlays
@codeui/solid v0.3.0
Open a popover, menu, or tooltip inside a dialog.
Kit overlays stack. A menu, popover, select, or tooltip opened inside a dialog becomes a new layer above it. Escape closes the innermost layer only, as Inkstone asks, and focus returns to the control that opened that layer.
Put a segmented control in a dialog
import { createSignal } from "solid-js";
import { Button, Dialog, DialogPanelContent, SegmentedControl, SegmentedControlItem } from "@codeui/solid";
export default function SegmentedInDialog() {
const [open, setOpen] = createSignal(false);
const [format, setFormat] = createSignal("png");
return (
<>
<Button variant="secondary" onClick={() => setOpen(true)}>
Export settings
</Button>
<Dialog open={open()} onOpenChange={setOpen} size="sm" title="Export settings">
<DialogPanelContent>
<SegmentedControl value={format()} onChange={setFormat} aria-label="Format">
<SegmentedControlItem value="png">PNG</SegmentedControlItem>
<SegmentedControlItem value="svg">SVG</SegmentedControlItem>
<SegmentedControlItem value="jpeg">JPEG</SegmentedControlItem>
</SegmentedControl>
</DialogPanelContent>
</Dialog>
</>
);
}Inside a modal dialog, use a SegmentedControl, a Switch, or option cards to pick a value. Inkstone does not use a Select there.
A segmented control is not a layer. When it has focus, Escape goes to the dialog and closes it.
Kobalte's tabs would block that Escape, so SegmentedControl releases it to the enclosing layer. You do not need to do anything.
Keep the dialog open on Escape
import { createSignal } from "solid-js";
import { Button, Dialog, DialogPanelContent, SegmentedControl, SegmentedControlItem } from "@codeui/solid";
export default function KeepOpenOnEscape() {
const [open, setOpen] = createSignal(false);
const [align, setAlign] = createSignal("left");
return (
<>
<Button variant="secondary" onClick={() => setOpen(true)}>
Text alignment
</Button>
<Dialog open={open()} onOpenChange={setOpen} size="sm" title="Text alignment">
<DialogPanelContent>
<SegmentedControl
value={align()}
onChange={setAlign}
aria-label="Alignment"
onEscapeKeyDown={event => event.preventDefault()}
>
<SegmentedControlItem value="left">Left</SegmentedControlItem>
<SegmentedControlItem value="center">Center</SegmentedControlItem>
<SegmentedControlItem value="right">Right</SegmentedControlItem>
</SegmentedControl>
</DialogPanelContent>
</Dialog>
</>
);
}To stop Escape on a segmented control from closing the dialog, call preventDefault() in its onEscapeKeyDown.
To guard the whole dialog instead, for example while a form has unsaved changes, call preventDefault() in the dialog's onEscapeKeyDown.
Open a menu or a tooltip in a dialog
import { createSignal } from "solid-js";
import {
Button,
Dialog,
DialogPanelContent,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuPortal,
DropdownMenuTrigger,
Tooltip,
} from "@codeui/solid";
export default function MenuInDialog() {
const [open, setOpen] = createSignal(false);
const [scale, setScale] = createSignal("2x");
return (
<>
<Button variant="secondary" onClick={() => setOpen(true)}>
Export snippet
</Button>
<Dialog open={open()} onOpenChange={setOpen} size="sm" title="Export snippet">
<DialogPanelContent>
<div style={{ display: "flex", gap: "8px", "align-items": "center" }}>
<DropdownMenu>
<DropdownMenuTrigger as={Button} variant="secondary">
Scale: {scale()}
</DropdownMenuTrigger>
<DropdownMenuPortal>
<DropdownMenuContent>
<DropdownMenuItem onSelect={() => setScale("1x")}>1x</DropdownMenuItem>
<DropdownMenuItem onSelect={() => setScale("2x")}>2x</DropdownMenuItem>
<DropdownMenuItem onSelect={() => setScale("3x")}>3x</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenuPortal>
</DropdownMenu>
<Tooltip content="Higher scales make sharper, larger files" variant="secondary">
<Button variant="secondary">Why scale?</Button>
</Tooltip>
</div>
</DialogPanelContent>
</Dialog>
</>
);
}Open the Scale menu and press Escape. The menu closes, focus returns to the Scale button, and the dialog stays open. Press Escape again to close the dialog.
The same order holds for a Popover, a Select, and a submenu of a DropdownMenu: each Escape closes one layer, from the top down. A Tooltip is a layer too. Focus the Why scale? button and press Escape. The tooltip closes first, and the dialog stays open.
A click outside the menu closes the menu only. A click on the scrim, outside every layer, closes the dialog.
Return focus to the right control
Each layer returns focus to its own trigger when it closes:
- Closing the menu returns focus to the Scale button inside the dialog.
- Closing the dialog returns focus to the Export snippet button on the page.
The ready Dialog has no trigger part. It records the focused element when it opens and returns focus there on close. See focus return without a trigger.
Choose where nested overlays mount
Every kit overlay portals to document.body by default. A menu inside a dialog is not a DOM child of the dialog, but Kobalte still tracks it as a child layer.
So pointer events inside the menu do not count as outside the dialog, and a modal dialog does not hide the menu from assistive technology.
Set mount on Dialog or PopoverContent, or on the Portal part, when the overlay must render inside a themed subtree, for example an element with data-cui-theme.
Nested overlays do not inherit the parent's mount. Pass the same node to each one that needs it.
Related
- Dialog explains focus trapping and
onEscapeKeyDown. - DropdownMenu lists the menu keyboard map.
- Build settings builds a full settings dialog.