ConfirmationDialog
A glass bubble of actions with a tail pointing at what opened it: above it, else below, else beside.
import { ConfirmationDialog } from "@adamexu/glass";From a button
source is what it points at: a ref to an element, or a box in the window. As in research's demo app, the ref goes on the button's label. The cancel action isn't drawn: Escape and a tap outside cancel. The title is hidden by default, as on iOS.
const source = useRef<HTMLSpanElement>(null);
const [open, setOpen] = useState(false);
<Button buttonStyle="glass" onClick={() => setOpen(true)}>
<span ref={source}>Share</span>
</Button>
<ConfirmationDialog
title="Share This Album?"
actions={[
{ title: "Share Link", action: () => setChoice("Share Link") },
{ title: "Invite People", action: () => setChoice("Invite People") },
{ title: "Stop Sharing", role: "destructive", action: () => setChoice("Stop Sharing") },
{ title: "Cancel", role: "cancel" },
]}
source={source}
isPresented={open}
onDismiss={() => setOpen(false)}
/>With a visible title and a message
titleVisibility="visible" draws the title over the message. The demos sit in a PresentationRoot over their phone frame (see Alert), so the bubble is placed in the frame.
const source = useRef<HTMLSpanElement>(null);
const [open, setOpen] = useState(false);
<Button buttonStyle="glass" onClick={() => setOpen(true)}>
<span ref={source}>Options</span>
</Button>
<ConfirmationDialog
title="Draft 3"
titleVisibility="visible"
message="This draft hasn't been sent."
actions={[
{ title: "Send Now", action: () => setChoice("Send Now") },
{ title: "Discard", role: "destructive", action: () => setChoice("Discard") },
{ title: "Cancel", role: "cancel" },
]}
source={source}
isPresented={open}
onDismiss={() => setOpen(false)}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Required. Labels the dialog, drawn when visible. |
| titleVisibility | "visible" | "hidden" | "automatic" | "automatic" | automatic hides it, as iOS does. |
| message | string | - | Text under the title. |
| actions | PresentationAction[] | - | Required. The cancel action isn't drawn: Escape and a tap outside cancel. |
| source | RefObject<HTMLElement | null> | Frame | - | What it points at: an element, or a box in the window's coordinates. |
| isPresented | boolean | - | Required. Shown while true. |
| onDismiss | () => void | - | Runs after a button's action, a tap outside or Escape: 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 not drawn, and its action runs on Escape. |
| action | () => void | - | Runs when it is chosen, before onDismiss. |
Frame
| Prop | Type | Default | Description |
|---|---|---|---|
| { x, y, width, height } | number | - | A box in the presentation window, in CSS px from its top left. |
Behaviour
The bubble is a role="dialog". It portals to the root of its PresentationRoot, or document.body, takes focus when it opens (Tab stays inside), gives it back when it closes, and closes on Escape or a tap outside. On a phone it grows from its top center; on an iPad from the side facing the source when there isn't much room below. The glass targets Chromium.
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| .confirmationDialog(title, isPresented:, titleVisibility:) { } message: { } | <ConfirmationDialog title titleVisibility message actions isPresented onDismiss> |
| Button(title, role: .destructive / .cancel) { } in a presentation | { title, role: "destructive" / "cancel", action } |
| the view it is attached to | source={ref} (an element, or a Frame) |