ReScript-VanJS provides bindings for VanJS, an ultra-lightweight, zero-dependency reactive UI framework. This project enables developers to create web applications using ReScript, a strongly-typed programming language that compiles to JavaScript, in combination with the simplicity and efficiency of VanJS.
- Seamless integration of ReScript with VanJS.
- Type-safe bindings for VanJS functionality.
- Reactive UI components with minimal overhead.
- Easy-to-use API for creating dynamic web applications.
First, create a new ReScript application using one of the following commands:
npm create rescript-app@latest
pnpm create rescript-app
bun create rescript-app
For more information on setting up a ReScript project, refer to the official ReScript documentation.
Add the required dependencies to your project:
npm install vanjs-core rescript-vanjs
pnpm add vanjs-core rescript-vanjs
bun add vanjs-core rescript-vanjs
In your rescript.json
file, add the following dependency:
{
"bs-dependencies": ["rescript-vanjs"]
}
Here's a simple example of how to use ReScript-VanJS to create a reactive UI component:
- Create a file named
Main.res
in yoursrc
folder. - Add the following code to
Main.res
:
@val @scope("document") @return(nullable)
external getElementById: string => option<Dom.element> = "getElementById"
let root = switch getElementById("root") {
| Some(el) => el
| None => Exn.raiseError("Root element not found")
}
@get external getEventTarget: Dom.event => Dom.eventTarget = "target"
@get external getInputValue: Dom.eventTarget => string = "value"
let deriveState: unit => Dom.element = () => {
let vanText = Van.state("VanJs")
let length = Van.derive(() => vanText.val->String.length)
open Van.Child
Van.Dom.createElement("span")
->Van.Dom.addChildren([
"The length of the text is: "->toText,
Van.Dom.createElement("input")
->Van.Dom.setAttrs({
"type": "text",
"value": vanText,
"oninput": (event: Dom.event) => vanText.val = event->getEventTarget->getInputValue,
})
->Van.Dom.build
->toDom,
length->toState,
])
->Van.Dom.build
}
Van.add(root, deriveState())->ignore
This example creates a reactive input field that displays the length of its content in real-time.
Follow these steps to build and run your rescript-vanjs application:
-
Start the ReScript development server:
npm run res:dev
-
If there are no errors, build the JavaScript files:
npm run res:build
-
Build the JavaScript bundle for browser use. For example, using Bun (you can use any other JavaScript bundler):
bun build ./src/Main.res.mjs --outdir ./out --format esm
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.