-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathConfirmationDialog.stories.tsx
105 lines (99 loc) · 2.96 KB
/
ConfirmationDialog.stories.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React, {useState, useRef, useCallback} from 'react'
import {Meta} from '@storybook/react'
import {BaseStyles, Button, Box, ThemeProvider, useTheme} from '..'
import {ConfirmationDialog, useConfirm} from '../Dialog/ConfirmationDialog'
import {ActionMenu} from '../ActionMenu'
export default {
title: 'Internal components/ConfirmationDialog',
component: ConfirmationDialog,
decorators: [
Story => {
// Since portal roots are registered globally, we need this line so that each storybook
// story works in isolation.
return (
<ThemeProvider>
<BaseStyles>
<Story />
</BaseStyles>
</ThemeProvider>
)
}
]
} as Meta
export const BasicConfirmationDialog = () => {
const [isOpen, setIsOpen] = useState(false)
const buttonRef = useRef<HTMLButtonElement>(null)
const onDialogClose = useCallback(() => setIsOpen(false), [])
return (
<>
<Button ref={buttonRef} onClick={() => setIsOpen(!isOpen)}>
Show dialog
</Button>
{isOpen && (
<ConfirmationDialog
title="Delete universe?"
onClose={onDialogClose}
confirmButtonContent="Delete it!"
confirmButtonType="danger"
>
Deleting the universe could have disastrous effects, including but not limited to destroying all life on
Earth.
</ConfirmationDialog>
)}
</>
)
}
export const ShorthandHook = () => {
const confirm = useConfirm()
const {theme} = useTheme()
const onButtonClick = useCallback(
async (event: React.MouseEvent) => {
if (
(await confirm({title: 'Are you sure?', content: 'Do you really want to turn this button green?'})) &&
event.target instanceof HTMLElement
) {
event.target.style.backgroundColor = theme?.colors.auto.green[3] ?? 'green'
event.target.textContent = "I'm green!"
}
},
[confirm, theme]
)
return (
<Box display="flex" flexDirection="column" alignItems="flex-start">
<Button mb={2} onClick={onButtonClick}>
Turn me green!
</Button>
<Button mb={2} onClick={onButtonClick}>
Turn me green!
</Button>
<Button mb={2} onClick={onButtonClick}>
Turn me green!
</Button>
<Button mb={2} onClick={onButtonClick}>
Turn me green!
</Button>
</Box>
)
}
export const ShorthandHookFromActionMenu = () => {
const confirm = useConfirm()
const [text, setText] = useState('open me')
const onButtonClick = useCallback(async () => {
if (await confirm({title: 'Are you sure?', content: 'Do you really want to do a trick?'})) {
setText('tada!')
}
}, [confirm])
return (
<Box display="flex" flexDirection="column" alignItems="flex-start">
<ActionMenu
renderAnchor={props => <Button {...props}>{text}</Button>}
items={[
{
text: 'Do a trick!',
onAction: onButtonClick
}
]}
/>
</Box>
)
}