Skip to content

Commit c2565cb

Browse files
committed
feat: add vanta.js demo & support *.vue highlighting
1 parent 90e1198 commit c2565cb

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

public/code/index.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- vanta.jsx
12
- vue-sfc.vue
23
- moon-star-single-div.js
34
- vue-3d-model.js

public/code/vanta.jsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
await loadJs('https://unpkg.com/[email protected]')
2+
3+
appendCss(`
4+
header { background-color: silver }
5+
header canvas { display: block }
6+
main { padding: 20px }
7+
main h2 a { font-size: 60%; }
8+
`)
9+
10+
let themes = [
11+
'birds', 'cells', 'clouds', 'clouds2', 'dots', 'fog', 'globe',
12+
'halo', 'net', 'rings', 'ripple', 'topology', 'trunk', 'waves',
13+
]
14+
15+
// https://github.com/tengbao/vanta
16+
// https://vantajs.com
17+
let App = () => {
18+
let [theme, setTheme] = useState('birds')
19+
return (
20+
<div>
21+
<VantaHeader theme={theme} />
22+
<main>
23+
<h2>Vanta.js - <a target="_blank" href="https://github.com/tengbao/vanta">https://github.com/tengbao/vanta</a></h2>
24+
<ul>{themes.map(t => {
25+
return <li key={t}>{t === theme ? t : <a onClick={() => {
26+
setTheme(t)
27+
}}>{t}</a>}</li>
28+
})}</ul>
29+
</main>
30+
</div>
31+
)
32+
}
33+
34+
let VantaHeader = ({ theme }) => {
35+
let headerRef = useRef(null)
36+
37+
useEffect(() => {
38+
let el = headerRef.current
39+
let destroy = loadVantaEffect(el, theme)
40+
return destroy
41+
}, [theme])
42+
43+
return <header ref={headerRef}></header>
44+
}
45+
46+
let loadVantaEffect = (el, theme) => {
47+
let promise = (async () => {
48+
setRendering(true)
49+
if (['halo', 'topology'].includes(theme)) {
50+
await loadJs('https://unpkg.com/[email protected]/vendor/p5.min.js')
51+
}
52+
let method = theme.toUpperCase()
53+
if (!(window.VANTA && VANTA[method])) {
54+
await loadJs(`https://unpkg.com/[email protected]/dist/vanta.${theme}.min.js`)
55+
}
56+
let ret = VANTA[method]({ el })
57+
let canvasHeight = el.querySelector('canvas').clientHeight
58+
el.style.height = `${canvasHeight}px`
59+
setRendering(false)
60+
return ret
61+
})()
62+
return () => {
63+
promise.then(ret => { ret.destroy() })
64+
}
65+
}

public/code/vanta.jsx.png

129 KB
Loading

src/playground/Playground.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'codemirror/lib/codemirror.css';
44
// import 'codemirror/mode/javascript/javascript';
55
import 'codemirror/mode/jsx/jsx';
66
import 'codemirror/mode/python/python';
7+
import 'codemirror/mode/vue/vue';
78
import 'codemirror/theme/material.css';
89
import immer from 'immer';
910
import _ from 'lodash';
@@ -62,6 +63,7 @@ Object.assign(window, {
6263
export let Playground: React.FC = () => {
6364
let history = useHistory();
6465
let { file } = useParams() as { file: string };
66+
let isVue = file.endsWith('.vue');
6567
let isPy = file.endsWith('.py');
6668

6769
// let initialCode = useMemo(() => {
@@ -225,7 +227,7 @@ export let Playground: React.FC = () => {
225227
<CodeMirror
226228
className="main-editor"
227229
options={{
228-
mode: isPy ? 'python' : 'text/typescript-jsx',
230+
mode: isVue ? 'vue' : isPy ? 'python' : 'text/typescript-jsx',
229231
theme: 'material',
230232
lineNumbers: true,
231233
}}

0 commit comments

Comments
 (0)