@modoock/components 1.0.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.
@@ -0,0 +1,78 @@
1
+ import Icon from "./icon";
2
+ import type { IconSize } from "./icon";
3
+
4
+ export interface LoaderProps {
5
+ /** Message displayed under the spinner (also announced to screen readers). */
6
+ message?: string;
7
+ /** Spinner size token (see Iconography). */
8
+ size?: IconSize;
9
+ /**
10
+ * - "overlay" (default): absolute overlay over the nearest positioned
11
+ * (relative) parent — modal body, card, section…
12
+ * - "inline": centered block in the normal flow — no positioning required.
13
+ */
14
+ variant?: "overlay" | "inline";
15
+ /** Covers the whole viewport (fixed) — use for full-page data loading. */
16
+ fullScreen?: boolean;
17
+ className?: string;
18
+ }
19
+
20
+ /**
21
+ * Chargement réutilisable — même visuel partout (page, modale, section…).
22
+ * - `<Loader fullScreen message="…" />` : couvre tout l'écran (page dont les
23
+ * données sont en cours de chargement) ;
24
+ * - `<Loader message="…" />` : superposition dans le parent positionné
25
+ * (`relative`) — corps de modale, carte, section ;
26
+ * - `<Loader variant="inline" message="…" />` : bloc centré dans le flux.
27
+ */
28
+ export default function Loader({
29
+ message,
30
+ size = "large",
31
+ variant = "overlay",
32
+ fullScreen = false,
33
+ className = "",
34
+ }: LoaderProps) {
35
+ const content = (
36
+ <div
37
+ role="status"
38
+ aria-live="polite"
39
+ className="flex flex-col items-center gap-inline"
40
+ >
41
+ <Icon
42
+ name="LoaderCircle"
43
+ size={size}
44
+ variant="primary"
45
+ className="animate-spin"
46
+ aria-hidden="true"
47
+ />
48
+ {message && <p className="text-small text-muted-foreground">{message}</p>}
49
+ <span className="sr-only">{message ?? "Chargement en cours"}</span>
50
+ </div>
51
+ );
52
+
53
+ if (fullScreen) {
54
+ return (
55
+ <div
56
+ className={`fixed inset-0 z-50 flex items-center justify-center bg-background/80 backdrop-blur-sm ${className}`}
57
+ >
58
+ {content}
59
+ </div>
60
+ );
61
+ }
62
+
63
+ if (variant === "inline") {
64
+ return (
65
+ <div className={`flex justify-center py-component ${className}`}>
66
+ {content}
67
+ </div>
68
+ );
69
+ }
70
+
71
+ return (
72
+ <div
73
+ className={`absolute inset-0 z-30 flex items-center justify-center bg-background/80 backdrop-blur-sm ${className}`}
74
+ >
75
+ {content}
76
+ </div>
77
+ );
78
+ }
@@ -0,0 +1,49 @@
1
+ type MetricTone =
2
+ | "default"
3
+ | "primary"
4
+ | "secondary"
5
+ | "success"
6
+ | "warning"
7
+ | "danger"
8
+ | "info"
9
+ | "accent";
10
+
11
+ const TONE_BG: Record<MetricTone, string> = {
12
+ default: "bg-muted",
13
+ primary: "bg-primary",
14
+ secondary: "bg-border-strong",
15
+ success: "bg-success",
16
+ warning: "bg-warning",
17
+ danger: "bg-danger",
18
+ info: "bg-info",
19
+ accent: "bg-accent",
20
+ };
21
+
22
+ /**
23
+ * Barre de métrique compacte avec pourcentage.
24
+ * Réutilisée par les tableaux de métriques et fiches détail.
25
+ */
26
+ export default function MetricBar({
27
+ value,
28
+ tone,
29
+ }: {
30
+ value: number;
31
+ tone: MetricTone;
32
+ }) {
33
+ const width = Math.min(100, Math.max(2, value));
34
+ return (
35
+ <div className="flex items-center gap-inline">
36
+ <div className="h-1.5 w-24 overflow-hidden rounded-full bg-surface-muted">
37
+ <div
38
+ className={`h-full rounded-full ${TONE_BG[tone]}`}
39
+ style={{ width: `${width}%` }}
40
+ />
41
+ </div>
42
+ <span className="text-small text-muted-foreground">
43
+ {value.toFixed(0)} %
44
+ </span>
45
+ </div>
46
+ );
47
+ }
48
+
49
+ export { type MetricTone };
@@ -0,0 +1,177 @@
1
+ "use client";
2
+
3
+ import {
4
+ useEffect,
5
+ useId,
6
+ useRef,
7
+ type KeyboardEvent as ReactKeyboardEvent,
8
+ type ReactNode,
9
+ } from "react";
10
+ import { createPortal } from "react-dom";
11
+ import Icon from "./icon";
12
+ import ClickableButton from "./button";
13
+
14
+ interface ModalProps {
15
+ open: boolean;
16
+ title: string;
17
+ children: ReactNode;
18
+ onClose: () => void;
19
+ footer?: ReactNode;
20
+ primaryAction?: {
21
+ label: string;
22
+ onClick: () => void;
23
+ };
24
+ secondaryAction?: {
25
+ label: string;
26
+ onClick: () => void;
27
+ };
28
+ }
29
+
30
+ /** Selector matching every focusable element the dialog can own. */
31
+ const FOCUSABLE_SELECTOR = [
32
+ "a[href]",
33
+ "button:not([disabled])",
34
+ "textarea:not([disabled])",
35
+ "input:not([disabled])",
36
+ "select:not([disabled])",
37
+ '[tabindex]:not([tabindex="-1"])',
38
+ ].join(", ");
39
+
40
+ /** Returns the focusable elements owned by a given dialog panel. */
41
+ function getFocusables(panel: HTMLElement | null): HTMLElement[] {
42
+ return panel
43
+ ? Array.from(panel.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR))
44
+ : [];
45
+ }
46
+
47
+ export default function Modal({
48
+ open,
49
+ title,
50
+ children,
51
+ onClose,
52
+ footer,
53
+ primaryAction,
54
+ secondaryAction,
55
+ }: ModalProps) {
56
+ // Unique, stable id used to bind the dialog label to its title.
57
+ const titleId = useId();
58
+ const panelRef = useRef<HTMLDivElement>(null);
59
+ // Element that had focus before the modal opened, restored on close.
60
+ const previouslyFocusedRef = useRef<HTMLElement | null>(null);
61
+
62
+ // Lock body scroll while open.
63
+ useEffect(() => {
64
+ if (!open) return;
65
+ const previousOverflow = document.body.style.overflow;
66
+ document.body.style.overflow = "hidden";
67
+ return () => {
68
+ document.body.style.overflow = previousOverflow;
69
+ };
70
+ }, [open]);
71
+
72
+ // Close on Escape.
73
+ useEffect(() => {
74
+ if (!open) return;
75
+ const handleKeyDown = (event: globalThis.KeyboardEvent) => {
76
+ if (event.key === "Escape") onClose();
77
+ };
78
+ document.addEventListener("keydown", handleKeyDown);
79
+ return () => document.removeEventListener("keydown", handleKeyDown);
80
+ }, [open, onClose]);
81
+
82
+ // Move focus into the dialog on open and restore it on close.
83
+ useEffect(() => {
84
+ if (!open) return;
85
+ previouslyFocusedRef.current =
86
+ document.activeElement instanceof HTMLElement
87
+ ? document.activeElement
88
+ : null;
89
+ const focusables = getFocusables(panelRef.current);
90
+ (focusables[0] ?? panelRef.current)?.focus();
91
+ return () => {
92
+ previouslyFocusedRef.current?.focus?.();
93
+ previouslyFocusedRef.current = null;
94
+ };
95
+ }, [open]);
96
+
97
+ if (!open) return null;
98
+
99
+ // Keep Tab / Shift+Tab cycling within the dialog panel.
100
+ const handlePanelKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>) => {
101
+ if (event.key !== "Tab") return;
102
+ const focusables = getFocusables(panelRef.current);
103
+ if (focusables.length === 0) return;
104
+
105
+ const first = focusables[0];
106
+ const last = focusables[focusables.length - 1];
107
+ const active = document.activeElement;
108
+
109
+ if (
110
+ !(event.shiftKey) &&
111
+ (!panelRef.current?.contains(active) || active === last)
112
+ ) {
113
+ event.preventDefault();
114
+ first.focus();
115
+ } else if (
116
+ event.shiftKey &&
117
+ (!panelRef.current?.contains(active) || active === first)
118
+ ) {
119
+ event.preventDefault();
120
+ last.focus();
121
+ }
122
+ };
123
+
124
+ return createPortal(
125
+ <div
126
+ className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4"
127
+ role="dialog"
128
+ aria-modal="true"
129
+ aria-labelledby={titleId}
130
+ onClick={onClose}
131
+ >
132
+ <div
133
+ ref={panelRef}
134
+ className="flex max-h-[90vh] w-full max-w-[560px] flex-col overflow-hidden rounded-large border border-border bg-surface shadow-lg"
135
+ onClick={(event) => event.stopPropagation()}
136
+ onKeyDown={handlePanelKeyDown}
137
+ >
138
+ <div className="flex shrink-0 items-center justify-between border-b border-border px-component py-control">
139
+ <h2 id={titleId} className="text-component-title text-foreground">
140
+ {title}
141
+ </h2>
142
+ <button
143
+ type="button"
144
+ onClick={onClose}
145
+ className="grid size-8 place-items-center rounded-small text-muted-foreground outline-none hover:bg-surface-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-primary [&_svg]:size-[var(--icon-small)] [&_svg]:shrink-0 [&_svg]:text-current"
146
+ aria-label="Fermer"
147
+ >
148
+ <Icon name="X" aria-hidden="true" />
149
+ </button>
150
+ </div>
151
+ <div className="min-h-0 flex-1 overflow-y-auto overscroll-contain px-component py-control">
152
+ {children}
153
+ </div>
154
+ {(footer || primaryAction || secondaryAction) && (
155
+ <div className="flex shrink-0 items-center justify-end gap-inline border-t border-border px-component py-control">
156
+ {secondaryAction && (
157
+ <ClickableButton
158
+ type="button"
159
+ onClick={secondaryAction.onClick}
160
+ variant="ghost"
161
+ >
162
+ {secondaryAction.label}
163
+ </ClickableButton>
164
+ )}
165
+ {footer && footer}
166
+ {primaryAction && (
167
+ <ClickableButton onClick={primaryAction.onClick}>
168
+ {primaryAction.label}
169
+ </ClickableButton>
170
+ )}
171
+ </div>
172
+ )}
173
+ </div>
174
+ </div>,
175
+ document.body,
176
+ );
177
+ }