domify-js v1.0.6
DomifyJS
DomifyJS is a lightweight, flexible frontend framework built from scratch in JavaScript to understand how modern frontend frameworks work under the hood. It allows the developers to describe their UI and let the framework handle the DOM operations Instead of manually creating and updating DOM elements.
Table of Contents
- Purpose
- Installation
- How It Works
- How to Use DomifyJS
- Usage Example
- Vanilla JS vs DomifyJS
- Benefits Over Vanilla JS
Purpose
The main purpose is to abstract away direct DOM manipulation and provide a more structured way to build web applications. Instead of manually creating and updating DOM elements, developers can describe their UI and let the framework handle the DOM operations.
Installation
npm install domify-js
How It Works
The framework works through three main systems:
- Virtual DOM System
- State Management
- DOM Updates
How to Use DomifyJS
API Reference
createElement(tag, props, children)
Creates a virtual DOM element.
Parameters:
tag
(string): HTML tag name ('div', 'p', 'button', etc.)props
(object): Properties and event handlerschildren
(array): Child elements or text content
createApp({ state, reducers, view })
Creates and initializes the application.
Parameters:
state
(object): Initial application statereducers
(object): State update functionsview
(function): Main view function that returns virtual DOM
Usage Example
// Counter App
const state = { count: 0 };
const reducers = {
increment: (state) => ({
...state,
count: state.count + 1,
}),
decrement: (state) => ({
...state,
count: state.count - 1,
}),
};
function App(state, emit) {
return createElement("div", {}, [
createElement("h1", {}, [`Count: ${state.count}`]),
createElement(
"button",
{
on: { click: () => emit("decrement") },
},
["-"]
),
createElement(
"button",
{
on: { click: () => emit("increment") },
},
["+"]
),
]);
}
createApp({ state, reducers, view: App }).mount(document.getElementById("app"));
Vanilla JS vs DomifyJS
Vanilla JavaScript:
// Create elements
const div = document.createElement("div");
const button = document.createElement("button");
button.textContent = "Click me";
div.appendChild(button);
// Add event listener
button.addEventListener("click", () => {
const count = parseInt(div.dataset.count || "0");
div.dataset.count = count + 1;
updateUI();
});
// Update UI
function updateUI() {
div.textContent = `Count: ${div.dataset.count}`;
div.appendChild(button);
}
DomifyJS:
// Define state
const state = { count: 0 };
// Define reducers
const reducers = {
increment: (state) => ({
...state,
count: state.count + 1,
}),
};
// Create view
function App(state, emit) {
return createElement("div", {}, [
createElement("h1", {}, [`Count: ${state.count}`]),
createElement(
"button",
{
on: { click: () => emit("increment") },
},
["Click me"]
),
]);
}
// Create app
createApp({ state, reducers, view: App }).mount(document.getElementById("app"));
Benefits Over Vanilla JS
- No Direct DOM Manipulation
- Framework handles DOM updates
- Developers focus on describing UI
- Structured State Management
- Centralized state
- Predictable updates
- Clear data flow
- Declarative Code
- Describe what you want
- Framework handles how to do it
- Event Handling
- Simplified event system
- Automatic cleanup
Current Limitations
- Performance
- Full DOM rebuild on every state change
- Will be optimized in future versions
- Basic Features
- Simple state management
- No component system yet
- No lifecycle methods
Framework Flow
Initial Setup:
- Create virtual DOM structure
- Set up state management
- Initialize event system
When State Changes:
- Reducer creates new state
- Framework rebuilds virtual DOM
- Updates real DOM
Event Handling:
- User interaction triggers event
- Event dispatches action
- Reducer updates state
- View updates automatically
This framework is designed for learning purposes and demonstrates fundamental concepts used in modern frontend frameworks like React, Vue, and others.