Skip to content

Commit 97c8e5c

Browse files
authored
docs(styles): document CDN styles usage (#232)
Closes #214
1 parent 5ea7dbd commit 97c8e5c

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,37 @@ Depending on your set-up, importing a CSS StyleSheet in Svelte may require a CSS
9393
<Highlight language={typescript} {code} />
9494
```
9595

96+
#### Linking from a CDN
97+
98+
CSS StyleSheets can also be externally linked from a Content Delivery Network (CDN) like [unpkg.com](https://unpkg.com/).
99+
100+
This is best suited for prototyping and not recommended for production use.
101+
102+
**HTML**
103+
104+
```html
105+
<!DOCTYPE html>
106+
<html lang="en">
107+
<head>
108+
<link
109+
rel="stylesheet"
110+
href="https://unpkg.com/svelte-highlight/styles/github.css"
111+
/>
112+
</head>
113+
</html>
114+
```
115+
116+
**svelte:head**
117+
118+
```svelte
119+
<svelte:head>
120+
<link
121+
rel="stylesheet"
122+
href="https://unpkg.com/svelte-highlight/styles/github.css"
123+
/>
124+
</svelte:head>
125+
```
126+
96127
## Svelte Syntax Highlighting
97128

98129
Use the `HighlightSvelte` component for Svelte syntax highlighting.

demo/lib/ScopedStyle.svelte

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@
22
export let name = "";
33
export let moduleName = "";
44
export let useInjectedStyles = true;
5+
export let useCdnImport = false;
56
67
import HighlightSvelte from "../../src/HighlightSvelte.svelte";
78
import * as styles from "../../src/styles";
89
10+
$: importStyles = useInjectedStyles
11+
? `import ${moduleName} from "svelte-highlight/styles/${name}";`
12+
: `import "svelte-highlight/styles/${name}.css";`;
913
$: code = `<script>
1014
import Highlight from "svelte-highlight";
1115
import typescript from "svelte-highlight/languages/typescript";
12-
${
13-
useInjectedStyles
14-
? `import ${moduleName} from "svelte-highlight/styles/${name}";`
15-
: `import "svelte-highlight/styles/${name}.css";`
16-
}
17-
16+
${!useInjectedStyles && useCdnImport ? "" : importStyles + "\n"}
1817
const code = "const add = (a: number, b: number) => a + b;";
1918
<\/script>
2019
${
21-
useInjectedStyles
20+
useInjectedStyles || useCdnImport
2221
? `\n<svelte:head>
23-
{@html ${moduleName}}
22+
${
23+
useInjectedStyles
24+
? `{@html ${moduleName}}`
25+
: `<link
26+
rel="stylesheet"
27+
href="https://unpkg.com/svelte-highlight/styles/${name}.css"
28+
/>`
29+
}
2430
</svelte:head>\n`
2531
: ""
2632
}

demo/routes/index.svelte

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
include: ["highlight.js", "highlight.js/lib/core"],
2525
},
2626
};`;
27+
const svelteHeadCdn = `<link
28+
rel="stylesheet"
29+
href="https://unpkg.com/svelte-highlight/styles/github.css"
30+
/>\n`;
2731
</script>
2832

2933
<Row>
@@ -158,6 +162,16 @@
158162
Refer to the <Link size="lg" href="{base}/styles">Styles page</Link> for a
159163
list of supported styles.
160164
</p>
165+
<p>
166+
CSS StyleSheets can also be externally linked from a Content Delivery
167+
Network (CDN) like <Link size="lg" href="https://unpkg.com/"
168+
>unpkg.com</Link
169+
>. This is best suited for prototyping and not recommended for production
170+
use.
171+
</p>
172+
</Column>
173+
<Column xlg={12}>
174+
<HighlightSvelte code={svelteHeadCdn} class="atomOneDark" />
161175
</Column>
162176
</Row>
163177

demo/routes/styles.svelte

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
StructuredListRow,
88
StructuredListCell,
99
StructuredListBody,
10+
Toggle,
1011
} from "carbon-components-svelte";
1112
import ScopedStyle from "../lib/ScopedStyle.svelte";
1213
import CodeSnippet from "../lib/CodeSnippet.svelte";
@@ -15,6 +16,7 @@
1516
1617
let currentLabel = "Injected styles";
1718
let filtered = [];
19+
let useCdnImport = false;
1820
1921
$: useInjectedStyles = currentLabel === "Injected styles";
2022
</script>
@@ -28,6 +30,23 @@
2830
bind:currentLabel
2931
bind:filtered
3032
>
33+
{#if !useInjectedStyles}
34+
<Row>
35+
<Column xlg={9} lg={12}>
36+
<p class="mb-5">
37+
CSS StyleSheets can also be externally linked from a Content Delivery
38+
Network (CDN). This is best suited for prototyping and not recommended
39+
for production use.
40+
</p>
41+
<Toggle
42+
size="sm"
43+
labelText="Import from UNPKG"
44+
bind:toggled={useCdnImport}
45+
/>
46+
</Column>
47+
</Row>
48+
{/if}
49+
3150
{#if filtered.length > 0}
3251
<Row>
3352
<Column noGutter>
@@ -55,7 +74,7 @@
5574
</div>
5675
</StructuredListCell>
5776
<StructuredListCell>
58-
<ScopedStyle {...style} {useInjectedStyles} />
77+
<ScopedStyle {...style} {useInjectedStyles} {useCdnImport} />
5978
</StructuredListCell>
6079
</StructuredListRow>
6180
{/each}

0 commit comments

Comments
 (0)