DomHelper

Helps with dom querying and manipulation.

DomHelper.createElement({
  tag       : 'div',
  className : 'parent',
  style     : 'background: red',
  children  : [
     { tag: 'div', className: 'child' },
     { tag: 'div', className: 'child' }
  ]
});

Properties

6
activeElement: HTMLElementreadonlystatic

Returns active element checking shadow dom too

Set to false to not show focus renditions when using keyboard

isDarkTheme: Booleanstatic

Check if the currently used theme is a dark theme, by checking the color-scheme property of .b-widget.

primaryColor: Stringreadonlystatic

Get the current themes primary color (by reading it from document.body)

scrollBarWidth: Numberreadonlystatic

Measures the scrollbar width using a hidden div. Caches result

themeInfo: ThemeInforeadonlystatic

A theme information object about the current theme. Note, when using Bryntum widgets inside a shadowRoot context, you need to use getThemeInfo and pass a context element.

Functions

67

Creation

appendstatic

Appends element to parentElement.

ParameterTypeDescription
parentElementHTMLElement

Parent element

elementOrConfigHTMLElement | DomConfig | String

Element to insert, or an element config passed on to createElement(), or an HTML string passed to createElementFromTemplate()

Returns: HTMLElement

Creates an Element, accepts a DomConfig object. Example usage:

DomHelper.createElement({
  tag         : 'table', // defaults to 'div'
  className   : 'nacho',
  html        : 'I am a nacho',
  children    : [ { tag: 'tr', ... }, myDomElement ],
  parent      : myExistingElement // Or its id
  style       : 'font-weight: bold;color: red',
  dataset     : { index: 0, size: 10 },
  tooltip     : 'Yay!',
  ns          : 'http://www.w3.org/1999/xhtml'
});
ParameterTypeDescription
configDomConfig

Element config object

optionsObject

An object specifying creation options. If this is a boolean value, it is understood to be the returnAll option.

options.ignoreRefsBoolean

Pass true to ignore element references.

options.returnAllBoolean

Specify true to return all elements & child elements created as an array.

Returns: HTMLElement | HTMLElement[] | Object<String, HTMLElement> -

Single element or array of elements returnAll was set to true. If any elements had a reference property, this will be an object containing a reference to all those elements, keyed by the reference name.

Inserts an element before beforeElement in into.

ParameterTypeDescription
intoHTMLElement

Parent element

elementHTMLElement

Element to insert, or an element config passed on to createElement()

beforeElementHTMLElement

Element before which passed element should be inserted

Returns: HTMLElement

Inserts an element at first position in into.

ParameterTypeDescription
intoHTMLElement

Parent element

elementHTMLElement

Element to insert, or an element config passed on to createElement()

Returns: HTMLElement

CSS

Adds a CSS class to an element during the specified duration

ParameterTypeDescription
elementHTMLElement

Target element

clsString

CSS class to add temporarily

durationNumber

Duration in ms, 0 means cls will not be applied

delayableDelayable | Window

The delayable to tie the setTimeout call to

Applies specified style to the passed element. Style can be an object or a string.

ParameterTypeDescription
elementHTMLElement

Target element

styleString | Object

Style to apply, 'border: 1px solid black' or { border: '1px solid black' }

overwriteBoolean

Specify true to replace style instead of applying changes

Adds or removes the classes contained in the specified object based on the truthy or falsy value of that key.

For example:

 DomHelper.assignClasses(element, {
     'b-class-one' : 1   // class will be added
     'b-class-two' : 0   // class will be removed
 });
ParameterTypeDescription
elementHTMLElement
classesObject

An object whose truthy keys will be added as classes and whose falsy keys will be removed.

Returns a style value or values for the passed element.

ParameterTypeDescription
elementHTMLElement

The element to read styles from

propNameString | String[]

The property or properties to read

inlineBoolean

Pass as true to read the element's inline style. Note that this could return inaccurate results if CSS rules apply to this element.

Returns: String | Object -

The value or an object containing the values keyed by the requested property name.

Replaces the passed element's className with the class names passed in either Array or String format or Object.

This method compares the existing class set with the incoming class set and avoids mutating the element's class name set if possible.

This can avoid browser style invalidations.

ParameterTypeDescription
elementHTMLElement

The element whose class list to synchronize.

newClassesString[] | String | Object

The incoming class names to set on the element.

Returns: Boolean -

true if the DOM class list was changed.

Toggle multiple classes in elements classList. Helper for toggling multiple classes at once.

ParameterTypeDescription
elementHTMLElement
classesString[]
forceBoolean

Specify true to add classes, false to remove. Leave blank to toggle

Applies the key state of the passed object or DomClassList to the passed element.

Properties with a falsy value mean that property name is removed as a class name.

Properties with a truthy value mean that property name is added as a class name.

This is different from syncClassList. That sets the className of the element to the sum of all its truthy keys, regardless of what the pre-existing value of the className was, and ignoring falsy keys.

This selectively updates the classes in the className. If there is a truthy key, the name is added. If there is a falsy key, the name is removed.

ParameterTypeDescription
elementHTMLElement

The element to apply the class list to .

classesObject | DomClassList

The classes to add or remove.

Returns: Boolean -

true if the DOM class list was changed.

Measure

Measures a relative size, such as a size specified in em units for the passed element.

ParameterTypeDescription
sizeString

The CSS size value to measure.

sourceElementHTMLElement
roundBoolean

Pass true to return exact width, not rounded value

Returns: Number -

size The size in pixels of the passed relative measurement.

Measures the text width using a hidden div

ParameterTypeDescription
textString
sourceElementHTMLElement
Returns: Number -

width

Other

Adds the passed string as a space-separated value to the passed attribute in the passed element.

ParameterTypeDescription
elementOrSelectorString | Element

The element to manipulate

attributeString

The attruibute name

valueString

The value to add

alignTostatic

Align the passed element with the passed target according to the align spec.

ParameterTypeDescription
elementHTMLElement

The element to align.

targetHTMLElement | Rectangle

The target element or rectangle to align to

alignSpecAlignSpec

See alignTo Defaults to { align : 't0-t0' }

roundBoolean

Round the calculated Rectangles (for example if dealing with scrolling which is integer based).

methodString

The method to use to align the element. Can be translate, topLeft or topInset.

Resolves element from point, checking shadow DOM if required

ParameterTypeDescription
xNumber
yNumber
Returns: HTMLElement

Returns element animations

ParameterTypeDescription
elementElement | Document
args*
Returns: Array

Returns an object with the parse style values for the top, right, bottom, and left components of the given edge style.

The return value is an object with top, right, bottom, and left properties for the respective components of the edge style, as well as width (the sum of left and right) and height (the sum of top and bottom).

ParameterTypeDescription
elementHTMLElement
edgeStyleString

The element's desired edge style such as 'padding', 'margin', or 'border'.

edgesString

A string with one character codes for each edge. Only those edges will be populated in the returned object. By default, all edges will be populated.

Returns: Object

Returns the specified element of the given event. If the event is an Element, it is returned. Otherwise, the eventName argument is used to retrieve the desired element property from event (this defaults to the 'target' property).

ParameterTypeDescription
eventEvent | Element
elementNameString
Returns: Element
getIdstatic

Returns the id of the passed element. Generates a unique id if the element does not have one.

ParameterTypeDescription
elementHTMLElement

The element to return the id of.

Gets theme information about the currently active theme by reading CSS custom properties from the .b-theme-info class.

The method creates a temporary DOM element with the b-theme-info class, reads all CSS custom properties that start with --b-theme-, converts them to camelCase property names, and returns them as an object.

Example theme CSS:

.b-theme-info {
    --b-theme-name             : "SvalbardDark";
    --b-theme-filename         : "svalbard-dark";
    --b-theme-button-rendition : "text";
    --b-theme-label-position   : "align-before";
    --b-theme-overlap-label    : "false";
}

This would result in:

{
    name            : "SvalbardDark",
    filename        : "svalbard-dark",
    buttonRendition : "text",
    labelPosition   : "align-before",
    overlapLabel    : false  // String "true"/"false" values are converted to boolean
}
ParameterTypeDescription
contextElementHTMLElement

The element for which to find the theme. If using a web component / shadowRoot, the theme will be encapsulated in the web component's encapsulated style so a context element is required. If no web components are in use, this may be omitted.

Returns: ThemeInfo -

Current theme information object with properties read from CSS custom properties

This method checks that the passed target is visible in all viewports

ParameterTypeDescription
targetHTMLElement | Event

The HTML element or Event to check for intersection with all ancestor viewports.

docRectRectangle

Window rectangle

methodcontains | intersect

'contains' or 'intersect'

Returns: Rectangle
highlightstatic

Highlights the passed element or Rectangle according to the theme's highlighting rules. Usually an animated framing effect.

The framing effect is achieved by adding the CSS class b-fx-highlight which references a keyframes animation named b-fx-highlight-animation. You may override the animation name referenced, or the animation itself in your own CSS.

ParameterTypeDescription
elementHTMLElement | Rectangle

The element or Rectangle to highlight.

Checks if an element clips its content.

ParameterTypeDescription
elementHTMLElement

The element to check.

Returns: Boolean -

Returns true if the element clips its content.

Returns true if element has opened shadow root

ParameterTypeDescription
elementHTMLElement

Element to check

Returns: Boolean

Returns true if the passed element accepts keystrokes to edit its contents.

Returns: Boolean -

Returns true if the passed element is editable.

isElementstatic

Returns true if the provided value is likely a DOM element. If the element can be assured to be from the same document, instanceof Element is more reliable.

ParameterTypeDescription
value*
Returns: Boolean

Returns true if the passed element is focusable either programmatically or through pointer gestures.

ParameterTypeDescription
elementHTMLElement

The element to test.

Returns: Boolean -

Returns true if the passed element is focusable

isInViewstatic

Returns the rectangle of the element or event which is currently visible in the browser viewport, i.e. user can find it on screen, or false if it is scrolled out of view.

ParameterTypeDescription
targetHTMLElement | Event

The element or event to test.

wholeBoolean

Whether to check that whole element is visible, not just part of it. If this is passed as true, the result will be a boolean, true or false.

Returns: Rectangle | Boolean -

Returns the rectangle of the element which is currently visible in the browser viewport, or false if it is out of view.

isNodestatic

Returns true if the provided value is likely a DOM node. If the node can be assured to be from the same document, instanceof Node is more reliable.

ParameterTypeDescription
value*
Returns: Boolean
isVisiblestatic

Returns true if the passed element is deeply visible. Meaning it is not hidden using display or visibility and no ancestor node is hidden.

ParameterTypeDescription
elementHTMLElement

The element to test.

Returns: Boolean -

true if deeply visible.

Converts the passed id to an id valid for usage as id on a DOM element.

ParameterTypeDescription
idString
Returns: String

Splits a style string up into object form. For example 'font-weight:bold;font-size:150%' would convert to

{
    font-weight : 'bold',
    font-size : '150%'
}
ParameterTypeDescription
styleString

A DOM style string

Returns: Object -

the style declaration in object form.

Removed the passed string as a space-separated value from the passed attribute in the passed element.

ParameterTypeDescription
elementOrSelectorString | Element

The element to manipulate

attributeString

The attruibute name

valueString

The value to remove

Removes the passed CSS classes from all descendants of the passed element.

ParameterTypeDescription
elementHTMLElement

Outermost element

classesString

CSS classes to remove

Resets DomHelper.scrollBarWidth cache, triggering a new measurement next time it is read

setLengthstatic

Sets a CSS length style value.

ParameterTypeDescription
elementString | HTMLElement

The element to set the style in, or, if just the result is required, the style magnitude to return with units added. If a nullish value is passed, an empty string is returned.

styleString

The name of a style property which specifies a length

valueNumber | String

The magnitude. If a number is used, the value will be set in px units.

Returns: String -

The style value string.

setThemestatic

Changes the theme to the passed theme name if possible.

Theme names are case-insensitive. The href used is all lower case.

To use this method, the <link rel="stylesheet"> must use the default, Bryntum-supplied CSS files where the href end with <themeName>.css, so that it can be found in the document, and switched out for a new link with the modified href. The new href will use the same path, just with the themeName portion substituted for the new name.

If no <link> with that name pattern can be found, an error will be thrown.

If you use this method, you must ensure that the theme files are all accessible on your server.

Because this is an asynchronous operation, a Promise is returned. The theme change event is passed to the success function. If the theme was not changed, because the theme name passed is the current theme, nothing is passed to the success function.

The theme change event contains two properties:

  • prev The previous Theme name.
  • theme The new Theme name.
ParameterTypeDescription
newThemeNameString

the name of the theme that should be applied

Returns: Promise -

A promise whose success callback receives the theme change event if the theme in fact changed. If the theme href could not be loaded, the failure callback is called, passing the error event caught.

slideInstatic

Animates the specified element to slide it into view within the visible viewport of its parentElement from the direction of movement.

So in a left-to-right Widget, direction 1 means it slides in from the right and direction -1 means it slides in from the left. RTL reverses the movement.

See the forward/backward navigations in DatePicker for an example of this in action.

If "next" should arrive from below and "previous" should arrive from above, add the class b-slide-vertical to the element.

ParameterTypeDescription
elementHTMLElement

The element to slide in.

directionNumber
  • 1 to slide in from the "next" direction.
  • -1 to slide in from the "previous" direction.

If the element is inside an RTL widget the directions are reversed.

syncstatic

Sync one source element attributes, children etc. to a target element. Source element can be specified as a html string or an actual HTMLElement.

NOTE: This function is superseded by DomSync.sync(), which works with DOM configs. For most usecases, use it instead.

ParameterTypeDescription
sourceElementString | HTMLElement

Source "element" to copy from

targetElementHTMLElement

Target element to apply to, can also be specified as part of the config object

Returns: HTMLElement -

Returns the updated targetElement (which is also updated in place)

Toggles between light and dark themes. Does not check if the theme is actually available, only does a string replace on the current themes filename for light/dark.

Position, get

Get elements X offset within a containing element

ParameterTypeDescription
elementHTMLElement
containerHTMLElement
Returns: Number -

X offset

Gets elements X and Y offset within containing element as an array [x, y]

ParameterTypeDescription
elementHTMLElement
containerHTMLElement
Returns: Number[] -

[x, y]

Get elements Y offset within a containing element

ParameterTypeDescription
elementHTMLElement
containerHTMLElement
Returns: Number -

Y offset

getPageXstatic

Get elements X position on page

ParameterTypeDescription
elementHTMLElement
Returns: Number
getPageYstatic

Get elements Y position on page

ParameterTypeDescription
elementHTMLElement
Returns: Number

Returns the x from the element's translate: x y value in pixels.

ParameterTypeDescription
elementHTMLElement
Returns: Number -

X translate

Gets both X and Y coordinates as an array [x, y]

ParameterTypeDescription
elementHTMLElement
Returns: Number[] -

[x, y]

Returns the y from the element's translate: x y value in pixels.

ParameterTypeDescription
elementHTMLElement
Returns: Number -

Y coordinate

Position, set

addLeftstatic

Increase X position

ParameterTypeDescription
elementHTMLElement
xNumber
addTopstatic

Increase Y position

ParameterTypeDescription
elementHTMLElement
yNumber

Increase X translation

ParameterTypeDescription
elementHTMLElement
xNumber

The number of pixels by which to increase the element's X translation.

Increase Y position

ParameterTypeDescription
elementHTMLElement
yNumber

The number of pixels by which to increase the element's Y translation.

setLeftstatic

Set element's style left.

ParameterTypeDescription
elementHTMLElement
xNumber | String

The top position. If numeric, 'px' is used as the unit.

setTopstatic

Set element's style top.

ParameterTypeDescription
elementHTMLElement
yNumber | String

The top position. If numeric, 'px' is used as the unit.

Set element's X translation in pixels (keepin Y translation as is).

ParameterTypeDescription
elementHTMLElement
xNumber

The value by which the element should be translated from its default position.

Set elements X and Y translation in pixels.

ParameterTypeDescription
elementHTMLElement
xNumber

The `X translation.

yNumber

The `Y translation.

Set element's Y translation in pixels (keeping X translation as is).

ParameterTypeDescription
elementHTMLElement
yNumber

The value by which the element should be translated from its default position.

Query children

childrenstatic

Returns all child elements (not necessarily direct children) that matches selector.

If selector starts with '>' or '# ', then all components of the selector must match inside of element. The scope selector, :scope is prepended to the selector (and if # was used, it is removed).

These are equivalent:

 DomHelper.children(el, '# .foo .bar');

 el.querySelectorAll(':scope .foo .bar');

These are also equivalent:

 DomHelper.children(el, '> .foo .bar');

 el.querySelectorAll(':scope > .foo .bar');
ParameterTypeDescription
elementHTMLElement

The parent element

selectorString

The CSS selector

Returns: HTMLElement[] -

Matched elements, somewhere below element

downstatic

Looks at the specified element and all of its children for the one that first matches selector.

ParameterTypeDescription
elementHTMLElement

Parent element

selectorString

CSS selector

Returns: HTMLElement -

Matched element, either element or an element below it

Iterates over the direct child elements of the specified element.

ParameterTypeDescription
elementHTMLElement

Parent element

fnfunction

Function called for each child element

Iterates over each result returned from element.querySelectorAll(selector). Can also be called with only two arguments, in which case the first argument is used as selector and document is used as the element.

ParameterTypeDescription
elementHTMLElement

Parent element

selectorString

CSS selector

fnfunction

Function called for each found element

getChildstatic

Gets the first direct child of element that matches selector.

ParameterTypeDescription
elementHTMLElement

Parent element

selectorString

CSS selector

Returns: HTMLElement
hasChildstatic

Checks if element has any child that matches selector.

ParameterTypeDescription
elementHTMLElement

Parent element

selectorString

CSS selector

Returns: Boolean -

true if any child matches selector

Checks if childElement is a descendant of parentElement (contained in it or a sub element)

ParameterTypeDescription
parentElementHTMLElement

Parent element

childElementHTMLElement

Child element, at any level below parent (includes nested shadow roots)

Returns: Boolean

Removes each element returned from element.querySelectorAll(selector).

ParameterTypeDescription
elementHTMLElement
selectorString

Query parents

Retrieves all parents to the specified element.

ParameterTypeDescription
elementHTMLElement

Element

Returns: HTMLElement[] -

All parent elements, bottom up

Typedefs

3
DomConfig: Object<String, *>

An object that describes a DOM element. Used for example by createElement() and by DomSync.sync().

DomHelper.createElement({
   class : {
       big   : true,
       small : false
   },
   children : [
       { tag : 'img', src : 'img.png' },
       { html : '<b style="color: red">Red text</b>' }
   ]
});
ParameterTypeDescription
tagString

Tag name, for example 'span'. If this is passed as '#fragment', a DocumentFragment will be created.

parentHTMLElement

Parent element

nextSiblingHTMLElement

Element's next sibling in the parent element

classString | Object

CSS classes, as a string or an object (truthy keys will be applied)

classNameString | Object

Alias for class

styleString | Object

Style, as a string or an object (keys will be hyphenated)

elementDataObject

Data object stored as an expando on the resulting element

datasetObject

Dataset applied to the resulting element

childrenDomConfig[] | Object<String, DomConfig> | String[] | HTMLElement[]

Child elements, as an array that can include DomConfigs that will be turned into elements, plain strings that will be used as text nodes or existing elements that will be moved. Also accepts an object map of DomConfigs, but please note that it cannot be used with DomHelper.createElement()

htmlString

Html string, used as the resulting elements innerHTML. Mutually exclusive with the children property

tooltipTooltipConfig | String

Tooltip config applied to the resulting element

textString

Text content, XSS safe when you want to display text in the element. Mutually exclusive with the children property

idString

Element's id

hrefString

Element's href

nsString

Element's namespace

srcString

Element's src

A theme information object about the current theme.

Theme information object containing metadata about the active theme. The theme information is read from CSS custom properties defined in the .b-theme-info class.

Example CSS:

.b-theme-info {
    --b-theme-name             : "SvalbardDark";
    --b-theme-filename         : "svalbard-dark";
    --b-theme-button-rendition : "text";
    --b-theme-label-position   : "align-before";
    --b-theme-overlap-label    : "false";
}
ParameterTypeDescription
nameString

The display name of the current theme (e.g., "SvalbardDark")

filenameString

The filename/identifier of the theme (e.g., "svalbard-dark")

buttonRenditionString

The button rendering style for the theme (e.g., "text", "outlined", "filled")

labelPositionString

The default label position for form fields (e.g., "align-before", "above")

overlapLabelBoolean

Whether labels should overlap with fields

An object that describes a vue component configuration.

{
    vue  : true
    is   : 'VueWidget'
    bind : { prop1: 'value1', prop2: 'value2' }
    on   : { myclick: handleMyClick }
}
ParameterTypeDescription
vueBoolean

Mandatory flag to mark as vue configuration. Should be set to true

isString

Name of the Vue component

bindObject

Object with properties to bind to the Vue Component

onObject

Object with event listeners. Events emitted by Vue Component