kanciljs
Version:
π Kancil.JS β A fast, lightweight, and clever frontend micro-library
163 lines (114 loc) β’ 3.83 kB
Markdown
# KancilJS πβ‘
<p align="center">
<a href="LICENSE">
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License">
</a>
<a href="https://github.com/pinoruswana/kanciljs">
<img src="https://img.shields.io/badge/github-pinoruswana/kanciljs-blue?logo=github" alt="GitHub Repo">
</a>
<a href="https://github.com/pinoruswana/kanciljs/releases">
<img src="https://img.shields.io/badge/version-0.1.0-orange" alt="Version">
</a>
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript">
<img src="https://img.shields.io/badge/made%20with-JavaScript-blue?logo=javascript" alt="JS Library">
</a>
<a href="https://bundlephobia.com/package/kanciljs">
<img src="https://img.shields.io/bundlephobia/min/kanciljs?label=min+size" alt="Bundle Size">
</a>
</p>
**A fast and lightweight frontend library for reactive components without the complexity.**
> "KancilJS is a minimal solution to build reactive UI without heavyweight frameworks."
## β¨ Features
- π **Reactive State Binding** using `{{variable}}` directly in HTML
- π§© **Simple Component** architecture (no class inheritance needed)
- π±οΈ **Event Binding** with HTML attributes like `@click`, `@input`, `@change`, `@keyup`, etc.
- πΎ **Automatic localStorage persistence** using `storeKey`
- π **Auto re-render** on state updates
- π₯ **Composable and lightweight**, ideal for small to medium projects
## π¦ Installation
### From GitHub (not yet on npm)
```bash
npm install pinoruswana/kanciljs
```
### Import it in your browser:
```js
import { KancilComponent } from './node_modules/kanciljs/src/kancil.js';
```
## π Usage Example
### 1. Initialize Component
```html
<div id="app">
<h1>Hello {{name}}</h1>
<input type="text" @input="name" />
<button @click="count++">Click {{count}}</button>
</div>
<script type="module">
import { KancilComponent } from './node_modules/kanciljs/src/kancil.js';
new KancilComponent({
target: '#app',
state: {
name: 'Kancil Boss',
count: 0,
},
});
</script>
```
### 2. Enable Persistence with `storeKey`
```js
new KancilComponent({
target: '#app',
state: {
name: '',
count: 0,
},
storeKey: 'kancil-store',
});
```
> State will be automatically saved and loaded from localStorage.
## π§ Reactive State
- Bind values in HTML using `{{variable}}`
- Inputs are auto-bound via `@input`
- State updates (e.g. `state.count++`) will trigger re-render
## β‘ Event Binding
Use `@event="expression"` directly in HTML:
| Attribute | Description |
| --------- | ------------------------------------- |
| `@click` | When clicked |
| `@input` | On input change |
| `@change` | On value change (select, radio, etc.) |
| `@keyup` | On key release (supports filters) |
Example:
```html
<input @keyup="name = $el.value" />
```
`$el` refers to the element that triggered the event.
## π Cursor and Focus Handling
KancilJS automatically restores the cursor position and element focus after re-renders during input.
## πΎ Auto localStorage Integration
When `storeKey` is defined, state will persist using `localStorage`. No manual handling required.
## π§ͺ Extra Tips
- Use `$el` to access the event target element inside expressions
- Example: `@input="username = $el.value"`
## π Project Structure
```
project/
ββ index.html
ββ node_modules/
β ββ kanciljs/
β ββ src/kancil.js
ββ main.js
```
## π License
MIT Β© 2025 [Pino Ruswana](https://github.com/pinoruswana)
> βKancil is not just clever β it's agile, minimal, and perfect for fast and modern development.β