All files / utils css-alter.ts

89.95% Statements 224/249
77.04% Branches 47/61
92.3% Functions 48/52
87.62% Lines 177/202

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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924                                                        94x 94x       94x             94x 94x 94x 94x                           94x         196x     196x 196x 58x 58x     196x 146x 146x     196x                                                                                                 94x           199x   199x 56x     196x 193x   15x     178x 178x   178x 50x         128x 128x 128x     128x                             94x 7x                   94x 14x                 94x 5x                   94x 60x                     94x 3x                   94x 26x                 94x 3x                   94x 85x                   94x 2x                       94x 6x                       94x 2x                   94x 6x                 94x 108x             94x 35x             94x 701x                 94x 9x 14x                 94x 9x             94x 2032x             94x             94x 499x             94x                 94x       28x             94x                   94x 8x 16x                     94x                 94x       2x                 94x                       94x 3561x                           94x 10x 10x                 94x                   94x 1262x             94x                       94x       1155x               94x                 94x               94x                       94x 441x               94x                 94x               94x                   94x 332x             94x               94x 571x             94x               94x               94x             94x       1396x             94x             94x 105x             94x                     94x     2x 2x 2x                                     94x     2x 2x                     94x                                   94x                           94x 133x   133x   1x   1x       1x                 94x 245x 245x 30x     242x 242x             94x           94x           94x                                                                                 94x                 120x       120x 120x 386x 386x       386x 87x   299x   299x 299x     386x 87x   299x                 94x                       94x   94x         94x 196x 196x 16x     180x 360x 360x 183x         94x 199x 199x 79x 79x     199x 199x   183x 183x     178x     193x       199x    
/**
 * @module Utils
 *
 * @categoryDescription DOM: Querying
 * These functions query the style, attributes or other aspects of elements, but
 * could lead to forced layout if not scheduled using {@link waitForMeasureTime}.
 *
 * @categoryDescription DOM: Querying (optimized)
 * These functions query the style, attributes or other aspects of elements in
 * an optimized way. Functions that could cause a forced layout use
 * {@link waitForMeasureTime} and so are asynchronous. Functions that can
 * perform the check without forcing a re-layout are synchronous.
 *
 * @categoryDescription Style: Altering
 * These functions transition an element from one CSS class to another, but
 * could lead to forced layout if not scheduled using {@link waitForMutateTime}.
 * If a delay is supplied, then the transition is "scheduled" and if the
 * opposite transition is executed before the scheduled one, the original one
 * is cancelled. See {@link transitionElement} for an example.
 *
 * @categoryDescription Style: Altering (optimized)
 * These functions transition an element from one CSS class to another in an
 * optimized way using {@link waitForMutateTime} and so are asynchronous.
 * If a delay is supplied, then the transition is "scheduled" and if the
 * opposite transition is executed before the scheduled one, the original one
 * is cancelled. See {@link transitionElement} for an example.
 */
 
import * as MC from "@lisn/globals/minification-constants";
import * as MH from "@lisn/globals/minification-helpers";
 
import { DOMElement, FlexDirection } from "@lisn/globals/types";
 
import {
  waitForMeasureTime,
  waitForMutateTime,
  asyncMeasurerFor,
  asyncMutatorFor,
  waitForSubsequentMutateTime,
} from "@lisn/utils/dom-optimize";
import { isDOMElement } from "@lisn/utils/dom-query";
import { isValidNum, roundNumTo } from "@lisn/utils/math";
import { waitForDelay } from "@lisn/utils/tasks";
import { camelToKebabCase, splitOn } from "@lisn/utils/text";
 
/**
 * Removes the given `fromCls` class and adds the given `toCls` class to the
 * element.
 *
 * Unlike {@link https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/replace | DOMTokenList:replace},
 * this will always add `toCls` even if `fromCls` isn't in the element's class list.
 *
 * @returns True if there was a change made (class removed or added), false
 * otherwise.
 *
 * @category Style: Altering
 */
export const transitionElementNow = (
  element: Element,
  fromCls: string,
  toCls: string,
) => {
  cancelCSSTransitions(element, fromCls, toCls);
 
  // Avoid triggering MutationObserver unnecessarily.
  let didChange = false;
  if (hasClass(element, fromCls)) {
    didChange = true;
    removeClassesNow(element, fromCls);
  }
 
  if (!hasClass(element, toCls)) {
    addClassesNow(element, toCls);
    didChange = true;
  }
 
  return didChange;
};
 
/**
 * Like {@link transitionElementNow} except it will {@link waitForMutateTime},
 * and optionally a delay, and it finally awaits for the effective style's
 * transition-duration.
 *
 * If a delay is supplied, then the transition is "scheduled" and if the
 * opposite transition is executed before the scheduled one, this one is
 * cancelled.
 *
 * @example
 *
 * - {@link showElement} with delay of 100 schedules `lisn-hide` -> `lisn-show`
 *   in 100ms
 * - then if {@link hideElementNow} is called, or a scheduled
 *   {@link hideElement} completes  before that timer runs out, this call to
 *   {@link showElement} aborts
 *
 * ```javascript
 * hideElement(someElement, 10);
 * // this will be aborted in 10ms when the scheduled hideElement above
 * // completes
 * showElement(someElement, 100);
 * ```
 *
 * ```javascript
 * // this will be aborted in 10ms when the hideElement that will be scheduled
 * // below completes
 * showElement(someElement, 100);
 * hideElement(someElement, 10);
 * ```
 *
 * ```javascript
 * // this will be aborted immediately by hideElementNow that runs straight
 * // afterwards
 * showElement(someElement, 100);
 * hideElementNow(someElement);
 * ```
 *
 * ```javascript
 * hideElementNow(someElement);
 * // this will NOT be aborted because hideElementNow has completed already
 * showElement(someElement, 100);
 * ```
 *
 * @category Style: Altering (optimized)
 */
export const transitionElement = async (
  element: Element,
  fromCls: string,
  toCls: string,
  delay = 0,
) => {
  const thisTransition = scheduleCSSTransition(element, toCls);
 
  if (delay) {
    await waitForDelay(delay);
  }
 
  await waitForMutateTime();
  if (thisTransition._isCancelled()) {
    // it has been overridden by a later transition
    return false;
  }
 
  const didChange = transitionElementNow(element, fromCls, toCls);
  thisTransition._finish();
 
  if (!didChange) {
    return false;
  }
 
  // Await for the transition duration so that caller awaiting on us knows when
  // it's complete.
  const transitionDuration = await getMaxTransitionDuration(element);
  if (transitionDuration) {
    await waitForDelay(transitionDuration);
  }
 
  return true;
};
 
/**
 * Transitions an element from class `lisn-undisplay` (which applies `display:
 * none`) to `lisn-display` (no style associated with this).
 *
 * The difference between this and simply removing the `lisn-undisplay` class
 * is that previously scheduled transitions to `lisn-undisplay` will be
 * cancelled.
 *
 * @see {@link transitionElementNow}
 *
 * @category Style: Altering
 */
export const displayElementNow = (element: Element) =>
  transitionElementNow(element, MC.PREFIX_UNDISPLAY, MC.PREFIX_DISPLAY);
 
/**
 * Like {@link displayElementNow} except it will {@link waitForMutateTime}, and
 * optionally a delay.
 *
 * @see {@link transitionElement}
 *
 * @category Style: Altering (optimized)
 */
export const displayElement = (element: Element, delay = 0) =>
  transitionElement(element, MC.PREFIX_UNDISPLAY, MC.PREFIX_DISPLAY, delay);
 
/**
 * The opposite of {@link displayElementNow}.
 *
 * @see {@link transitionElementNow}
 *
 * @category Style: Altering
 */
export const undisplayElementNow = (element: Element) =>
  transitionElementNow(element, MC.PREFIX_DISPLAY, MC.PREFIX_UNDISPLAY);
 
/**
 * Like {@link undisplayElementNow} except it will {@link waitForMutateTime},
 * and optionally a delay.
 *
 * @see {@link transitionElement}
 *
 * @category Style: Altering (optimized)
 */
export const undisplayElement = (element: Element, delay = 0) =>
  transitionElement(element, MC.PREFIX_DISPLAY, MC.PREFIX_UNDISPLAY, delay);
 
/**
 * Transitions an element from class `lisn-hide` (which makes the element
 * hidden) to `lisn-show` (which shows it). These classes have CSS
 * transitions applied so the element is faded into and out of view.
 *
 * @see {@link transitionElementNow}.
 *
 * @category Style: Altering
 */
export const showElementNow = (element: Element) =>
  transitionElementNow(element, MC.PREFIX_HIDE, MC.PREFIX_SHOW);
 
/**
 * Like {@link showElementNow} except it will {@link waitForMutateTime}, and
 * optionally a delay.
 *
 * @see {@link transitionElement}
 *
 * @category Style: Altering (optimized)
 */
export const showElement = (element: Element, delay = 0) =>
  transitionElement(element, MC.PREFIX_HIDE, MC.PREFIX_SHOW, delay);
 
/**
 * The opposite of {@link showElementNow}.
 *
 * @see {@link transitionElementNow}
 *
 * @category Style: Altering
 */
export const hideElementNow = (element: Element) =>
  transitionElementNow(element, MC.PREFIX_SHOW, MC.PREFIX_HIDE);
 
/**
 * Like {@link hideElementNow} except it will {@link waitForMutateTime}, and
 * optionally a delay.
 *
 * @see {@link transitionElement}
 *
 * @category Style: Altering (optimized)
 */
export const hideElement = (element: Element, delay = 0) =>
  transitionElement(element, MC.PREFIX_SHOW, MC.PREFIX_HIDE, delay);
 
/**
 * If {@link isElementUndisplayed}, it will {@link displayElementNow},
 * otherwise it will {@link undisplayElementNow}.
 *
 * @see {@link transitionElementNow}
 *
 * @category Style: Altering
 */
export const toggleDisplayElementNow = (element: Element) =>
  isElementUndisplayed(element)
    ? displayElementNow(element)
    : undisplayElementNow(element);
 
/**
 * Like {@link toggleDisplayElementNow} except it will {@link waitForMutateTime},
 * and optionally a delay.
 *
 * @see {@link transitionElement}
 *
 * @category Style: Altering (optimized)
 */
export const toggleDisplayElement = (element: Element, delay = 0) =>
  isElementUndisplayed(element)
    ? displayElement(element, delay)
    : undisplayElement(element, delay);
 
/**
 * If {@link isElementHidden}, it will {@link showElementNow}, otherwise
 * {@link hideElementNow}.
 *
 * @see {@link transitionElementNow}
 *
 * @category Style: Altering
 */
export const toggleShowElementNow = (element: Element) =>
  isElementHidden(element) ? showElementNow(element) : hideElementNow(element);
 
/**
 * Like {@link toggleShowElementNow} except it will {@link waitForMutateTime}, and
 * optionally a delay.
 *
 * @see {@link transitionElement}
 *
 * @category Style: Altering (optimized)
 */
export const toggleShowElement = (element: Element, delay = 0) =>
  isElementHidden(element)
    ? showElement(element, delay)
    : hideElement(element, delay);
 
/**
 * Returns true if the element's class list contains `lisn-hide`.
 *
 * @category DOM: Querying (optimized)
 */
export const isElementHidden = (element: Element) =>
  hasClass(element, MC.PREFIX_HIDE);
 
/**
 * Returns true if the element's class list contains `lisn-undisplay`.
 *
 * @category DOM: Querying (optimized)
 */
export const isElementUndisplayed = (element: Element) =>
  hasClass(element, MC.PREFIX_UNDISPLAY);
 
/**
 * Returns true if the element's class list contains the given class.
 *
 * @category DOM: Querying (optimized)
 */
export const hasClass = (element: Element, className: string) =>
  MH.classList(element).contains(className);
 
/**
 * Returns true if the element's class list contains all of the given classes.
 *
 * @since v1.2.0
 *
 * @category DOM: Querying (optimized)
 */
export const hasAllClasses = (element: Element, ...classNames: string[]) =>
  MH.lengthOf(classNames) > 0 &&
  !MH.some(classNames, (className) => !hasClass(element, className));
 
/**
 * Returns true if the element's class list contains any of the given classes.
 *
 * @since v1.2.0
 *
 * @category DOM: Querying (optimized)
 */
export const hasAnyClass = (element: Element, ...classNames: string[]) =>
  MH.some(classNames, (className) => hasClass(element, className));
 
/**
 * Adds the given classes to the element.
 *
 * @category Style: Altering
 */
export const addClassesNow = (element: Element, ...classNames: string[]) =>
  MH.classList(element).add(...classNames);
 
/**
 * Like {@link addClassesNow} except it will {@link waitForMutateTime}.
 *
 * @category Style: Altering (optimized)
 */
export const addClasses = asyncMutatorFor(addClassesNow);
 
/**
 * Removes the given classes to the element.
 *
 * @category Style: Altering
 */
export const removeClassesNow = (element: Element, ...classNames: string[]) =>
  MH.remove(MH.classList(element), ...classNames);
 
/**
 * Like {@link removeClassesNow} except it will {@link waitForMutateTime}.
 *
 * @category Style: Altering (optimized)
 */
export const removeClasses = asyncMutatorFor(removeClassesNow);
 
/**
 * Toggles the given class on the element.
 *
 * @param force See {@link https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/toggle | DOMTokenList:toggle}
 *
 * @category Style: Altering
 */
export const toggleClassNow = (
  element: Element,
  className: string,
  force?: boolean,
) => MH.classList(element).toggle(className, force);
 
/**
 * Like {@link toggleClassNow} except it will {@link waitForMutateTime}.
 *
 * @category Style: Altering (optimized)
 */
export const toggleClass = asyncMutatorFor(toggleClassNow);
 
/**
 * Toggles the given classes on the element. This function does not accept the
 * `force` parameter.
 *
 * @since v1.2.0
 *
 * @category Style: Altering
 */
export const toggleClassesNow = (element: Element, ...classNames: string[]) => {
  for (const cls of classNames) {
    toggleClassNow(element, cls);
  }
};
 
/**
 * Like {@link toggleClassesNow} except it will {@link waitForMutateTime}.
 *
 * @since v1.2.0
 *
 * @category Style: Altering (optimized)
 */
export const toggleClasses = asyncMutatorFor(toggleClassesNow);
 
/**
 * Replaces the given class on the element with a new one.
 *
 * @since v1.2.0
 *
 * @category Style: Altering
 */
export const replaceClassNow = (
  element: Element,
  oldClassName: string,
  newClassName: string,
) => MH.classList(element).replace(oldClassName, newClassName);
 
/**
 * Like {@link replaceClassNow} except it will {@link waitForMutateTime}.
 *
 * @since v1.2.0
 *
 * @category Style: Altering (optimized)
 */
export const replaceClass = asyncMutatorFor(replaceClassNow);
 
// For *Data: to avoid unnecessary type checking that ensures element is
// HTMLElement or SVGElement, use getAttribute instead of dataset.
 
/**
 * Returns the value of the given data attribute. The name of the attribute
 * must _not_ start with `data`. It can be in either camelCase or kebab-case,
 * it is converted as needed.
 *
 * @category DOM: Querying (optimized)
 */
export const getData = (element: Element, name: string) =>
  MH.getAttr(element, MH.prefixData(name));
 
/**
 * Returns the value of the given data attribute as a boolean. Its value is
 * expected to be either blank or "true" (which result in `true`), or "false"
 * (which results in `false`).
 *
 * The name of the attribute must _not_ start with `data`. It can be in either
 * camelCase or kebab-case, it is converted as needed.
 *
 * @since v1.2.0
 *
 * @category DOM: Querying (optimized)
 */
export const getBooleanData = (element: Element, name: string) => {
  const value = getData(element, name);
  return value !== null && value !== "false";
};
 
/**
 * @ignore
 * @deprecated
 *
 * Deprecated alias for {@link getBooleanData}
 */
export const getBoolData = getBooleanData;
 
/**
 * Sets the given data attribute.
 *
 * The name of the attribute must _not_ start with `data`. It can be in either
 * camelCase or kebab-case, it is converted as needed.
 *
 * @category Style: Altering
 */
export const setDataNow = (element: Element, name: string, value: string) =>
  MH.setAttr(element, MH.prefixData(name), value);
 
/**
 * Like {@link setDataNow} except it will {@link waitForMutateTime}.
 *
 * @category Style: Altering (optimized)
 */
export const setData = asyncMutatorFor(setDataNow);
 
/**
 * Sets the given data attribute with value "true" (default) or "false".
 *
 * The name of the attribute must _not_ start with `data`. It can be in either
 * camelCase or kebab-case, it is converted as needed.
 *
 * @since v1.2.0
 *
 * @category Style: Altering
 */
export const setBooleanDataNow = (
  element: Element,
  name: string,
  value = true,
) => MH.setAttr(element, MH.prefixData(name), value + "");
 
/**
 * @ignore
 * @deprecated
 *
 * Deprecated alias for {@link setBooleanData}
 */
export const setBoolDataNow = setBooleanDataNow;
 
/**
 * Like {@link setBooleanDataNow} except it will {@link waitForMutateTime}.
 *
 * @since v1.2.0
 *
 * @category Style: Altering (optimized)
 */
export const setBooleanData = asyncMutatorFor(setBooleanDataNow);
 
/**
 * @ignore
 * @deprecated
 *
 * Deprecated alias for {@link setBoolean}
 */
export const setBoolData = setBooleanData;
 
/**
 * Sets the given data attribute with value "false".
 *
 * The name of the attribute must _not_ start with `data`. It can be in either
 * camelCase or kebab-case, it is converted as needed.
 *
 * @since v1.2.0
 *
 * @category Style: Altering
 */
export const unsetBooleanDataNow = (element: Element, name: string) =>
  MH.unsetAttr(element, MH.prefixData(name));
 
/**
 * @ignore
 * @deprecated
 *
 * Deprecated alias for {@link unsetBooleanNow}
 */
export const unsetBoolDataNow = unsetBooleanDataNow;
 
/**
 * Like {@link unsetBooleanDataNow} except it will {@link waitForMutateTime}.
 *
 * @since v1.2.0
 *
 * @category Style: Altering (optimized)
 */
export const unsetBooleanData = asyncMutatorFor(unsetBooleanDataNow);
 
/**
 * @ignore
 * @deprecated
 *
 * Deprecated alias for {@link unsetBoolean}
 */
export const unsetBoolData = unsetBooleanData;
 
/**
 * Deletes the given data attribute.
 *
 * The name of the attribute must _not_ start with `data`. It can be in either
 * camelCase or kebab-case, it is converted as needed.
 *
 * @category Style: Altering
 */
export const delDataNow = (element: Element, name: string) =>
  MH.delAttr(element, MH.prefixData(name));
 
/**
 * Like {@link delDataNow} except it will {@link waitForMutateTime}.
 *
 * @category Style: Altering (optimized)
 */
export const delData = asyncMutatorFor(delDataNow);
 
/**
 * Returns the value of the given property from the computed style of the
 * element.
 *
 * @category DOM: Querying
 */
export const getComputedStylePropNow = (element: Element, prop: string) =>
  getComputedStyle(element).getPropertyValue(prop);
 
/**
 * Like {@link getComputedStylePropNow} except it will {@link waitForMeasureTime}.
 *
 * @category DOM: Querying (optimized)
 */
export const getComputedStyleProp = asyncMeasurerFor(getComputedStylePropNow);
 
/**
 * Returns the value of the given property from the inline style of the
 * element.
 *
 * @category DOM: Querying
 */
export const getStylePropNow = (element: Element, prop: string) =>
  (element as DOMElement).style?.getPropertyValue(prop);
 
/**
 * Like {@link getStylePropNow} except it will {@link waitForMeasureTime}.
 *
 * @category DOM: Querying (optimized)
 */
export const getStyleProp = asyncMeasurerFor(getStylePropNow);
 
/**
 * Sets the given property on the inline style of the element.
 *
 * @category DOM: Altering
 */
export const setStylePropNow = (
  element: Element,
  prop: string,
  value: string,
) => (element as DOMElement).style?.setProperty(prop, value);
 
/**
 * Like {@link setStylePropNow} except it will {@link waitForMutateTime}.
 *
 * @category DOM: Altering (optimized)
 */
export const setStyleProp = asyncMutatorFor(setStylePropNow);
 
/**
 * Deletes the given property on the inline style of the element.
 *
 * @category DOM: Altering
 */
export const delStylePropNow = (element: Element, prop: string) =>
  (element as DOMElement).style?.removeProperty(prop);
 
/**
 * Like {@link delStylePropNow} except it will {@link waitForMutateTime}.
 *
 * @category DOM: Altering (optimized)
 */
export const delStyleProp = asyncMutatorFor(delStylePropNow);
 
/**
 * Returns the flex direction of the given element **if it has a flex layout**.
 *
 * @returns `null` if the element does not have a flex layout.
 *
 * @category DOM: Querying (optimized)
 *
 * @since v1.2.0
 */
export const getFlexDirection = async (
  element: Element,
): Promise<FlexDirection | null> => {
  const displayStyle = await getComputedStyleProp(element, "display");
  if (!displayStyle.includes("flex")) {
    return null;
  }
 
  return (await getComputedStyleProp(
    element,
    "flex-direction",
  )) as FlexDirection;
};
 
/**
 * Returns the flex direction of the given element's parent **if it has a flex
 * layout**.
 *
 * @returns `null` if the element's parent does not have a flex layout.
 *
 * @category DOM: Querying (optimized)
 *
 * @since v1.2.0
 */
export const getParentFlexDirection = async (
  element: Element,
): Promise<FlexDirection | null> => {
  const parent = MH.parentOf(element);
  return parent ? getFlexDirection(parent) : null;
};
 
/**
 * Returns true if the given element has a flex layout. If direction is given,
 * then it also needs to match.
 *
 * @category DOM: Querying (optimized)
 *
 * @since v1.2.0
 */
export const isFlex = async (element: Element, direction?: FlexDirection) => {
  const flexDirection = await getFlexDirection(element);
 
  Iif (direction) {
    return direction === flexDirection;
  }
 
  return flexDirection !== null;
};
 
/**
 * Returns true if the given element's parent has a flex layout. If direction is
 * given, then it also needs to match.
 *
 * @category DOM: Querying (optimized)
 *
 * @since v1.2.0
 */
export const isFlexChild = async (
  element: Element,
  direction?: FlexDirection,
) => {
  const parent = MH.parentOf(element);
  return parent ? isFlex(parent, direction) : false;
};
 
/**
 * In milliseconds.
 *
 * @ignore
 * @internal
 */
export const getMaxTransitionDuration = async (element: Element) => {
  const propVal = await getComputedStyleProp(element, "transition-duration");
 
  return MH.max(
    ...splitOn(propVal, ",", true).map((strValue) => {
      let duration = MH.parseFloat(strValue) || 0;
 
      Iif (strValue === duration + "s") {
        duration *= 1000;
      }
 
      return duration;
    }),
  );
};
 
/**
 * @ignore
 * @internal
 */
export const disableInitialTransition = async (element: Element, delay = 0) => {
  await addClasses(element, MC.PREFIX_TRANSITION_DISABLE);
  if (delay) {
    await waitForDelay(delay);
  }
 
  await waitForSubsequentMutateTime();
  removeClassesNow(element, MC.PREFIX_TRANSITION_DISABLE);
};
 
/**
 * @ignore
 * @internal
 */
export const setHasModal = () => setBooleanData(MH.getBody(), PREFIX_HAS_MODAL);
 
/**
 * @ignore
 * @internal
 */
export const delHasModal = () => delData(MH.getBody(), PREFIX_HAS_MODAL);
 
/**
 * @ignore
 * @internal
 */
export const copyStyle = async (
  fromElement: Element,
  toElement: Element,
  includeComputedProps?: string[],
) => {
  Iif (!isDOMElement(fromElement) || !isDOMElement(toElement)) {
    return;
  }
 
  await waitForMeasureTime();
  const props: Record<string, string> = {};
 
  Iif (includeComputedProps) {
    for (const prop of includeComputedProps) {
      props[prop] = getComputedStylePropNow(fromElement, prop);
    }
  }
 
  const style = fromElement.style; // only inline styles
  for (const prop in style) {
    const value = style.getPropertyValue(prop);
    Iif (value) {
      props[prop] = value;
    }
  }
 
  for (const prop in props) {
    setStyleProp(toElement, prop, props[prop]);
  }
 
  addClasses(toElement, ...MH.classList(fromElement));
};
 
/**
 * If the props keys are in camelCase they are converted to kebab-case
 *
 * If a value is null or undefined, the property is deleted.
 *
 * @ignore
 * @internal
 */
export const setNumericStyleJsVarsNow = (
  element: Element,
  props: CssNumericProps,
  options: {
    _prefix?: string;
    _units?: string;
    _numDecimal?: number;
  } = {},
) => {
  Iif (!isDOMElement(element)) {
    return;
  }
 
  const varPrefix = MH.prefixCssJsVar(options?._prefix ?? "");
  for (const prop in props) {
    const cssPropSuffix = camelToKebabCase(prop);
    const varName = `${varPrefix}${cssPropSuffix}`;
 
    let value: number | null;
 
    if (!isValidNum(props[prop])) {
      value = null;
    } else {
      value = props[prop];
      const thisNumDecimal =
        options?._numDecimal ?? (value > 0 && value < 1 ? 2 : 0);
      value = roundNumTo(value, thisNumDecimal);
    }
 
    if (value === null) {
      delStylePropNow(element, varName);
    } else {
      setStylePropNow(element, varName, value + (options?._units ?? ""));
    }
  }
};
 
/**
 * @ignore
 * @internal
 */
export const setNumericStyleJsVars = asyncMutatorFor(setNumericStyleJsVarsNow);
 
// ----------------------------------------
 
type CssNumericProps = Record<string, number | undefined | null>;
 
type CSSTransition = {
  _cancel: () => undefined;
  _finish: () => undefined;
  _isCancelled: () => boolean;
};
 
const PREFIX_HAS_MODAL = MH.prefixName("has-modal");
 
const scheduledCSSTransitions = MH.newWeakMap<
  Element,
  Record<string, CSSTransition>
>();
 
const cancelCSSTransitions = (element: Element, ...toClasses: string[]) => {
  const scheduledTransitions = scheduledCSSTransitions.get(element);
  if (!scheduledTransitions) {
    return;
  }
 
  for (const toCls of toClasses) {
    const scheduledTransition = scheduledTransitions[toCls];
    if (scheduledTransition) {
      scheduledTransition._cancel();
    }
  }
};
 
const scheduleCSSTransition = (element: Element, toCls: string) => {
  let scheduledTransitions = scheduledCSSTransitions.get(element);
  if (!scheduledTransitions) {
    scheduledTransitions = {};
    scheduledCSSTransitions.set(element, scheduledTransitions);
  }
 
  let isCancelled = false;
  scheduledTransitions[toCls] = {
    _cancel: () => {
      isCancelled = true;
      MH.deleteObjKey(scheduledTransitions, toCls);
    },
    _finish: () => {
      MH.deleteObjKey(scheduledTransitions, toCls);
    },
    _isCancelled: () => {
      return isCancelled;
    },
  };
 
  return scheduledTransitions[toCls];
};