@hyperfrontend/ui-utils/elementelement
DOM element creation, retrieval, and dimension-tracking helpers.
createElement builds a configured element from an ElementConfig (tag, attributes, children, listeners) and returns it with ElementMethods attached for fluent updates. The per-tag shortcuts (div, span, button, input, img, paragraph, header, section, unorderedList, table-cell helpers, etc.) are pre-bound versions of createElement for the common HTML tags. getElementAsync resolves an element by ref or selector once it appears in the DOM, with OnSuccess/OnFail callbacks for the timeout case. onElementResize registers an ElementResizeCallback against ResizeObserver to track dimension changes without polling.
API Reference§
ƒ Functions
Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLAnchorElement>Example
Creating anchor element
const link = anchor({ className: 'nav-link' })
link.ref.href = '/home'Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLElement>Example
Creating article element
const post = article({ className: 'blog-post' })
post.ref // => HTMLElementParameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLElement>Example
Creating aside element
const sidebar = aside({ className: 'sidebar' })
sidebar.ref // => HTMLElementParameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLButtonElement>Example
Creating button element
const submitBtn = button({ className: 'btn-primary' })
submitBtn.ref.textContent = 'Submit'Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLCanvasElement>Example
Creating canvas element
const drawing = canvas({ className: 'stage' })
drawing.ref.width = 800Parameters
| Name | Type | Description |
|---|---|---|
§tagName | unknown | The HTML tag name to create |
§config? | ElementConfig | Optional configuration for the element including styles and class names |
Returns
ElementMethods<T>Example
Creating element with configuration
const card = createElement('div', {
className: 'card',
classNames: ['shadow', 'rounded'],
inlineStyle: { padding: '16px', margin: '8px' }
})
const title = createElement('h2')
title.ref.textContent = 'Card Title'
card.addChild(title)
card.attachTo(document.body)
card.show(300) // Fade in over 300msParameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLDivElement>Example
Creating div element
const container = div({ className: 'container' })
container.ref.id = 'main'getElementAsync(elementRefOrString: ElementRefOrString, options?: GetElementAsyncOptions): () => void
Parameters
| Name | Type | Description |
|---|---|---|
§elementRefOrString | ElementRefOrString | Either an HTMLElement reference or a CSS selector string |
§options? | GetElementAsyncOptions | Configuration options including duration, interval, and callbacks |
Returns
() => voidExample
Waiting for dynamic element
const cancel = getElementAsync('#dynamic-content', {
duration: 5000,
interval: 100,
onSuccess: (element) => {
console.log('Element found:', element)
},
onFail: () => {
console.log('Element not found within timeout')
},
})
// Cancel polling if no longer needed
cancel()Parameters
| Name | Type | Description |
|---|---|---|
§level | number | The heading level (1-6) |
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLHeadingElement>Example
Creating heading element
const title = header(1, { className: 'page-title' })
title.ref.textContent = 'Page Title'Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLImageElement>Example
Creating image element
const avatar = img({ className: 'avatar' })
avatar.ref.src = '/avatar.png'Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLInputElement>Example
Creating input element
const email = input({ className: 'field' })
email.ref.type = 'email'Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLLabelElement>Example
Creating label element
const emailLabel = label({ className: 'field-label' })
emailLabel.ref.htmlFor = 'email'Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLLIElement>Example
Creating list item element
const item = listItem({ className: 'nav-item' })
item.ref.textContent = 'First item'Parameters
| Name | Type | Description |
|---|---|---|
§element | HTMLElement | The element to observe for resize events |
§callback | ElementResizeCallback | The function to call when the element is resized |
Returns
() => voidExample
Observing element resize
const container = document.getElementById('resizable-panel')
const stopObserving = onElementResize(container, (rect) => {
console.log(`New size: ${rect.width}x${rect.height}`)
})
// Stop observing when done
stopObserving()Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLOListElement>Example
Creating ordered list element
const steps = orderedList({ className: 'instructions' })
steps.ref // => HTMLOListElementParameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLParagraphElement>Example
Creating paragraph element
const text = paragraph({ className: 'lede' })
text.ref.textContent = 'Hello, world!'Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLElement>Example
Creating section element
const about = section({ className: 'page-section' })
about.ref.id = 'about'Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLSpanElement>Example
Creating span element
const badge = span({ className: 'badge' })
badge.ref.textContent = 'New'syncElementDimensions<S, T>(sourceElementRefOrString: ElementRefOrString<S>, targetElementRefOrString: ElementRefOrString<T>, options?: GetElementAsyncOptions): () => void
Parameters
| Name | Type | Description |
|---|---|---|
§sourceElementRefOrString | ElementRefOrString<S> | The source element to copy dimensions from (element or selector) |
§targetElementRefOrString | ElementRefOrString<T> | The target element to apply dimensions to (element or selector) |
§options? | GetElementAsyncOptions | Optional configuration for element retrieval and callbacks. onSuccess fires once, after both elements are found and the first sync has been applied, and receives the target element. onFail fires if either lookup times out. |
Returns
() => voidExample
Syncing overlay to video dimensions
// Sync an overlay to match a video player's dimensions
const stopSync = syncElementDimensions('#video-player', '#overlay', {
onSuccess: (overlay) => overlay.classList.add('ready'),
})
// Stop syncing when component unmounts
stopSync()Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLTableSectionElement>Example
Creating table body element
const body = tableBody({ className: 'data-rows' })
body.ref // => HTMLTableSectionElementParameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLTableCellElement>Example
Creating table cell element
const cell = tableCell({ className: 'name-cell' })
cell.ref.textContent = 'John Doe'Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLTableSectionElement>Example
Creating table head element
const head = tableHead({ className: 'sticky-header' })
head.ref // => HTMLTableSectionElementParameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLTableElement>Example
Creating table element
const grid = tableHeader({ className: 'data-table' })
grid.ref // => HTMLTableElementParameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLTableCellElement>Example
Creating table header cell
const nameHeader = tableHeaderCell({ className: 'col-name' })
nameHeader.ref.textContent = 'Name'Parameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLTableRowElement>Example
Creating table row element
const row = tableRow({ className: 'data-row' })
row.ref // => HTMLTableRowElementParameters
| Name | Type | Description |
|---|---|---|
§config? | ElementConfig | Optional inline styles and class names to apply to the element |
Returns
ElementMethods<HTMLUListElement>Example
Creating unordered list element
const menu = unorderedList({ className: 'nav-menu' })
menu.ref // => HTMLUListElement◈ Interfaces
Properties
◆ Types
type ElementRefOrString = T | stringtype ElementResizeCallback = (rect: DOMRectReadOnly) => voidtype HtmlTagName = unknowntype OnSuccess = (element: HTMLElement) => void