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