Skip to content

Commit 30799e8

Browse files
authored
Feature components option (#56)
* componentsオプション仮実装 * examples実装 * 引数でコンポーネントを渡す設計をやめる * README追記 * ModalProviderでvalueを介す必要をなくす * elementIdの型をREADMEに合わせる * 修正 * 不要になったファイルを削除z * README: ModelProviderのvalueオプションを削除
1 parent bb312c6 commit 30799e8

File tree

13 files changed

+278
-238
lines changed

13 files changed

+278
-238
lines changed

README.md

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ render(<App />, document.getElementById('root'));
3838
### [ModalComponent, openFunc, closeFunc, isOpenBool] = useModal(domNode?, { preventScroll?, focusTrapOptions?, showCloseButton?, renderCloseButton? })
3939
4040
`ModalComponent`
41+
Type: React.FC<{ title?: React.ReactNode; description?: React.ReactNode, children?: React.ReactNode }>
4142
Modal component that displays children in the screen center.
43+
If you specify `title` and `description`, you must implement custom components with the `components` option's `Modal` property and render in them.
4244
4345
`openFunc`
4446
A function to open modal.
@@ -71,44 +73,74 @@ useModal('root', {
7173
});
7274
```
7375
74-
`showCloseButton`
75-
Optional to choose whether to add a close button in the upper right corner.
76-
Default value is false.
77-
78-
`renderCloseButton`
79-
Type: (close: () => void) => React.ReactElement
76+
`components`
8077
Optional.
81-
Can be specified when `showCloseButton` is true, allowing you to override the default close button and implement your own close button.
82-
Use the following
78+
This is an option to customize the `ModalWrapper` returned by useModal.
79+
Use as follows
8380
8481
```jsx
8582
useModal('root', {
86-
showCloseButton: true,
87-
renderCloseButton: (close) => (
88-
<button
89-
style={{
90-
position: 'absolute',
91-
left: 0,
92-
right: 0,
93-
bottom: '20px',
94-
margin: '0 auto',
95-
width: '100px',
96-
}}
97-
onClick={close}
98-
type="button"
99-
>
100-
Close
101-
</button>
102-
),
83+
components: {
84+
Modal: ({ title, description, children }) => {
85+
return (
86+
<div
87+
style={{
88+
padding: '60px 100px',
89+
backgroundColor: '#fff',
90+
borderRadius: '10px',
91+
}}
92+
>
93+
{title && <h1>{title}</h1>}
94+
{description && <p>{description}</p>}
95+
{children}
96+
</div>
97+
);
98+
},
99+
Overlay: () => {
100+
return (
101+
<div
102+
style={{
103+
position: 'fixed',
104+
top: 0,
105+
left: 0,
106+
bottom: 0,
107+
right: 0,
108+
backgroundColor: 'rgba(0, 0, 0, 0.5)',
109+
}}
110+
/>
111+
);
112+
},
113+
Wrapper: ({ children }) => {
114+
return (
115+
<div
116+
style={{
117+
position: 'fixed',
118+
top: 0,
119+
left: 0,
120+
bottom: 0,
121+
right: 0,
122+
display: 'flex',
123+
justifyContent: 'center',
124+
alignItems: 'center',
125+
zIndex: 1000,
126+
}}
127+
>
128+
{children}
129+
</div>
130+
);
131+
},
132+
},
103133
});
104134
```
105135
136+
Combined with `ModalProvider` (described below), you can specify the default style for all `useModal` in the project.
137+
106138
## Global Settings
107139
108140
The `ModalProvider` component allows you to apply a common default configuration to all `useModal` hooks.
109141
110142
```jsx
111-
<ModalProvider value={options}>
143+
<ModalProvider {...options}>
112144
<Component />
113145
</ModalProvider>
114146
```
@@ -135,11 +167,7 @@ const Component2 = () => {
135167

136168
const App = () => {
137169
return (
138-
<ModalProvider
139-
value={{
140-
preventScroll: true,
141-
}}
142-
>
170+
<ModalProvider preventScroll>
143171
<Component1 />
144172
<Component2 />
145173
</ModalProvider>

examples-routes.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module.exports = [
22
{ path: '/', title: 'useModal example' },
3-
{ path: '/prevent-scroll', title: 'useModal with preventScroll example' },
4-
{ path: '/close-button', title: 'useModal with closeButton example' },
53
{
6-
path: '/close-button/render-option',
7-
title: 'useModal with renderCloseButton option example',
4+
path: '/prevent-scroll',
5+
title: 'useModal with preventScroll option example',
86
},
97
{ path: '/modal-provider', title: 'useModal with ModalProvider example' },
8+
{
9+
path: '/components-option',
10+
title: 'useModal with components option example',
11+
},
1012
];

examples/src/index.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { render } from 'react-dom';
44
import routes from '../../examples-routes';
55

66
import { Modal as CommonModal } from './js';
7-
import { Modal as CloseButtonModal } from './js/close-button';
8-
import { Modal as CloseButtonWithRenderOptionModal } from './js/close-button/render-option';
7+
import { ModalWrapper as ComponentsOptionModal } from './js/components-option';
98
import { ModalWrapper as ModalProviderModal } from './js/modal-config';
109
import { Modal as PreventScrollModal } from './js/prevent-scroll';
1110

@@ -18,15 +17,12 @@ const CurrentModal = () => {
1817
case '/prevent-scroll': {
1918
return <PreventScrollModal />;
2019
}
21-
case '/close-button': {
22-
return <CloseButtonModal />;
23-
}
24-
case '/close-button/render-option': {
25-
return <CloseButtonWithRenderOptionModal />;
26-
}
2720
case '/modal-provider': {
2821
return <ModalProviderModal />;
2922
}
23+
case '/components-option': {
24+
return <ComponentsOptionModal />;
25+
}
3026
default: {
3127
return <CommonModal />;
3228
}
@@ -40,6 +36,7 @@ const Wrapper = ({ children }: PropsWithChildren<{}>) => {
4036
<nav style={{ marginTop: '40px' }}>
4137
{routes.map(({ path }, i) => (
4238
<a
39+
key={path}
4340
href={`/react-hooks-use-modal${path}`}
4441
style={{ marginLeft: i !== 0 ? '10px' : '' }}
4542
>

examples/src/js/close-button/index.tsx

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/src/js/close-button/render-option.tsx

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React from 'react';
2+
import { ModalProvider, useModal } from '../../../../src';
3+
4+
const Modal = () => {
5+
const [Modal, open, close, isOpen] = useModal('root');
6+
7+
return (
8+
<div>
9+
<div>Modal is Open? {isOpen ? 'Yes' : 'No'}</div>
10+
<button onClick={open}>OPEN</button>
11+
<Modal title="Title" description="This is a customizable modal.">
12+
<button onClick={close}>CLOSE</button>
13+
</Modal>
14+
</div>
15+
);
16+
};
17+
const ModalWithOverrideOptions = () => {
18+
const [Modal, open, close, isOpen] = useModal('root', {
19+
components: {
20+
Modal: ({ title, description, children }) => {
21+
return (
22+
<div
23+
style={{
24+
padding: '60px 100px',
25+
backgroundColor: 'cyan', // override
26+
borderRadius: '10px',
27+
}}
28+
>
29+
{title && <h1>{title}</h1>}
30+
{description && <p>{description}</p>}
31+
{children}
32+
</div>
33+
);
34+
},
35+
},
36+
});
37+
38+
return (
39+
<div style={{ marginTop: '40px' }}>
40+
<div>Overrided style modal is Open? {isOpen ? 'Yes' : 'No'}</div>
41+
<button onClick={open}>OPEN</button>
42+
<Modal title="Title" description="This is a customizable modal.">
43+
<button onClick={close}>CLOSE</button>
44+
</Modal>
45+
</div>
46+
);
47+
};
48+
49+
export const ModalWrapper = () => {
50+
return (
51+
<ModalProvider
52+
focusTrapOptions={{
53+
clickOutsideDeactivates: true,
54+
}}
55+
components={{
56+
Modal: ({ title, description, children }) => {
57+
return (
58+
<div
59+
style={{
60+
padding: '60px 100px',
61+
backgroundColor: '#fff',
62+
borderRadius: '10px',
63+
}}
64+
>
65+
{title && <h1>{title}</h1>}
66+
{description && <p>{description}</p>}
67+
{children}
68+
</div>
69+
);
70+
},
71+
}}
72+
>
73+
<Modal />
74+
<ModalWithOverrideOptions />
75+
</ModalProvider>
76+
);
77+
};

examples/src/js/modal-config/index.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ const ModalWithOverrideOptions = () => {
4949
export const ModalWrapper = () => {
5050
return (
5151
<ModalProvider
52-
value={{
53-
focusTrapOptions: {
54-
clickOutsideDeactivates: true,
55-
},
52+
focusTrapOptions={{
53+
clickOutsideDeactivates: true,
5654
}}
5755
>
5856
<Modal />

src/components/DefaultCloseButton.tsx

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)