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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 2x 2x 2x 94x 1031x 1031x 1031x 1015x 1015x 908x 10x 908x 123x 123x 107x 16x 123x 123x 123x 123x 123x 1x 123x 123x 99x 123x 123x 94x 94x 20x 94x 2x 2x 2x 2x 2x 94x 1031x 1031x 1031x 1031x 94x 1033x 94x 1033x 1033x 1027x 981x 1027x 1004x 1033x 94x 2x 94x 1031x 94x 123x 123x 123x 123x 80x 123x 123x 474x 123x | /** * @module Utils */ import * as MC from "@lisn/globals/minification-constants"; import * as MH from "@lisn/globals/minification-helpers"; import { addClasses, addClassesNow, setDataNow, setStylePropNow, } from "@lisn/utils/css-alter"; import { moveElement, tryWrapContent } from "@lisn/utils/dom-alter"; import { waitForElement } from "@lisn/utils/dom-events"; import { waitForMutateTime } from "@lisn/utils/dom-optimize"; import { camelToKebabCase, objToStrKey } from "@lisn/utils/text"; import { isScrollable, tryGetMainContentElement, fetchMainContentElement, } from "@lisn/utils/scroll"; import { newXWeakMap } from "@lisn/modules/x-map"; /** * @category Overlays * @interface */ export type OverlayOptions = { /** * The parent element to insert the overlay into. * * If not given, then: * - if the overlay is to have a `position: fixed`, then `document.body` is used * - otherwise, * {@link Settings.settings.mainScrollableElementSelector | the main scrolling element} * is used */ parent?: HTMLElement; /** * If set, then it will be assigned as the DOM element ID for the new * overlay. * * Furthermore, the new overlay will be created and will not be saved for * future reuse. * * By default, if `id` is not given, the overlay will be saved, and if * {@link createOverlay} is called again with the same `style`, `data` and * parent, the previous overlay is returned. */ id?: string; /** * Every entry in this object will be set on the `style` of the new overlay. * * **IMPORTANT:** By default overlays are positioned absolutely, so if you need * another positioning, override this here by setting a `position` key in * `style`. If position is either "absolute" (by default or explicitly set) or * "fixed", and none of `top` or `bottom` is given, `top: 0` is set; and * similarly, if none of `left` or `right` is given, `left: 0` is set. */ style?: Record<string, string>; /** * Every entry in this object will be set as a data attribute on the new * overlay. * * Keys can be either kebab-case or camelCase (they will be converted if * needed). Do _not_ include the "data-" prefix which will be added * automatically. E.g. both "foo-bar" and "fooBar" will result in * "data-foo-bar" being set. */ data?: Record<string, string>; }; /** * Returns an existing overlay for this specification. If the overlay was just * created it may not yet be attached to the DOM. * * @category Overlays */ export const getOverlay = (userOptions?: OverlayOptions) => { const options = tryGetOverlayOptions(userOptions); Iif (!options) { return null; } return overlays.get(options._parent)?.get(options._overlayKey) ?? null; }; /** * Creates a new overlay, and inserts it into the DOM as soon as * {@link waitForMutateTime} resolves, or returns an already existing matching * overlay. * * **Note** that if {@link OverlayOptions.id} is set, a new overlay will * _always_ be created. * * @category Overlays */ export const createOverlay = async (userOptions?: OverlayOptions) => { const options = await fetchOverlayOptions(userOptions); const canReuse = !options._id; if (canReuse) { const existingOverlay = overlays .get(options._parent) ?.get(options._overlayKey); if (existingOverlay) { if (!MH.parentOf(existingOverlay)) { // not yet inserted into the DOM, so wait until it is await waitForMutateTime(); } return existingOverlay; } } // Create a new one const overlay = createOnlyOverlay(options); if (canReuse) { // Save it now before awating, so that concurrent requests to create the // same one use it overlays.sGet(options._parent).set(options._overlayKey, overlay); } else { overlay.id = options._id; } const isPercentageHOffset = MH.includes( (options._style.left || "") + (options._style.right || ""), "%", ); const isPercentageVOffset = MH.includes( (options._style.top || "") + (options._style.bottom || ""), "%", ); let needsContentWrapping = false; let parentEl = options._parent; if (isPercentageHOffset || isPercentageVOffset) { needsContentWrapping = (isPercentageHOffset && isScrollable(parentEl, { axis: "x" })) || (isPercentageVOffset && isScrollable(parentEl, { axis: "y" })); } Iif (needsContentWrapping) { // TODO Is it possible to unwrap the children when no longer needing this // overlay? Probably not worth the effort. ViewWatcher doesn't remove old // olverlays anyway. parentEl = await tryWrapContent(parentEl, { _classNames: [ MC.PREFIX_WRAPPER, MC.PREFIX_WRAPPER_CONTENT, PREFIX_WRAPPER, ], _required: true, _requiredBy: "percentage offset view trigger with scrolling root", }); } if (options._style.position === MC.S_ABSOLUTE) { // Ensure parent has non-static positioning addClasses(parentEl, MC.PREFIX_RELATIVE); } await moveElement(overlay, { to: parentEl }); return overlay; }; // ---------------------------------------- type OverlayOptionsInternal = { _parent: HTMLElement; _id: string; _style: Record<string, string>; _data: Record<string, string>; _overlayKey: string; }; const PREFIX_WRAPPER = MH.prefixName("overlay-wrapper"); const overlays = newXWeakMap<HTMLElement, Map<string, HTMLElement>>(() => MH.newMap(), ); const tryGetOverlayOptions = ( userOptions: OverlayOptions | undefined, ): OverlayOptionsInternal | null => { const style = getCssProperties(userOptions?.style); const data = userOptions?.data ?? {}; const parentEl = tryGetParent(userOptions?.parent, style.position); Iif (!parentEl) { return null; } return { _parent: parentEl, _id: userOptions?.id ?? "", _style: style, _data: data, _overlayKey: getOverlayKey(style, data), }; }; const fetchOverlayOptions = async ( userOptions: OverlayOptions | undefined, ): Promise<OverlayOptionsInternal> => { const style = getCssProperties(userOptions?.style); const data = userOptions?.data ?? {}; const parentEl = await fetchParent(userOptions?.parent, style.position); return { _parent: parentEl, _id: userOptions?.id ?? "", _style: style, _data: data, _overlayKey: getOverlayKey(style, data), }; }; const getOverlayKey = ( style: Record<string, string>, data: Record<string, string>, ) => objToStrKey(style) + "|" + objToStrKey(data); const getCssProperties = (style: Record<string, string> | undefined) => { const finalCssProperties: Record<string, string> = MH.merge( style, { position: style?.position || MC.S_ABSOLUTE }, // default ); if ( finalCssProperties.position === MC.S_ABSOLUTE || finalCssProperties.position === MC.S_FIXED ) { if ( MH.isEmpty(finalCssProperties.top) && MH.isEmpty(finalCssProperties.bottom) ) { finalCssProperties.top = "0px"; } if ( MH.isEmpty(finalCssProperties.left) && MH.isEmpty(finalCssProperties.right) ) { finalCssProperties.left = "0px"; } } return finalCssProperties; }; const tryGetParent = ( userSuppliedParent: HTMLElement | undefined | null, position: string, ) => userSuppliedParent ?? (position === MC.S_FIXED ? MH.getBody() : tryGetMainContentElement()); const fetchParent = async ( userSuppliedParent: HTMLElement | undefined | null, position: string, ) => userSuppliedParent ?? (position === MC.S_FIXED ? await waitForElement(MH.getBody) : await fetchMainContentElement()); const createOnlyOverlay = (options: OverlayOptionsInternal) => { const overlay = MH.createElement("div"); addClassesNow(overlay, MH.prefixName("overlay")); const data = options._data; for (const attr of MH.keysOf(data)) { setDataNow(overlay, camelToKebabCase(attr), data[attr]); } const style = options._style; for (const prop of MH.keysOf(style)) { setStylePropNow(overlay, prop, style[prop]); } return overlay; }; |