update api better to match existing standards

This commit is contained in:
Geoff Doty 2024-05-04 20:47:34 -04:00
parent e79e241236
commit 72fc4f3be7
3 changed files with 22 additions and 23 deletions

View File

@ -15,16 +15,16 @@ This is an experimental composable ui library that takes ideas from Elm Architec
the `app` builder takes an `opts` object that expects it to include: the `app` builder takes an `opts` object that expects it to include:
- `data` as initial data `{object}` - `state` as initial data `{object}`
- `methods` as `{object}` with functions definitions - `actions` as `{object}` with functions definitions
- `view` as `{function}` that returns valid dom - `view` as `{function}` that returns valid dom
and a querySelector compatible `selector` to mount the ui. and a querySelector compatible `selector` to mount the ui.
`app` returns: `app` returns:
- a object that returns a `state` object that you can use to update in other components - `state` current of component as updatable function
- `methods` you can call outside the app component - `actions` to call on component
### Example ### Example
@ -33,8 +33,8 @@ and a querySelector compatible `selector` to mount the ui.
import {app, h} from "./tagged.js"; import {app, h} from "./tagged.js";
const myapp = app({ const myapp = app({
data: {name: "[Your Name Here]", job: "Developer"}, state: {name: "[Your Name Here]", job: "Developer"},
view(state, methods) { view(state, actions) {
return h("main", [ return h("main", [
h("strong", `Greeting from ${state.name}`), h("strong", `Greeting from ${state.name}`),
h("div", `Your local ${state.job}`), h("div", `Your local ${state.job}`),
@ -59,7 +59,6 @@ and a querySelector compatible `selector` to mount the ui.
## Todo ## Todo
- Methods - Actions Example
- Update API: state, actions
> WORK-IN-PROGRESS > WORK-IN-PROGRESS

View File

@ -2,7 +2,7 @@ export default function app(opts, selector = "body") {
// initial setup // initial setup
let data = {}; let data = {};
let view = () => null; let view = () => null;
let methods = {}; let actions = {};
// query helper // query helper
const $ = document.querySelector.bind(document); const $ = document.querySelector.bind(document);
@ -14,39 +14,39 @@ export default function app(opts, selector = "body") {
} }
// update ui // update ui
render(); update();
// return current state // return current state
return data; return data;
} }
const render = () => { const update = () => {
$(selector).replaceChildren(view(data, methods)); $(selector).replaceChildren(view(data, actions));
} }
// setup view // setup view function
if (opts.view && typeof opts.view === "function") { if (opts.view && typeof opts.view === "function") {
view = opts?.view; view = opts?.view;
} }
// setup data // setup data object
if (opts.data && typeof opts.data === "object") { if (opts.state && typeof opts.state === "object") {
// wrap data in state object // wrap data in state object
data = state(opts.data); data = state(opts.state);
} }
// setup methods // setup actions object
if (opts.methods && typeof opts.methods === "object") { if (opts.actions && typeof opts.actions === "object") {
methods = opts.methods; actions = opts.actions;
} }
// mount view // mount view
if (opts.view && selector) { if (opts.view && selector) {
render(); update();
} }
return { return {
state, state,
methods, actions,
}; };
} }

View File

@ -14,8 +14,8 @@
import {app, h} from "../src/index.js"; import {app, h} from "../src/index.js";
const myapp = app({ const myapp = app({
data: {name: "[Your Name Here]", job: "Developer"}, state: {name: "[Your Name Here]", job: "Developer"},
view(state, methods) { view(state, actions) {
return h("main", [ return h("main", [
h("strong", `Greeting from ${state.name}`), h("strong", `Greeting from ${state.name}`),
h("div", `Your local ${state.job}`), h("div", `Your local ${state.job}`),