Skip to content

Commit 921c3e1

Browse files
authored
Remove clipboard-copy dependency from CodeSnippet, CopyButton (#726)
* chore(deps): remove clipboard-copy * feat: add copy prop, use navigator.clipboard API * docs: add clipboard-copy back to docsite for more browser support * docs(component-api): use outbound link * docs: add override/prevent copy examples
1 parent 6ed4aaa commit 921c3e1

19 files changed

+163
-68
lines changed

COMPONENT_INDEX.md

Lines changed: 25 additions & 23 deletions
Large diffs are not rendered by default.

docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"autoprefixer": "^10.2.3",
1414
"carbon-components": "10.38.0",
1515
"carbon-components-svelte": "../",
16+
"clipboard-copy": "^4.0.1",
1617
"mdsvex": "^0.8.8",
1718
"npm-run-all": "^4.1.5",
1819
"postcss": "^8.2.4",

docs/src/COMPONENT_API.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,16 @@
839839
"constant": false,
840840
"reactive": false
841841
},
842+
{
843+
"name": "copy",
844+
"kind": "let",
845+
"description": "Override the default copy behavior of using the navigator.clipboard.writeText API to copy text",
846+
"type": "(code: string) => void",
847+
"value": "async (code) => { try { await navigator.clipboard.writeText(code); } catch (e) { console.log(e); } }",
848+
"isFunction": true,
849+
"constant": false,
850+
"reactive": false
851+
},
842852
{
843853
"name": "expanded",
844854
"kind": "let",
@@ -1911,6 +1921,16 @@
19111921
"isFunction": false,
19121922
"constant": false,
19131923
"reactive": false
1924+
},
1925+
{
1926+
"name": "copy",
1927+
"kind": "let",
1928+
"description": "Override the default copy behavior of using the navigator.clipboard.writeText API to copy text",
1929+
"type": "(text: string) => void",
1930+
"value": "async (text) => { try { await navigator.clipboard.writeText(text); } catch (e) { console.log(e); } }",
1931+
"isFunction": true,
1932+
"constant": false,
1933+
"reactive": false
19141934
}
19151935
],
19161936
"slots": [],

docs/src/components/ComponentApi.svelte

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1010
import {
1111
Link,
12+
OutboundLink,
1213
StructuredList,
1314
StructuredListHead,
1415
StructuredListRow,
@@ -43,10 +44,9 @@
4344

4445
<p style="margin-bottom: var(--cds-layout-02)">
4546
Source code:
46-
<Link inline size="lg" href="{source}" target="_blank">
47+
<OutboundLink inline href="{source}">
4748
{component.filePath}
48-
<Launch16 />
49-
</Link>
49+
</OutboundLink>
5050
</p>
5151

5252
<h3 id="props">Props</h3>
@@ -94,14 +94,12 @@
9494
Carbon Svelte icon
9595
</TooltipDefinition>
9696
{:else if type.startsWith("HTML")}
97-
<Link
97+
<OutboundLink
9898
href="{mdn_api}{type}"
99-
target="_blank"
10099
style="white-space: nowrap"
101100
>
102101
{type}
103-
<Launch16 />
104-
</Link>
102+
</OutboundLink>
105103
{:else if type in typeMap}
106104
<code>{typeMap[type]}</code>
107105
{:else if type.startsWith("(")}

docs/src/components/InlineSnippet.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<script>
22
export let code = "";
33
4+
import copy from "clipboard-copy";
45
import { CodeSnippet } from "carbon-components-svelte";
56
</script>
67

78
<div>
8-
<CodeSnippet code="{code}" type="inline" />
9+
<CodeSnippet code="{code}" type="inline" copy="{(text) => copy(text)}" />
910
</div>
1011

1112
<style>

docs/src/components/Preview.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
export let src = "";
55
export let framed = false;
66
7+
import copy from "clipboard-copy";
78
import { CodeSnippet, Button } from "carbon-components-svelte";
89
import Launch16 from "carbon-icons-svelte/lib/Launch16";
910
import { url } from "@sveltech/routify";
@@ -40,7 +41,7 @@
4041
{/if}
4142
</div>
4243
<div class="code-override">
43-
<CodeSnippet type="multi" code="{codeRaw}">
44+
<CodeSnippet type="multi" code="{codeRaw}" copy="{(text) => copy(text)}">
4445
{@html code}
4546
</CodeSnippet>
4647
</div>

docs/src/pages/components/CodeSnippet.svx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
import { CodeSnippet, InlineNotification, Link } from "carbon-components-svelte";
2+
import { CodeSnippet } from "carbon-components-svelte";
33
import Preview from "../../components/Preview.svelte";
44

55
let code = `// helpers.js
@@ -27,14 +27,31 @@ let comment = `
2727
`
2828
</script>
2929

30-
<InlineNotification svx-ignore title="Note:" kind="info" hideCloseButton>
31-
<div class="body-short-01">As of version 0.32, the CodeSnippet will copy the provided `code` text when clicking the copy button.</div>
32-
</InlineNotification>
30+
This component uses the native, asynchronous [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText) to copy text.
31+
32+
Please note that the `clipboard.writeText` API is not supported in [IE 11 nor Safari iOS version 13.3 or lower](https://caniuse.com/mdn-api_clipboard_writetext).
33+
34+
You can override the default copy functionality with your own copy text implementation. See [Overriding copy functionality](#overriding-copy-functionality).
35+
3336

3437
### Default (single-line)
3538

3639
<CodeSnippet code="yarn add -D carbon-components-svelte" />
3740

41+
### Overriding copy functionality
42+
43+
To override the default copy behavior, pass a custom function to the `copy` prop.
44+
45+
In this example, we use the open source module [clipboard-copy](https://github.com/feross/clipboard-copy) to copy the text instead of the default Clipboard API.
46+
47+
<FileSource src="/framed/CodeSnippet/CodeSnippetOverride" />
48+
49+
### Preventing copy functionality
50+
51+
To prevent text from being copied entirely, pass a "noop" function to the `copy` prop.
52+
53+
<CodeSnippet code="yarn add -D carbon-components-svelte" copy={() => {}} />
54+
3855
### Inline
3956

4057
<CodeSnippet type="inline" code="rm -rf node_modules/" />
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
<script>
2-
import { CopyButton, InlineNotification, Link } from "carbon-components-svelte";
2+
import { CopyButton } from "carbon-components-svelte";
33
import Preview from "../../components/Preview.svelte";
44
</script>
55

6-
<InlineNotification svx-ignore title="Note:" kind="info" hideCloseButton>
7-
<div class="body-short-01">As of version 0.32, this component will copy the provided `code` text when clicking the button.</div>
8-
</InlineNotification>
6+
This component uses the native, asynchronous [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText) to copy text.
7+
8+
Please note that the `clipboard.writeText` API is not supported in [IE 11 nor Safari iOS version 13.3 or lower](https://caniuse.com/mdn-api_clipboard_writetext).
9+
10+
You can override the default copy functionality with your own copy text implementation. See [Overriding copy functionality](#overriding-copy-functionality).
911

1012
### Default
1113

1214
<CopyButton text="Carbon svelte" />
1315

1416
### Custom feedback text
1517

16-
<CopyButton text="Carbon svelte" feedback="Copied to clipboard" />
18+
<CopyButton text="Carbon svelte" feedback="Copied to clipboard" />
19+
20+
### Overriding copy functionality
21+
22+
To override the default copy behavior, pass a custom function to the `copy` prop.
23+
24+
In this example, we use the open source module [clipboard-copy](https://github.com/feross/clipboard-copy) to copy the text instead of the default Clipboard API.
25+
26+
<FileSource src="/framed/CopyButton/CopyButtonOverride" />
27+
28+
### Preventing copy functionality
29+
30+
To prevent text from being copied entirely, pass a "noop" function to the `copy` prop.
31+
32+
<CopyButton text="This text should not be copied" copy={() => {}} />
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import copy from "clipboard-copy";
3+
import { CodeSnippet } from "carbon-components-svelte";
4+
</script>
5+
6+
<CodeSnippet
7+
code="yarn add -D carbon-components-svelte"
8+
copy="{(text) => copy(text)}"
9+
/>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import copy from "clipboard-copy";
3+
import { CopyButton } from "carbon-components-svelte";
4+
</script>
5+
6+
<CopyButton text="Carbon svelte" copy="{(text) => copy(text)}" />

0 commit comments

Comments
 (0)