Skip to content

imad-07/mini-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MiniDOM Framework Documentation

MiniDOM is a lightweight JavaScript framework for building modern web applications. It provides a simple yet powerful API for DOM manipulation, state management, routing, and event handling.

Table of Contents

  1. Installation
  2. Core Concepts
  3. API Reference
  4. Examples
  5. Best Practices

Installation

Include the MiniDOM framework in your project:

<script type="module">
  import { createApp } from './src/index.js';
  
  // Your application code here
</script>

Core Concepts

MiniDOM is built around four core concepts:

  1. Virtual DOM: A lightweight representation of the real DOM, making updates efficient.
  2. State Management: A predictable state container for your application.
  3. Routing: Simple URL-based navigation for single-page applications.
  4. Event System: Custom event handling separate from the DOM.

API Reference

DOM Manipulation

Creating Elements

MiniDOM uses a function called h() (hyperscript) to create virtual DOM elements:

import { h } from './src/index.js';

// Create a simple div
const div = h('div', { class: 'container' }, [
  'Hello, World!'
]);

// Create a nested structure
const app = h('div', { class: 'app' }, [
  h('header', {}, [
    h('h1', {}, ['My App'])
  ]),
  h('main', {}, [
    h('p', {}, ['Welcome to my application!'])
  ])
]);

Mounting Elements

To render a virtual DOM tree to the real DOM:

import { mount } from './src/index.js';

const vnode = h('div', {}, ['Hello, World!']);
mount(vnode, document.getElementById('app'));

Adding Attributes

Attributes are specified as an object in the second parameter of h():

h('input', {
  type: 'text',
  placeholder: 'Enter your name',
  value: 'John',
  class: 'input-field',
  style: {
    color: 'blue',
    fontSize: '16px'
  },
  onClick: () => console.log('Clicked!')
});

Nesting Elements

Child elements are passed as an array in the third parameter of h():

h('ul', { class: 'list' }, [
  h('li', {}, ['Item 1']),
  h('li', {}, ['Item 2']),
  h('li', {}, ['Item 3'])
]);

State Management

Creating a Store

The store is the single source of truth for your application state:

import { createStore } from './src/index.js';

const store = createStore({
  todos: [],
  filter: 'all'
});

Getting State

const state = store.getState();
console.log(state.todos);

Updating State

// Update with an object
store.setState({
  filter: 'active'
});

// Update with a function
store.setState(prevState => ({
  todos: [...prevState.todos, { id: 1, text: 'New Todo', completed: false }]
}));

Subscribing to Changes

const unsubscribe = store.subscribe(state => {
  console.log('State changed:', state);
  renderApp();
});

// Later, to unsubscribe
unsubscribe();

Connecting Components to State

store.connect(
  // Selector function
  state => state.todos.filter(todo => todo.completed),
  // Change handler
  completedTodos => {
    console.log('Completed todos changed:', completedTodos);
  }
);

Routing

Creating a Router

import { createRouter } from './src/index.js';

const router = createRouter({
  '/': ()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •