UIElement Docs Version 0.13.3

🔗 UIElement

Version 0.13.3

UIElement - a HTML-first library for reactive Web Components

UIElement is a set of functions to build reusable, loosely coupled Web Components with reactive properties. It provides structure through components and simplifies state management and DOM synchronization using declarative signals and effects, leading to more organized and maintainable code without a steep learning curve.

Unlike SPA frameworks (React, Vue, Svelte, etc.) UIElement takes a HTML-first approach, progressively enhancing sever-rendered HTML rather than recreating (rendering) it using JavaScript. UIElement achieves the same result as SPA frameworks with SSR, but with a simpler, more efficient approach. It works with a backend written in any language or with any static site generator.

🔗 Key Features

  • ðŸ§ą HTML Web Components: Build on standard HTML and enhance it with encapsulated, reusable Web Components. No virtual DOM – UIElement works directly with the real DOM.
  • ðŸšĶ Reactive Properties: Define reactive properties for fine-grained, efficient state management (signals). Changes automatically propagate only to the parts of the DOM that need updating, avoiding unnecessary re-renders.
  • ðŸ§Đ Function Composition: Declare component behavior by composing small, reusable functions (attribute parsers and effects). This promotes cleaner code compared to spaghetti code problems that commonly occur when writing low-level imperative code.
  • 🛠ïļ Customizable: UIElement is designed to be easily customizable and extensible. You can create your own custom attribute parsers and effects to suit your specific needs.
  • 🌐 Context Support: Share global states across components without prop drilling or tightly coupling logic.
  • ðŸŠķ Tiny footprint: Minimal core (~4kB gzipped) with tree-shaking support, adding only the necessary JavaScript to enhance your HTML.
  • ðŸ›Ąïļ Type Safety: Get early warnings when types don't match, improving code quality and reducing bugs.

UIElement uses Cause & Effect internally for state management with signals and for scheduled DOM updates. But you could easily rewrite the component() function to use a signals library of your choice or to produce something else than Web Components.

🔗 Installation

bash

# with npm
npm install @zeix/ui-element

# or with bun
bun add @zeix/ui-element

🔗 Documentation

The full documentation is still work in progress. The following chapters are already reasonably complete:

🔗 Basic Usage

🔗 Counter

Server-rendered markup:

html

<basic-counter count="5">
  <button type="button">💐 <span>5</span></button>
</basic-counter>

UIElement component:

js

import { asInteger, component, on, setText } from '@zeix/ui-element'

export default component(
  'basic-counter',
  {
    count: asInteger(), // Get initial value from count attribute
  },
  (el, { first }) => [
    // Update count display when state changes
    first('span', setText('count')),

    // Handle click events to change state
    first(
      'button',
      on('click', () => {
        el.count++
      }),
    ),
  ],
)

Example styles:

css

basic-counter {
  & button {
    border: 1px solid var(--color-border);
    border-radius: var(--space-xs);
    background-color: var(--color-secondary);
    padding: var(--space-xs) var(--space-s);
    cursor: pointer;
    color: var(--color-text);
    font-size: var(--font-size-m);
    line-height: var(--line-height-xs);
    transition: background-color var(--transition-short) var(--easing-inout);

    &:hover {
      background-color: var(--color-secondary-hover);
    }

    &:active {
      background-color: var(--color-secondary-active);
    }
  }
}

🔗 Tab Group

An example demonstrating how to create a fully accessible tab navigation.

Server-rendered markup:

html

<module-tabgroup>
  <div role="tablist">
    <button
      type="button"
      role="tab"
      id="trigger1"
      aria-controls="panel1"
      aria-selected="true"
      tabindex="0"
    >
      Tab 1
    </button>
    <button
      type="button"
      role="tab"
      id="trigger2"
      aria-controls="panel2"
      aria-selected="false"
      tabindex="-1"
    >
      Tab 2
    </button>
    <button
      type="button"
      role="tab"
      id="trigger3"
      aria-controls="panel3"
      aria-selected="false"
      tabindex="-1"
    >
      Tab 3
    </button>
  </div>
  <div role="tabpanel" id="panel1" aria-labelledby="trigger1">
    Tab 1 content
  </div>
  <div role="tabpanel" id="panel2" aria-labelledby="trigger2" hidden>
    Tab 2 content
  </div>
  <div role="tabpanel" id="panel3" aria-labelledby="trigger3" hidden>
    Tab 3 content
  </div>
</module-tabgroup>

UIElement component:

`js
export const manageArrowKeyFocus = (elements, index) => e => {
if (!(e instanceof KeyboardEvent))
throw new TypeError('Event is not a KeyboardEvent')
const handledKeys = [
'ArrowLeft',
'ArrowRight',
'ArrowUp',
'ArrowDown',
'Home',
'End',
]
if (handledKeys.includes(e.key)) {
e.preventDefault()
switch (e.key) {
case 'ArrowLeft':
case 'ArrowUp':
index = index < 1 ? elements.length - 1 : index - 1
break
case 'ArrowRight':
case 'ArrowDown':
index = index >= elements.length - 1 ? 0 : index + 1
break
case 'Home':
index = 0
break
case 'End':
index = elements.length - 1
break
}
if (elements[index]) elements[index].focus()
}
}


Example styles:



### Lazy Load

An example demonstrating how to use a custom attribute parser (sanitize an URL) and a signal producer (async fetch) to implement lazy loading.



UIElement component:



Custom attribute parser:



## Contributing & License

Feel free to contribute, report issues, or suggest improvements.

License: [MIT](_media/LICENSE)

(c) 2025 [Zeix AG](https://zeix.com)