Skip to content

Commit b307462

Browse files
committed
feat: code snippets passed as props to Example.vue
1 parent 75dd860 commit b307462

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

docs/.vitepress/components/CodeSnippet.vue

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,58 @@
66
code: String,
77
lang: String,
88
langs: Array,
9+
snippetId: String,
10+
highlight: Array,
911
});
1012
11-
const code = props.code ?? '';
12-
const { lang = 'js', langs = ['javascript'] } = props;
13+
const {
14+
code = '',
15+
lang = 'js',
16+
langs = ['javascript'],
17+
snippetId = '',
18+
highlight = [],
19+
} = props;
1320
1421
onMounted(() => {
1522
setCDN('https://unpkg.com/shiki/');
1623
// @ts-ignore
1724
getHighlighter({ theme: 'material-palenight', langs: langs }).then(
1825
highlighter => {
19-
const snippet = highlighter.codeToHtml(code, { lang: lang });
26+
let lineOptions = [];
27+
highlight.forEach(lineNumber => {
28+
if (typeof lineNumber === 'string') {
29+
const range = lineNumber.match(/\d+/g);
30+
if (range) {
31+
for (let i = +range[0]; i <= +range[1]; i++) {
32+
lineOptions.push({
33+
line: i,
34+
classes: ['highlighted'],
35+
});
36+
}
37+
}
38+
} else if (typeof lineNumber === 'number') {
39+
lineOptions.push({
40+
line: lineNumber,
41+
classes: ['highlighted'],
42+
});
43+
}
44+
});
45+
46+
const snippet = highlighter.codeToHtml(code, {
47+
lang: lang,
48+
lineOptions: lineOptions,
49+
});
50+
2051
// @ts-ignore
21-
document.querySelector('.shiki-code').innerHTML = snippet;
22-
document.querySelector('.code-snippet pre')?.removeAttribute('style');
52+
document.querySelector(`#${snippetId} .shiki-code`).innerHTML = snippet;
53+
document.querySelector(`#${snippetId} pre`)?.removeAttribute('style');
2354
}
2455
);
2556
});
2657
</script>
2758
2859
<template>
29-
<div class="code-snippet language-js">
60+
<div :id="snippetId" class="code-snippet" :class="`language-${lang}`">
3061
<button title="Copy Code" class="copy"></button>
3162
<span class="lang">{{ lang }}</span>
3263
<div class="shiki-code"></div>

docs/.vitepress/components/Example.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import Button from './Button.vue';
44
import Content from './Content.vue';
55
import AnimationForm from './AnimationForm.vue';
6+
import CodeSnippet from './CodeSnippet.vue';
67
78
defineEmits(['resetAnimation']);
89
</script>
@@ -18,6 +19,7 @@
1819
contentList: Array,
1920
animOpts: Object,
2021
fieldsList: Array,
22+
codeSnippet: Array,
2123
},
2224
mounted() {
2325
const jsCssAnimations = this.animationApi();
@@ -67,6 +69,15 @@
6769
/>
6870

6971
<section>
72+
<CodeSnippet
73+
v-if="codeSnippet"
74+
v-for="(snippet, i) in codeSnippet"
75+
:snippet-id="`code-snippet__${animationName}-${i}`"
76+
:code="snippet.code"
77+
:highlight="snippet.highlight"
78+
:langs="snippet.langs"
79+
:lang="snippet.lang"
80+
/>
7081
<slot />
7182
</section>
7283
</Container>

docs/examples/index.md

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ aside: false
116116
function animationApi() {
117117
return jsCssAnimations;
118118
}
119-
120-
const snippet = `
121-
jsCssAnimations.init.slideDown({maintainSpace: true});
122-
`
123-
124119
</script>
125120

126121
# Examples
@@ -134,12 +129,9 @@ aside: false
134129
:content-list="examples.slide.contentList"
135130
:anim-opts="{}"
136131
@reset-animation="(opts) => {resetAnimation('slide', opts);}"
137-
:fields-list="['duration', 'delay', 'staggerDelay', 'maintainSpace', 'easing', 'dimensionsTransition', 'overflowHidden']">
138-
139-
<CodeSnippet :code="snippet" />
140-
141-
<!-- ```js{4}
142-
jsCssAnimations.init.slideUp({
132+
:fields-list="['duration', 'delay', 'staggerDelay', 'maintainSpace', 'easing', 'dimensionsTransition', 'overflowHidden']"
133+
:code-snippet="[{
134+
code: `jsCssAnimations.init.slideUp({
143135
trigger: '.slide-up--btn',
144136
targetSelector: '.example__slide',
145137
on: 'click', // This is the default value, so it can be ommited
@@ -155,10 +147,10 @@ jsCssAnimations.init.slideLeft({
155147
jsCssAnimations.init.slideRight({
156148
trigger: '.slide-right--btn',
157149
targetSelector: '.example__slide',
158-
});
159-
``` -->
160-
161-
</Example>
150+
});`,
151+
highlight: [4]
152+
}]"
153+
/>
162154

163155
<Example
164156
:animation-api="animationApi"
@@ -169,23 +161,23 @@ jsCssAnimations.init.slideRight({
169161
:content-list="examples.fade.contentList"
170162
:anim-opts="fadeOpts"
171163
:fields-list="['duration', 'delay', 'staggerDelay', 'maintainSpace', 'easing', 'blur', 'dimensionsTransition', 'iteration', 'direction']"
172-
@reset-animation="(opts) => {resetAnimation('fade', opts);}">
164+
@reset-animation="(opts) => {resetAnimation('fade', opts);}"
165+
:code-snippet="[{
166+
code: `// When 'trigger' option is omitted, .init will look for
167+
// any element(s) that have the 'js-anim--trigger' class
168+
jsCssAnimations.init.fade({
169+
maintainSpace: true,
170+
});`,
171+
highlight: [4],
172+
}]">
173173

174174
```html{1}
175175
<button class="js-anim--trigger" target-selector=".fade__example">
176-
Fade In/Out
176+
Fade Out
177177
</button>
178178
<div class="fade__example"><p>...</p></div>
179179
```
180180

181-
```js{4}
182-
// When 'trigger' option is omitted, .init will look for
183-
// any element(s) that have the 'js-anim--trigger' class
184-
jsCssAnimations.init.fade({
185-
maintainSpace: true,
186-
});
187-
```
188-
189181
</Example>
190182

191183
<Example
@@ -197,17 +189,16 @@ jsCssAnimations.init.fade({
197189
:content-list="examples.collapse.contentList"
198190
:anim-opts="collapseOpts"
199191
:fields-list="['duration', 'delay', 'staggerDelay', 'maintainSpace', 'easing', 'dimensionsTransition', 'transfOrigin']"
200-
@reset-animation="(opts) => {resetAnimation('collapse', opts);}">
201-
202-
```js{4}
203-
jsCssAnimations.init.collapse({
192+
@reset-animation="(opts) => {resetAnimation('collapse', opts);}"
193+
:code-snippet="[{
194+
code: `jsCssAnimations.init.collapse({
204195
trigger: '.collapse--btn',
205196
targetSelector: '.example__collapse',
206197
staggerDelay: 500, // '0.5s' or '500ms' would also work
207198
});
208-
```
209-
210-
</Example>
199+
`,
200+
highlight: [4]
201+
}]"/>
211202

212203
<style>
213204
</style>

0 commit comments

Comments
 (0)