@mantine-vue/hooks 3.3.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/index.cjs CHANGED
@@ -2521,6 +2521,10 @@ function useRadialMove(onChange, { step = .01, onChangeEnd, onScrubStart, onScru
2521
2521
  //#endregion
2522
2522
  //#region src/use-scroll-spy/use-scroll-spy.ts
2523
2523
  var scrollSpyId = 0;
2524
+ function resolveHeadingNode(heading, selector, index) {
2525
+ if (heading.isConnected) return heading;
2526
+ return (heading.id ? document.getElementById(heading.id) : null) ?? document.querySelectorAll(selector)[index] ?? heading;
2527
+ }
2524
2528
  function getActiveElement(rects, offset = 0) {
2525
2529
  if (rects.length === 0) return -1;
2526
2530
  return rects.reduce((closest, rect, index) => Math.abs(closest.position - offset) < Math.abs(rect.y - offset) ? closest : {
@@ -2543,11 +2547,12 @@ function useScrollSpy(options = {}) {
2543
2547
  if (typeof document === "undefined") return;
2544
2548
  const getDepth = options.getDepth ?? ((element) => Number(element.tagName[1]));
2545
2549
  const getValue = options.getValue ?? ((element) => element.textContent || "");
2546
- data.value = Array.from(document.querySelectorAll((0, vue.toValue)(options.selector) ?? "h1, h2, h3, h4, h5, h6")).map((heading) => ({
2550
+ const selector = (0, vue.toValue)(options.selector) ?? "h1, h2, h3, h4, h5, h6";
2551
+ data.value = Array.from(document.querySelectorAll(selector)).map((heading, index) => ({
2547
2552
  depth: getDepth(heading),
2548
2553
  value: getValue(heading),
2549
2554
  id: heading.id || `mantine-scroll-spy-${++scrollSpyId}`,
2550
- getNode: () => heading.id ? document.getElementById(heading.id) ?? heading : heading
2555
+ getNode: () => resolveHeadingNode(heading, selector, index)
2551
2556
  }));
2552
2557
  initialized.value = true;
2553
2558
  updateActive();
@@ -3041,6 +3046,9 @@ function getElementContentSize(element, dimension) {
3041
3046
  function getCssSize(size) {
3042
3047
  return typeof size === "number" ? `${size}px` : size;
3043
3048
  }
3049
+ function isMeasured(size) {
3050
+ return typeof size === "number" && size > 0;
3051
+ }
3044
3052
  function createCollapse({ dimension, getElementSize, expanded, transitionDuration, transitionTimingFunction = "ease", onTransitionEnd, onTransitionStart, keepMounted }) {
3045
3053
  const isExpanded = () => (0, vue.toValue)(expanded);
3046
3054
  const getTransitionDuration = () => (0, vue.toValue)(transitionDuration);
@@ -3054,17 +3062,24 @@ function createCollapse({ dimension, getElementSize, expanded, transitionDuratio
3054
3062
  const styles = (0, vue.ref)(isExpanded() ? {} : collapsedStyles);
3055
3063
  const state = (0, vue.ref)(isExpanded() ? "entered" : "exited");
3056
3064
  const raf = typeof window === "undefined" ? (callback) => callback(0) : window.requestAnimationFrame;
3065
+ let transitionId = 0;
3057
3066
  const mergeStyles = (nextStyles) => {
3058
3067
  styles.value = {
3059
3068
  ...styles.value,
3060
3069
  ...nextStyles
3061
3070
  };
3062
3071
  };
3063
- const measureExpandedSize = (attempt = 0) => {
3064
- if (!isExpanded()) return;
3072
+ const measureExpandedSize = (currentTransitionId, attempt = 0) => {
3073
+ if (transitionId !== currentTransitionId || !isExpanded() || !elementRef.value) return;
3065
3074
  const size = getElementSize(elementRef.value);
3066
3075
  if (size === 0 && elementRef.value?.children.length && attempt < 5) {
3067
- raf(() => measureExpandedSize(attempt + 1));
3076
+ raf(() => measureExpandedSize(currentTransitionId, attempt + 1));
3077
+ return;
3078
+ }
3079
+ if (!isMeasured(size)) {
3080
+ styles.value = {};
3081
+ state.value = "entered";
3082
+ onTransitionEnd?.();
3068
3083
  return;
3069
3084
  }
3070
3085
  mergeStyles({
@@ -3078,8 +3093,12 @@ function createCollapse({ dimension, getElementSize, expanded, transitionDuratio
3078
3093
  };
3079
3094
  (0, vue.watch)(() => isExpanded(), (value, oldValue) => {
3080
3095
  if (value === oldValue) return;
3096
+ transitionId += 1;
3097
+ const currentTransitionId = transitionId;
3098
+ const isCurrentTransition = () => transitionId === currentTransitionId && elementRef.value !== null;
3081
3099
  if (getTransitionDuration() !== 0) onTransitionStart?.();
3082
3100
  if (value) raf(() => {
3101
+ if (!isCurrentTransition()) return;
3083
3102
  state.value = "entering";
3084
3103
  mergeStyles({
3085
3104
  willChange: dimension,
@@ -3088,24 +3107,37 @@ function createCollapse({ dimension, getElementSize, expanded, transitionDuratio
3088
3107
  });
3089
3108
  (0, vue.nextTick)(() => {
3090
3109
  raf(() => {
3091
- raf(() => measureExpandedSize());
3110
+ if (!isCurrentTransition()) return;
3111
+ raf(() => measureExpandedSize(currentTransitionId));
3092
3112
  });
3093
3113
  });
3094
3114
  });
3095
3115
  else raf(() => {
3116
+ if (!isCurrentTransition()) return;
3096
3117
  state.value = "exiting";
3097
3118
  const size = getElementSize(elementRef.value);
3119
+ if (!isMeasured(size)) {
3120
+ styles.value = collapsedStyles;
3121
+ state.value = "exited";
3122
+ onTransitionEnd?.();
3123
+ return;
3124
+ }
3098
3125
  mergeStyles({
3099
3126
  ...getTransitionStyles(size),
3100
3127
  willChange: dimension,
3101
3128
  [dimension]: getCssSize(size)
3102
3129
  });
3103
- raf(() => mergeStyles({
3104
- [dimension]: 0,
3105
- overflow: "hidden"
3106
- }));
3130
+ raf(() => {
3131
+ if (isCurrentTransition()) mergeStyles({
3132
+ [dimension]: 0,
3133
+ overflow: "hidden"
3134
+ });
3135
+ });
3107
3136
  });
3108
3137
  }, { flush: "post" });
3138
+ (0, vue.onBeforeUnmount)(() => {
3139
+ transitionId += 1;
3140
+ });
3109
3141
  const handleTransitionEnd = (event) => {
3110
3142
  if (event.target !== elementRef.value || event.propertyName !== dimension) return;
3111
3143
  if (isExpanded()) {
@@ -3402,9 +3434,11 @@ function useMask(options) {
3402
3434
  };
3403
3435
  const initializeInputValue = (inputNode) => {
3404
3436
  if (!inputNode.value) return false;
3437
+ const isOwnValue = inputNode.value === display;
3438
+ const parse = (slots, slotChar) => isOwnValue ? applyMaskToRaw(rawInternal, slots, slotChar, options.transform) : processInput(inputNode.value, slots, slotChar);
3405
3439
  const { slots: initialSlots, slotChar: initialSlotChar } = getResolvedOptions(options, "");
3406
- const { slots: resolvedSlots, slotChar } = getResolvedOptions(options, extractRaw(processInput(inputNode.value, initialSlots, initialSlotChar), initialSlots));
3407
- const reprocessed = processInput(inputNode.value, resolvedSlots, slotChar);
3440
+ const { slots: resolvedSlots, slotChar } = getResolvedOptions(options, extractRaw(parse(initialSlots, initialSlotChar), initialSlots));
3441
+ const reprocessed = parse(resolvedSlots, slotChar);
3408
3442
  const newRaw = extractRaw(reprocessed, resolvedSlots);
3409
3443
  const showSlots = options.alwaysShowMask || isFocused;
3410
3444
  const showOnFocus = options.showMaskOnFocus !== false;