Bram
Bram is a 3k web components library with everything you need to build reactive user interfaces. Bram embraces ES2015, the <template>
element, and Proxys.
Table of Contents
Example
Click counter Click me Clicks: {{count}}
Install
Using npm:
npm install bram --save
Using bower:
bower install bram --save
Or grab one of our releases.
Then add the scripts to your page at the end of the <body>
tag.
API
Bram.Element
The primary base class for extending elements. Deriving your classes from Bram.Element gives you templating, observable models and more.
Element
If using the ES6 build you can do either of these:
; Element
or
;
Bram
Additionally Bram is a function that takes an element and returns an extended version. This can be used to extend elements other than HTMLElement:
HTMLButtonElement
template
The static getter template is used if you want to render a template to your element. You can return either a selector like:
Element static { return '#my-template'; }
Or the template element itself:
const myTemplate = document; Element static { return myTemplate; }
renderMode
By default Bram renders to the element's shadowRoot
, but you can change this if you don't want to use Shadow DOM. You might do this if you don't want to add the Shadow DOM polyfill, or your element depends on global styles.
static { return 'light'; }
Valid options are:
- shadow (the default) renders to the Shadow DOM. You never need to add this unless you just want to be explicit.
- light renders to the element itself (the template becomes a child).
events
Specify custom events that your element emits. Bram will set up onevent
properties for each of these events, as is common with most built in elements. This also will make your component compatible with React, see this thread.
Element static { return 'namechanged' } ... { thisname = newName; this; } customElements; let form = ;form{ // This is called when the user's name changes console;};
childrenConnectedCallback
The childrenConnectedCallback is a callback on the element's prototype. Use this to be notified when your element has received children. This allows you to write more resilent custom elements that take into account the dynamic nature of HTML in the case where you have special behavior depending on children.
Element { this; } { // Perform some kind of sorting operation var childNodes = thischildNodes; } customElements;
Templating
One of Bram's biggest advantages is its declarative template syntax with automatic binding.
Each Bram.Element
contains an object called this.model
which drives the template. Any changes to this.model
, whether they change a property, add a new property, remove a property, or reorder an Array, will result in the template being updated to reflect those changes.
Bram's templates support conditionals and loops, and allow declarative binding on properties, attributes, text, and events.
Conditionals
To conditionally render, use an if attribute on an inner template.
User {{name}} Admin section
Any time isAdmin
changes value, the template will either be removed or readded.
Element static { return '#user-template'; } { // Not an admin by default thismodelisAdmin = false; } { thismodelisAdmin = !!val; } customElements; let page = ;documentbody; pageisAdmin = true; // Admin section is shown. pageisAdmin = false; // Admin section is removed.
Looping over arrays
To loop over an array use an inner template with the each attribute. Like so:
Volleyball players {{name}}
Rendered with this data:
Element static { return '#player-template'; } { thismodelplayers = name: 'Matthew' name: 'Anne' name: 'Wilbur' ; } customElements;
Will show all three players as separate <li>
elements.
Properties
You can set properties on an element using the special colon character like :foo
on attributes. This allows you to pass non-string data to elements.
Foo!
Element static { return '#foo-template'; } { thismodelfoo = 'bar'; } customElements;
Will render the <div>
and set its foo
property to the string "bar"
.
Events
Events can be assigned to an element using the on-
notation on attributes. This example handle a form being submitted:
This will call the handleSubmit
method on the user-form element:
Element static { return '#user-form'; } { ev; // User fetch() or something instead } customElements;
Bram.template
Creates a function that is used to hydrate (render) a template using a set of data.
let hydrate = Bram;let link = ; documentbody;
Hydrating creates a link object which holds the data-bindings and the rendered DocumentFragment (the .tree
property).