Dialog
Una ventana que se abre sobre la página para una decisión o una tarea corta que necesita a la persona ya. La página de atrás espera, atenuada, hasta que se cierre.
import { Dialog } from '@sypnia/ui/react'
Una tarea corta
Cambia el nombre del proyecto: Enter guarda, Escape lo deja como estaba.
Código
import { useState } from 'react';
import { Actions, Button, Dialog, Field, Input } from '@sypnia/ui/react';
// Press the button. Escape, the cross or a press outside close it, and the focus returns to the button.
export default function Basic() {
const [open, setOpen] = useState(false);
const [name, setName] = useState('Storefront');
const [draft, setDraft] = useState(name);
const save = () => { setName(draft.trim() || name); setOpen(false); };
return (
<div className="sy-row">
<Button icon="edit" onClick={() => { setDraft(name); setOpen(true); }}>Rename project</Button>
<span className="sy-text--sm sy-text--soft">The project is called {name}.</span>
<Dialog open={open} onClose={() => setOpen(false)} title="Rename the project"
footer={<Actions><Button onClick={() => setOpen(false)}>Cancel</Button><Button variant="primary" onClick={save}>Rename</Button></Actions>}>
<Field label="Name" hint="Members see the new name at once.">
<Input autoFocus value={draft} onChange={(e) => setDraft(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') save(); }} />
</Field>
</Dialog>
</div>
);
} Tamaños
Cuatro anchos. En un celular todos son iguales: todo el ancho.
Código
import { useState } from 'react';
import { Actions, Button, Dialog } from '@sypnia/ui/react';
const sizes = { sm: 'A short question', md: 'A short form', lg: 'A form with two columns', xl: 'A preview or a comparison' } as const;
export default function Sizes() {
const [size, setSize] = useState<keyof typeof sizes | null>(null);
return (
<div className="sy-row sy-row--pairs">
{(Object.keys(sizes) as (keyof typeof sizes)[]).map((s) => <Button key={s} onClick={() => setSize(s)}>Open {s}</Button>)}
<Dialog open={size !== null} size={size ?? 'md'} onClose={() => setSize(null)} title={size ? sizes[size] : ''}
footer={<Actions><Button variant="primary" onClick={() => setSize(null)}>Done</Button></Actions>}>
<p>This dialog has the size <code>{size}</code>. On a phone every size takes the full width and sits at the bottom, near the thumb.</p>
</Dialog>
</div>
);
} No pierde lo que escribiste
Cambia el mensaje y luego pulsa afuera o Escape: sigue abierto y dice cómo cerrar. Sin cambios, un toque afuera lo cierra.
Código
import { useState } from 'react';
import { Actions, Button, Dialog, Field, Textarea } from '@sypnia/ui/react';
// Pass `dirty` while there are unsaved changes: a press outside or Escape keeps the text and says how to close.
// Without changes, a press outside closes it like any dialog.
const start = 'Hello Laura, your order left our warehouse this morning.';
export default function KeepsWork() {
const [open, setOpen] = useState(false);
const [text, setText] = useState(start);
const close = () => { setOpen(false); setText(start); };
return (
<>
<Button icon="mail" onClick={() => setOpen(true)}>Write to the customer</Button>
<Dialog open={open} dirty={text !== start} onClose={close} title="Message about order 1042"
footer={<Actions><Button onClick={close}>Cancel</Button><Button variant="primary" onClick={close}>Send message</Button></Actions>}>
<Field label="Message" hint="The customer receives it by email.">
<Textarea autoFocus value={text} onChange={(e) => setText(e.target.value)} />
</Field>
</Dialog>
</>
);
} Contenido largo
La lista se desplaza; el título y los botones quedan a la vista.
Código
import { useState } from 'react';
import { Actions, Button, Checkbox, Dialog } from '@sypnia/ui/react';
const tables = ['customers', 'invoices', 'invoice_lines', 'order_items', 'orders', 'payments', 'products', 'product_images', 'refunds', 'reviews', 'shipments', 'stock_levels', 'suppliers', 'warehouses'];
// When the content is long, the body scrolls and the title and the buttons stay in view.
export default function LongContent() {
const [open, setOpen] = useState(false);
return (
<>
<Button icon="download" onClick={() => setOpen(true)}>Export tables</Button>
<Dialog open={open} onClose={() => setOpen(false)} title="Which tables do you want to export?"
footer={<Actions><Button onClick={() => setOpen(false)}>Cancel</Button><Button variant="primary" onClick={() => setOpen(false)}>Export as CSV</Button></Actions>}>
{tables.map((t) => <Checkbox key={t} label={<span className="sy-machine">{t}</span>} defaultChecked={t.startsWith('order')} />)}
</Dialog>
</>
);
} Cargando
Ábrelo: el título y el botón están desde el principio; el cuerpo espera como líneas de skeleton.
Código
import { useState } from 'react';
import { Actions, Button, Dialog } from '@sypnia/ui/react';
// The dialog opens at once with its title and buttons; its body is a skeleton until the details arrive.
export default function Loading() {
const [open, setOpen] = useState(false);
const [loading, setLoading] = useState(true);
const show = () => { setOpen(true); setLoading(true); setTimeout(() => setLoading(false), 1500); };
return (
<>
<Button onClick={show}>Show the table's details</Button>
<Dialog open={open} onClose={() => setOpen(false)} title="Table orders" loading={loading} lines={3}
footer={<Actions><Button variant="primary" onClick={() => setOpen(false)}>Done</Button></Actions>}>
<p>48,210 rows, 12 columns. Two policies decide who reads it; nobody outside the team can write to it.</p>
</Dialog>
</>
);
} Cuándo usarlo
Úsalo cuando
- Una tarea corta que pertenece a la página de atrás: renombrar, invitar, elegir qué exportar.
- La persona debe responder antes de seguir.
- Cerrarlo por accidente perdería trabajo: pon
dismissable={false}, así solo lo cierran los botones.
No lo uses cuando
- Solo pregunta «¿estás seguro?»: ConfirmDialog hace eso con el foco y las palabras correctas.
- La tarea es larga o tiene muchos campos: un Drawer, o una página propia.
- Informa un resultado («Guardado»): un toast. Advierte sin preguntar nada: una Alert.
- Se abriría solo, sin que la persona presione nada.
Para todos
- Se dibuja en la capa superior de la ventana, nunca dentro de la página, y la página de atrás no se puede desplazar ni alcanzar con Tab.
- El foco entra al abrirse (al primer campo, o al marcado con
autoFocus) y vuelve al botón que lo abrió al cerrarse. - Un toque en el fondo lo cierra, igual que Escape y la cruz. Un arrastre que empieza adentro (al seleccionar texto) y termina afuera no lo cierra. Con
dirty(cambios sin guardar), un toque afuera o Escape conserva el trabajo y dice cómo cerrar;dismissable={false}desactiva el fondo. La cruz se llama «Cerrar». - Se anuncia como diálogo con su título. En el celular queda en el borde de abajo, a todo el ancho.
Props
| Dialog | Tipo | Por defecto |
|---|---|---|
onCloseobligatorio | () => void | |
titleobligatorio | ReactNode | |
dirtyThe form inside has changes that are not saved. A press outside or Escape then does not close it and does not lose them: the panel nudges and says to save or cancel. Only the buttons close it. | boolean | |
dismissableWhether a press on the backdrop (outside the panel) closes it. On by default; Escape always closes it too. Set false for a form that must be finished or cancelled with its buttons. | boolean | |
footerButtons at the bottom, inside <Actions>. | ReactNode | |
linesHow many lines of skeleton the body shows while loading. | number | |
loadingWhat it shows is on its way: the title and the buttons stay, the body is lines of skeleton. | boolean | |
openWhether it is open. It can also simply be mounted and unmounted: it is open while mounted. | boolean | |
size | "sm" | "md" | "lg" | "xl" | md |