@agencecinq/utils 2.0.0 → 2.0.1

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.
@@ -0,0 +1,19 @@
1
+ declare const html: HTMLElement;
2
+ declare const body: HTMLElement;
3
+ declare const isDebug: boolean;
4
+ declare const scroll: {
5
+ y: number;
6
+ x: number;
7
+ };
8
+ declare const mouse: {
9
+ x: number;
10
+ y: number;
11
+ };
12
+ declare const breakpoints: {
13
+ lg: MediaQueryList;
14
+ xl: MediaQueryList;
15
+ '2xl': MediaQueryList;
16
+ '3xl': MediaQueryList;
17
+ };
18
+ declare const production: boolean;
19
+ export { html, body, isDebug, scroll, mouse, breakpoints, production };
@@ -0,0 +1,8 @@
1
+ export declare const EVENTS: {
2
+ DRAWER_CLOSE: string;
3
+ DRAWER_OPEN: string;
4
+ DRAWER_TOGGLE: string;
5
+ CART_UPDATE: string;
6
+ VARIANT_CHANGE: string;
7
+ };
8
+ export type EventNames = typeof EVENTS[keyof typeof EVENTS];
@@ -0,0 +1,4 @@
1
+ declare function getFocusableElements(container: HTMLElement): HTMLElement[];
2
+ declare function addTrapFocus(container: HTMLElement, elementToFocus?: HTMLElement): void;
3
+ declare function removeTrapFocus(elementToFocus?: HTMLElement | null): void;
4
+ export { addTrapFocus, removeTrapFocus, getFocusableElements };
@@ -3,4 +3,4 @@ export * from './scroll';
3
3
  export * from './throttle';
4
4
  export * from './environment';
5
5
  export * from './focus-trap';
6
- export * from './keycode';
6
+ export * from './keycode';
package/dist/index.js ADDED
@@ -0,0 +1,109 @@
1
+ const O = {
2
+ DRAWER_CLOSE: "drawer-close",
3
+ DRAWER_OPEN: "drawer-open",
4
+ DRAWER_TOGGLE: "drawer-toggle",
5
+ CART_UPDATE: "cart-update",
6
+ VARIANT_CHANGE: "variant-change"
7
+ }, f = (e, t) => {
8
+ let n = null, r = null;
9
+ const s = () => {
10
+ r && e(...r), n = null;
11
+ };
12
+ return (...c) => {
13
+ r = c, n || (n = setTimeout(s, t));
14
+ };
15
+ }, l = document.documentElement, { body: d } = document, N = l.hasAttribute("data-debug"), o = {
16
+ y: 0,
17
+ x: 0
18
+ }, a = {
19
+ x: 0,
20
+ y: 0
21
+ };
22
+ window.addEventListener(
23
+ "pointermove",
24
+ f(({ x: e, y: t }) => {
25
+ a.x = e, a.y = t;
26
+ }, 100),
27
+ { passive: !0 }
28
+ );
29
+ const W = {
30
+ lg: window.matchMedia("(width >= 64rem)"),
31
+ xl: window.matchMedia("(min-width: 1280px)"),
32
+ "2xl": window.matchMedia("(min-width: 1440px)"),
33
+ "3xl": window.matchMedia("(min-width: 1920px)")
34
+ }, k = process.env.NODE_ENV === "production", u = (e, t) => {
35
+ typeof e < "u" && (o.x = e), typeof t < "u" && (o.y = t), window.scrollTo(o.x, o.y);
36
+ };
37
+ function C() {
38
+ const e = l.scrollLeft, t = l.scrollTop, n = d.scrollLeft, r = d.scrollTop;
39
+ o.x = window.scrollX || e || n, o.y = window.scrollY || t || r || 0, l.style.setProperty("overflow", "hidden"), l.style.setProperty("height", "100%"), l.style.setProperty("scroll-padding-top", "0px"), u(o.x, o.y);
40
+ }
41
+ function H(e = 0) {
42
+ let t = !0, n = o.y;
43
+ typeof e == "number" ? n = e : typeof e == "boolean" && e === !1 && (t = !1), l.style.removeProperty("overflow"), l.style.removeProperty("height"), l.style.removeProperty("scroll-padding-top"), t && u(o.x, n);
44
+ }
45
+ const i = {};
46
+ function y(e) {
47
+ return !!(e.offsetWidth || e.offsetHeight || e.getClientRects().length);
48
+ }
49
+ function m(e) {
50
+ if (!e) return [];
51
+ const t = [
52
+ "summary",
53
+ "a[href]",
54
+ "button:enabled",
55
+ '[tabindex]:not([tabindex^="-"])',
56
+ "input:not([type=hidden]):enabled",
57
+ "select:enabled",
58
+ "textarea:enabled",
59
+ "object",
60
+ "iframe",
61
+ "[contenteditable]"
62
+ ].join(",");
63
+ return Array.from(e.querySelectorAll(t)).filter((n) => y(n) && n.getAttribute("tabindex") !== "-1");
64
+ }
65
+ function G(e, t = e) {
66
+ const n = m(e);
67
+ if (n.length === 0) return;
68
+ const r = n[0], s = n[n.length - 1];
69
+ E(), i.keydown = (c) => {
70
+ c.key === "Tab" && (c.shiftKey ? (document.activeElement === r || document.activeElement === e) && (c.preventDefault(), s.focus()) : document.activeElement === s && (c.preventDefault(), r.focus()));
71
+ }, document.addEventListener("keydown", i.keydown), t.focus(), t instanceof HTMLInputElement && ["search", "text", "email", "url"].includes(t.type) && t.value && t.setSelectionRange(0, t.value.length);
72
+ }
73
+ function E(e = null) {
74
+ i.keydown && document.removeEventListener("keydown", i.keydown), e && e.focus();
75
+ }
76
+ const p = 8, w = 9, h = 13, b = 16, A = 27, x = 32, g = 33, R = 34, v = 35, P = 36, S = 37, T = 38, D = 39, L = 40, _ = 46, M = {
77
+ BACKSPACE: p,
78
+ TAB: w,
79
+ ENTER: h,
80
+ SHIFT: b,
81
+ ESCAPE: A,
82
+ SPACE: x,
83
+ PAGE_UP: g,
84
+ PAGE_DOWN: R,
85
+ END: v,
86
+ HOME: P,
87
+ ARROW_LEFT: S,
88
+ ARROW_UP: T,
89
+ ARROW_RIGHT: D,
90
+ ARROW_DOWN: L,
91
+ DELETE: _
92
+ };
93
+ export {
94
+ O as EVENTS,
95
+ G as addTrapFocus,
96
+ d as body,
97
+ W as breakpoints,
98
+ C as disableScroll,
99
+ H as enableScroll,
100
+ m as getFocusableElements,
101
+ l as html,
102
+ N as isDebug,
103
+ M as keycode,
104
+ a as mouse,
105
+ k as production,
106
+ E as removeTrapFocus,
107
+ o as scroll,
108
+ f as throttle
109
+ };
@@ -0,0 +1,17 @@
1
+ export declare const keycode: {
2
+ BACKSPACE: number;
3
+ TAB: number;
4
+ ENTER: number;
5
+ SHIFT: number;
6
+ ESCAPE: number;
7
+ SPACE: number;
8
+ PAGE_UP: number;
9
+ PAGE_DOWN: number;
10
+ END: number;
11
+ HOME: number;
12
+ ARROW_LEFT: number;
13
+ ARROW_UP: number;
14
+ ARROW_RIGHT: number;
15
+ ARROW_DOWN: number;
16
+ DELETE: number;
17
+ };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * disableScroll
3
+ *
4
+ * Lock scroll position, but retain settings for later
5
+ *
6
+ * @see http://stackoverflow.com/a/3656618
7
+ */
8
+ export declare function disableScroll(): void;
9
+ export declare function enableScroll(position?: number | boolean | undefined): void;
@@ -0,0 +1,12 @@
1
+ interface ThrottleFunction {
2
+ (func: (...args: any[]) => void, wait: number): (...args: any[]) => void;
3
+ }
4
+ /**
5
+ * Creates a throttled function that only invokes `func` at most once per every `wait` milliseconds.
6
+ *
7
+ * @param func - The function to throttle.
8
+ * @param wait - The number of milliseconds to wait before allowing `func` to be called again.
9
+ * @returns A new throttled function.
10
+ */
11
+ export declare const throttle: ThrottleFunction;
12
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agencecinq/utils",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,6 +17,9 @@
17
17
  "import": "./dist/index.js"
18
18
  }
19
19
  },
20
+ "files": [
21
+ "dist"
22
+ ],
20
23
  "scripts": {
21
24
  "dev": "vite",
22
25
  "build": "vite build"
package/CHANGELOG.md DELETED
@@ -1,13 +0,0 @@
1
- # @agencecinq/utils
2
-
3
- ## 2.0.0
4
-
5
- ### Major Changes
6
-
7
- - Publish on npm
8
-
9
- ## 1.0.0
10
-
11
- ### Major Changes
12
-
13
- - Setup CINQ Shopify monorepo
@@ -1,35 +0,0 @@
1
- import { throttle } from "./throttle";
2
-
3
- const html = document.documentElement;
4
- const { body } = document;
5
- const isDebug = html.hasAttribute('data-debug');
6
-
7
- const scroll = {
8
- y: 0,
9
- x: 0,
10
- };
11
-
12
- const mouse = {
13
- x: 0,
14
- y: 0,
15
- };
16
-
17
- window.addEventListener(
18
- 'pointermove',
19
- throttle(({ x, y }) => {
20
- mouse.x = x;
21
- mouse.y = y;
22
- }, 100),
23
- { passive: true }
24
- );
25
-
26
- const breakpoints = {
27
- lg: window.matchMedia('(width >= 64rem)'),
28
- xl: window.matchMedia('(min-width: 1280px)'),
29
- '2xl': window.matchMedia('(min-width: 1440px)'),
30
- '3xl': window.matchMedia('(min-width: 1920px)'),
31
- };
32
-
33
- const production = 'production' === process.env.NODE_ENV;
34
-
35
- export { html, body, isDebug, scroll, mouse, breakpoints, production };
package/src/events.ts DELETED
@@ -1,9 +0,0 @@
1
- export const EVENTS = {
2
- DRAWER_CLOSE: 'drawer-close',
3
- DRAWER_OPEN: 'drawer-open',
4
- DRAWER_TOGGLE: 'drawer-toggle',
5
- CART_UPDATE: 'cart-update',
6
- VARIANT_CHANGE: 'variant-change',
7
- };
8
-
9
- export type EventNames = typeof EVENTS[keyof typeof EVENTS];
package/src/focus-trap.ts DELETED
@@ -1,87 +0,0 @@
1
- const trapFocusHandlers: {
2
- focusin?: (event: FocusEvent) => void;
3
- focusout?: () => void;
4
- keydown?: (event: KeyboardEvent) => void;
5
- } = {};
6
-
7
- /**
8
- * Filtre les éléments réellement visibles et focalisables
9
- */
10
- function isVisible(el: HTMLElement): boolean {
11
- return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
12
- }
13
-
14
- function getFocusableElements(container: HTMLElement): HTMLElement[] {
15
- if (!container) return [];
16
-
17
- const selector = [
18
- 'summary',
19
- 'a[href]',
20
- 'button:enabled',
21
- '[tabindex]:not([tabindex^="-"])',
22
- 'input:not([type=hidden]):enabled',
23
- 'select:enabled',
24
- 'textarea:enabled',
25
- 'object',
26
- 'iframe',
27
- '[contenteditable]'
28
- ].join(',');
29
-
30
- return Array.from(container.querySelectorAll<HTMLElement>(selector))
31
- .filter(el => isVisible(el) && el.getAttribute('tabindex') !== '-1');
32
- }
33
-
34
- function addTrapFocus(container: HTMLElement, elementToFocus: HTMLElement = container): void {
35
- const elements = getFocusableElements(container);
36
- if (elements.length === 0) return;
37
-
38
- const first = elements[0];
39
- const last = elements[elements.length - 1];
40
-
41
- // Nettoyage avant d'ajouter (pour éviter les doublons)
42
- removeTrapFocus();
43
-
44
- trapFocusHandlers.keydown = (event: KeyboardEvent): void => {
45
- if (event.key !== 'Tab') return;
46
-
47
- if (event.shiftKey) {
48
- // Tab arrière
49
- if (document.activeElement === first || document.activeElement === container) {
50
- event.preventDefault();
51
- last.focus();
52
- }
53
- } else {
54
- // Tab avant
55
- if (document.activeElement === last) {
56
- event.preventDefault();
57
- first.focus();
58
- }
59
- }
60
- };
61
-
62
- document.addEventListener('keydown', trapFocusHandlers.keydown);
63
-
64
- // Focus initial
65
- elementToFocus.focus();
66
-
67
- // Support spécifique pour les inputs texte (sélection du contenu)
68
- if (
69
- elementToFocus instanceof HTMLInputElement &&
70
- ['search', 'text', 'email', 'url'].includes(elementToFocus.type) &&
71
- elementToFocus.value
72
- ) {
73
- elementToFocus.setSelectionRange(0, elementToFocus.value.length);
74
- }
75
- }
76
-
77
- function removeTrapFocus(elementToFocus: HTMLElement | null = null): void {
78
- if (trapFocusHandlers.keydown) {
79
- document.removeEventListener('keydown', trapFocusHandlers.keydown);
80
- }
81
-
82
- if (elementToFocus) {
83
- elementToFocus.focus();
84
- }
85
- }
86
-
87
- export { addTrapFocus, removeTrapFocus, getFocusableElements };
package/src/keycode.ts DELETED
@@ -1,33 +0,0 @@
1
- const BACKSPACE = 8;
2
- const TAB = 9;
3
- const ENTER = 13;
4
- const SHIFT = 16;
5
- const ESCAPE = 27;
6
- const SPACE = 32;
7
- const PAGE_UP = 33;
8
- const PAGE_DOWN = 34;
9
- const END = 35;
10
- const HOME = 36;
11
- const ARROW_LEFT = 37;
12
- const ARROW_UP = 38;
13
- const ARROW_RIGHT = 39;
14
- const ARROW_DOWN = 40;
15
- const DELETE = 46;
16
-
17
- export const keycode = {
18
- BACKSPACE,
19
- TAB,
20
- ENTER,
21
- SHIFT,
22
- ESCAPE,
23
- SPACE,
24
- PAGE_UP,
25
- PAGE_DOWN,
26
- END,
27
- HOME,
28
- ARROW_LEFT,
29
- ARROW_UP,
30
- ARROW_RIGHT,
31
- ARROW_DOWN,
32
- DELETE,
33
- };
package/src/scroll.ts DELETED
@@ -1,76 +0,0 @@
1
- import { html, body, scroll } from "./environment";
2
-
3
-
4
- /**
5
- * Resets the scroll position of the window to the specified coordinates.
6
- *
7
- * @param positionX - The X coordinate to scroll to, as a number. If undefined, the X coordinate will not be changed.
8
- * @param positionY - The Y coordinate to scroll to, as a number. If undefined, the Y coordinate will not be changed.
9
- */
10
- const reset = (positionX: number, positionY: number) => {
11
- // console.log('resetScroll', positionX, positionY)
12
- if ("undefined" !== typeof positionX) {
13
- scroll.x = positionX;
14
- }
15
-
16
- if ("undefined" !== typeof positionY) {
17
- scroll.y = positionY;
18
- }
19
-
20
-
21
- window.scrollTo(scroll.x, scroll.y);
22
- };
23
-
24
- /**
25
- * disableScroll
26
- *
27
- * Lock scroll position, but retain settings for later
28
- *
29
- * @see http://stackoverflow.com/a/3656618
30
- */
31
- export function disableScroll() {
32
- // console.info('disableScroll');
33
-
34
- const documentElementScrollLeft = html.scrollLeft;
35
- const documentElementScrollTop = html.scrollTop;
36
-
37
- const bodyScrollLeft = body.scrollLeft;
38
- const bodyScrollTop = body.scrollTop;
39
-
40
- scroll.x = window.scrollX || documentElementScrollLeft || bodyScrollLeft;
41
- scroll.y = window.scrollY || documentElementScrollTop || bodyScrollTop || 0;
42
-
43
- // console.log('disableScroll',scroll)
44
-
45
- html.style.setProperty("overflow", "hidden");
46
- html.style.setProperty("height", "100%");
47
- html.style.setProperty("scroll-padding-top", "0px");
48
-
49
- // eslint-disable-next-line
50
- reset(scroll.x, scroll.y);
51
- }
52
-
53
- export function enableScroll(position: number | boolean | undefined = 0): void {
54
- // console.info('enableScroll');
55
-
56
- let resumeScroll = true;
57
- let currentPosition: number = scroll.y;
58
-
59
- if (typeof position === "number") {
60
- currentPosition = position;
61
- } else if (typeof position === "boolean" && position === false) {
62
- resumeScroll = false;
63
- }
64
-
65
- // console.log('enableScroll', { scroll, resumeScroll, currentPosition });
66
-
67
- // unlock scroll position
68
- html.style.removeProperty("overflow");
69
- html.style.removeProperty("height");
70
- html.style.removeProperty("scroll-padding-top");
71
-
72
- // resume scroll position if possible
73
- if (resumeScroll) {
74
- reset(scroll.x, currentPosition);
75
- }
76
- }
package/src/throttle.ts DELETED
@@ -1,28 +0,0 @@
1
- interface ThrottleFunction {
2
- (func: (...args: any[]) => void, wait: number): (...args: any[]) => void;
3
- }
4
- /**
5
- * Creates a throttled function that only invokes `func` at most once per every `wait` milliseconds.
6
- *
7
- * @param func - The function to throttle.
8
- * @param wait - The number of milliseconds to wait before allowing `func` to be called again.
9
- * @returns A new throttled function.
10
- */
11
- export const throttle: ThrottleFunction = (func, wait) => {
12
- let timeout: ReturnType<typeof setTimeout> | null = null;
13
- let lastArgs: any[] | null = null;
14
-
15
- const later = () => {
16
- if (lastArgs) {
17
- func(...lastArgs);
18
- }
19
- timeout = null;
20
- };
21
-
22
- return (...args: any[]) => {
23
- lastArgs = args;
24
- if (!timeout) {
25
- timeout = setTimeout(later, wait);
26
- }
27
- };
28
- };
package/vite.config.ts DELETED
@@ -1,19 +0,0 @@
1
- import { defineConfig } from 'vite';
2
- import dts from 'vite-plugin-dts';
3
- import { resolve } from 'path';
4
-
5
- export default defineConfig({
6
- build: {
7
- lib: {
8
- entry: resolve(__dirname, 'src/index.ts'),
9
- name: 'Utils',
10
- fileName: 'index',
11
- formats: ['es']
12
- }
13
- },
14
- plugins: [
15
- dts({
16
- insertTypesEntry: true,
17
- })
18
- ]
19
- });