☰ Getting Started

VanJS: Getting Started

To get started, add the line below to your script:

import van from "https://cdn.jsdelivr.net/gh/vanjs-org/van/public/van-1.5.0.min.js"

To code without ES6 modules, add the following line to your HTML file instead:

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/vanjs-org/van/public/van-1.5.0.nomodule.min.js"></script>

Alternative, you can download the files (van-1.5.0.min.js, van-1.5.0.nomodule.min.js) and serve them locally.

NPM Integration


It's also possible to integrate with VanJS via NPM, making it handy to build web applications with tools like Vite or Parcel. You can also build your own NPM packages that depend on VanJS. To get started with VanJS via NPM, run:

npm install vanjs-core

To use the VanJS NPM package, add this line to your script:

import van from "vanjs-core"

or this line if you want to import the debug version of VanJS:

import van from "vanjs-core/debug"

You can check out the Hello World app built with VanJS NPM + Vite (source code).

TypeScript Support for Non-NPM Integration


For ESM Modules

To get TypeScript support for your ESM modules, download the corresponding .d.ts file from the Download Table and store it alongside the .js source file, and then import the .js file as normal:

import van from "./van-1.5.0.min.js"

For Script Tag

To get TypeScript support for code that would be imported via a <script> tag, download a .d.ts file from the Download Table (any file from the table would work), and then add the following code at the top of your .ts file:

import type { Van } from "./van-1.5.0.d.ts"

declare const van: Van

Test It Out


The following code will produce a funnier Hello component:

const {button, div, pre} = van.tags

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

const Run = ({sleepMs}) => {
  const steps = van.state(0)
  ;(async () => { for (; steps.val < 40; ++steps.val) await sleep(sleepMs) })()
  return pre(() => `${" ".repeat(40 - steps.val)}πŸšπŸ’¨Hello VanJS!${"_".repeat(steps.val)}`)
}

const Hello = () => {
  const dom = div()
  return div(
    dom,
    button({onclick: () => van.add(dom, Run({sleepMs: 2000}))}, "Hello 🐌"),
    button({onclick: () => van.add(dom, Run({sleepMs: 500}))}, "Hello 🐒"),
    button({onclick: () => van.add(dom, Run({sleepMs: 100}))}, "Hello πŸšΆβ€β™‚οΈ"),
    button({onclick: () => van.add(dom, Run({sleepMs: 10}))}, "Hello 🏎️"),
    button({onclick: () => van.add(dom, Run({sleepMs: 2}))}, "Hello πŸš€"),
  )
}

van.add(document.body, Hello())

Demo:

Try on jsfiddle

Download Table