Skip to content

Commit

Permalink
feat: optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
winchesHe committed Dec 27, 2024
1 parent b9e837f commit f535734
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/hooks/use-clipboard/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export interface UseClipboardProps {
timeout?: number;
}

const transformValue = (text: string) => {
// Manually replace all   to avoid get different unicode characters;
return text.replace(/[\u00A0]/g, " ");
};

/**
* Copies the given text to the clipboard.
* @param {number} timeout - timeout in ms, default 2000
Expand All @@ -33,16 +38,11 @@ export function useClipboard({timeout = 2000}: UseClipboardProps = {}) {
[onClearTimeout, timeout],
);

const transformWhitespace = useCallback((text: string) => {
// Manually replace all   to avoid get different unicode characters;
return text.replace(/[\u00A0]/g, " ");
}, []);

const copy = useCallback(
(valueToCopy: any) => {
if ("clipboard" in navigator) {
const transformedValue =
typeof valueToCopy === "string" ? transformWhitespace(valueToCopy) : valueToCopy;
typeof valueToCopy === "string" ? transformValue(valueToCopy) : valueToCopy;

navigator.clipboard
.writeText(transformedValue)
Expand All @@ -52,7 +52,7 @@ export function useClipboard({timeout = 2000}: UseClipboardProps = {}) {
setError(new Error("useClipboard: navigator.clipboard is not supported"));
}
},
[handleCopyResult, transformWhitespace],
[handleCopyResult],
);

const reset = useCallback(() => {
Expand Down

0 comments on commit f535734

Please sign in to comment.