Skip to content

Build Elements / mixin

mixin ​

js
import { Headless, Height, Hidden, Identity, Template, Value, Width } from '@t2e1/kuba/mixin'

Reusable attribute behavior. Each mixin takes a class and returns a subclass adding one property backed by one attribute — composed in a chain:

js
class Stack extends Hidden(Width(Height(Echo(HTMLElement)))) {}

Order matters only in that each wraps the previous; the properties don't interact.

Hidden(Base) ​

Adds hidden, reflected from the hidden attribute, and mirrors it onto internals.states as a hidden custom state.

css
:host(:state(hidden)) { display: none; }

The mixin sets the state; the element's own stylesheet decides what that means. Every kuba element using it applies display: none, but the mixin doesn't impose it.

Setting hidden = false also removes the attribute, so the DOM stays consistent with the property.

MemberTypeDefault
hiddenbooleanfalse

Requires the host to expose internals (an ElementInternals) through a lazy getter.

Identity(Base) ​

Gives the element an identity in the accessibility tree: the role it plays, and — for elements with no readable text of their own — the name it answers to. Without it a custom element is an anonymous box: <kb-progress value="40"> announces as nothing at all.

Unlike the other mixins, the role is not an attribute. The element declares it by implementing [role], which the mixin reads on connect:

js
import { Identity, role } from '@t2e1/kuba/mixin'

class Progress extends Identity(Echo(HTMLElement)) {
  get [role]() {
    return 'progressbar'
  }
}

role is a Symbol.for, so the element declaring it and the mixin reading it agree on the same key even when bundled separately.

The name comes from the alt attribute, for elements whose content is a glyph rather than text:

html
<kb-button variant="icon" alt="Delete">
  <kb-icon use="delete"></kb-icon>
</kb-button>

Use alt only where there is no visible text. When the element has a label the reader can see, use <kb-label> — setting both makes the announced name differ from the written one, which breaks voice control.

MemberTypeDefault
altstring''
[role]string— (the element declares it)

Both are published as default semantics on ElementInternals, so a role or aria-label written in the markup still wins — the element ships correct defaults without taking the decision away from the consumer.

Requires the host to expose internals, as Hidden does. The mixin never calls attachInternals() itself: it may be called only once per element, so the element owns that call.

Headless(Base) ​

Hides the element unconditionally, by setting display: none on it once connected. For elements that hold or fetch data and render nothing — <kb-fetch>, <kb-dataset>, <kb-filter>, <kb-find>, <kb-headers>, <kb-on>, <kb-redirect>.

It adds no attribute and no property. There is nothing to configure: an element using it is invisible by construction.

Width(Base) and Height(Base) ​

Add width and height, reflected from the matching attributes and applied directly to the host. Both use @retouch, so a size change replays only the stylesheet, not the markup.

MemberTypeDefault
widthstring'auto'
heightstring'auto'

Values pass through a resizing filter: numeric px/% values are used as-is, and the keywords fill and hug normalize to 100% and auto.

Value(Base) ​

Adds value, kept in sync with the value attribute. No default, no transformation — the plainest of the set, for elements whose payload is a single string.

MemberTypeDefault
valuestring | undefinedundefined

Template(Base) ​

Adds template, resolving to the markup of a <template> — either the element's own child, or one referenced by id through the template attribute.

html
<kb-render>
  <template>{name}</template>
</kb-render>

<kb-render template="shared-row"></kb-render>

Reading the property returns the template's innerHTML, falling back to the concatenated outerHTML of its children.

!> Frameworks that create the <template> in JavaScript can break this. React appends template children to the element's own child list instead of into its content fragment, which leaves innerHTML empty. The fallback covers the element case but drops text nodes and whitespace between elements. Reading the property before the template's content exists returns nothing, and nothing re-reads it later.

MemberTypeDescription
templatestringThe resolved markup. Setting the attribute points at a <template> by id.