From ac484b223d0b9cf43d00e5f1d102719813d619df Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Fri, 22 Dec 2023 18:43:47 +0100 Subject: [PATCH] Re-structure readme --- readme.md | 78 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/readme.md b/readme.md index acff9f3..b8e7e52 100644 --- a/readme.md +++ b/readme.md @@ -10,52 +10,60 @@ templating. ## Overview ```js +const text = new State({value: "Skooma is cool"}) +setTimeout(() => {text.value = "Skooma is awesome!"}, 1e5) + document.body.append(html.div( html.h1("Hello, World!"), - html.p("Skooma is cool", {class: "amazing"}), - html.button("Show Proof", click: event => { alert("It's true!") }) + html.p(text, {class: "amazing"}), + html.button("Show Proof", {click: event => { alert("It's true!") }}) )) ``` ## Interface / Examples -### HTML generation +### Basic DOM generation + +Accessing the `html` proxy with any string key returns a new node generator +function: ```js -html.div() -// Creates a
element -html.div("hello", "world") -//
helloworld
-html.div(html.span()) -//
-html.div([html.span(), html.span()]) -//
-html.div({class: "foo"}) -//
-html.div({class: ["foo", "bar"]}) -//
-html.div({click: 1}) -//
-html.div({click: event => console.log(event.target)}) -// Creates a
with an event listener for "click" events -html.div(player: {username: "KhajiitSlayer3564"}) -// Creates a
with the attribute "player" set to a JSON-encoded Object -html.div("Old content", self => self.innerText = "Hello, World!") -// Creates a
and passes it to a function for further processing -html.div({foo: true}) -//
-html.div({foo: "bar"}, {foo: false}) -//
- -// Special keys: - -html.div({dataset: {foo: 1, bar: 2}}) -//
- -html.div({shadowRoot: html.span("Shadow root content")}) -// Attaches a shadow root with a span +html.div("Hello, World!") ``` +Attributes can be set by passing objects to the generator: + +```js +html.div("Big Text", {style: "font-size: 1.4em"}) +``` + +Complex structures can easily achieved by nesting generator functions: + +```js +html.div( + html.p( + html.b("Bold Text") + ) +) +``` + +For convenience, arrays assigned as attributes will be joined with spaces: + +```js +html.a({class: ["button", "important"]}) +``` + +Assigning a function as an attribute will instead attach it as an event +listener: + +```js +html.button("Click me!", {click: event => { + alert("You clicked the button.") +}}) +``` + + + Generators can be called with many arguments. Arrays get iterated recursively as if they were part of a flat argument list.