HTML driven reactivity and glue for customElements
npm install @timberframejs/timberframejs html
tf-trigger='click'
tf-target="main"
tf-swap='innerHTML'
tf-ced='post hello-msg'
name='msg'
value='Hello World'>Show Message
// main before button click
// main after button click
Hello World
`
Example interacting with the server fist then rendering the described component.
` html
tf-trigger='click'
tf-server='post ./api/translate/german'
tf-target="main"
tf-swap='innerHTML'
tf-ced='post hello-msg'
name='msg'
value='Hello World'>Show Message
// main before button click
// main after button click
Hallo Welt
`
Install
`npm i timberframejs/timberframejs`
Project Technical Summary
TimberFrameJs is an attribute-based 'reactive' (really re-rendering) framework for web components. Inspired by HTMX.
TimberFrameJs renders HTML on the client using the ES6 customElement specification. HTMX renders html on the server.
CED Component Element Description
CED explained
MDN CreateElement for web components
` html
click
`
Translates to
` js
const helloWorldCED = {
tagName:'div',
attributes:{
is:'hello-world',
msg:'nice to meet you'
}
}
createElement(helloWorldCED)
`
Which is equivalent to
` js
const ele = document.createElement('div', {is: 'hello-world'} )
ele.setAttribute('msg','nice to meet you')
ele.setAttribute('is', 'hello-world')
// before render
// rendered
nice to meet you
`
customElement
` js
export class HelloWorld extends HTMLElement{
connectedCallback(){
this.textContent = this.getAttribute('msg')
}
}
`
Example Click Counter
` js
// simple input and button. Clicking the button updates the input value.
// the rendered live html
tf-trigger="click"
tf-ced="click-counter"
tf-include="closest click-counter"
// tf-server="post ./api/click" // to post the data to a server, then send the results to the component
tf-target="post closest click-counter">add one
`
Click counter custom element
html is a simple template literal sanitization library that returns a document fragment
` js
import { html } from '../../src/utils/index.js'
export const CLICK_COUNTER = 'click-counter'
export class ClickCounter extends HTMLElement {
body: FormData;
connectedCallback() {
const previousCount = Number(this?.body?.get('counter') ?? 0)
const frag = html
tf-trigger='click'
tf-ced='${CLICK_COUNTER}'
tf-include='closest ${CLICK_COUNTER}'
tf-target='post closest ${CLICK_COUNTER}'>click me
this.append(frag)
}
}
``
Details
* primarily a mutation observer that creates events.
* form binding for all elements, not just forms
* Can make api calls
* no virtual DOM
* leverages custom elements
* the goal is to leverage web standards as much as possible
Values
* simplicity
* stability - always strive for backward compatibility - we reserve the right to make breaking changes in cases of security issues.
* developer experience - balance helpful feedback and runtime performance.
* admire PEP 20 especially the preferrably one way to do it.
* select few configurable defaults
* default attributes will be added at runtime for visibility. (a small relaxing of explicit over implicit to balance against developer experience.)
* plug-in architecture
* attempt to discover and throw potential attribute errors before the event is triggered.
* testing - unit and integration
* low/no intrusion into custom elements. Custom elements only have to know about their data model.
* New features must have a valid use case.
Common Workflows
$3
- event happens
- create element
- place element on page
$3
- click
- create new element
- place element on page
$3
- click
- include data from page
- create new element
- place element on page with new data passed to it.
$3
- click
- include data from page
- send data to server
- create new element
- place element on page with both page and server data passed to it
$3
- input or change element
- re-render changed component
$3
- input or change element
- render parent component
$3
- show spinner
- fetch data
- create custom element and add to DOM
- hide spinner