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.
- package/CHANGELOG.md +36 -0
- package/ab-brand/index.tsx +30 -1
- package/ab-brand/styles.module.css +40 -1
- package/ab-button/helpers.ts +90 -0
- package/ab-button/index.tsx +164 -87
- package/ab-button/styles.module.css +79 -1
- package/ab-carousel/helpers.ts +146 -0
- package/ab-carousel/index.tsx +643 -0
- package/ab-carousel/styles.module.css +206 -0
- package/ab-chip/index.tsx +75 -0
- package/ab-chip/styles.module.css +87 -0
- package/ab-collapsible/index.tsx +30 -1
- package/ab-collapsible/styles.module.css +40 -1
- package/ab-flow-diagram/helpers.ts +77 -0
- package/ab-flow-diagram/index.tsx +152 -0
- package/ab-flow-diagram/styles.module.css +158 -0
- package/ab-icon/builtin.ts +61 -0
- package/ab-icon/helpers.ts +102 -0
- package/ab-icon/index.tsx +77 -16
- package/ab-icon/styles.module.css +53 -9
- package/ab-icon-button/index.tsx +110 -46
- package/ab-icon-button/styles.module.css +28 -10
- package/ab-install-snippet/helpers.ts +62 -0
- package/ab-install-snippet/index.tsx +170 -0
- package/ab-install-snippet/styles.module.css +104 -0
- package/ab-like-button/index.tsx +33 -1
- package/ab-like-button/styles.module.css +41 -6
- package/ab-linear-progress/index.tsx +5 -1
- package/ab-linear-progress/styles.module.css +8 -24
- package/ab-locale-switcher/index.tsx +213 -0
- package/ab-locale-switcher/styles.module.css +127 -0
- package/ab-pill/index.tsx +74 -0
- package/ab-progress-chips/index.tsx +30 -1
- package/ab-progress-chips/styles.module.css +40 -1
- package/ab-searchbar/index.tsx +33 -2
- package/ab-searchbar/styles.module.css +41 -5
- package/ab-splash/index.tsx +3 -1
- package/ab-splash/styles.module.css +2 -5
- package/ab-theme-toggle/index.tsx +188 -0
- package/ab-theme-toggle/styles.module.css +89 -0
- package/css/dialog.css +38 -3
- package/index.ts +41 -0
- package/package.json +33 -17
- package/server/ab-brand/index.tsx +29 -1
- package/server/ab-brand/styles.module.css +40 -1
- package/server/ab-button/index.tsx +96 -79
- package/server/ab-button/styles.module.css +79 -1
- package/server/ab-icon/index.tsx +68 -16
- 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
|
+
}
|
package/ab-like-button/index.tsx
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
-
/*
|
|
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:
|
|
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
|
-
|
|
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
|
+
}
|