Skip to content

Commit a681630

Browse files
authored
Merge pull request #2525 from hashicorp/hds-3840/clipboard-polyfill
`hds-clipboard`: added `clipboard-polyfill` to support non-secure environments
2 parents 972c0c0 + 59384ba commit a681630

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

.changeset/soft-pets-hear.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hashicorp/design-system-components": minor
3+
---
4+
5+
`hds-clipboard`: added `clipboard-polyfill` to support product usage in non-secure environments. This impacts `Copy::Button`, `Copy::Snippet`, `CodeBlock`, and `MaskedInput`.

packages/components/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@floating-ui/dom": "^1.6.3",
4343
"@hashicorp/design-system-tokens": "^2.2.1",
4444
"@hashicorp/flight-icons": "^3.7.0",
45+
"clipboard-polyfill": "^4.1.0",
4546
"decorator-transforms": "^1.1.0",
4647
"ember-a11y-refocus": "^4.1.3",
4748
"ember-cli-sass": "^11.0.1",

packages/components/src/modifiers/hds-clipboard.ts

+20-10
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,27 @@ export const writeTextToClipboard = async (
117117
// DEBUG uncomment this for easy debugging
118118
// console.log('success', textToCopy);
119119
return true;
120-
} catch (error) {
121-
// clipboard write failed
122-
// this probably never happens (see comment above) or happens only for very old browsers that don't for which `navigator.clipboard` is undefined
123-
warn(
124-
`copy action failed, please check your browser‘s permissions: ${JSON.stringify(
125-
error
126-
)}`,
127-
{
128-
id: 'hds-clipboard.write-text-to-clipboard.catch-error',
120+
} catch {
121+
// if it is not a secure context, use the polyfill
122+
// to test that this works in a non-secure context, access the port through your IP address (ie. XXX.XXX.X.XXX:4200/)
123+
if (!navigator.clipboard) {
124+
try {
125+
const clipboard = await import('clipboard-polyfill');
126+
await clipboard.writeText(textToCopy);
127+
return true;
128+
} catch (error) {
129+
warn(
130+
`copy action failed, unable to use clipboard-polyfill: ${JSON.stringify(
131+
error
132+
)}`,
133+
{
134+
id: 'hds-clipboard.write-text-to-clipboard.catch-error',
135+
}
136+
);
137+
return false;
129138
}
130-
);
139+
}
140+
131141
return false;
132142
}
133143
} else {

yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -4257,6 +4257,7 @@ __metadata:
42574257
"@typescript-eslint/eslint-plugin": "npm:^8.5.0"
42584258
"@typescript-eslint/parser": "npm:^8.5.0"
42594259
babel-plugin-ember-template-compilation: "npm:^2.2.4"
4260+
clipboard-polyfill: "npm:^4.1.0"
42604261
concurrently: "npm:^8.2.2"
42614262
decorator-transforms: "npm:^1.1.0"
42624263
ember-a11y-refocus: "npm:^4.1.3"
@@ -10418,6 +10419,13 @@ __metadata:
1041810419
languageName: node
1041910420
linkType: hard
1042010421

10422+
"clipboard-polyfill@npm:^4.1.0":
10423+
version: 4.1.0
10424+
resolution: "clipboard-polyfill@npm:4.1.0"
10425+
checksum: 10/083a91203e9fc628acb356243f14f36912875d92dc9235ebb20fa954dc9feaa23048de9f5d59e5db9497c1d58a6b5827391c1ce3a09c4c03eff39914c60209b1
10426+
languageName: node
10427+
linkType: hard
10428+
1042110429
"clipboard@npm:^2.0.11":
1042210430
version: 2.0.11
1042310431
resolution: "clipboard@npm:2.0.11"

0 commit comments

Comments
 (0)