Replace the native confirm and alert with an in-app dialog. [skip build]
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user