Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zetsuboii/respond.js

A single page website library, in 200 lines
https://github.com/zetsuboii/respond.js

Last synced: about 1 month ago
JSON representation

A single page website library, in 200 lines

Awesome Lists containing this project

README

        

# Respond

Respond is a single page application library, written in 200 LOC.

## Features
- Reactive DOM: DOM elements update automatically when state changes
- Granular state updates: Only the DOM elements that depend on a state change are updated
- Native HTML APIs: No custom syntax or DSL, just plain JavaScript

## Installation
Just build the project with `yarn build` and copy `respond/index.es.js` into your project.
To run the development server, run `yarn dev`.

## Example: Counter App
```js
import {
getState,
createState,
reactive,
p,
button,
div,
} from "./respond.js";

const counterState = createState({ count: 0 });

const CounterText = p({
innerText: reactive(counterState, ([state]) => state.count),
});

const IncrementButton = button({
innerText: "Increment",
onclick: () => getState(counterState).count++,
});

const Counter = div([CounterText, IncrementButton]);

createApp("body", [Counter]);
```

## Example: Todo App
```js
import {
getState,
createState,
reactiveChildren,
p,
button,
input,
div,
} from "./respond.js";

const todoState = createState({ todos: [] });

const TodoList = div(
reactiveChildren(
todoState,
([state]) => state.todos.map((todo) => p({ innerText: todo }))
)
);

const TodoInput = input({
oninput: (event) => {
const value = event.target.value;
if (value != undefined) {
getState(todoState).todos.push(value);
event.target.value = "";
}
},
});

const TodoApp = div([TodoList, TodoInput]);

createApp("body", [TodoApp]);
```

## Contributing
This is a hobby project, but if you feel like contributing, feel free to open a pull request.