1- import React , { useState } from 'react'
1+ import React , { useRef , useCallback } from 'react'
22import { StoryFn as Story , Meta } from '@storybook/react'
33
44import Modal , { ModalProps } from '.'
@@ -10,117 +10,106 @@ export default {
1010} as Meta
1111
1212export const Default : Story < ModalProps > = ( args ) => {
13- const [ visible , setVisible ] = useState < boolean > ( false )
14-
15- const toggleVisible = ( ) => {
16- setVisible ( ! visible )
17- }
18-
13+ const ref = useRef < HTMLDialogElement > ( null )
14+ const handleShow = useCallback ( ( ) => {
15+ ref . current ?. showModal ( )
16+ } , [ ref ] )
1917 return (
2018 < div className = "font-sans" >
21- < Button onClick = { toggleVisible } > Open Modal</ Button >
22- < Modal { ...args } open = { visible } >
23- < Modal . Header className = "font-bold" >
24- Congratulations random Interner user!
25- </ Modal . Header >
26-
19+ < Button onClick = { handleShow } > Open Modal</ Button >
20+ < Modal { ...args } ref = { ref } >
21+ < Modal . Header className = "font-bold" > Hello!</ Modal . Header >
2722 < Modal . Body >
28- You've been selected for a chance to get one year of subscription to
29- use Wikipedia for free!
23+ Press ESC key or click the button below to close
3024 </ Modal . Body >
31-
3225 < Modal . Actions >
33- < Button onClick = { toggleVisible } > Yay! </ Button >
26+ < Button > Close </ Button >
3427 </ Modal . Actions >
3528 </ Modal >
3629 </ div >
3730 )
3831}
3932
40- export const CloseButton : Story < ModalProps > = ( args ) => {
41- const [ visible , setVisible ] = useState < boolean > ( false )
42-
43- const toggleVisible = ( ) => {
44- setVisible ( ! visible )
45- }
33+ export const ClickedOutside : Story < ModalProps > = ( args ) => {
34+ const ref = useRef < HTMLDialogElement > ( null )
35+ const handleShow = useCallback ( ( ) => {
36+ ref . current ?. showModal ( )
37+ } , [ ref ] )
38+ return (
39+ < div className = "font-sans" >
40+ < Button onClick = { handleShow } > Open Modal</ Button >
41+ < Modal { ...args } ref = { ref } >
42+ < Modal . Header className = "font-bold" > Hello!</ Modal . Header >
43+ < Modal . Body > Press ESC key or click outside to close</ Modal . Body >
44+ </ Modal >
45+ </ div >
46+ )
47+ }
48+ ClickedOutside . args = {
49+ backdrop : true ,
50+ }
4651
52+ export const CloseButton : Story < ModalProps > = ( args ) => {
53+ const ref = useRef < HTMLDialogElement > ( null )
54+ const handleShow = useCallback ( ( ) => {
55+ ref . current ?. showModal ( )
56+ } , [ ref ] )
4757 return (
4858 < div className = "font-sans" >
49- < Button onClick = { toggleVisible } > Open Modal</ Button >
50- < Modal { ...args } open = { visible } >
59+ < Button onClick = { handleShow } > Open Modal</ Button >
60+ < Modal { ...args } ref = { ref } >
5161 < Button
5262 size = "sm"
63+ color = "ghost"
5364 shape = "circle"
5465 className = "absolute right-2 top-2"
55- onClick = { toggleVisible }
5666 >
57- ✕
67+ x
5868 </ Button >
59- < Modal . Header className = "font-bold" >
60- Congratulations random Interner user!
61- </ Modal . Header >
62-
63- < Modal . Body >
64- You've been selected for a chance to get one year of subscription to
65- use Wikipedia for free!
66- </ Modal . Body >
69+ < Modal . Header className = "font-bold" > Hello!</ Modal . Header >
70+ < Modal . Body > Press ESC key or click on X button to close</ Modal . Body >
6771 </ Modal >
6872 </ div >
6973 )
7074}
7175
72- export const ClickedOutside : Story < ModalProps > = ( args ) => {
73- const [ visible , setVisible ] = useState < boolean > ( false )
74-
75- const toggleVisible = ( ) => {
76- setVisible ( ! visible )
77- }
78-
76+ export const CustomWidth : Story < ModalProps > = ( args ) => {
77+ const ref = useRef < HTMLDialogElement > ( null )
78+ const handleShow = useCallback ( ( ) => {
79+ ref . current ?. showModal ( )
80+ } , [ ref ] )
7981 return (
8082 < div className = "font-sans" >
81- < Button onClick = { toggleVisible } > Open Modal</ Button >
82- < Modal { ...args } open = { visible } onClickBackdrop = { toggleVisible } >
83- < Modal . Header className = "font-bold" >
84- Congratulations random Interner user!
85- </ Modal . Header >
86-
83+ < Button onClick = { handleShow } > Open Modal</ Button >
84+ < Modal { ...args } ref = { ref } >
85+ < Modal . Header className = "font-bold" > Hello!</ Modal . Header >
8786 < Modal . Body >
88- You've been selected for a chance to get one year of subscription to
89- use Wikipedia for free!
87+ Press ESC key or click the button below to close
9088 </ Modal . Body >
89+ < Modal . Actions >
90+ < Button > Close</ Button >
91+ </ Modal . Actions >
9192 </ Modal >
9293 </ div >
9394 )
9495}
9596
96- export const CustomWidth : Story < ModalProps > = ( args ) => {
97- const [ visible , setVisible ] = useState < boolean > ( false )
98-
99- const toggleVisible = ( ) => {
100- setVisible ( ! visible )
101- }
97+ CustomWidth . args = {
98+ className : 'w-11/12 max-w-5xl' ,
99+ }
102100
101+ export const UseDialogHook : Story < ModalProps > = ( args ) => {
102+ const { Dialog, handleShow } = Modal . useDialog ( )
103103 return (
104104 < div className = "font-sans" >
105- < Button onClick = { toggleVisible } > Open Modal</ Button >
106- < Modal { ...args } open = { visible } >
107- < Modal . Header className = "font-bold" >
108- Congratulations random Interner user!
109- </ Modal . Header >
110-
111- < Modal . Body >
112- You've been selected for a chance to get one year of subscription to
113- use Wikipedia for free!
114- </ Modal . Body >
115-
105+ < Button onClick = { handleShow } > Open Modal</ Button >
106+ < Dialog { ...args } >
107+ < Modal . Header className = "font-bold" > Hello!</ Modal . Header >
108+ < Modal . Body > This modal works with useDialog hook!</ Modal . Body >
116109 < Modal . Actions >
117- < Button onClick = { toggleVisible } > Yay! </ Button >
110+ < Button > Close </ Button >
118111 </ Modal . Actions >
119- </ Modal >
112+ </ Dialog >
120113 </ div >
121114 )
122115}
123-
124- CustomWidth . args = {
125- className : 'w-11/12 max-w-5xl' ,
126- }
0 commit comments