VanJS Documentation
repository·main·Indexed 26 days ago
https://github.com/vanjs-org/vanAn ultra-lightweight (1.0kB gzipped), zero-dependency reactive UI framework based on vanilla JavaScript and the DOM. It avoids virtual DOM and JSX by default, though it provides optional addons including vanjs-jsx for JSX support, vanjs-element for Web Components, vanjs-prerender for static HTML generation, and van_dml for auto-appending elements and dynamic CSS. The ecosystem also includes VanUI for pre-built components like Modal and Await.
What's inside VanJS
- VanX is the official 1.2kB extension for VanJS. It provides a collection of utility functions designed to improve developer ergonomics and bring the VanJS developer experience closer to other popular UI frameworks.
Configure TypeScript support for Script Tag integration
mainIf you are using VanUI via a
<script>tag but want TypeScript type definitions, download thevan-ui.d.tsfile and declare the components at the top of your.tsfile to enable autocompletion and type checking.import type { Modal as ModalType } from "./van-ui.d.ts" declare const Modal: typeof ModalTypeUpdate index.html for JSX entry point
mainWhen switching to a
.tsxentry point, ensure yourindex.htmlscript tag points to the correct file path (e.g.,/src/main.tsx).<body id="app"> <div id="app"></div> <!-- change script src --> <script type="module" src="/src/main.tsx"></script> </body>Install vanjs-jsx and vanjs-core
mainTo use JSX with VanJS, you need to install bothvanjs-jsxandvanjs-core. You can create a new project using Vite with a vanilla TypeScript template and then add the dependencies.Create Web Components with vanjs-element
mainUse
vanjs-elementto wrap VanJS components into standard Web Components. This allows you to define custom HTML tags that encapsulate VanJS logic, styles, and slots.Key benefits include:
- Automatic hydration of VanJS inside your HTML.
- Reusable components with minimal boilerplate.
- Isolated styles and support for
<slot>elements via the Web Components API. - Extremely lightweight (~300b min+gzip).
import van from "vanjs-core"; import { define } from "vanjs-element"; const { button, div, slot } = van.tags; define("custom-counter", () => { const counter = van.state(0); return div( slot(), counter, button({ onclick: () => ++counter.val }, "+"), button({ onclick: () => --counter.val }, "-") ); });Use the HTML and MD to VanJS Code Converter
mainThe tool used to convert HTML and Markdown into VanJS code is no longer part of this repository. It has been migrated to a standalone repository.
To use the converter, visit the dedicated repository: https://github.com/vanjs-org/converter.
Use TypeScript with VanJS
mainVanJS provides first-class TypeScript support via.d.tsfiles. To enable type-checking and IntelliSense, download the appropriate.d.tsfile corresponding to your version from the official VanJS start page.Mount a JSX component in main.tsx
mainTo render your JSX component, rename your entry point from
main.tstomain.tsx, importvanfromvanjs-core, and usevan.add()to mount the component to a DOM element.import van from "vanjs-core"; import Hello from "./Hello"; van.add(document.getElementById("app")!, <Hello name={"your name"} />);Use auto-append with begin() and end()
mainThe
begin(ID)function selects a base element (either a DOM element or an HTML ID string). All elements created afterbegin()are automatically appended as children to this base. Useend()to stop appending and return to the previous base.begin()andend()can be nested.begin(ID)is transparent and returns the DOM reference of the first child, allowing you to chain property assignments.Install VanUI via Script Tag
mainYou can include VanUI in your project using a CDN script tag.
Important: For VanUI to work properly, you must also import VanJS via a
<script type="text/javascript">tag in your HTML.Install van_dml
mainTo use
van_dml, include thevan_dml.jsscript immediately after the VanJS script. You can load them locally or via a CDN.<!-- Local installation --> <script src="van-latest.nomodule-min.js"></script> <script src="van_dml.js"></script> <!-- CDN installation --> <script src="https://cdn.jsdelivr.net/gh/vanjs-org/van/public/van-latest.nomodule.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/vanjs-org/van/addons/van_dml/src/van_dml.js"></script>Install and setup van_dml.js
mainTo use the
van_dmladdon, include the core VanJS library followed by thevan_dml.jsscript. The addon extends thevanobject with new functions likebegin,end,base,sp, andcss.<script src="https://cdn.jsdelivr.net/gh/vanjs-org/van/public/van-latest.nomodule.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/vanjs-org/van/addons/van_dml/src/van_dml.js"></script> <script> const { h1, h2, div, p, button } = van.tags; const { begin, end, base, sp, css } = van; // new functions of van_dml </script>