Class LayoutTrigger

LayoutTrigger allows you to run actions when the viewport or a target element's width or aspect ratio matches a given specification, and undo those actions when the target's width or aspect ratio no longer matches.


To use with auto-widgets (HTML API), see registerTrigger for the specification.

  • Arguments (required): A single DeviceSpec or AspectRatioSpec. In this case you can use a dash ("-") instead of space if needed (for example if using CSS classes instead of data attributes), e.g. "min-tablet" instead of "min tablet".

  • Additional trigger options:

Show the element when the window width matches "tablet" breakpoint, hide otherwise:

<div data-lisn-on-layout="tablet @show"></div>

As above, but using a CSS class instead of data attribute:

<div class="lisn-on-layout--tablet@show"></div>

Show the element 1000ms after the window width matches "tablet" breakpoint, hide otherwise:

<div data-lisn-on-layout="tablet @show +delay=1000"></div>

Add class tablet when the window width is at least "tablet", hide otherwise:

<div data-lisn-on-layout="min tablet @add-class=tablet"></div>

Add class mobile when the window width is "mobile" or mobile-wide, add class tablet, when it's "tablet" and so on; undo that otherwise:

<div data-lisn-on-layout="max mobile-wide @add-class=mobile ;
tablet @add-class=tablet ;
desktop @add-class=desktop"
></div>

Show the element when window width is at least "mobile-wide" and at most "tablet", hide otherwise:

<div data-lisn-on-layout="mobile-wide to tablet @show"></div>

When the aspect ratio of the next element with class box is square, then add classes c1 and c2 to the element (that the trigger is defined on) and enable trigger my-trigger defined on this same element; undo all of that otherwise (on other aspect ratios of the reference root):

<div data-lisn-on-layout="square @add-class=c1,c2 @enable=my-trigger +root=next.box"
data-lisn-on-run="@show +id=my-trigger"
></div>
<div class="box"></div>

As above, but using data-lisn-ref attribute instead of class selector.

<div data-lisn-on-layout="square @add-class=c1,c2 @enable=my-trigger +root=next-box"
data-lisn-on-run="@show +id=my-trigger"
></div>
<div data-lisn-ref="box"></div>

Hierarchy (View Summary)

Methods

Constructors

Properties

destroy: () => Promise<void>

Undoes all modifications to the element and returns it to its original state.

You will need to recreate it if you want to enable its functionality again.

disable: () => Promise<void>

Disables the functionality of the widget. What this means is specific to each widget.

enable: () => Promise<void>

Re-enables the functionality of the widget. What this means is specific to each widget.

getActions: () => Action[]

Returns the trigger's actions.

getConfig: () => LayoutTriggerConfig

Returns the trigger config.

getElement: () => Element

Returns the element passed to the widget constructor.

isDestroyed: () => boolean

Returns true if the widget is destroyed.

isDisabled: () => boolean

Returns true if the widget is currently disabled.

onDestroy: (handler: WidgetHandler) => void

The given handler will be called when the widget is destroyed.

onDisable: (handler: WidgetHandler) => void

The given handler will be called when the widget is disabled.

onEnable: (handler: WidgetHandler) => void

The given handler will be called when the widget is enabled.

reverse: () => Promise<void>

"Undo"es all the Actions linked to the trigger.

run: () => Promise<void>

"Do"es all the Actions linked to the trigger.

toggle: () => Promise<void>

"Toggle"s all the Actions linked to the trigger.

toggleEnable: () => Promise<void>

Re-enables the widget if disabled, otherwise disables it.