|
| 1 | +# useClipboard |
| 2 | +Hook to handle Clipboard. Refers to [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API). __N.B.__: The hook has the same compatibility issues as the Clipboard API for Firefox, i.e. it is currently impossible to read from the clipboard. |
| 3 | + |
| 4 | +## Usage |
| 5 | + |
| 6 | +```tsx |
| 7 | +export const UseClipboard = () => { |
| 8 | + const ref = useRef<HTMLDivElement>(null); |
| 9 | + const [val, setVal] = useState(""); |
| 10 | + const [value, write, read] = useClipboard({ useValue: true, target: ref, dataType: "text" }); |
| 11 | + const copy = useCallback(async () => { |
| 12 | + await write(getSelection()?.toString() || ""); |
| 13 | + }, [write]) |
| 14 | + |
| 15 | + const paste = useCallback(async () => { |
| 16 | + const value = await read(); |
| 17 | + setVal(value as string); |
| 18 | + }, [read]) |
| 19 | + |
| 20 | + return <div style={{ display: "grid", gridTemplateColumns: "50% 50%", columnGap: 15 }}> |
| 21 | + <div ref={ref} style={{ position: "relative", border: "1px solid lightgray" }}> |
| 22 | + <div style={{ display: "grid", gridTemplateColumns: "100px 100px", justifyContent: "center", columnGap: 15 }}> |
| 23 | + <button onClick={copy}>Copy</button> |
| 24 | + <button onClick={paste}>Paste</button> |
| 25 | + </div> |
| 26 | + <div> |
| 27 | + <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Incidunt repudiandae fugit distinctio molestiae excepturi ex qui, impedit iste odit. Explicabo quis reprehenderit voluptates reiciendis nostrum minima autem temporibus sint doloribus</p> |
| 28 | + </div> |
| 29 | + <div> |
| 30 | + <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Incidunt repudiandae fugit distinctio molestiae excepturi ex qui, impedit iste odit. Explicabo quis reprehenderit voluptates reiciendis nostrum minima autem temporibus sint doloribus</p> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", columnGap: 15, border: "1px solid lightgray" }}> |
| 34 | + <div style={{ textAlign: "left", padding: "0 1em", maxHeight: 300, overflow: "auto" }}> |
| 35 | + <p><strong>Use ClipboardValue:</strong></p> |
| 36 | + <pre>{JSON.stringify(value, null, 2)}</pre> |
| 37 | + </div> |
| 38 | + <div style={{ textAlign: "left", padding: "0 1em", maxHeight: 300, overflow: "auto" }}> |
| 39 | + <p><strong>Internal value:</strong></p> |
| 40 | + <pre>{JSON.stringify(val, null, 2)}</pre> |
| 41 | + </div> |
| 42 | + </div> |
| 43 | + </div> |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +> - The component has an internal state _val_ and invokes _useClipboard_ hook with these properties: _useValue_=__true__ _target_=__ref__ and _dataType_=__text__. It means that hook will return _value_ of type __string__ and _write_ and _read_ functions that handle __text__ data type. The component declares also two functions: |
| 48 | +> - _copy_ that invokes __write__ function to write into clipboard text selected. |
| 49 | +> - _paste_ that invokes __read__ function to get value from clipboard and setting it to internal stave variable _val_. |
| 50 | +> - __ref__ is attached to a div that contains two buttons _COPY_ and _PASTE_, that have _copy_ and _paste_ functions respectively to handle their click event and two paragraph texts. |
| 51 | +> - Another div is rendered that displays the internal state _val_ and current _clipboard_ values. |
| 52 | +
|
| 53 | + |
| 54 | +## API |
| 55 | + |
| 56 | +```tsx |
| 57 | +useClipboard({ useValue, dataType, target }: { useValue: boolean, dataType: "text" | "any", target?: RefObject<HTMLElement> | HTMLElement }): [string, (text: string) => Promise<void>, () => Promise<string>] | [string | Blob | (string | Blob)[], (blob: Blob | Blob[]) => Promise<void>, () => Promise<string | Blob | (string | Blob)[]>] | [(text: string) => Promise<void>, () => Promise<string>] | [(blob: Blob | Blob[]) => Promise<void>, () => Promise<string | Blob | (string | Blob)[]>] |
| 58 | +``` |
| 59 | +
|
| 60 | +> ### Params |
| 61 | +> |
| 62 | +> - __param__: _Object_ |
| 63 | +> - __param.useValue__: _boolean_ |
| 64 | +return a value with current clipboard value or not. |
| 65 | +> - __param.target?__: _RefObject<HTMLElement>|HTMLElement_ |
| 66 | +target on which delimiter handling. |
| 67 | +> - __param.dataType?__: _"text"|"any"_ |
| 68 | +data type handling. Based on it, Hook will return the functions for writing or reading text only or any type of data. |
| 69 | +> |
| 70 | +
|
| 71 | +> ### Returns |
| 72 | +> |
| 73 | +> __array__: elements depends on _useValue_ and _dataType_ values: if _dataType_ equals __text__ there will are only function to writing and reading text data type, otherwise any data type. If _useValue_ is true the first element will be _clipboard current value_. |
| 74 | +> - __Union of__: |
| 75 | +> - __Array__: |
| 76 | +> - _string_ |
| 77 | +> - _(text: string) => Promise<void>_ |
| 78 | +> - _() => Promise<string>_ |
| 79 | +> - __Array__: |
| 80 | +> - _string|Blob|(string|Blob)[]_ |
| 81 | +> - _(blob: Blob|Blob[]) => Promise<void>_ |
| 82 | +> - _() => Promise<string|Blob|(string|Blob)[]>_ |
| 83 | +> - __Array__: |
| 84 | +> - _(text: string) => Promise<void>_ |
| 85 | +> - _() => Promise<string>_ |
| 86 | +> - __Array__: |
| 87 | +> - _(blob: Blob|Blob[]) => Promise<void>_ |
| 88 | +> - _() => Promise<string|Blob|(string|Blob)[]>_ |
| 89 | +> |
0 commit comments