50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import FocusTrap from 'focus-trap-react';
|
|
import { useAtom } from 'jotai';
|
|
import { Box, Button, Dialog, Overlay, OverlayBackdrop, OverlayCenter, config, Text } from 'folds';
|
|
import { dialogAtom } from '../state/dialog';
|
|
import { stopPropagation } from '../utils/keyboard';
|
|
|
|
export function DialogHost() {
|
|
const [dialog, setDialog] = useAtom(dialogAtom);
|
|
|
|
if (!dialog) return null;
|
|
|
|
const close = (answer: boolean) => {
|
|
setDialog(null);
|
|
dialog.resolve(answer);
|
|
};
|
|
|
|
return (
|
|
<Overlay open backdrop={<OverlayBackdrop />}>
|
|
<OverlayCenter>
|
|
<FocusTrap
|
|
focusTrapOptions={{
|
|
initialFocus: false,
|
|
onDeactivate: () => close(false),
|
|
clickOutsideDeactivates: true,
|
|
escapeDeactivates: stopPropagation,
|
|
}}
|
|
>
|
|
<Dialog variant="Surface">
|
|
<Box style={{ padding: config.space.S400 }} direction="Column" gap="400">
|
|
<Text priority="400" style={{ whiteSpace: 'pre-line' }}>
|
|
{dialog.message}
|
|
</Text>
|
|
<Box direction="Row" gap="200">
|
|
{dialog.confirm && (
|
|
<Button variant="Secondary" onClick={() => close(false)} radii="300">
|
|
<Text size="B300">Cancel</Text>
|
|
</Button>
|
|
)}
|
|
<Button variant="Primary" onClick={() => close(dialog.confirm)} radii="300">
|
|
<Text size="B300">{dialog.confirm ? 'Confirm' : 'OK'}</Text>
|
|
</Button>
|
|
</Box>
|
|
</Box>
|
|
</Dialog>
|
|
</FocusTrap>
|
|
</OverlayCenter>
|
|
</Overlay>
|
|
);
|
|
}
|