Skip to content

Commit 46ca8f9

Browse files
committed
[IMPL] useDialogBox hook
1 parent e77bdc6 commit 46ca8f9

11 files changed

Lines changed: 149 additions & 9 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useState } from "react";
2+
import { useDialogBox } from "../../../../../../packages/react-tools/src"
3+
4+
/**
5+
The component renders three button that execute three _useDialogBox_ hook with a __type__ for each one.
6+
*/
7+
export const UseDialogBox = () => {
8+
const [response, setResponse] = useState("");
9+
const openPrompt = useDialogBox("prompt");
10+
const openAlert = useDialogBox("alert");
11+
const openConfirm = useDialogBox("confirm");
12+
13+
return <div style={{ display: 'flex', flexDirection: "column", justifyContent: "center" }}>
14+
<p>Response: {response}</p>
15+
<div style={{ display: 'flex', gap: 10, justifyContent: "center" }}>
16+
<button
17+
onClick={() => {
18+
const resp = openPrompt("Are you open a prompt?");
19+
setResponse(resp!);
20+
}}
21+
>
22+
Open Prompt
23+
</button>
24+
<button
25+
onClick={() => {
26+
openAlert("You open a alert");
27+
}}
28+
>
29+
Open Alert
30+
</button>
31+
<button
32+
onClick={() => {
33+
const resp = openConfirm("You open a confirm. it's ok?");
34+
setResponse(resp ? "ok" : "ko");
35+
}}
36+
>
37+
Open Confirm
38+
</button>
39+
</div>
40+
</div>
41+
}

apps/react-tools-demo/src/components/hooks/useEyeDropper/UseEyeDropper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ export const UseEyeDropper = () => {
1818
<button onClick={onClick}>Get Color</button>
1919
<br />
2020
<br />
21-
<label>Color </label><input type="color" value={color}/>
21+
<label>Color </label><input type="color" defaultValue={color}/>
2222
</div>
2323
}

apps/react-tools-demo/src/constants/components.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ export const COMPONENTS = [
8282
"useBattery",
8383
"useGeolocation",
8484
"useShare",
85-
"useEyeDropper"
85+
"useEyeDropper",
86+
"useDialogBox"
8687
]
8788
],
8889
//UTILS
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# useDialogBox
2+
Hook to use Dialog Box _prompt_, _alert_ or _confirm_.
3+
4+
## Usage
5+
6+
```tsx
7+
export const UseDialogBox = () => {
8+
const [response, setResponse] = useState("");
9+
const openPrompt = useDialogBox("prompt");
10+
const openAlert = useDialogBox("alert");
11+
const openConfirm = useDialogBox("confirm");
12+
13+
return <div style={{ display: 'flex', flexDirection: "column", justifyContent: "center" }}>
14+
<p>Response: {response}</p>
15+
<div style={{ display: 'flex', gap: 10, justifyContent: "center" }}>
16+
<button
17+
onClick={() => {
18+
const resp = openPrompt("Are you open a prompt?");
19+
setResponse(resp!);
20+
}}
21+
>
22+
Open Prompt
23+
</button>
24+
<button
25+
onClick={() => {
26+
openAlert("You open a alert");
27+
}}
28+
>
29+
Open Alert
30+
</button>
31+
<button
32+
onClick={() => {
33+
const resp = openConfirm("You open a confirm. it's ok?");
34+
setResponse(resp ? "ok" : "ko");
35+
}}
36+
>
37+
Open Confirm
38+
</button>
39+
</div>
40+
</div>
41+
}
42+
```
43+
44+
> The component renders three button that execute three _useDialogBox_ hook with a __type__ for each one.
45+
46+
47+
## API
48+
49+
```tsx
50+
useDialogBox(type: "prompt" | "confirm" | "alert"): ((message?: string, _default?: string) => string | null) | ((message?: any) => void) | ((message?: string) => boolean)
51+
```
52+
53+
> ### Params
54+
>
55+
> - __type__: _"prompt"|"alert"|"confirm"_
56+
set dialog box type.
57+
>
58+
59+
> ### Returns
60+
>
61+
> __open__: function to activate dialog box.
62+
> - __Union of__:
63+
> - _((message?: string, default?: string) => string|null)_
64+
> - _((message?: any) => void)_
65+
> - _((message?: string) => boolean)_
66+
>

apps/react-tools-demo/src/markdown/useEventListener.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export { UseEventListener };
3535
## API
3636

3737
```tsx
38-
useEventListener ({ type, listener, element = window, listenerOpts }: { type: string, listener: (evt: Event | CustomEvent) => void, element?: RefObject<HTMLElement> | Window, listenerOpts?: boolean | AddEventListenerOptions})
38+
useEventListener ({ type, listener, element = window, listenerOpts, effectType="normal" }: { type: string, listener: (evt: Event | CustomEvent) => void, element?: RefObject<HTMLElement> | Window, listenerOpts?: boolean | AddEventListenerOptions, effectType?: "normal" | "layout" })
3939
```
4040

4141
> ### Params
@@ -49,6 +49,8 @@ listener to be executed on specified event
4949
element on which attaching eventListener
5050
> - __options.listenerOpts?__: _boolean | AddEventListenerOptions_
5151
options for listener
52+
> - __options.effectType="normal"?__: _"normal"|"layout"_
53+
option to set which hook is used to attach event listener.
5254
>
5355
5456
> ### Returns

apps/react-tools-demo/src/markdown/useEyeDropper.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ export const UseEyeDropper = () => {
1515

1616
return <div style={{ textAlign: "center" }}>
1717
<p>Is supported: {isSupported.toString()}</p>
18-
<p>Color: {color}</p>
1918
<button onClick={onClick}>Get Color</button>
19+
<br />
20+
<br />
21+
<label>Color </label><input type="color" defaultValue={color}/>
2022
</div>
2123
}
2224
```

packages/react-tools/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
- [x] useGeolocation
9797
- [x] useShare
9898
- [x] useEyeDropper
99+
- [x] useDialogBox
99100
- [ ] useSpeech (https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance#examples)
100101
- [ ] useFetch (with suspense ???)
101102
- [ ] useAsync

packages/react-tools/src/hooks/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ export { useLongPress } from './useLongPress';
6363
export { useBattery } from './useBattery';
6464
export { useGeolocation } from './useGeolocation';
6565
export { useShare } from './useShare';
66-
export { useEyeDropper } from './useEyeDropper';
66+
export { useEyeDropper } from './useEyeDropper';
67+
export { useDialogBox } from './useDialogBox';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { useCallback } from "react"
3+
4+
5+
/**
6+
* **`useDialogBox`**: Hook to use Dialog Box _prompt_, _alert_ or _confirm_.
7+
* @param {"prompt"|"alert"|"confirm"} type - set dialog box type.
8+
* @returns {((message?: string, default?: string) => string|null) | ((message?: any) => void) | ((message?: string) => boolean)} open - function to activate dialog box.
9+
*/
10+
function useDialogBox(type: "prompt"): ((message?: string, _default?: string) => string | null);
11+
function useDialogBox(type: "alert"): ((message?: any) => void);
12+
function useDialogBox(type: "confirm"): ((message?: string) => boolean);
13+
function useDialogBox(type: "prompt" | "confirm" | "alert"): ((message?: string, _default?: string) => string | null) | ((message?: any) => void) | ((message?: string) => boolean) {
14+
const prompt = useCallback((message?: string, _default?: string) => window.prompt(message, _default), []);
15+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
16+
const alert = useCallback((message?: any) => window.alert(message), []);
17+
const confirm = useCallback((message?: string) => window.confirm(message), []);
18+
19+
return type === "prompt"
20+
? prompt
21+
: type === "alert"
22+
? alert
23+
: confirm;
24+
}
25+
26+
export { useDialogBox };

packages/react-tools/src/hooks/useSyncExternalStore.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import { useSyncExternalStore as legacy, useCallback, useEffect, useRef, useStat
88
*/
99
function useSyncExternalStorePolyfill<Snapshot>(subscribe: (onStoreChange: () => void) => () => void, getSnapshot: () => Snapshot): Snapshot {
1010
const [state, setState] = useState<Snapshot>(getSnapshot());
11-
const subscribeRef = useRef(subscribe);
1211
const getSnapshotRef = useRef(getSnapshot);
1312
const update = useCallback(() => { setState(getSnapshotRef.current()) }, []);
1413

1514
useEffect(() => {
1615
update();
17-
return subscribeRef.current(update);
18-
}, [update]);
16+
return subscribe(update);
17+
}, [update, subscribe]);
1918

2019
return state;
2120
}

0 commit comments

Comments
 (0)