Skip to content

Commit 4f0ef58

Browse files
winchesHewingkwong
andauthored
fix: clipboard get the different unicode whitespace (#4392)
* fix: clipboard get the different unicode whitespace * fix: clipboard get the different unicode whitespace * fix(snippet): incorrect MultiLine story * fix: nbsp in editor * fix: rename * fix: md * feat: optimization --------- Co-authored-by: WK Wong <wingkwong.code@gmail.com>
1 parent a83388a commit 4f0ef58

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
lines changed

.changeset/rich-moles-compare.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nextui-org/use-clipboard": patch
3+
---
4+
5+
Fix clipboard get different unicode whitespace (#4225)

packages/components/snippet/stories/snippet.stories.tsx

+3-26
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,9 @@ export const MultiLine = {
9292
args: {
9393
...defaultProps,
9494
children: [
95-
// "npm install @nextui-org/react",
96-
// "yarn add @nextui-org/react",
97-
// "pnpm add @nextui-org/react",
98-
`
99-
{
100-
"name": "Next.js PWA",
101-
"short_name": "NextPWA",
102-
"description": "A Progressive Web App built with Next.js and React",
103-
"start_url": "/",
104-
"display": "standalone",
105-
"background_color": "#ffffff",
106-
"theme_color": "#000000",
107-
"icons": [
108-
{
109-
"src": "/icon-192x192.png",
110-
"sizes": "192x192",
111-
"type": "image/png"
112-
},
113-
{
114-
"src": "/icon-512x512.png",
115-
"sizes": "512x512",
116-
"type": "image/png"
117-
}
118-
]
119-
}
120-
`,
95+
"npm install @nextui-org/react",
96+
"yarn add @nextui-org/react",
97+
"pnpm add @nextui-org/react",
12198
],
12299
},
123100
};

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

+9-1
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 &nbsp; 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
@@ -36,8 +41,11 @@ export function useClipboard({timeout = 2000}: UseClipboardProps = {}) {
3641
const copy = useCallback(
3742
(valueToCopy: any) => {
3843
if ("clipboard" in navigator) {
44+
const transformedValue =
45+
typeof valueToCopy === "string" ? transformValue(valueToCopy) : valueToCopy;
46+
3947
navigator.clipboard
40-
.writeText(valueToCopy)
48+
.writeText(transformedValue)
4149
.then(() => handleCopyResult(true))
4250
.catch((err) => setError(err));
4351
} else {

0 commit comments

Comments
 (0)