@dative-gpi/foundation-shared-components 1.0.179-utils-composable → 1.0.180-fix-utils-composable

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.
@@ -1,8 +1,11 @@
1
+ export * from "./useAccessibilityPreferences";
1
2
  export * from "./useAddress";
2
3
  export * from "./useAutocomplete";
3
4
  export * from "./useBreakpoints";
4
5
  export * from "./useColors";
6
+ export * from "./useCountUp";
5
7
  export * from "./useDebounce";
8
+ export * from "./useElementVisibility"
6
9
  export * from "./useMapLayers";
7
10
  export * from "./useRules";
8
11
  export * from "./useSlots";
@@ -1,7 +1,20 @@
1
+ import { ref, onMounted } from 'vue';
2
+
1
3
  export function useAccessibilityPreferences() {
2
- const prefersReducedMotion = window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches || false;
4
+ const prefersReducedMotion = ref(false);
5
+
6
+ onMounted(() => {
7
+ prefersReducedMotion.value = window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches || false;
8
+
9
+ const mediaQuery = window.matchMedia?.("(prefers-reduced-motion: reduce)");
10
+ if (mediaQuery?.addEventListener) {
11
+ mediaQuery.addEventListener('change', (e) => {
12
+ prefersReducedMotion.value = e.matches;
13
+ });
14
+ }
15
+ });
3
16
 
4
17
  return {
5
18
  prefersReducedMotion
6
19
  };
7
- }
20
+ }
@@ -1,94 +1,104 @@
1
- import { ref, computed, onBeforeUnmount, watch } from 'vue';
1
+ import { ref, computed, onBeforeUnmount, watch, type Ref, type ComputedRef, isRef } from 'vue';
2
2
  import { useAccessibilityPreferences } from './useAccessibilityPreferences';
3
3
 
4
+ // Fonction utilitaire pour obtenir la valeur d'une référence ou d'une valeur simple
5
+ function unrefValue<T>(value: T | Ref<T> | ComputedRef<T>): T {
6
+ return isRef(value) ? value.value : value;
7
+ }
8
+
4
9
  export function useCountUp(options: {
5
- value: number | string,
6
- duration?: number,
7
- countUp?: boolean,
8
- pad?: number,
9
- startOnVisible?: boolean,
10
+ value: number | string | Ref<number | string> | ComputedRef<number | string>,
11
+ duration?: number | Ref<number>,
12
+ countUp?: boolean | Ref<boolean>,
13
+ pad?: number | Ref<number>,
14
+ startOnVisible?: boolean | Ref<boolean>,
10
15
  easing?: (t: number) => number
11
16
  }) {
12
- const {
13
- value,
14
- duration = 800,
15
- countUp = true,
16
- pad = 2,
17
- startOnVisible = true,
18
- easing = easeOutCubic
19
- } = options;
20
-
21
17
  const { prefersReducedMotion } = useAccessibilityPreferences();
18
+
19
+ // Internal states
22
20
  const current = ref(0);
23
21
  const rafId = ref(0);
24
22
  const hasAnimated = ref(false);
25
-
26
- // Easing function
23
+
24
+ // Default easing function
27
25
  function easeOutCubic(t: number) {
28
26
  return 1 - Math.pow(1 - t, 3);
29
27
  }
30
-
31
- // Target value computation
28
+
29
+ // Get options with their default values
30
+ const easing = options.easing || easeOutCubic;
31
+
32
+ // Calculate derived values
32
33
  const target = computed(() => {
33
- const n = Number(value);
34
+ const rawValue = unrefValue(options.value);
35
+ const n = Number(rawValue);
34
36
  return Number.isFinite(n) ? Math.trunc(n) : 0;
35
37
  });
36
38
 
37
- // Formatted display value
38
39
  const displayText = computed(() => {
39
- const s = String(countUp ? current.value : target.value);
40
- return pad > 0 ? s.padStart(pad, "0") : s;
40
+ const showCountUp = unrefValue(options.countUp ?? true);
41
+ const padLength = unrefValue(options.pad ?? 2);
42
+
43
+ const s = String(showCountUp ? current.value : target.value);
44
+ return padLength > 0 ? s.padStart(padLength, "0") : s;
41
45
  });
42
46
 
43
- // Animation function
47
+ // Main animation function
44
48
  function animate(from: number, to: number, animDuration: number) {
45
49
  cancelAnimationFrame(rafId.value);
46
- const start = performance.now();
50
+ const startTime = performance.now();
47
51
 
48
52
  const step = (now: number) => {
49
- const t = Math.min(1, (now - start) / animDuration);
50
- const v = from + (to - from) * easing(t);
51
- current.value = Math.round(v);
52
- if (t < 1) {
53
+ const elapsed = now - startTime;
54
+ const progress = Math.min(1, elapsed / animDuration);
55
+
56
+ const easedProgress = easing(progress);
57
+ const currentValue = from + (to - from) * easedProgress;
58
+
59
+ current.value = Math.round(currentValue);
60
+
61
+ if (progress < 1) {
53
62
  rafId.value = requestAnimationFrame(step);
54
63
  }
55
64
  };
56
65
 
57
66
  rafId.value = requestAnimationFrame(step);
58
67
  }
59
-
60
- // Start animation
68
+
69
+ // Start the animation
61
70
  function start() {
62
- if (!countUp) {
63
- current.value = target.value;
64
- return;
65
- }
71
+ const shouldCountUp = unrefValue(options.countUp ?? true);
66
72
 
67
- if (prefersReducedMotion) {
73
+ if (!shouldCountUp || prefersReducedMotion.value) {
74
+ // If animation is disabled or reduced motion preference is set, jump to final value
68
75
  current.value = target.value;
69
76
  return;
70
77
  }
71
78
 
72
- animate(current.value, target.value, duration);
79
+ const animDuration = unrefValue(options.duration ?? 800);
80
+ animate(current.value, target.value, animDuration);
73
81
  hasAnimated.value = true;
74
82
  }
75
-
76
- // Clean up
77
- onBeforeUnmount(() => {
78
- cancelAnimationFrame(rafId.value);
79
- });
80
-
81
- // The restart method can be useful when the value changes.
83
+
84
+ // Restart the animation (useful for value changes)
82
85
  function restart() {
83
- if (hasAnimated.value || !startOnVisible) {
86
+ const startWhenVisible = unrefValue(options.startOnVisible ?? true);
87
+
88
+ if (!startWhenVisible || hasAnimated.value) {
84
89
  start();
85
90
  }
86
91
  }
87
-
88
- // Monitor changes in value
89
- watch(() => value, () => {
92
+
93
+ // Watch for value changes to restart the animation
94
+ watch(() => unrefValue(options.value), () => {
90
95
  restart();
91
96
  });
97
+
98
+ // Clean up ongoing animations on unmount
99
+ onBeforeUnmount(() => {
100
+ cancelAnimationFrame(rafId.value);
101
+ });
92
102
 
93
103
  return {
94
104
  current,
@@ -14,6 +14,7 @@ export function useElementVisibility(element: HTMLElement | null, options: {
14
14
 
15
15
  onMounted(() => {
16
16
  if (!element) {
17
+ console.warn('useElementVisibility: No element provided');
17
18
  return;
18
19
  }
19
20
 
@@ -36,4 +37,4 @@ export function useElementVisibility(element: HTMLElement | null, options: {
36
37
  return {
37
38
  isVisible
38
39
  };
39
- }
40
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dative-gpi/foundation-shared-components",
3
3
  "sideEffects": false,
4
- "version": "1.0.179-utils-composable",
4
+ "version": "1.0.180-fix-utils-composable",
5
5
  "description": "",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -10,8 +10,8 @@
10
10
  "author": "",
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
- "@dative-gpi/foundation-shared-domain": "1.0.179-utils-composable",
14
- "@dative-gpi/foundation-shared-services": "1.0.179-utils-composable"
13
+ "@dative-gpi/foundation-shared-domain": "1.0.180-fix-utils-composable",
14
+ "@dative-gpi/foundation-shared-services": "1.0.180-fix-utils-composable"
15
15
  },
16
16
  "peerDependencies": {
17
17
  "@dative-gpi/bones-ui": "^1.0.0",
@@ -35,5 +35,5 @@
35
35
  "sass": "1.71.1",
36
36
  "sass-loader": "13.3.2"
37
37
  },
38
- "gitHead": "c2d2ca5e8ea8a151a41f26e08e75f5369cd876e2"
38
+ "gitHead": "b8bbf51892bfcd61e7fdd2d89b4378203ab5cf6d"
39
39
  }