Skip to content

afrocodeur/native-document

Repository files navigation

NativeDocument

License: MIT Build Status Version Bundle Size

A reactive JavaScript framework that preserves native DOM simplicity without sacrificing modern features

NativeDocument combines the familiarity of vanilla JavaScript with the power of modern reactivity. No compilation, no virtual DOM, just pure JavaScript with an intuitive API.

Why NativeDocument?

Instant Start

<script src="https://cdn.jsdelivr.net/gh/afrocodeur/native-document/dist/native-document.min.js"></script>

Familiar API

import { Div, Button } from 'native-document/src/elements';
import { Observable } from 'native-document';

// CDN
// const { Div, Button } = NativeDocument.elements;
// const { Observable } = NativeDocument;

const count = Observable(0);

const App = Div({ class: 'app' }, [
    Div([ 'Count ', count]),
    // OR Div(`Count ${count}`),
    Button('Increment').nd.onClick(() => count.set(count.val() + 1))
]);

document.body.appendChild(App);

Complete Features

  • Native reactivity with observables
  • Global store for state management
  • Built-in conditional rendering
  • Full-featured router (hash, history, memory modes)
  • Advanced debugging system
  • Automatic memory management via FinalizationRegistry

Quick Installation

Option 1: CDN (Instant Start)

<script src="https://cdn.jsdelivr.net/gh/afrocodeur/native-document/dist/native-document.min.js"></script>
<script>
  const { Div } = NativeDocument.elements
  const { Observable } = NativeDocument
  // Your code here
</script>

Option 2: Vite Template (Complete Project)

npx degit afrocodeur/native-document-vite my-app
cd my-app
npm install
npm run start

Option 3: NPM/Yarn

npm install native-document
# or
yarn add native-document

Quick Example

import { Div, Input, Button, ShowIf, ForEach } from 'native-document/src/elements'
import { Observable } from 'native-document'

// CDN
// const { Div, Input, Button, ShowIf, ForEach } = NativeDocument.elements;
// const { Observable } = NativeDocument;

// Reactive state
const todos = Observable.array([])
const newTodo = Observable('')

// Todo Component
const TodoApp = Div({ class: 'todo-app' }, [

    // Input for new todo
    Input({ placeholder: 'Add new task...', value: newTodo }),

    // Add button
    Button('Add Todo').nd.onClick(() => {
        if (newTodo.val().trim()) {
            todos.push({ id: Date.now(), text: newTodo.val(), done: false })
            newTodo.set('')
        }
    }),

    // Todo list
    ForEach(todos, (todo, index) =>
        Div({ class: 'todo-item' }, [
            Input({ type: 'checkbox', checked: todo.done }),
            `${todo.text}`,
            Button('Delete').nd.onClick(() => todos.splice(index.val(), 1))
        ]), /*item key (string | callback) */(item) => item),

    // Empty state
    ShowIf(
        todos.check(list => list.length === 0),
        Div({ class: 'empty' }, 'No todos yet!')
    )
]);

document.body.appendChild(TodoApp)

Core Concepts

Observables

Reactive data that automatically updates the DOM:

import { Div } from 'native-document/src/elements'
import { Observable } from 'native-document'

// CDN
// const { Div  } = NativeDocument.elements;
// const { Observable } = NativeDocument;

const user = Observable({ name: 'John', age: 25 });
const greeting = Observable.computed(() => `Hello ${user.$value.name}!`, [user])
// Or const greeting = Observable.computed(() => `Hello ${user.val().name}!`, [user])

document.body.appendChild(Div(greeting));

// user.name = 'Fausty'; // will not work
// user.$value = { ...user.$value, name: ' Hermes!' }; // will work
// user.set(data => ({ ...data, name: 'Hermes!' })); // will work
user.set({ ...user.val(), name: 'Hermes!' });

Elements

Familiar HTML element creation with reactive bindings:

import { Div, Button } from 'native-document/src/elements'
import { Observable } from 'native-document'

// CDN
// const { Div, Button  } = NativeDocument.elements;
// const { Observable } = NativeDocument;

const App  = function() {
    const isVisible = Observable(true)
    
    return Div([
        Div({
            class: { 'hidden': isVisible.check(v => !v) },
            style: { opacity: isVisible.check(v => v ? 1 : 0.2) }
        }, 'Content'),
        Button('Toggle').nd.onClick(() => isVisible.set(v => !v)),
    ]);
};

document.body.appendChild(App());

Conditional Rendering

Built-in components for dynamic content:

ShowIf(user.check(u => u.isLoggedIn), 
  Div('Welcome back!')
)

Match(theme, {
  'dark': Div({ class: 'dark-mode' }),
  'light': Div({ class: 'light-mode' })
})

Switch(condition, onTrue, onFalse)

When(condition)
    .show(onTrue)
    .otherwise(onFalse)

Documentation

Key Features Deep Dive

Performance Optimized

  • Direct DOM manipulation (no virtual DOM overhead)
  • Automatic batching of updates
  • Lazy evaluation of computed values
  • Efficient list rendering with keyed updates

Developer Experience

// Built-in debugging
Observable.debug.enable()

// Argument validation
const createUser = (function (name, age) {
  // Auto-validates argument types
}).args(ArgTypes.string('name'), ArgTypes.number('age'))

// Error boundaries
const AppWithBoundayError = App.errorBoundary(() => {
    return Div('Error in the Create User component');
})

document.body.appendChild(AppWithBoundayError());

Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

git clone https://github.com/afrocodeur/native-document
cd native-document
npm install
npm run dev

License

MIT © AfroCodeur

❤️ Support the Project

NativeDocument is developed and maintained in my spare time.
If it helps you build better applications, consider supporting its development:

Ko-fi

You can also support the project via crypto donations:

  • USDT (TRC20)
  • USDT (BSC)
  • USDC (Base)
0xCe426776DDb07256aBd58c850dd57041BC85Ea7D

Your support helps me:

  • Maintain and improve NativeDocument
  • Write better documentation and examples
  • Fix bugs and ship new features
  • Produce tutorials and learning content

Thanks for your support! 🙏

Acknowledgments

Thanks to all contributors and the JavaScript community for inspiration.


Ready to build with native simplicity? Get Started →

About

No description, website, or topics provided.

Resources

Contributing

Stars

Watchers

Forks

Packages

No packages published