ab-nextjs-components 0.1.9 โ†’ 0.1.11

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.
Files changed (49) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/ab-brand/index.tsx +30 -1
  3. package/ab-brand/styles.module.css +40 -1
  4. package/ab-button/helpers.ts +90 -0
  5. package/ab-button/index.tsx +164 -87
  6. package/ab-button/styles.module.css +79 -1
  7. package/ab-carousel/helpers.ts +146 -0
  8. package/ab-carousel/index.tsx +643 -0
  9. package/ab-carousel/styles.module.css +206 -0
  10. package/ab-chip/index.tsx +75 -0
  11. package/ab-chip/styles.module.css +87 -0
  12. package/ab-collapsible/index.tsx +30 -1
  13. package/ab-collapsible/styles.module.css +40 -1
  14. package/ab-flow-diagram/helpers.ts +77 -0
  15. package/ab-flow-diagram/index.tsx +152 -0
  16. package/ab-flow-diagram/styles.module.css +158 -0
  17. package/ab-icon/builtin.ts +61 -0
  18. package/ab-icon/helpers.ts +102 -0
  19. package/ab-icon/index.tsx +77 -16
  20. package/ab-icon/styles.module.css +53 -9
  21. package/ab-icon-button/index.tsx +110 -46
  22. package/ab-icon-button/styles.module.css +28 -10
  23. package/ab-install-snippet/helpers.ts +62 -0
  24. package/ab-install-snippet/index.tsx +170 -0
  25. package/ab-install-snippet/styles.module.css +104 -0
  26. package/ab-like-button/index.tsx +33 -1
  27. package/ab-like-button/styles.module.css +41 -6
  28. package/ab-linear-progress/index.tsx +5 -1
  29. package/ab-linear-progress/styles.module.css +8 -24
  30. package/ab-locale-switcher/index.tsx +213 -0
  31. package/ab-locale-switcher/styles.module.css +127 -0
  32. package/ab-pill/index.tsx +74 -0
  33. package/ab-progress-chips/index.tsx +30 -1
  34. package/ab-progress-chips/styles.module.css +40 -1
  35. package/ab-searchbar/index.tsx +33 -2
  36. package/ab-searchbar/styles.module.css +41 -5
  37. package/ab-splash/index.tsx +3 -1
  38. package/ab-splash/styles.module.css +2 -5
  39. package/ab-theme-toggle/index.tsx +188 -0
  40. package/ab-theme-toggle/styles.module.css +89 -0
  41. package/css/dialog.css +38 -3
  42. package/index.ts +41 -0
  43. package/package.json +33 -17
  44. package/server/ab-brand/index.tsx +29 -1
  45. package/server/ab-brand/styles.module.css +40 -1
  46. package/server/ab-button/index.tsx +96 -79
  47. package/server/ab-button/styles.module.css +79 -1
  48. package/server/ab-icon/index.tsx +68 -16
  49. package/server/ab-icon/styles.module.css +53 -9
@@ -0,0 +1,104 @@
1
+ /*
2
+ * @license MIT
3
+ * ~~~~~~~~~~~~
4
+ * ab-nextjs-components
5
+ * ~~~~~~~~~~~~
6
+ * Copyright (c) 2026 Abraham Ukachi. The abElements Project.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the 'Software'), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ *
26
+ * @project: ab-nextjs-components
27
+ * @name: AbInstallSnippet - Styles
28
+ * @file: ab-install-snippet/styles.module.css
29
+ * @type: CSS module
30
+ * @authors: Abraham Ukachi <abrahamukachi@gmail.com>
31
+ */
32
+
33
+ /*
34
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35
+ * MOTTO: We'll always do more ๐Ÿ˜œ!!!
36
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
37
+ */
38
+
39
+ /* ๐Ÿ“ฆ box */
40
+ .root {
41
+ display: grid; /* ๐Ÿ“ */
42
+ gap: 0.5rem; /* ๐Ÿงฑ */
43
+ padding: 0.75rem; /* ๐Ÿงฑ */
44
+ border-radius: 0.875rem; /* ๐Ÿ”ฒ */
45
+ border: 1px solid var(--ab-color-outline-variant, rgb(0 0 0 / 0.12)); /* ๐Ÿ–Š๏ธ */
46
+ background: var(--ab-color-surface-container, transparent); /* ๐ŸŽจ */
47
+ max-width: 100%; /* ๐Ÿ“ */
48
+ }
49
+
50
+ /* ๐Ÿ—‚๏ธ tabs row */
51
+ .tabs {
52
+ display: flex; /* ๐Ÿ“ */
53
+ gap: 0.25rem; /* ๐Ÿงฑ */
54
+ }
55
+
56
+ /* ๐Ÿ”˜ tab */
57
+ .tab {
58
+ min-height: 28px; /* ๐Ÿ“ โ‰ฅ 24px target */
59
+ padding: 0.25rem 0.625rem; /* ๐Ÿงฑ */
60
+ border: 0; /* ๐Ÿงน */
61
+ border-radius: 9999px; /* ๐Ÿ’Š */
62
+ background: transparent; /* ๐Ÿซฅ */
63
+ color: var(--ab-color-on-surface-variant, currentColor); /* ๐ŸŽจ */
64
+ font: inherit; /* ๐Ÿ”ค */
65
+ font-size: 0.8125rem; /* ๐Ÿ”ก */
66
+ cursor: pointer; /* ๐Ÿ‘† */
67
+ }
68
+ .tab[aria-selected='true'] {
69
+ color: var(--ab-color-bronze-text, #8A6440); /* โ™ฟ AA small text */
70
+ background: color-mix(in srgb, var(--ab-color-bronze, #A67C52) 12%, transparent); /* ๐ŸŽจ */
71
+ font-weight: 600; /* ๐Ÿ’ช */
72
+ }
73
+ .tab:focus-visible {
74
+ outline: 2px solid var(--ab-focus-ring, var(--ab-color-primary, #A67C52)); /* ๐Ÿ’ */
75
+ outline-offset: 2px; /* โ†”๏ธ */
76
+ }
77
+
78
+ /* ๐Ÿงพ command row */
79
+ .panel {
80
+ display: flex; /* ๐Ÿ“ */
81
+ align-items: center; /* โ†•๏ธ */
82
+ gap: 0.5rem; /* ๐Ÿงฑ */
83
+ min-width: 0; /* ๐Ÿšซ overflow */
84
+ }
85
+ .prompt {
86
+ opacity: 0.5; /* ๐ŸŒซ๏ธ */
87
+ font-family: var(--ab-font-mono, ui-monospace, monospace); /* ๐Ÿ”ค */
88
+ }
89
+ .code {
90
+ flex: 1; /* โ†”๏ธ */
91
+ overflow-x: auto; /* ๐Ÿ“œ long commands scroll */
92
+ white-space: nowrap; /* ๐Ÿšซ wrap */
93
+ font-family: var(--ab-font-mono, ui-monospace, monospace); /* ๐Ÿ”ค */
94
+ font-size: 0.875rem; /* ๐Ÿ”ก */
95
+ }
96
+ .copy {
97
+ flex: none; /* ๐Ÿšซ squash */
98
+ }
99
+
100
+ /* ๐Ÿ™ˆ visually hidden */
101
+ .srOnly {
102
+ position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; /* ๐Ÿ“ */
103
+ overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; /* โœ‚๏ธ */
104
+ }
@@ -5,17 +5,48 @@
5
5
  * ~~~~~~~~~~~~
6
6
  * Copyright (c) 2026 Abraham Ukachi. The abElements Project.
7
7
  *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the 'Software'), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ *
8
26
  * @project: ab-nextjs-components
9
27
  * @name: AbLikeButton - Client (port of lyd-like-button)
10
28
  * @file: ab-like-button/index.tsx
29
+ * @type: TypeScript (React)
30
+ * @authors: Abraham Ukachi <abrahamukachi@gmail.com>
11
31
  */
12
32
 
33
+ /*
34
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35
+ * MOTTO: We'll always do more ๐Ÿ˜œ!!!
36
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
37
+ */
38
+
39
+
13
40
  'use client';
14
41
 
15
42
  import type { CSSProperties, MouseEvent, ReactElement } from 'react';
16
43
  import Link from 'next/link';
17
44
  import clsx from 'clsx';
18
45
  import styles from './styles.module.css';
46
+ import { AB_MOTION } from 'ab-nextjs-animations/motion'; // ๐ŸŽฌ transition class names (ab-nextjs-animations >= 0.2.4)
47
+ import 'ab-nextjs-animations/motion/styles.css'; // ๐ŸŽจ .ab-motion-*
48
+ import { AB_SPIN_CLASS } from 'ab-nextjs-animations/spin'; // ๐ŸŒ€ spinner (ab-nextjs-animations >= 0.2.4)
49
+ import 'ab-nextjs-animations/spin/styles.css'; // ๐ŸŒ€ .ab-spin
19
50
 
20
51
  export interface AbLikeButtonProps {
21
52
  className?: string;
@@ -58,6 +89,7 @@ const AbLikeButton = ({
58
89
  const classNames = clsx(
59
90
  'AbLikeButton',
60
91
  styles.abLikeButton,
92
+ AB_MOTION.interactive, // ๐ŸŽฌ hover / state motion (ab-nextjs-animations)
61
93
  liked && styles.isLiked,
62
94
  isActive && styles.isActive,
63
95
  isNaked && styles.isNaked,
@@ -69,7 +101,7 @@ const AbLikeButton = ({
69
101
  );
70
102
 
71
103
  const inner = isLoading ? (
72
- <span className={styles.abLikeButtonSpinner} aria-hidden />
104
+ <span className={clsx(styles.abLikeButtonSpinner, AB_SPIN_CLASS)} aria-hidden />
73
105
  ) : (
74
106
  <span className={clsx('material-symbols-rounded', styles.abLikeButtonIcon)} aria-hidden>
75
107
  {icon}
@@ -1,3 +1,42 @@
1
+ /*
2
+ * @license MIT
3
+ * ~~~~~~~~~~~~
4
+ * ab-nextjs-components
5
+ * ~~~~~~~~~~~~
6
+ * Copyright (c) 2026 Abraham Ukachi. The abElements Project.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the 'Software'), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ *
26
+ * @project: ab-nextjs-components
27
+ * @name: ab-like-button - styles
28
+ * @file: ab-like-button/styles.module.css
29
+ * @type: CSS Module
30
+ * @authors: Abraham Ukachi <abrahamukachi@gmail.com>
31
+ */
32
+
33
+ /*
34
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35
+ * MOTTO: We'll always do more ๐Ÿ˜œ!!!
36
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
37
+ */
38
+
39
+
1
40
  .abLikeButton {
2
41
  position: relative;
3
42
  display: inline-flex;
@@ -13,7 +52,7 @@
13
52
  color: inherit;
14
53
  text-decoration: none;
15
54
  cursor: pointer;
16
- transition: transform 0.2s ease, background 0.2s ease;
55
+ /* ๐ŸŽฌ motion = .ab-motion-interactive (ab-nextjs-animations) */
17
56
  }
18
57
 
19
58
  .abLikeButton:hover:not(.isDisabled):not(.isNaked) {
@@ -52,9 +91,5 @@
52
91
  border: 2px solid color-mix(in srgb, currentColor 25%, transparent);
53
92
  border-top-color: currentColor;
54
93
  border-radius: 9999px;
55
- animation: abLikeSpin 0.7s linear infinite;
56
- }
57
-
58
- @keyframes abLikeSpin {
59
- to { transform: rotate(360deg); }
94
+ /* ๐ŸŒ€ rotation = .ab-spin (ab-nextjs-animations) */
60
95
  }
@@ -42,6 +42,10 @@ import type { CSSProperties, ReactElement } from 'react';
42
42
  import clsx from 'clsx';
43
43
  // ๐ŸŽจ package-shipped CSS module (no Tailwind scanning of node_modules needed)
44
44
  import styles from './styles.module.css';
45
+ import { AB_MOTION } from 'ab-nextjs-animations/motion'; // ๐ŸŽฌ transition class names (ab-nextjs-animations >= 0.2.4)
46
+ import 'ab-nextjs-animations/motion/styles.css'; // ๐ŸŽจ .ab-motion-*
47
+ import { AB_PROGRESS_SWEEP_CLASS } from 'ab-nextjs-animations/progress-sweep'; // ๐Ÿ” indeterminate sweep (ab-nextjs-animations >= 0.2.4)
48
+ import 'ab-nextjs-animations/progress-sweep/styles.css'; // ๐Ÿ” .ab-progress-sweep
45
49
 
46
50
  // ๐Ÿ“ public props of <AbLinearProgress />
47
51
  export interface AbLinearProgressProps {
@@ -113,7 +117,7 @@ const AbLinearProgress = ({
113
117
  {/* ๐ŸŸซ the moving / filling bar */}
114
118
  <div
115
119
  // ๐Ÿท๏ธ bar class (indeterminate gets the sweep class)
116
- className={clsx(styles.abLinearProgress__bar, isIndeterminate && styles.abLinearProgress__bar_indeterminate)}
120
+ className={clsx(styles.abLinearProgress__bar, isIndeterminate ? [styles.abLinearProgress__bar_indeterminate, AB_PROGRESS_SWEEP_CLASS] : AB_MOTION.transform)} // ๐ŸŽฌ sweep / value tween from ab-nextjs-animations
117
121
  // ๐Ÿ“ determinate width via transform (GPU friendly) โ€” none in indeterminate
118
122
  style={isIndeterminate ? undefined : { transform: `scaleX(${value / 100})` }}
119
123
  />
@@ -72,7 +72,8 @@
72
72
  border-radius: inherit; /* ๐Ÿ’Š same pill */
73
73
  background-color: var(--_ab-lp-color); /* ๐ŸŽจ bar color */
74
74
  transform-origin: left center; /* โฌ…๏ธ grow from the start */
75
- transition: transform 240ms ease-out; /* ๐ŸŽž๏ธ smooth value changes */
75
+ --ab-motion-duration: 240ms; /* ๐ŸŽž๏ธ value tween = .ab-motion-transform (ab-nextjs-animations) */
76
+ --ab-motion-easing: ease-out;
76
77
  }
77
78
 
78
79
  /* โ†”๏ธ RTL: grow from the right */
@@ -80,42 +81,25 @@
80
81
  transform-origin: right center;
81
82
  }
82
83
 
83
- /* ๐Ÿ” indeterminate: a 40% segment sweeping across */
84
+ /* ๐Ÿ” indeterminate: a 40% segment; the sweep itself = .ab-progress-sweep (ab-nextjs-animations, RTL-mirrored) */
84
85
  .abLinearProgress__bar_indeterminate {
85
86
  right: auto; /* ๐Ÿ”“ free the right edge */
86
87
  width: 40%; /* ๐Ÿ“ segment width */
87
- animation: abLinearProgressSweep 1.4s ease-in-out infinite; /* ๐Ÿ” loop */
88
88
  }
89
89
 
90
- /* ๐ŸŽž๏ธ sweep keyframes */
91
- @keyframes abLinearProgressSweep {
92
- 0% { transform: translateX(-100%); } /* โฌ…๏ธ off-screen left */
93
- 100% { transform: translateX(250%); } /* โžก๏ธ off-screen right */
94
- }
95
-
96
- /* โ†”๏ธ RTL indeterminate: anchor on the right and sweep right โ†’ left (mirrored keyframes) */
90
+ /* โ†”๏ธ RTL indeterminate: anchor on the right (the package mirrors the sweep) */
97
91
  :global([dir='rtl']) .abLinearProgress__bar_indeterminate {
98
92
  left: auto; /* ๐Ÿ”“ free the left edge */
99
93
  right: 0; /* ๐Ÿ“Œ start on the right */
100
- animation-name: abLinearProgressSweepRtl; /* โฌ…๏ธ mirrored sweep */
101
- }
102
-
103
- /* ๐ŸŽž๏ธ mirrored sweep keyframes (RTL) */
104
- @keyframes abLinearProgressSweepRtl {
105
- 0% { transform: translateX(100%); } /* โžก๏ธ off-screen right */
106
- 100% { transform: translateX(-250%); } /* โฌ…๏ธ off-screen left */
107
94
  }
108
95
 
109
- /* โ™ฟ reduced motion: no animation, static bar */
96
+ /* โ™ฟ reduced motion: the package stops the sweep + tween; this only lays the static bar out across the track */
110
97
  @media (prefers-reduced-motion: reduce) {
111
- .abLinearProgress__bar {
112
- transition: none; /* ๐Ÿšซ no value tween */
113
- }
114
98
  .abLinearProgress__bar_indeterminate,
115
99
  :global([dir='rtl']) .abLinearProgress__bar_indeterminate {
116
- animation: none; /* ๐Ÿšซ no sweep (LTR + RTL) */
100
+ left: 0; /* ๐Ÿ“Œ */
101
+ right: 0; /* ๐Ÿ“Œ */
117
102
  width: 100%; /* ๐ŸŸซ full static bar */
118
- opacity: 0.55; /* ๐ŸŒซ๏ธ softened so it reads as "busy", not "done" */
119
- transform: none; /* ๐Ÿ“ in place */
120
103
  }
121
104
  }
105
+
@@ -0,0 +1,213 @@
1
+ /*
2
+ * @license MIT
3
+ * ~~~~~~~~~~~~
4
+ * ab-nextjs-components
5
+ * ~~~~~~~~~~~~
6
+ * Copyright (c) 2026 Abraham Ukachi. The abElements Project.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the 'Software'), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ *
26
+ * @project: ab-nextjs-components
27
+ * @name: AbLocaleSwitcher - Client (G7)
28
+ * @file: ab-locale-switcher/index.tsx
29
+ * @type: Component (client) โ€” globe menu button (APG menu + menuitemradio)
30
+ * @authors: Abraham Ukachi <abrahamukachi@gmail.com>
31
+ *
32
+ * Example usage:
33
+ * 1+|> const { switchLocale } = useAbLocaleSwitch() // ๐ŸŒ from ab-nextjs-i18n/navigation
34
+ * -|> <AbLocaleSwitcher label={t('language')} current={locale} locales={[{ code: 'en', label: 'English' }, { code: 'fr', label: 'Franรงais' }]} onSelect={switchLocale} />
35
+ */
36
+
37
+ /*
38
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
39
+ * MOTTO: We'll always do more ๐Ÿ˜œ!!!
40
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
41
+ */
42
+
43
+ 'use client';
44
+
45
+ // โš›๏ธ React
46
+ import type { CSSProperties, KeyboardEvent, ReactNode } from 'react';
47
+ import { useCallback, useEffect, useId, useRef, useState } from 'react';
48
+ // ๐Ÿงต classes
49
+ import clsx from 'clsx';
50
+ // ๐Ÿ”ฃ globe glyph
51
+ import AbIcon from '../ab-icon';
52
+ // ๐ŸŽจ styles
53
+ import styles from './styles.module.css';
54
+
55
+ // ๐ŸŒ one locale option
56
+ export interface AbLocaleOption {
57
+ code: string; // ๐Ÿท๏ธ BCP 47, e.g. 'fr'
58
+ label: string; // ๐Ÿ—ฃ๏ธ endonym, e.g. 'Franรงais' (shown in its own language)
59
+ }
60
+
61
+ // โš™๏ธ props
62
+ export interface AbLocaleSwitcherProps {
63
+ locales: readonly AbLocaleOption[]; // ๐ŸŒ choices
64
+ current: string; // โœ… active locale code
65
+ label: string; // ๐Ÿท๏ธ trigger name, e.g. "Language"
66
+ onSelect?: (code: string) => void; // ๐Ÿ“ฃ button items (e.g. useAbLocaleSwitch().switchLocale)
67
+ getHref?: (code: string) => string; // ๐Ÿ”— link items instead (real navigation, hreflang)
68
+ variant?: 'icon' | 'menu'; // ๐ŸŽจ 'icon' = globe only; 'menu' = globe + current endonym
69
+ align?: 'start' | 'end'; // โ†”๏ธ menu alignment under the trigger
70
+ icon?: ReactNode; // ๐Ÿ–ผ๏ธ override the globe
71
+ className?: string; // ๐Ÿท๏ธ
72
+ style?: CSSProperties; // ๐ŸŽจ
73
+ }
74
+
75
+ /**
76
+ * @name getAbTypeaheadIndex
77
+ * @description ๐Ÿ”ค Index of the next option whose label starts with `char` (wraps), else -1.
78
+ */
79
+ export function getAbTypeaheadIndex(labels: readonly string[], from: number, char: string): number {
80
+ const c = char.toLocaleLowerCase(); // ๐Ÿ”ก
81
+ for (let step = 1; step <= labels.length; step++) {
82
+ const i = (from + step) % labels.length; // ๐Ÿ”
83
+ if (labels[i].toLocaleLowerCase().startsWith(c)) return i; // โœ…
84
+ }
85
+ return -1; // ๐Ÿ™…
86
+ }
87
+
88
+ export function AbLocaleSwitcher(props: AbLocaleSwitcherProps) {
89
+ // ๐Ÿงฉ props
90
+ const { locales, current, label, onSelect, getHref, variant = 'icon', align = 'end', icon, className, style } = props;
91
+ // ๐Ÿšช open?
92
+ const [open, setOpen] = useState(false);
93
+ // ๐Ÿ†” ids
94
+ const base = useId();
95
+ // ๐Ÿ“ refs
96
+ const triggerRef = useRef<HTMLButtonElement>(null);
97
+ const itemRefs = useRef<Array<HTMLElement | null>>([]);
98
+ const rootRef = useRef<HTMLDivElement>(null);
99
+ // ๐Ÿท๏ธ current option
100
+ const active = locales.find((l) => l.code === current);
101
+ const activeIndex = Math.max(0, locales.findIndex((l) => l.code === current));
102
+
103
+ // ๐ŸŽฏ focus an item
104
+ const focusItem = (i: number) => itemRefs.current[i]?.focus();
105
+ // ๐Ÿšช close and return focus to the trigger
106
+ const close = useCallback((returnFocus = true) => {
107
+ setOpen(false); // ๐Ÿšช
108
+ if (returnFocus) triggerRef.current?.focus(); // ๐ŸŽฏ
109
+ }, []);
110
+
111
+ // ๐ŸŽฏ on open, focus the checked item (after paint)
112
+ useEffect(() => {
113
+ if (!open) return; // ๐Ÿ’ค
114
+ const id = requestAnimationFrame(() => focusItem(activeIndex)); // โญ๏ธ
115
+ return () => cancelAnimationFrame(id); // ๐Ÿงน
116
+ }, [open, activeIndex]);
117
+
118
+ // ๐Ÿ–ฑ๏ธ click outside closes (no focus steal)
119
+ useEffect(() => {
120
+ if (!open) return; // ๐Ÿ’ค
121
+ const onDown = (e: PointerEvent) => { if (!rootRef.current?.contains(e.target as Node)) close(false); }; // ๐Ÿšช
122
+ document.addEventListener('pointerdown', onDown); // ๐Ÿ‘‚
123
+ return () => document.removeEventListener('pointerdown', onDown); // ๐Ÿงน
124
+ }, [open, close]);
125
+
126
+ // โŒจ๏ธ trigger: ArrowDown/Up/Enter/Space open the menu
127
+ const onTriggerKey = (e: KeyboardEvent<HTMLButtonElement>) => {
128
+ if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { e.preventDefault(); setOpen(true); } // โฌ‡๏ธ
129
+ };
130
+
131
+ // โŒจ๏ธ menu keys
132
+ const onMenuKey = (e: KeyboardEvent<HTMLUListElement>) => {
133
+ const i = itemRefs.current.findIndex((el) => el === document.activeElement); // ๐Ÿ“
134
+ const n = locales.length; // ๐Ÿ”ข
135
+ if (e.key === 'ArrowDown') { e.preventDefault(); focusItem((i + 1) % n); } // โฌ‡๏ธ
136
+ else if (e.key === 'ArrowUp') { e.preventDefault(); focusItem((i - 1 + n) % n); } // โฌ†๏ธ
137
+ else if (e.key === 'Home') { e.preventDefault(); focusItem(0); } // โฎ๏ธ
138
+ else if (e.key === 'End') { e.preventDefault(); focusItem(n - 1); } // โญ๏ธ
139
+ else if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); close(); } // ๐Ÿšช
140
+ else if (e.key === 'Tab') { close(false); } // โ‡ฅ let Tab move on
141
+ else if (e.key.length === 1 && /\S/.test(e.key)) { // ๐Ÿ”ค typeahead
142
+ const j = getAbTypeaheadIndex(locales.map((l) => l.label), Math.max(i, 0), e.key);
143
+ if (j >= 0) focusItem(j);
144
+ }
145
+ };
146
+
147
+ // โœ… choose
148
+ const choose = (code: string) => {
149
+ onSelect?.(code); // ๐Ÿ“ฃ
150
+ close(); // ๐Ÿšช
151
+ };
152
+
153
+ return (
154
+ <div ref={rootRef} className={clsx('AbLocaleSwitcher', styles.root, className)} style={style} data-ab-part="locale-switcher" data-align={align}>
155
+ {/* ๐ŸŒ trigger */}
156
+ <button
157
+ ref={triggerRef} // ๐Ÿ“
158
+ id={`${base}-trigger`} // ๐Ÿ†”
159
+ type="button" // ๐Ÿงพ
160
+ className={clsx(styles.trigger, variant === 'menu' && styles.withText)} // ๐ŸŽจ
161
+ aria-haspopup="menu" // โ™ฟ
162
+ aria-expanded={open} // โ™ฟ
163
+ aria-controls={`${base}-menu`} // ๐Ÿ”—
164
+ aria-label={variant === 'icon' ? `${label}: ${active?.label ?? current}` : undefined} // ๐Ÿท๏ธ "Language: Franรงais"
165
+ title={label} // ๐Ÿ’ฌ
166
+ onClick={() => setOpen((o) => !o)} // ๐Ÿ–ฑ๏ธ
167
+ onKeyDown={onTriggerKey} // โŒจ๏ธ
168
+ >
169
+ <span aria-hidden="true">{icon ?? <AbIcon name="language" size={20} />}</span>
170
+ {variant === 'menu' ? <span lang={current}>{active?.label ?? current}</span> : null /* ๐Ÿ—ฃ๏ธ endonym */}
171
+ </button>
172
+ {/* ๐Ÿ“œ menu */}
173
+ <ul
174
+ id={`${base}-menu`} // ๐Ÿ†”
175
+ role="menu" // โ™ฟ
176
+ aria-labelledby={`${base}-trigger`} // ๐Ÿ”—
177
+ hidden={!open} // ๐Ÿ™ˆ
178
+ className={styles.menu} // ๐ŸŽจ
179
+ onKeyDown={onMenuKey} // โŒจ๏ธ
180
+ >
181
+ {locales.map((l, i) => {
182
+ const checked = l.code === current; // โœ…
183
+ const common = {
184
+ ref: (el: HTMLElement | null) => { itemRefs.current[i] = el; }, // ๐Ÿ“
185
+ role: 'menuitemradio' as const, // โ™ฟ
186
+ 'aria-checked': checked, // โœ…
187
+ tabIndex: -1, // โŒจ๏ธ focus managed by arrows
188
+ lang: l.code, // ๐Ÿ—ฃ๏ธ pronounced in its own language
189
+ className: styles.item, // ๐ŸŽจ
190
+ };
191
+ return (
192
+ <li key={l.code} role="none">
193
+ {getHref ? (
194
+ // ๐Ÿ”— real navigation (works with middle-click, crawlers see hreflang)
195
+ <a {...common} href={getHref(l.code)} hrefLang={l.code} onClick={() => choose(l.code)}>
196
+ <span className={styles.check} aria-hidden="true">{checked ? 'โœ“' : ''}</span>{l.label}
197
+ </a>
198
+ ) : (
199
+ // ๐Ÿ”˜ callback item
200
+ <button {...common} type="button" onClick={() => choose(l.code)}>
201
+ <span className={styles.check} aria-hidden="true">{checked ? 'โœ“' : ''}</span>{l.label}
202
+ </button>
203
+ )}
204
+ </li>
205
+ );
206
+ })}
207
+ </ul>
208
+ </div>
209
+ );
210
+ }
211
+
212
+ // ๐Ÿ“ฆ default export
213
+ export default AbLocaleSwitcher;
@@ -0,0 +1,127 @@
1
+ /*
2
+ * @license MIT
3
+ * ~~~~~~~~~~~~
4
+ * ab-nextjs-components
5
+ * ~~~~~~~~~~~~
6
+ * Copyright (c) 2026 Abraham Ukachi. The abElements Project.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the 'Software'), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ *
26
+ * @project: ab-nextjs-components
27
+ * @name: AbLocaleSwitcher - Styles
28
+ * @file: ab-locale-switcher/styles.module.css
29
+ * @type: CSS module
30
+ * @authors: Abraham Ukachi <abrahamukachi@gmail.com>
31
+ */
32
+
33
+ /*
34
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35
+ * MOTTO: We'll always do more ๐Ÿ˜œ!!!
36
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
37
+ */
38
+
39
+ /* ๐Ÿ“ฆ anchor for the menu */
40
+ .root {
41
+ position: relative; /* ๐Ÿ“Œ */
42
+ display: inline-block; /* ๐Ÿ“ */
43
+ }
44
+
45
+ /* ๐ŸŒ trigger (40px round, 44px on coarse pointers) */
46
+ .trigger {
47
+ display: inline-flex; /* ๐Ÿ“ */
48
+ align-items: center; /* โ†•๏ธ */
49
+ justify-content: center; /* โ†”๏ธ */
50
+ gap: 0.375rem; /* ๐Ÿงฑ */
51
+ min-width: 40px; /* ๐Ÿ“ */
52
+ height: 40px; /* ๐Ÿ“ */
53
+ padding: 0 0.5rem; /* ๐Ÿงฑ */
54
+ border: 0; /* ๐Ÿงน */
55
+ border-radius: 9999px; /* ๐Ÿ’Š */
56
+ background: transparent; /* ๐Ÿซฅ */
57
+ color: inherit; /* ๐ŸŽจ */
58
+ font: inherit; /* ๐Ÿ”ค */
59
+ cursor: pointer; /* ๐Ÿ‘† */
60
+ }
61
+ .withText {
62
+ padding: 0 0.75rem; /* ๐Ÿงฑ room for the endonym */
63
+ }
64
+ .trigger:hover,
65
+ .trigger[aria-expanded='true'] {
66
+ background: color-mix(in srgb, currentColor 10%, transparent); /* ๐ŸŒซ๏ธ */
67
+ }
68
+ .trigger:focus-visible,
69
+ .item:focus-visible {
70
+ outline: 2px solid var(--ab-focus-ring, var(--ab-color-primary, #A67C52)); /* ๐Ÿ’ */
71
+ outline-offset: 2px; /* โ†”๏ธ */
72
+ }
73
+ @media (pointer: coarse) {
74
+ .trigger { min-width: 44px; height: 44px; } /* ๐Ÿ‘† mobile target */
75
+ }
76
+
77
+ /* ๐Ÿ“œ menu */
78
+ .menu {
79
+ position: absolute; /* ๐Ÿ“Œ */
80
+ top: calc(100% + 0.25rem); /* โฌ‡๏ธ */
81
+ inset-inline-end: 0; /* โ†”๏ธ align end (RTL-aware) */
82
+ z-index: 50; /* ๐Ÿงฑ above content */
83
+ min-width: 11rem; /* ๐Ÿ“ */
84
+ margin: 0; /* ๐Ÿงน */
85
+ padding: 0.25rem; /* ๐Ÿงฑ */
86
+ list-style: none; /* ๐Ÿงน */
87
+ border-radius: 0.75rem; /* ๐Ÿ”ฒ */
88
+ border: 1px solid var(--ab-color-outline-variant, rgb(0 0 0 / 0.12)); /* ๐Ÿ–Š๏ธ */
89
+ background: var(--ab-color-surface, #fff); /* ๐ŸŽจ */
90
+ box-shadow: 0 8px 24px rgb(0 0 0 / 0.12); /* ๐ŸŒซ๏ธ */
91
+ }
92
+ .root[data-align='start'] .menu {
93
+ inset-inline-start: 0; /* โ†”๏ธ */
94
+ inset-inline-end: auto; /* ๐Ÿงน */
95
+ }
96
+ .menu[hidden] {
97
+ display: none; /* ๐Ÿ™ˆ */
98
+ }
99
+
100
+ /* ๐Ÿ—ฃ๏ธ item */
101
+ .item {
102
+ display: flex; /* ๐Ÿ“ */
103
+ align-items: center; /* โ†•๏ธ */
104
+ gap: 0.5rem; /* ๐Ÿงฑ */
105
+ width: 100%; /* โ†”๏ธ */
106
+ min-height: 40px; /* ๐Ÿ“ */
107
+ padding: 0 0.75rem; /* ๐Ÿงฑ */
108
+ border: 0; /* ๐Ÿงน */
109
+ border-radius: 0.5rem; /* ๐Ÿ”ฒ */
110
+ background: transparent; /* ๐Ÿซฅ */
111
+ color: inherit; /* ๐ŸŽจ */
112
+ font: inherit; /* ๐Ÿ”ค */
113
+ text-align: start; /* โ†”๏ธ */
114
+ text-decoration: none; /* ๐Ÿงน links */
115
+ cursor: pointer; /* ๐Ÿ‘† */
116
+ }
117
+ .item:hover,
118
+ .item:focus-visible {
119
+ background: color-mix(in srgb, currentColor 8%, transparent); /* ๐ŸŒซ๏ธ */
120
+ }
121
+ .item[aria-checked='true'] {
122
+ font-weight: 600; /* ๐Ÿ’ช */
123
+ }
124
+ .check {
125
+ width: 1em; /* ๐Ÿ“ fixed column */
126
+ color: var(--ab-color-bronze-text, #8A6440); /* โ™ฟ */
127
+ }