Alert
A glass panel centered in the window over a dim. Its glass is interactive: it swells, lights and leans after a finger.
import { Alert } from "@adamexu/glass";Actions
As in SwiftUI, the alert shows while isPresented is true, and onDismiss is how it asks to go: after any of its buttons runs its action, set your state back to false. With no actions an alert has one OK button. Two short actions share a row with the cancel action first; more stack, with the cancel action last.
Presentations center in a window. With no PresentationRoot it is the browser's, and its width picks the layout: 668 px or wider is an iPad's. Each phone frame on these pages is its own window, <PresentationRoot idiom="phone" window={frameRect} root={frameElement}> around its content, so the alert centers and dims in the frame.
const [shown, setShown] = useState<"two" | "stacked" | "ok" | null>(null);
const close = () => setShown(null);
<Button buttonStyle="glass" onClick={() => setShown("two")}>Two Actions</Button>
<Button buttonStyle="glass" onClick={() => setShown("stacked")}>Stacked</Button>
<Button buttonStyle="glass" onClick={() => setShown("ok")}>Default OK</Button>
<Alert
title="Delete Album?"
message="Its items stay in your library."
actions={[
{ title: "Delete", role: "destructive", action: () => setLast("Delete") },
{ title: "Cancel", role: "cancel" },
]}
isPresented={shown === "two"}
onDismiss={close}
/>
<Alert
title="Save Changes?"
actions={[
{ title: "Save" },
{ title: "Don't Save", role: "destructive" },
{ title: "Cancel", role: "cancel" },
]}
isPresented={shown === "stacked"}
onDismiss={close}
/>
<Alert title="Copied" isPresented={shown === "ok"} onDismiss={close} />With a text field
field adds a text field above the buttons. It is controlled: pass its value and update it in onChange.
const [open, setOpen] = useState(false);
const [name, setName] = useState("");
<Button buttonStyle="glass" onClick={() => setOpen(true)}>Rename</Button>
<Alert
title="Rename Folder"
message="Enter a new name for this folder."
field={{ placeholder: "Name", value: name, onChange: setName }}
actions={[
{ title: "Cancel", role: "cancel" },
{ title: "Save", action: () => setSaved(name) },
]}
isPresented={open}
onDismiss={() => setOpen(false)}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Required. Labels the dialog. |
| message | string | - | Text under the title. |
| actions | PresentationAction[] | one OK | Two short ones share a row (cancel first); more stack (cancel last). |
| field | { placeholder: string; value: string; onChange: (value: string) => void } | - | A text field above the buttons. |
| isPresented | boolean | - | Required. Shown while true. |
| onDismiss | () => void | - | Runs after a button's action: set isPresented back to false. |
| glass | { frostSize?: number; tableSize?: number } | - | Fitting only: which measured sizes draw the glass. |
PresentationAction
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | The button's title. |
| role | "destructive" | "cancel" | - | destructive draws it red; cancel is what Escape runs. |
| action | () => void | - | Runs when it is chosen, before onDismiss. |
PresentationRoot
| Prop | Type | Default | Description |
|---|---|---|---|
| idiom | "phone" | "pad" | "duoOuter" | "duoInner" | from the window | Layout and motion of what presents inside. Unset: the browser window's width picks phone or pad. |
| window | { x: number; y: number; width: number; height: number } | the browser window | The window presentations center in, in viewport px. |
| root | HTMLElement | null | document.body | Where they portal. Set, the layer sits at its top left, sized to window. |
| children | ReactNode | - | What presents in it: alerts, dialogs, popovers, sheets and menus. |
Behaviour
The panel is a role="alertdialog" labelled by its title. It portals to the root of its PresentationRoot, or document.body, takes focus when it opens (Tab stays inside) and gives it back when it closes. A tap outside is refused; Escape runs the cancel action, and only an alert with one closes on Escape. A press on a button acts when it lifts on it, and the panel leans after the finger.
The layout and motion follow the idiom. The glass targets Chromium. PresentationLayer (the portal, dim and outside touch) and usePresentation (a presentation's phases and springs) are what the alert, dialog and popover are built from, exported for your own.
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| .alert(title, isPresented:) { actions } message: { } | <Alert title message actions isPresented onDismiss> |
| Button(title, role: .destructive / .cancel) { } in a presentation | { title, role: "destructive" / "cancel", action } |
| TextField(placeholder, text:) in an alert | field={{ placeholder, value, onChange }} |