Skip to content

Commit f469811

Browse files
committed
[WIP] useClipboard hook
1 parent bc6d4d8 commit f469811

2 files changed

Lines changed: 106 additions & 12 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# useDocumentVisibility
2+
Hook to track document visibility. Refers to [Document VisibilityState](https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilityState).
3+
4+
## Usage
5+
6+
```tsx
7+
export const UseDocumentVisibility = () => {
8+
const status = useDocumentVisibility();
9+
const [messages, setMessages] = useState <{ status: DocumentVisibilityState, date: string }[]>([]);
10+
11+
useEffect(() => {
12+
setMessages(m => ([...m, {status: status, date: new Date().toLocaleTimeString()}]))
13+
}, [status])
14+
15+
return (<div>
16+
<h3>Status:</h3>
17+
{
18+
messages.map(el => (
19+
<p key={el.date}>{el.date}: {el.status}</p>
20+
))
21+
}
22+
</div>);
23+
}
24+
```
25+
26+
> The component tracks every document visibility status change and display them on screen with date they taking place. Minimize window or change tab to show them.
27+
28+
29+
## API
30+
31+
```tsx
32+
useDocumentVisibility (): DocumentVisibilityState
33+
```
34+
35+
> ### Params
36+
>
37+
>
38+
>
39+
40+
> ### Returns
41+
>
42+
> __documentVisibility__
43+
> - _DocumentVisibilityState_
44+
>

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

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ const askPermission = (type: "clipboard-read" | "clipboard-write") => {
1414
throw err
1515
});
1616
}
17-
export const useClipboard = ({ useValue, target }: { useValue: boolean, target?: RefObject<HTMLElement>|HTMLElement }) => {
17+
18+
const isFirefox = () => navigator.clipboard.read === undefined;
19+
20+
export const useClipboard = ({ useValue, target }: { useValue: boolean, target?: RefObject<HTMLElement> | HTMLElement }) => {
21+
const valueRef = useRef<string | Blob>("");
1822
const copy = useRef((blob: Blob | Blob[]) => (
1923
askPermission("clipboard-write")
2024
.then(() => navigator.clipboard.write(
@@ -29,6 +33,7 @@ export const useClipboard = ({ useValue, target }: { useValue: boolean, target?:
2933
: target as HTMLElement
3034
: document;
3135
element!.dispatchEvent(new ClipboardEvent("copy", {
36+
//TODO
3237
clipboardData: new DataTransfer()
3338
}))
3439
})
@@ -40,13 +45,27 @@ export const useClipboard = ({ useValue, target }: { useValue: boolean, target?:
4045
const copyText = useRef((text: string) => (
4146
askPermission("clipboard-write")
4247
.then(() => navigator.clipboard.writeText(text))
48+
.then(() => {
49+
const element = target
50+
? (target as RefObject<HTMLElement>).current
51+
? (target as RefObject<HTMLElement>).current
52+
: target as HTMLElement
53+
: document;
54+
element!.dispatchEvent(new ClipboardEvent("copy", {
55+
//TODO
56+
clipboardData: new DataTransfer()
57+
}))
58+
})
4359
.catch(err => {
4460
throw err;
4561
})
4662
));
4763

48-
const paste = useRef(() => (
49-
askPermission("clipboard-read")
64+
const paste = useRef(() => {
65+
if (isFirefox()) {
66+
throw Error("Paste from Clipboard isn't allowed in Firefox.");
67+
}
68+
return askPermission("clipboard-read")
5069
.then(() => navigator.clipboard.read())
5170
.then(items => {
5271
const promises = [];
@@ -57,27 +76,58 @@ export const useClipboard = ({ useValue, target }: { useValue: boolean, target?:
5776
}
5877
return Promise.all(promises);
5978
})
79+
.then(() => {
80+
const element = target
81+
? (target as RefObject<HTMLElement>).current
82+
? (target as RefObject<HTMLElement>).current
83+
: target as HTMLElement
84+
: document;
85+
element!.dispatchEvent(new ClipboardEvent("paste", {
86+
//TODO
87+
clipboardData: new DataTransfer()
88+
}))
89+
})
6090
.catch(err => {
6191
throw err;
6292
})
63-
));
93+
});
6494

65-
const pasteText = useRef(() => (
66-
askPermission("clipboard-read")
95+
const pasteText = useRef(() => {
96+
if (isFirefox()) {
97+
throw Error("Paste from Clipboard isn't allowed in Firefox.");
98+
}
99+
return askPermission("clipboard-read")
67100
.then(() => navigator.clipboard.readText())
101+
.then(() => {
102+
const element = target
103+
? (target as RefObject<HTMLElement>).current
104+
? (target as RefObject<HTMLElement>).current
105+
: target as HTMLElement
106+
: document;
107+
element!.dispatchEvent(new ClipboardEvent("paste", {
108+
//TODO
109+
clipboardData: new DataTransfer()
110+
}))
111+
})
68112
.catch(err => {
69113
throw err;
70114
})
71-
));
115+
});
72116

73117
const value = useSyncExternalStore(
74118
useCallback(notif => {
75-
119+
const element = target
120+
? (target as RefObject<HTMLElement>).current
121+
? (target as RefObject<HTMLElement>).current
122+
: target as HTMLElement
123+
: document;
76124
}, []),
77-
useCallback(async () => (
78-
askPermission("clipboard-read")
79-
.then(()=>pasteText.current)
80-
), [])
125+
useCallback(async () => {
126+
if (isFirefox()) {
127+
return valueRef.current;
128+
}
129+
return paste.current();
130+
}, [])
81131
);
82132

83133
if (useValue) {

0 commit comments

Comments
 (0)