Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f535734

Browse files
committedDec 27, 2024
feat: optimization
1 parent b9e837f commit f535734

File tree

1 file changed

+7
-7
lines changed
  • packages/hooks/use-clipboard/src

1 file changed

+7
-7
lines changed
 

‎packages/hooks/use-clipboard/src/index.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export interface UseClipboardProps {
88
timeout?: number;
99
}
1010

11+
const transformValue = (text: string) => {
12+
// Manually replace all   to avoid get different unicode characters;
13+
return text.replace(/[\u00A0]/g, " ");
14+
};
15+
1116
/**
1217
* Copies the given text to the clipboard.
1318
* @param {number} timeout - timeout in ms, default 2000
@@ -33,16 +38,11 @@ export function useClipboard({timeout = 2000}: UseClipboardProps = {}) {
3338
[onClearTimeout, timeout],
3439
);
3540

36-
const transformWhitespace = useCallback((text: string) => {
37-
// Manually replace all   to avoid get different unicode characters;
38-
return text.replace(/[\u00A0]/g, " ");
39-
}, []);
40-
4141
const copy = useCallback(
4242
(valueToCopy: any) => {
4343
if ("clipboard" in navigator) {
4444
const transformedValue =
45-
typeof valueToCopy === "string" ? transformWhitespace(valueToCopy) : valueToCopy;
45+
typeof valueToCopy === "string" ? transformValue(valueToCopy) : valueToCopy;
4646

4747
navigator.clipboard
4848
.writeText(transformedValue)
@@ -52,7 +52,7 @@ export function useClipboard({timeout = 2000}: UseClipboardProps = {}) {
5252
setError(new Error("useClipboard: navigator.clipboard is not supported"));
5353
}
5454
},
55-
[handleCopyResult, transformWhitespace],
55+
[handleCopyResult],
5656
);
5757

5858
const reset = useCallback(() => {

0 commit comments

Comments
 (0)