Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | 94x 94x 94x 94x 94x 94x 94x 188x 7x 7x 8x 8x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 94x 7x 7x 7x 7x | /** * @module Triggers * * @categoryDescription Layout * {@link 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. */ import * as MH from "@lisn/globals/minification-helpers"; import { DeviceSpec, Device, AspectRatioSpec, AspectRatio, } from "@lisn/globals/types"; import { waitForReferenceElement } from "@lisn/utils/dom-search"; import { isValidDeviceList, isValidAspectRatioList, getOtherDevices, getOtherAspectRatios, } from "@lisn/utils/layout"; import { validateStringRequired } from "@lisn/utils/validation"; import { Action } from "@lisn/actions/action"; import { LayoutWatcher } from "@lisn/watchers/layout-watcher"; import { registerTrigger, Trigger, TriggerConfig, } from "@lisn/triggers/trigger"; import { WidgetConfigValidatorFunc } from "@lisn/widgets/widget"; /** * {@link 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 {@link registerTrigger} for the * specification. * * - Arguments (required): A single {@link DeviceSpec} or * {@link AspectRatioSpec} (see {@link LayoutTriggerConfig.layout}). 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 (see {@link LayoutTriggerConfig}: * - `root`: A string element specification. See * {@link Utils.getReferenceElement | getReferenceElement}. * * @example * Show the element when the window width matches "tablet" breakpoint, hide * otherwise: * * ```html * <div data-lisn-on-layout="tablet @show"></div> * ``` * * @example * As above, but using a CSS class instead of data attribute: * * ```html * <div class="lisn-on-layout--tablet@show"></div> * ``` * * @example * Show the element 1000ms after the window width matches "tablet" breakpoint, * hide otherwise: * * ```html * <div data-lisn-on-layout="tablet @show +delay=1000"></div> * ``` * * @example * Add class `tablet` when the window width is at least "tablet", hide * otherwise: * * ```html * <div data-lisn-on-layout="min tablet @add-class: tablet"></div> * ``` * * @example * 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: * * ```html * <div data-lisn-on-layout="max mobile-wide @add-class: mobile ; * tablet @add-class: tablet ; * desktop @add-class: desktop" * ></div> * ``` * * @example * Show the element when window width is at least "mobile-wide" and at most * "tablet", hide otherwise: * * ```html * <div data-lisn-on-layout="mobile-wide to tablet @show"></div> * ``` * * @example * 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): * * ```html * <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> * ``` * * @example * As above, but using `data-lisn-ref` attribute instead of class selector. * * ```html * <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> * * @category Layout */ export class LayoutTrigger extends Trigger { readonly getConfig: () => LayoutTriggerConfig; static register() { registerTrigger( "layout", (element, args, actions, config) => { return new LayoutTrigger( element, actions, MH.assign(config, { layout: validateStringRequired( "layout", MH.strReplace( MH.strReplace(args[0] ?? "", /(min|max)-/g, "$1 "), /-to-/g, " to ", ), (value) => isValidDeviceList(value) || isValidAspectRatioList(value), ), }), ); }, newConfigValidator, ); } /** * If no actions are supplied, nothing is done. * * @throws {@link Errors.LisnUsageError | LisnUsageError} * If the config is invalid. */ constructor( element: Element, actions: Action[], config: LayoutTriggerConfig, ) { const layout = config?.layout ?? ""; if (!layout) { throw MH.usageError("'layout' is required"); } super(element, actions, config); this.getConfig = () => MH.copyObject(config); Iif (!MH.lengthOf(actions)) { return; } let devices: DeviceSpec | Device[] = []; let aspectRatios: AspectRatioSpec | AspectRatio[] = []; let otherDevices: Device[] = []; let otherAspectRatios: AspectRatio[] = []; if (isValidDeviceList(layout)) { devices = layout; otherDevices = getOtherDevices(layout); } else E{ aspectRatios = layout; otherAspectRatios = getOtherAspectRatios(layout); } const root = config.root; const watcher = LayoutWatcher.reuse({ root }); watcher.onLayout(this.run, { devices, aspectRatios }); if (MH.lengthOf(otherDevices) || MH.lengthOf(otherAspectRatios)) { watcher.onLayout(this.reverse, { devices: otherDevices, aspectRatios: otherAspectRatios, }); } } } /** * @category Layout * @interface */ export type LayoutTriggerConfig = TriggerConfig & { /** * The {@link DeviceSpec} or {@link AspectRatioSpec} to use. Required. * See {@link Watchers/LayoutWatcher.OnLayoutOptions | OnLayoutOptions} for * accepted formats. * * Actions will be "done" when the layout of the root matches the given spec * and "undone" otherwise. */ layout: DeviceSpec | Device[] | AspectRatioSpec | AspectRatio[]; /** * The root to use for the {@link LayoutWatcher}. * See {@link Watchers/LayoutWatcher.LayoutWatcherConfig | LayoutWatcherConfig} * * @defaultValue {@link LayoutWatcher} default, the viewport */ root?: HTMLElement | null; }; // -------------------- const newConfigValidator: WidgetConfigValidatorFunc< Omit<LayoutTriggerConfig, "layout"> > = (element) => { return { root: async (key, value) => { const root = MH.isLiteralString(value) ? await waitForReferenceElement(value, element) : undefined; Iif (root && !MH.isHTMLElement(root)) { throw MH.usageError("root must be HTMLElement"); } return root; }, }; }; |