Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clipboard get the different unicode whitespace #4392

Merged
merged 7 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/rich-moles-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/use-clipboard": patch
"@nextui-org/snippet": patch
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
---

Fix clipboard get different unicode whitespace (#4225)
29 changes: 3 additions & 26 deletions packages/components/snippet/stories/snippet.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,9 @@ export const MultiLine = {
args: {
...defaultProps,
children: [
// "npm install @nextui-org/react",
// "yarn add @nextui-org/react",
// "pnpm add @nextui-org/react",
`
{
"name": "Next.js PWA",
"short_name": "NextPWA",
"description": "A Progressive Web App built with Next.js and React",
"start_url": "/",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#000000",
"icons": [
{
"src": "/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
`,
"npm install @nextui-org/react",
"yarn add @nextui-org/react",
"pnpm add @nextui-org/react",
winchesHe marked this conversation as resolved.
Show resolved Hide resolved
],
},
};
12 changes: 10 additions & 2 deletions packages/hooks/use-clipboard/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,26 @@ export function useClipboard({timeout = 2000}: UseClipboardProps = {}) {
[onClearTimeout, timeout],
);

const transformWhitespace = useCallback((text: string) => {
// Manually replace all whitespace to avoid get different unicode characters;
return text.replace(/\s/g, " ");
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
}, []);

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

navigator.clipboard
.writeText(valueToCopy)
.writeText(decodedValue)
.then(() => handleCopyResult(true))
.catch((err) => setError(err));
} else {
setError(new Error("useClipboard: navigator.clipboard is not supported"));
}
},
[handleCopyResult],
[handleCopyResult, transformWhitespace],
);

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