v7.3.0

Widget
Widget

The base class for all other widgets in the Bryntum libraries. The Widget base class encapsulates a native DOM element, and can optionally contain html. See also Container which lets you build a component tree where widgets can contain other widgets. Simple widget example (inspect with the DOM inspector to see it's really just a simple DIV):

Rendering

Subclasses should override the compose method to return their encapsulating element and internal DOM structure. The compose() method returns a createElement config object that is* used to create the DOM structure, based on its configurable properties:

 class Button extends Widget {
     static configurable = {
         cls  : null,
         text : null
     };

compose() { const { cls, text } = this; // collect all relevant configs properties (for auto-detection)

return { tag : 'button', class : cls, text }; } }

The config properties used by the compose() method are auto-detected when the method is first called for a class. All relevant properties must be read, even if they end up not being used so that future changes to these properties will mark the rendering as dirty.

When a config property used by compose() is modified, the recompose method is called. Since recompose() is a delayable method, calling it schedules a delayed call to compose() and a DOM update. Accessing the Widget's primary element or any reference element property will force the DOM update to occur immediately.

Lifecycle events

The lifecycle events provide hooks into the various stages of a widget's existence, allowing for custom behavior at key points.

These events include

  • elementCreated - Fired once, after the widget's main element has been created but before it is added to the DOM. At this stage it will have its style and cls applied. But since the element is not yet in the DOM, it will not be visible, nor will styles from CSS be applied.

    If you need to measure the element (for example to set its size based on its content), you should add a listener for the paint event and perform your measurements there.

  • paint - Fired whenever the widget becomes visible and eligible for layout. This event may be fired multiple times during the lifetime of a widget, whenever it becomes visible after being hidden, or when an ancestor widget is shown after being hidden.

    Note that to only react the first time a widget is painted, you can use the once option when adding a listener early in the widget's lifecycle or check the firstPaint property of the event object.

  • beforeDestroy - Fired when the widget is being destroyed. At this stage the widget is still fully functional and can be interacted with if certain actions need to be called.

  • destroy - Fired after the widget has been destroyed. At this stage the widget's DOM elements have been removed from the DOM and all event listeners have been detached. The widget is completely inert and has no properties or methods.

Child Elements

Unlike typical DOM config objects, the object returned by compose() can use an object to simplify naming:

 class Button extends Widget {
     ...

compose() { const { cls, iconCls, text } = this; // collect all relevant configs properties (for auto-detection)

return { tag : 'button', class : cls,

children : { iconElement : iconCls && { class : { 'button-icon' : 1, [iconCls] : 1 } },

textElement : { text } } }; } }

The keys of the children are iterated to convert the values into the array required by createElement. The names of the properties becomes the reference of the element.

For example, the above is equivalent to the following:

 class Button extends Widget {
     ...

compose() { const { cls, iconCls, text } = this; // collect all relevant configs properties (for auto-detection)

return { tag : 'button', class : cls,

children : [iconCls && { reference : 'iconElement', class : { 'button-icon' : 1, [iconCls] : 1 } }, { reference : 'textElement', text }] }; } }

In other places inside your class, you can then access those elements as this.iconElement and this.textElement, no need to use querySelector etc.

The object form of children is preferred for clarity but also because it facilitates inheritance.

Inheritance

When a derived class implements compose(), the object it returns is automatically merged with the object returned by the base class.

For example, the following class adds a new child element:

 class MenuButton extends Button {
     ...

compose() { const { menuCls } = this; // collect all relevant configs properties (for auto-detection)

return { children : { menuElement : { class : { 'button-menu' : 1, [menuCls] : 1 } } } }; } }

Listeners

Reference elements may also define event listeners in the compose() method:

 class Button extends Widget {
     compose() {
         const { cls, text } = this;

return { tag : 'button', class : cls, text,

listeners : { click : 'onClick' } }; }

onClick(event) { // handle click event } }

Resolving properties

Values for a Widgets properties can be resolved from the ownership hierarchy. For example a text field in a toolbar can get its initial value from a property on the container owning the toolbar. This is achieved by prefixing the desired property name with 'up.':

 const grid = new Grid((
     tbar : [{
         type  : 'numberfield',
         // Fields value will be retrieved from the grids rowHeight property
         value : 'up.rowHeight'
     }]
 });

NOTE: this is for now a one way one time binding, the value will only be read initially and not kept up to date on later changes.

No results

Configs

Configs are options you supply in a configuration object when creating an instance of this class
  • bottom : Number/String
    internal

    The bottom CSS absolute position for this widget. If specified, the widget is implicitly configured as positionable.

  • Programmatic control over which column to start in when used in a grid layout.

    Has a corresponding runtime column property.

  • left : Number/String
    internal

    The inset-inline-start CSS absolute position for this widget. If specified, the widget is implicitly configured as positionable.

  • recomposeAsync : Booleantrue
    internal

    Set this config to false to disable batching DOM updates on animation frames for this widget. This has the effect of synchronously updating the DOM when configs affecting the rendered DOM are modified. Depending on the situation, this could simplify code while increasing time spent updating the DOM.

  • right : Number/String
    internal

    The inset-inline-end CSS absolute position for this widget. If specified, the widget is implicitly configured as positionable.

  • Programmatic control over how many columns to span when used in a grid layout.

    Has a corresponding runtime span property.

  • top : Number/String
    internal

    The top CSS absolute position for this widget. If specified, the widget is implicitly configured as positionable.

  • The CSS class(es) to add when HTML content is being applied to this widget.

  • Custom style spec to add to element

    Has a corresponding runtime style property.

  • Element (or the id of an element) to append this widget's element to. Can be configured, or set once at runtime. To access the element of a rendered widget, see element.

    Has a corresponding runtime appendTo property.

  • content : String
    ADVANCED

    The HTML content that coexists with sibling elements which may have been added to the contentElement by plugins and features. When specifying html, this widget's element will also have the htmlCls class added to its classList, to allow targeted styling.

    Has a corresponding runtime content property.

  • dataset : Object<String, String>

    Object to apply to elements dataset (each key will be used as a data-attribute on the element)

    Has a corresponding runtime dataset property.

  • A createElement config object or HTML string from which to create the Widget's element.

    Has a corresponding runtime element property.

  • Widget id, if not specified one will be generated. Also used for lookups through Widget.getById

    Has a corresponding runtime id property.

  • Element (or element id) to insert this widget before. If provided, appendTo config is ignored.

    Has a corresponding runtime insertBefore property.

  • Element (or element id) to append this widget element to, as a first child. If provided, appendTo config is ignored.

    Has a corresponding runtime insertFirst property.

  • tag : Stringdiv
    ADVANCED

    The tag name of this Widget's root element

  • A title to display for the widget. Only in effect when inside a container that uses it (such as TabPanel)

  • anchor : Booleanfalse

    Only valid if this Widget is floating and being shown through showBy.true to show a connector arrow pointing to the align target.

  • Only valid if this Widget is floating. Set to true to centre the Widget in browser viewport space.

  • Only valid if this Widget is floating or positioned. Element, Widget or Rectangle to which this Widget is constrained.

  • Set to true to move the widget out of the document flow and position it absolutely in browser viewport space.

  • scrollAction : 'hide'/'realign'/null

    Defines what to do if document is scrolled while Widget is visible (only relevant when floating is set to true). Valid values: ´null´: do nothing, ´hide´: hide the widget or ´realign´: realign to the target if possible.

  • When this widget is a child of a Container, it will by default be participating in a flexbox layout. This config allows you to set this widget's align-self style.

    Has a corresponding runtime alignSelf property.

  • When this widget is a child of a Container, it will by default be participating in a flexbox layout. This config allows you to set this widget's flex style. This may be configured as a single number or a <flex-grow> <flex-shrink> <flex-basis> format string. numeric-only values are interpreted as the flex-grow value.

    Has a corresponding runtime flex property.

  • Widget's height, used to set element style.height. Either specify a valid height string or a number, which will get 'px' appended. We recommend using CSS as the primary way to control height, but in some cases this config is convenient.

    Has a corresponding runtime height property.

  • hidden : Booleanfalse

    Configure with true to make widget initially hidden.

    Has a corresponding runtime hidden property.

  • Widget's margin. This may be configured as a single number or a TRBL format string. numeric-only values are interpreted as pixels.

    Has a corresponding runtime margin property.

  • The element's maxHeight. Can be either a String or a Number (which will have 'px' appended). Note that like height, reading the value will return the numeric value in pixels.

    Has a corresponding runtime maxHeight property.

  • The elements maxWidth. Can be either a String or a Number (which will have 'px' appended). Note that like width, reading the value will return the numeric value in pixels.

    Has a corresponding runtime maxWidth property.

  • The element's minHeight. Can be either a String or a Number (which will have 'px' appended). Note that like height, reading the value will return the numeric value in pixels.

    Has a corresponding runtime minHeight property.

  • The elements minWidth. Can be either a String or a Number (which will have 'px' appended). Note that like width, reading the value will return the numeric value in pixels.

    Has a corresponding runtime minWidth property.

  • A widgets weight determines its position among siblings when added to a Container. Higher weights go further down.

  • Widget's width, used to set element style.width. Either specify a valid width string or a number, which will get 'px' appended. We recommend using CSS as the primary way to control width, but in some cases this config is convenient.

    Has a corresponding runtime width property.

  • dataField : String
    ADVANCED

    When this Widget configuration is used in the Grid's RowExpander feature's widget config, provide the field on the expanded record to use for populating this widget's store (if applicable)

  • Event that should be considered the default action of the widget. When that event is triggered the widget is also expected to trigger an action event. Purpose is to allow reacting to most widgets in a coherent way.

  • The name of the property to set when a single value is to be applied to this Widget. Such as when used in a grid WidgetColumn, this is the property to which the column's field is applied.

  • This config object contains the defaults for the Mask created for the masked config. Any properties specified in the masked config will override these values.

  • Set to true to apply the default mask to the widget. Alternatively, this can be the mask message or a Mask config object.

  • Prevent tooltip from being displayed on touch devices. Useful for example for buttons that display a menu on click etc, since the tooltip would be displayed at the same time.

  • If you are rendering this widget to a shadow root inside a web component, set this config to the shadowRoot. If not inside a web component, set it to document.body

  • Set to false to not show the tooltip when this widget is disabled

  • The class to instantiate to use as the scrollable. Defaults to Scroller.

Properties

Properties are getters/setters or publicly accessible variables on this class
  • all : Widget[]
    internal
    READONLY
    static

    Returns an array containing all existing Widgets. The returned array is generated by this call and is not an internal structure.

  • focusVisible : Boolean
    internal
    READONLY
    static

    This property yields true if the currently focused element has been reached through other means than mouse click. If the activeElement matches :focus-visible.

  • recomposeAsync : Boolean
    internal
    static

    Get/set the recomposeAsync config for all widgets. Setting this value will set the config for all existing widgets and will be the default value for newly created widgets. Set this value to null to disable the default setting for new widgets while leaving existing widgets unaffected.

    Has a corresponding recomposeAsync config.

  • tooltip : Tooltip
    READONLY
    static

    The shared Tooltip instance which handles tooltips which are not configured with newInstance : true.

    Has a corresponding tooltip config.

  • isWidget : Booleantrue
    READONLY
    static
    ADVANCED
    Identifies an object as an instance of Widget class, or subclass thereof.
  • Programmatic control over which column to start in when used in a grid layout.

    Has a corresponding column config.

  • isComposable : Boolean
    internal
    READONLY

    Returns true if this class uses compose() to render itself.

  • overflowTwin : Widget
    internal
    READONLY

    This widget's twin that is placed in an overflow menu when this widget has been hidden by its owner, typically a Toolbar due to overflow. The overflowTwin is created lazily by ensureOverflowTwin.

  • Programmatic control over how many columns to span when used in a grid layout.

    Has a corresponding span config.

  • focusVisible : Boolean
    READONLY

    This property yields true if the currently focused element has been reached through other means than mouse click. If the activeElement matches :focus-visible.

  • isWidget : Booleantrue
    READONLY
    ADVANCED
    Identifies an object as an instance of Widget class, or subclass thereof.
  • Element (or the id of an element) to append this widget's element to. Can be configured, or set once at runtime. To access the element of a rendered widget, see element.

    Has a corresponding appendTo config.

  • content : String
    ADVANCED

    Set HTML content safely, without disturbing sibling elements which may have been added to the contentElement by plugins and features. When specifying html, this widget's element will also have the htmlCls added to its classList, to allow targeted styling.

    Has a corresponding content config.

  • contentElement : HTMLElement
    READONLY
    ADVANCED

    The child element into which content should be placed. This means where html should be put, or, for Containers, where child items should be rendered.

  • Get widgets elements dataset or assign to it

    Has a corresponding dataset config.

  • element : HTMLElement
    READONLY

    Get this widget's encapsulating HTMLElement, which is created along with the widget but added to DOM at render time.

    Has a corresponding element config.

  • focusElement : HTMLElement
    READONLY
    ADVANCED

    Get this widget's primary focus holding element if this widget is itself focusable, or contains focusable widgets.

  • focusability : Focusability
    internal
    READONLY

    Returns an object describing the focus and keyboard navigation aspects of this widget's focusElement.

  • focusableElement : HTMLElement
    READONLY
    ADVANCED

    Returns this widget's focusElement if that element can currently be given focus (e.g., this widget is not disabled, or hidden).

  • hasPainted : Boolean
    internal
    READONLY

    This property is set to true after processing the initial paint for the widget. It remains false during the initial paint. The intended use for this flag is to avoid processing that will be handled by the first paint (similar to not firing events during the widget's initial configuration). If this field is true, the initial paint has already taken place, otherwise, it has yet to run. This field differs from isPainted which checks that the widget's element is attached to the DOM.

  • Get/set widgets id

    Has a corresponding id config.

  • Element (or element id) to insert this widget before. If provided, appendTo config is ignored.

    Has a corresponding insertBefore config.

  • Element (or element id) to append this widget element to, as a first child. If provided, appendTo config is ignored.

    Has a corresponding insertFirst config.

  • overflowElement : HTMLElement
    READONLY
    ADVANCED

    The child element which scrolls if any. This means the element used by the scrollable.

  • staticClassList : DomClassList
    internal
    READONLY

    Returns the DomClassList for this widget's class. This object should not be mutated.

  • Get/set widgets elements style. The setter accepts a cssText string or a style config object, the getter always returns a CSSStyleDeclaration

    Has a corresponding style config.

  • Get/set this widget's align-self flexbox setting. This may be set to modify how this widget is aligned within the cross axis of a flexbox layout container.

    Has a corresponding alignSelf config.

  • Get element's offsetHeight or sets its style.height, or specified height if element no created yet.

    Has a corresponding height config.

  • Get element's margin property. This may be configured as a single number or a TRBL format string. numeric-only values are interpreted as pixels.

    Has a corresponding margin config.

  • Get/set element's maxHeight. Getter returns max-height from elements style, which is always a string. Setter accepts either a String or a Number (which will have 'px' appended). Note that like height, reading the value will return the numeric value in pixels.

    Has a corresponding maxHeight config.

  • Get/set elements maxWidth. Getter returns max-width from elements style, which is always a string. Setter accepts either a String or a Number (which will have 'px' appended). Note that like width, reading the value will return the numeric value in pixels.

    Has a corresponding maxWidth config.

  • Get/set element's minHeight. Getter returns min-height from elements style, which is always a string. Setter accepts either a String or a Number (which will have 'px' appended). Note that like height, reading the value will return the numeric value in pixels.

    Has a corresponding minHeight config.

  • Get/set elements minWidth. Getter returns min-width from elements style, which is always a string. Setter accepts either a String or a Number (which will have 'px' appended). Note that like width, reading the value will return the numeric value in pixels.

    Has a corresponding minWidth config.

  • Accessor to the Scroller which can be used to both set and read scroll information.

    Has a corresponding scrollable config.

  • Get elements offsetWidth or sets its style.width, or specified width if element not created yet.

    Has a corresponding width config.

  • assignedId : String
    private
    READONLY

    Get id assigned by user (not generated id)

  • disabled : Boolean/'inert'

    Get/set element's disabled state. Set to 'inert' to also set the inert DOM attribute.

    Has a corresponding disabled config.

  • true if no id was set, will use generated id instead (widget1, ...). Toggle automatically on creation

  • innerItem : Boolean
    internal

    This readonly property is true for normal widgets in the items of a container. It is false for special widgets such as a tbar.

  • tab : Tab
    READONLY

    The tab created for this widget when it is placed in a TabPanel.

    Has a corresponding tab config.

  • isVisible : Boolean
    READONLY

    Determines visibility by checking if the Widget is hidden, or any ancestor is hidden and that it has an element which is visible in the DOM

  • nextSibling : Widget
    READONLY

    Get this Widget's next sibling in the parent Container, or, if not in a Container, the next sibling widget in the same parentElement.

  • Get this Widget's parent when used as a child in a Container,

  • Get this Widget's previous sibling in the parent Container, or, if not in a Container, the previous sibling widget in the same parentElement.

Functions

Functions are methods available for calling on the class
  • afterRecompose( )
    internal

    This method is called following an update to the widget's rendered DOM.

  • Disable the widget

  • enable( )

    Enable the widget

  • unmask( )

    Unmask the widget

  • emptyCache( )
    internal

    Clear caches, forces all calls to fromCache to requery dom. Called on render/rerender.

  • Only valid for floating Widgets. Moves to the front of the visual stacking order.

  • finalizeInit( )
    internal

    Called by the Base constructor after all configs have been applied.

Events

Events are triggered for certain actions in this class and can be listened for to react to those actions in your code
  • recompose
    ADVANCED

    This event is fired after a widget's elements have been synchronized due to a direct or indirect call to recompose, if this results in some change to the widget's rendered DOM elements.

Event handlers

Event handlers are callbacks called as a result of certain actions in this class
  • onRecompose
    ADVANCED

    This event is called after a widget's elements have been synchronized due to a direct or indirect call to recompose, if this results in some change to the widget's rendered DOM elements.

Type definitions

CSS variables

CSS variables that can be set to adjust appearance
Name Description
--b-aligned-above-floating-box-shadow Box shadow used by floating widgets which are aligned above their target, so that the shadow extends above.
--b-default-transition-duration Default duration for transitions. This is used for transitions resulting from user interaction throughout Bryntum widgets and components. For example, it is used for selection rendition transitions in Grids and for task and event transitions in Scheduler, SchedulerPro, Calendar, Gantt and TaskBoard. This is ignored if user has requested reduced motion in OS settings
--b-label-color Base label color. Defined in Widget since some widgets have labels that do not use the Label widget
--b-widget-anchor-height Block size of the anchor for an anchored floating widget
--b-widget-anchor-width Inline size of the anchor for an anchored floating widget
--b-widget-color Base color for text etc. Where it is applied may vary depending on theme
--b-widget-floating-border Base border for floating widgets like popups and menus. Where it is applied may vary depending on theme
--b-widget-floating-box-shadow Base box shadow used by floating widgets like popups and menus. Where it is applied may vary depending on theme
--b-widget-focus-outline-color Base focus outline color. Where it is applied may vary depending on theme
--b-widget-focus-outline-width Base focus outline width. Where it is applied may vary depending on theme
--b-widget-font-size Widget font-size. Defaults to 1em = the font size used by the parent element.
--b-widget-font-weight Widget font-weight
--b-widget-gap Base gap. Where it is applied may vary depending on theme
--b-widget-icon-font-family The default themes use Font Awesome 6 Free for icons. If you use a different icon font, you can override this variable. Note that you will also have to override the individual icon classes.
--b-widget-icon-font-weight The font-weight used for icons. The default themes use Font Awesome 6 Free solid icons which require font-weight 900.
--b-widget-padding Base padding. Where it is applied may vary depending on theme
--b-widget-padding-large Base padding for widgets that want a larger padding. Where it is applied may vary depending on theme
--b-widget-sub-menu-icon Which character of the icon font to use for sub-menu icons
Disabled
--b-widget-disabled-background Base background for disabled widgets. Where it is applied may vary depending on theme
--b-widget-disabled-color Base color for disabled widgets. Where it is applied may vary depending on theme
type: widget

Source path

Core/widget/Widget.js

Contents