-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce useClipboard hook to remove dependency (#16751)
- Loading branch information
1 parent
750b6c8
commit e233d8a
Showing
17 changed files
with
200 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
docs/docs/docs/01-core/helper-plugin/hooks/use-clipboard.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: useClipboard | ||
description: API reference for the useClipboard hook in Strapi | ||
tags: | ||
- hooks | ||
- helper-plugin | ||
--- | ||
|
||
A small abstraction around the [`navigation.clipboard`](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard) API. | ||
Currently we only expose a `copy` method which abstracts the `writeText` method of the clipboard API. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { useClipboard } from '@strapi/helper-plugin'; | ||
|
||
const MyComponent = () => { | ||
const { copy } = useClipboard(); | ||
const handleClick = async () => { | ||
const didCopy = await copy('hello world'); | ||
|
||
if (didCopy) { | ||
alert('copied!'); | ||
} | ||
}; | ||
|
||
return <button onClick={handleClick}>Copy text</button>; | ||
}; | ||
``` | ||
|
||
## Typescript | ||
|
||
```ts | ||
function useClipboard(): { | ||
copy: (text: string) => Promise<boolean>; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 28 additions & 26 deletions
54
packages/core/admin/admin/src/pages/SettingsPage/components/Tokens/TokenBox/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 11 additions & 9 deletions
20
...e/admin/admin/src/pages/SettingsPage/pages/Users/components/MagicLink/MagicLinkWrapper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/core/helper-plugin/src/hooks/tests/useClipboard.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { useClipboard } from '../useClipboard'; | ||
|
||
describe('useClipboard', () => { | ||
it('should return false if the value passed to the function is not a string or number', async () => { | ||
const { result } = renderHook(() => useClipboard()); | ||
|
||
expect(await result.current.copy({})).toBe(false); | ||
}); | ||
|
||
it('should return false if the value passed to copy is an empty string', async () => { | ||
const { result } = renderHook(() => useClipboard()); | ||
|
||
expect(await result.current.copy('')).toBe(false); | ||
}); | ||
|
||
it('should return true if the copy was successful', async () => { | ||
const { result } = renderHook(() => useClipboard()); | ||
|
||
expect(await result.current.copy('test')).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useCallback } from 'react'; | ||
|
||
export const useClipboard = () => { | ||
const copy = useCallback(async (value) => { | ||
try { | ||
// only strings and numbers casted to strings can be copied to clipboard | ||
if (typeof value !== 'string' && typeof value !== 'number') { | ||
throw new Error(`Cannot copy typeof ${typeof value} to clipboard, must be a string`); | ||
} | ||
// empty strings are also considered invalid | ||
else if (value === '') { | ||
throw new Error(`Cannot copy empty string to clipboard.`); | ||
} | ||
|
||
const stringifiedValue = value.toString(); | ||
|
||
await navigator.clipboard.writeText(stringifiedValue); | ||
|
||
return true; | ||
} catch (error) { | ||
/** | ||
* Realistically this isn't useful in production as there's nothing the user can do. | ||
*/ | ||
if (process.env.NODE_ENV === 'development') { | ||
console.warn('Copy failed', error); | ||
} | ||
|
||
return false; | ||
} | ||
}, []); | ||
|
||
return { copy }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.