ab-nextjs-components 0.1.12 → 0.1.14
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 +64 -0
- package/README.md +52 -0
- package/ab-app-bar/index.tsx +210 -0
- package/ab-app-bar/styles.module.css +158 -0
- package/ab-avatar/index.tsx +2 -1
- package/ab-avatar/styles.module.css +32 -0
- package/ab-balloon/index.tsx +134 -23
- package/ab-balloon/styles.module.css +84 -7
- package/ab-bottom-drawer/index.tsx +71 -0
- package/ab-bottom-drawer/styles.module.css +66 -0
- package/ab-brand/index.tsx +1 -1
- package/ab-breadcrumbs/index.tsx +92 -0
- package/ab-breadcrumbs/styles.module.css +54 -0
- package/ab-button/helpers.ts +6 -1
- package/ab-button/index.tsx +23 -4
- package/ab-button/styles.module.css +70 -0
- package/ab-card/index.tsx +89 -0
- package/ab-card/styles.module.css +68 -0
- package/ab-carousel/styles.module.css +1 -1
- package/ab-chip/index.tsx +16 -4
- package/ab-chip/styles.module.css +12 -1
- package/ab-collapsible/index.tsx +1 -1
- package/ab-color/index.tsx +1 -0
- package/ab-color/styles.module.css +32 -0
- package/ab-confirm-drawer/index.tsx +97 -0
- package/ab-confirm-drawer/styles.module.css +41 -0
- package/ab-demo-box/index.tsx +1 -0
- package/ab-demo-box/styles.module.css +32 -0
- package/ab-demo-code/index.tsx +1 -0
- package/ab-demo-code/styles.module.css +32 -0
- package/ab-drawer/index.tsx +307 -0
- package/ab-drawer/styles.module.css +127 -0
- package/ab-drawer/use-ab-drawer.ts +89 -0
- package/ab-drawer-list/index.tsx +118 -0
- package/ab-drawer-list/styles.module.css +65 -0
- package/ab-flow-diagram/styles.module.css +2 -2
- package/ab-icon/builtin.ts +19 -1
- package/ab-icon/helpers.ts +37 -0
- package/ab-icon/index.tsx +14 -8
- package/ab-icon/registrar.tsx +68 -0
- package/ab-icon-button/index.tsx +19 -3
- package/ab-icon-button/styles.module.css +1 -1
- package/ab-image/index.tsx +1 -0
- package/ab-image/styles.module.css +32 -0
- package/ab-info/index.tsx +2 -1
- package/ab-info/styles.module.css +32 -0
- package/ab-input/index.tsx +1 -0
- package/ab-input/styles.module.css +1 -0
- package/ab-install-snippet/index.tsx +51 -29
- package/ab-install-snippet/styles.module.css +50 -4
- package/ab-kbd/index.tsx +65 -0
- package/ab-kbd/styles.module.css +55 -0
- package/ab-key-feature/index.tsx +2 -1
- package/ab-key-feature/styles.module.css +32 -0
- package/ab-like-button/index.tsx +1 -1
- package/ab-link/index.tsx +112 -0
- package/ab-link/locale.ts +53 -0
- package/ab-list/index.tsx +107 -0
- package/ab-list/styles.module.css +82 -0
- package/ab-locale-switcher/index.tsx +59 -10
- package/ab-locale-switcher/styles.module.css +15 -4
- package/ab-main-headline/index.tsx +1 -0
- package/ab-main-headline/styles.module.css +32 -0
- package/ab-menu/index.tsx +6 -5
- package/ab-menu/styles.module.css +32 -0
- package/ab-name/index.tsx +1 -0
- package/ab-name/styles.module.css +32 -0
- package/ab-navbar/index.tsx +54 -46
- package/ab-navbar/styles.module.css +102 -28
- package/ab-navbar/view.tsx +166 -0
- package/ab-package-card/index.tsx +141 -0
- package/ab-package-card/styles.module.css +81 -0
- package/ab-page-paginator/index.tsx +1 -0
- package/ab-page-paginator/styles.module.css +32 -0
- package/ab-page-switcher/index.tsx +1 -0
- package/ab-page-switcher/styles.module.css +32 -0
- package/ab-preview/index.tsx +1 -0
- package/ab-preview/styles.module.css +32 -0
- package/ab-price/index.tsx +1 -0
- package/ab-price/styles.module.css +32 -0
- package/ab-price-tag/index.tsx +1 -0
- package/ab-price-tag/styles.module.css +32 -0
- package/ab-product/index.tsx +1 -0
- package/ab-product/styles.module.css +32 -0
- package/ab-product-item/index.tsx +1 -0
- package/ab-product-item/styles.module.css +32 -0
- package/ab-search-result/index.tsx +1 -0
- package/ab-search-result/styles.module.css +32 -0
- package/ab-search-trigger/index.tsx +157 -0
- package/ab-search-trigger/styles.module.css +96 -0
- package/ab-sidebar/index.tsx +91 -49
- package/ab-sidebar/styles.module.css +179 -31
- package/ab-sidebar/view.tsx +192 -0
- package/ab-switch-back/index.tsx +2 -1
- package/ab-switch-back/styles.module.css +32 -0
- package/ab-tab/index.tsx +2 -1
- package/ab-tab/styles.module.css +32 -0
- package/ab-tabs/index.tsx +1 -0
- package/ab-tabs/styles.module.css +32 -0
- package/ab-test-hello/index.tsx +1 -0
- package/ab-test-hello/styles.module.css +32 -0
- package/ab-text/index.tsx +1 -0
- package/ab-text/styles.module.css +32 -0
- package/ab-theme-toggle/index.tsx +21 -6
- package/ab-theme-toggle/styles.module.css +18 -3
- package/ab-toc/index.tsx +131 -0
- package/ab-toc/styles.module.css +66 -0
- package/ab-version-switcher/index.tsx +143 -0
- package/ab-version-switcher/styles.module.css +76 -0
- package/css/chrome-mq.css +32 -0
- package/css/doodle.css +32 -0
- package/css/logo.css +32 -0
- package/css/spinner.css +32 -0
- package/css/toast.css +32 -0
- package/index.ts +45 -0
- package/package.json +42 -10
- package/server/ab-avatar/index.tsx +2 -1
- package/server/ab-avatar/styles.module.css +32 -0
- package/server/ab-badge/index.tsx +1 -0
- package/server/ab-badge/styles.module.css +32 -0
- package/server/ab-brand/index.tsx +1 -1
- package/server/ab-button/index.tsx +6 -2
- package/server/ab-button/styles.module.css +39 -0
- package/server/ab-icon/index.tsx +13 -8
- package/server/ab-key-feature/index.tsx +2 -1
- package/server/ab-key-feature/styles.module.css +32 -0
- package/server/ab-name/index.tsx +1 -0
- package/server/ab-name/styles.module.css +32 -0
- package/server/ab-nav-link/index.tsx +2 -1
- package/server/ab-nav-link/styles.module.css +32 -0
- package/server/ab-navbar/index.tsx +44 -45
- package/server/ab-polygon/index.tsx +1 -0
- package/server/ab-polygon/styles.module.css +32 -0
- package/server/ab-sidebar/index.tsx +44 -49
- package/server/ab-switch-back/index.tsx +2 -1
- package/server/ab-switch-back/styles.module.css +32 -0
- package/server/ab-test-hello/index.tsx +1 -0
- package/server/ab-test-hello/styles.module.css +32 -0
- package/server/ab-text/index.tsx +1 -0
- package/server/ab-text/styles.module.css +32 -0
- package/server/index.ts +1 -0
- package/styles.css +32 -0
- package/types/index.d.ts +54 -0
- package/server/ab-navbar/styles.module.css +0 -35
- package/server/ab-sidebar/styles.module.css +0 -41
|
@@ -0,0 +1,307 @@
|
|
|
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: AbDrawer (G53)
|
|
28
|
+
* @file: ab-drawer/index.tsx
|
|
29
|
+
* @type: Client component (SSR: in-flow no-JS fallback)
|
|
30
|
+
* @authors: Abraham Ukachi <abrahamukachi@gmail.com>
|
|
31
|
+
*
|
|
32
|
+
* Example usage:
|
|
33
|
+
* 1+|> const drawer = useAbDrawer('main')
|
|
34
|
+
* -|> <button {...drawer.triggerProps}>Filters</button>
|
|
35
|
+
* -|> <AbDrawer {...drawer.drawerProps} side="end" title="Filters">…</AbDrawer>
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/*
|
|
39
|
+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
40
|
+
* MOTTO: We'll always do more 😜!!!
|
|
41
|
+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
'use client';
|
|
46
|
+
|
|
47
|
+
// ⚛️ React
|
|
48
|
+
import { useCallback, useEffect, useId, useRef, useState, type CSSProperties, type ReactElement, type ReactNode } from 'react';
|
|
49
|
+
// 🌀 portal into the scope's drawer layer
|
|
50
|
+
import { createPortal } from 'react-dom';
|
|
51
|
+
// 🧵 classes
|
|
52
|
+
import clsx from 'clsx';
|
|
53
|
+
// 🎨 styles
|
|
54
|
+
import styles from './styles.module.css';
|
|
55
|
+
// 🎬 motion classes (ab-nextjs-animations >= 0.2.4): the slide + the scrim fade, reduced motion included
|
|
56
|
+
import { AB_MOTION } from 'ab-nextjs-animations/motion';
|
|
57
|
+
import 'ab-nextjs-animations/motion/styles.css';
|
|
58
|
+
// 🎛️ the controller hook + scope type
|
|
59
|
+
import { useAbDrawer, type AbDrawerScope, type UseAbDrawer } from './use-ab-drawer';
|
|
60
|
+
|
|
61
|
+
// 📦 re-exports: one import for the whole drawer API
|
|
62
|
+
export { useAbDrawer };
|
|
63
|
+
export type { AbDrawerScope, UseAbDrawer };
|
|
64
|
+
|
|
65
|
+
/** 🧭 where it slides in from. `start` / `end` follow the writing direction (RTL-aware). */
|
|
66
|
+
export type AbDrawerSide = 'top' | 'bottom' | 'left' | 'right' | 'start' | 'end';
|
|
67
|
+
|
|
68
|
+
/** ⚙️ props */
|
|
69
|
+
export interface AbDrawerProps {
|
|
70
|
+
open: boolean; // 🚪 controlled
|
|
71
|
+
onClose: () => void; // ⎋ Escape, scrim click and the close button call it
|
|
72
|
+
scope?: AbDrawerScope; // 🗺️ which box clips it (default `app`)
|
|
73
|
+
side?: AbDrawerSide; // 🧭 default `bottom`
|
|
74
|
+
title?: ReactNode; // 🏷️ a string becomes the `<h2>` that labels the dialog
|
|
75
|
+
'aria-labelledby'?: string; // ♿ your own visible title id (wins over `title`)
|
|
76
|
+
'aria-label'?: string; // ♿ when there is no visible title
|
|
77
|
+
id?: string; // 🆔 the dialog id (`aria-controls` target)
|
|
78
|
+
children?: ReactNode; // 📄 the body (it scrolls on its own)
|
|
79
|
+
footer?: ReactNode; // 🔚 actions row (stays put)
|
|
80
|
+
size?: string; // 📏 width (start/end/left/right) or height (top/bottom), e.g. `360px`
|
|
81
|
+
modal?: boolean; // 🔒 trap focus + make the rest of the scope inert (default `true`)
|
|
82
|
+
closeLabel?: string; // 🌍 default "Close"
|
|
83
|
+
noJsFallback?: boolean; // 🧩 D41: without JS, render the content in flow (default `true`)
|
|
84
|
+
initialFocus?: 'first' | 'panel'; // 🎯 on open: the first control (default) or the panel itself (no ring on the ✕; Tab then enters)
|
|
85
|
+
className?: string; // 🏷️
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** ⏱️ the slide, same as the panes aside (core 0.1.6) */
|
|
89
|
+
export const AB_DRAWER_DURATION = 240;
|
|
90
|
+
|
|
91
|
+
// 🔎 the element each scope is clipped by (core 0.1.6 data-ab-part hooks)
|
|
92
|
+
const SCOPE_SELECTOR: Record<AbDrawerScope, string> = {
|
|
93
|
+
app: '.AbAppLayout', // 🏠 the whole app box
|
|
94
|
+
main: '[data-ab-part="main"]', // 📄 the main pane (not its scroller, so the drawer doesn't scroll away)
|
|
95
|
+
aside: '[data-ab-part="aside"]', // 📑 the aside pane
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 🗺️ getAbDrawerHost — the box a drawer of `scope` is clipped by: the scope element that contains `from`
|
|
100
|
+
* (so two layouts on a page each get their own), else the first one in the document, else `<body>`.
|
|
101
|
+
* Effects / handlers only (it reads the DOM).
|
|
102
|
+
*/
|
|
103
|
+
export function getAbDrawerHost(scope: AbDrawerScope, from?: Element | null): HTMLElement {
|
|
104
|
+
const sel = SCOPE_SELECTOR[scope]; // 🔎
|
|
105
|
+
return from?.closest<HTMLElement>(sel) ?? document.querySelector<HTMLElement>(sel) ?? document.body; // 🎯
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ♿ everything that can take focus inside the panel
|
|
109
|
+
const FOCUSABLE = 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]), [contenteditable="true"]';
|
|
110
|
+
|
|
111
|
+
// 📍 hosts with a drawer layer, ref-counted: `data-ab-drawer-host` stays while ANY drawer still uses the box.
|
|
112
|
+
// CSS (`:where([data-ab-drawer-host])`, zero specificity) gives a static host `position: relative`; no inline
|
|
113
|
+
// styles are written. A core rule on its layout boxes could replace this later.
|
|
114
|
+
const hostCount = new WeakMap<HTMLElement, number>();
|
|
115
|
+
const holdHost = (host: HTMLElement) => { const n = (hostCount.get(host) ?? 0) + 1; hostCount.set(host, n); host.setAttribute('data-ab-drawer-host', ''); };
|
|
116
|
+
const releaseHost = (host: HTMLElement) => {
|
|
117
|
+
const n = (hostCount.get(host) ?? 1) - 1;
|
|
118
|
+
if (n > 0) { hostCount.set(host, n); return; }
|
|
119
|
+
hostCount.delete(host); host.removeAttribute('data-ab-drawer-host'); // 🧹 last one out
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// 🥞 open drawers, newest last: only the top one answers Escape / Tab (a drawer opened from a drawer)
|
|
123
|
+
const openStack: symbol[] = [];
|
|
124
|
+
/** 🥞 is `token` the topmost open drawer? */
|
|
125
|
+
const isTopDrawer = (token: symbol): boolean => openStack[openStack.length - 1] === token;
|
|
126
|
+
|
|
127
|
+
// ♿ reduced-motion check, read once per open / close on purpose: it only picks the unmount delay for THAT
|
|
128
|
+
// toggle (the slide itself is CSS, which follows the media query live), so a listener would buy nothing
|
|
129
|
+
const reducedMotion = (): boolean => typeof window !== 'undefined' && typeof window.matchMedia === 'function' && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 🗄️ AbDrawer (G53) — a panel that slides in from any side, clipped by its **scope** (app / main / aside).
|
|
133
|
+
*
|
|
134
|
+
* - 📐 Rendered into an absolute `inset: 0` layer INSIDE the scope box, so it never makes the document scroll
|
|
135
|
+
* (the D34 rule); its body is its own scroller (`overscroll-behavior: contain`).
|
|
136
|
+
* - ♿ `role="dialog"` + `aria-modal`, labelled by `title` / `aria-labelledby`; focus moves in on open, is
|
|
137
|
+
* trapped (Tab / Shift+Tab), and returns to the opener on close; Escape and a scrim click close it;
|
|
138
|
+
* the rest of the scope is `inert` while it's open (only the scope, unless `scope="app"`).
|
|
139
|
+
* - 🎞️ 240ms slide; no motion and instant open/close under `prefers-reduced-motion`.
|
|
140
|
+
* - 🧱 Scopes need **ab-nextjs-core 0.1.6+** markup (`.AbAppLayout`, `[data-ab-part="main"]`, `[data-ab-part="aside"]`);
|
|
141
|
+
* with older core (or no layout) it falls back to a viewport layer on `<body>`.
|
|
142
|
+
* - 🧩 D41 no-JS: the server renders the content as an in-flow `<section>` (hidden as soon as scripting runs).
|
|
143
|
+
*/
|
|
144
|
+
const AbDrawer = (props: AbDrawerProps): ReactElement => {
|
|
145
|
+
const { open, onClose, scope = 'app', side = 'bottom', title, id, children, footer, size, modal = true, closeLabel = 'Close', noJsFallback = true, initialFocus = 'first', className } = props;
|
|
146
|
+
const auto = useId().replace(/[^A-Za-z0-9_-]/g, ''); // 🆔 SSR-stable
|
|
147
|
+
const dialogId = id ?? `ab-drawer-${auto}`; // 🆔
|
|
148
|
+
const titleId = `${dialogId}-title`; // 🏷️
|
|
149
|
+
const labelledBy = props['aria-labelledby'] ?? (title != null ? titleId : undefined); // ♿
|
|
150
|
+
const anchor = useRef<HTMLDivElement>(null); // 📍 where we are in the tree (picks the right layout)
|
|
151
|
+
const panel = useRef<HTMLDivElement>(null); // 🗄️
|
|
152
|
+
const opener = useRef<Element | null>(null); // ↩️ focus goes back here
|
|
153
|
+
const close = useRef(onClose); // 🔁 latest onClose for the listeners
|
|
154
|
+
useEffect(() => { close.current = onClose; }, [onClose]); // 🔁 synced after render, never during
|
|
155
|
+
|
|
156
|
+
// 🧭 client-only state: the layer we portal into, whether the panel is in the DOM, whether it's slid in
|
|
157
|
+
const [layer, setLayer] = useState<HTMLElement | null>(null);
|
|
158
|
+
const [present, setPresent] = useState(open); // 🧱 mounted (open, or sliding out)
|
|
159
|
+
const [shown, setShown] = useState(false); // 🎞️ slid in
|
|
160
|
+
const [token] = useState(() => Symbol('ab-drawer')); // 🥞 our place in the open stack
|
|
161
|
+
|
|
162
|
+
// 🗺️ find the scope box once we're on the client and make a layer in it
|
|
163
|
+
useEffect(() => {
|
|
164
|
+
const host = getAbDrawerHost(scope, anchor.current); // 🎯
|
|
165
|
+
const el = document.createElement('div'); // 🧱 one layer per drawer (no shared state to clean up)
|
|
166
|
+
el.setAttribute('data-ab-drawer-layer', scope);
|
|
167
|
+
el.className = clsx('AbDrawerLayer', styles.abDrawerLayer);
|
|
168
|
+
if (host === document.body) el.setAttribute('data-host', 'body'); // 📌 no layout: cover the viewport
|
|
169
|
+
host.appendChild(el);
|
|
170
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect -- 🌀 the layer is external DOM that only exists after mount (SSR keeps the in-flow fallback); one extra render on hydration is the point
|
|
171
|
+
setLayer(el);
|
|
172
|
+
return () => { el.remove(); setLayer(null); }; // 🧹
|
|
173
|
+
}, [scope]);
|
|
174
|
+
|
|
175
|
+
// 📍 hold the host (position context) while the panel is in the DOM (open or sliding out)
|
|
176
|
+
useEffect(() => {
|
|
177
|
+
const host = layer?.parentElement;
|
|
178
|
+
if (!present || !host || host === document.body) return; // 📌 body layer is fixed: no context needed
|
|
179
|
+
holdHost(host);
|
|
180
|
+
return () => releaseHost(host);
|
|
181
|
+
}, [present, layer]);
|
|
182
|
+
|
|
183
|
+
// 🧮 derived during render (React's "adjust state on prop change"): open mounts at once, close starts the slide out
|
|
184
|
+
if (open && !present) setPresent(true);
|
|
185
|
+
if (!open && shown) setShown(false);
|
|
186
|
+
|
|
187
|
+
// 🎞️ open → mount, then slide in on the next frame; close → slide out, then unmount
|
|
188
|
+
useEffect(() => {
|
|
189
|
+
const ms = reducedMotion() ? 0 : AB_DRAWER_DURATION; // ♿
|
|
190
|
+
if (open) {
|
|
191
|
+
const raf = requestAnimationFrame(() => requestAnimationFrame(() => setShown(true))); // 🛝 two frames: start state painted first
|
|
192
|
+
return () => cancelAnimationFrame(raf);
|
|
193
|
+
}
|
|
194
|
+
const t = window.setTimeout(() => setPresent(false), ms); // ⏱️ one timer, so quick toggles never race
|
|
195
|
+
return () => window.clearTimeout(t);
|
|
196
|
+
}, [open]);
|
|
197
|
+
|
|
198
|
+
// ↩️ remember the trigger the moment we open (before the scope turns inert and blurs it)
|
|
199
|
+
useEffect(() => { if (open && !panel.current?.contains(document.activeElement)) opener.current = document.activeElement; }, [open]);
|
|
200
|
+
|
|
201
|
+
// ♿ focus in on open, back to the opener on close
|
|
202
|
+
useEffect(() => {
|
|
203
|
+
if (!open || !present || !panel.current) return;
|
|
204
|
+
const first = initialFocus === 'panel' ? null : panel.current.querySelector<HTMLElement>(FOCUSABLE); // 🎯 first control, else the panel
|
|
205
|
+
(first ?? panel.current).focus({ preventScroll: true });
|
|
206
|
+
return () => {
|
|
207
|
+
const back = opener.current as HTMLElement | null; // ↩️
|
|
208
|
+
// ⏭️ a microtask later: the inert cleanup below runs after this one, and an inert opener can't take focus
|
|
209
|
+
queueMicrotask(() => { if (back && back.isConnected && typeof back.focus === 'function') back.focus({ preventScroll: true }); });
|
|
210
|
+
};
|
|
211
|
+
}, [open, present, layer, initialFocus]); // 🎯 initialFocus is static in practice
|
|
212
|
+
|
|
213
|
+
// 🔒 the rest of the scope is inert while open (modal only); we only undo what we set
|
|
214
|
+
useEffect(() => {
|
|
215
|
+
if (!open || !modal || !layer?.parentElement) return;
|
|
216
|
+
const touched: HTMLElement[] = [];
|
|
217
|
+
for (const sib of Array.from(layer.parentElement.children) as HTMLElement[]) {
|
|
218
|
+
if (sib === layer || sib.hasAttribute('inert') || sib.hasAttribute('data-ab-drawer-layer')) continue; // 🙅 ours / already inert / other drawers
|
|
219
|
+
sib.setAttribute('inert', ''); touched.push(sib); // 🧊
|
|
220
|
+
}
|
|
221
|
+
return () => { for (const el of touched) el.removeAttribute('inert'); }; // 🔓
|
|
222
|
+
}, [open, modal, layer]);
|
|
223
|
+
|
|
224
|
+
// 🥞 join the open stack while open (StrictMode-safe: cleanup removes exactly us)
|
|
225
|
+
useEffect(() => {
|
|
226
|
+
if (!open) return;
|
|
227
|
+
openStack.push(token);
|
|
228
|
+
return () => { const i = openStack.lastIndexOf(token); if (i >= 0) openStack.splice(i, 1); };
|
|
229
|
+
}, [open, token]);
|
|
230
|
+
|
|
231
|
+
// ⎋ Escape closes (skips events an inner menu already handled); Tab stays inside (modal). Topmost drawer only
|
|
232
|
+
const onKeyDown = useCallback((e: KeyboardEvent) => {
|
|
233
|
+
if (!panel.current || !isTopDrawer(token)) return; // 🥞 a drawer under another one stays put
|
|
234
|
+
if (e.key === 'Escape' && !e.defaultPrevented) { e.preventDefault(); close.current(); return; } // ⎋
|
|
235
|
+
if (e.key !== 'Tab' || !modal) return;
|
|
236
|
+
const items = Array.from(panel.current.querySelectorAll<HTMLElement>(FOCUSABLE)).filter((el) => el.getClientRects().length > 0 || el === document.activeElement); // 👀 rendered (keeps position: fixed, which has no offsetParent)
|
|
237
|
+
if (items.length === 0) { e.preventDefault(); panel.current.focus(); return; } // 🔒 nothing to tab to
|
|
238
|
+
const [a, z] = [items[0], items[items.length - 1]];
|
|
239
|
+
const inside = panel.current.contains(document.activeElement);
|
|
240
|
+
if (e.shiftKey && (document.activeElement === a || !inside)) { e.preventDefault(); z.focus(); } // ⬅️ wrap
|
|
241
|
+
else if (!e.shiftKey && (document.activeElement === z || !inside)) { e.preventDefault(); a.focus(); } // ➡️ wrap
|
|
242
|
+
}, [modal, token]);
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
if (!open) return;
|
|
245
|
+
document.addEventListener('keydown', onKeyDown); // 🎧 document-level, so Escape works wherever focus is
|
|
246
|
+
return () => document.removeEventListener('keydown', onKeyDown);
|
|
247
|
+
}, [open, onKeyDown]);
|
|
248
|
+
|
|
249
|
+
// 🏷️ a plain-string title becomes the labelling heading
|
|
250
|
+
const heading = title == null ? null : typeof title === 'string' || typeof title === 'number'
|
|
251
|
+
? <h2 id={titleId} className={styles.abDrawerTitle}>{title}</h2>
|
|
252
|
+
: <div id={props['aria-labelledby'] ? undefined : titleId} className={styles.abDrawerTitle}>{title}</div>;
|
|
253
|
+
const sizeVar = size ? ({ '--ab-drawer-size': size } as CSSProperties) : undefined; // 📏
|
|
254
|
+
|
|
255
|
+
// 🧩 D41: before hydration (and with no JS at all) the content is an in-flow section
|
|
256
|
+
if (!layer) {
|
|
257
|
+
return (
|
|
258
|
+
<div ref={anchor} className={styles.abDrawerAnchor} data-ab-part="drawer-anchor">
|
|
259
|
+
{noJsFallback ? (
|
|
260
|
+
<section className={clsx('AbDrawerFallback', styles.abDrawerFallback)} data-ab-part="drawer-fallback" aria-labelledby={labelledBy} aria-label={labelledBy ? undefined : props['aria-label']}>
|
|
261
|
+
{heading}
|
|
262
|
+
<div className={styles.abDrawerBody}>{children}</div>
|
|
263
|
+
{footer != null && <div className={styles.abDrawerFooter}>{footer}</div>}
|
|
264
|
+
</section>
|
|
265
|
+
) : null}
|
|
266
|
+
</div>
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return (
|
|
271
|
+
<div ref={anchor} className={styles.abDrawerAnchor} data-ab-part="drawer-anchor">
|
|
272
|
+
{present && createPortal(
|
|
273
|
+
<div className={styles.abDrawerFrame} data-state={shown ? 'open' : 'closed'} data-side={side} data-scope={scope}>
|
|
274
|
+
{/* 🌫️ scrim: a click closes (pointer only; keyboard users have Escape and the close button) */}
|
|
275
|
+
<div className={clsx(styles.abDrawerScrim, AB_MOTION.colors)} data-ab-part="drawer-scrim" aria-hidden="true" onClick={() => close.current()} />
|
|
276
|
+
<div
|
|
277
|
+
ref={panel}
|
|
278
|
+
id={dialogId}
|
|
279
|
+
role="dialog"
|
|
280
|
+
aria-modal={modal ? 'true' : undefined}
|
|
281
|
+
aria-labelledby={labelledBy}
|
|
282
|
+
aria-label={labelledBy ? undefined : props['aria-label']}
|
|
283
|
+
tabIndex={-1} // 🎯 focus target when there are no controls
|
|
284
|
+
className={clsx('AbDrawer', styles.abDrawer, AB_MOTION.transform, className)}
|
|
285
|
+
data-ab-part="drawer"
|
|
286
|
+
data-side={side}
|
|
287
|
+
style={sizeVar}
|
|
288
|
+
>
|
|
289
|
+
<div className={styles.abDrawerHeader}>
|
|
290
|
+
{heading}
|
|
291
|
+
<button type="button" className={styles.abDrawerClose} data-ab-part="drawer-close" aria-label={closeLabel} onClick={() => close.current()}>
|
|
292
|
+
<span aria-hidden="true">✕</span>
|
|
293
|
+
</button>
|
|
294
|
+
</div>
|
|
295
|
+
<div className={styles.abDrawerBody} data-ab-part="drawer-body">{children}</div>
|
|
296
|
+
{footer != null && <div className={styles.abDrawerFooter} data-ab-part="drawer-footer">{footer}</div>}
|
|
297
|
+
</div>
|
|
298
|
+
</div>,
|
|
299
|
+
layer,
|
|
300
|
+
)}
|
|
301
|
+
</div>
|
|
302
|
+
);
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
// 📦 exports
|
|
306
|
+
export default AbDrawer;
|
|
307
|
+
export { AbDrawer };
|
|
@@ -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: AbDrawer styles (G53)
|
|
28
|
+
* @file: ab-drawer/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
|
+
|
|
40
|
+
/* 📍 the anchor is layout-neutral: the drawer itself lives in the scope's layer */
|
|
41
|
+
.abDrawerAnchor { display: contents; }
|
|
42
|
+
|
|
43
|
+
/* 🧱 the layer: absolute `inset: 0` INSIDE the scope box (app / main / aside), so a drawer is clipped
|
|
44
|
+
by its scope and never makes the document scroll (D34). Clicks pass through when no drawer is open. */
|
|
45
|
+
.abDrawerLayer {
|
|
46
|
+
position: absolute; /* 📌 inside the scope box */
|
|
47
|
+
inset: 0; /* 📐 the whole scope */
|
|
48
|
+
z-index: 60; /* ⬆️ above the panes and the aside sheet (30) */
|
|
49
|
+
overflow: hidden; /* ✂️ a sliding panel never leaks out */
|
|
50
|
+
pointer-events: none; /* 🖱️ idle layer = transparent to clicks */
|
|
51
|
+
contain: layout paint; /* 🚀 */
|
|
52
|
+
}
|
|
53
|
+
.abDrawerLayer[data-host='body'] { position: fixed; } /* 📌 no AbAppLayout: cover the viewport */
|
|
54
|
+
|
|
55
|
+
.abDrawerFrame { position: absolute; inset: 0; pointer-events: auto; } /* 🖱️ a drawer is up */
|
|
56
|
+
|
|
57
|
+
/* 🌫️ scrim */
|
|
58
|
+
.abDrawerScrim {
|
|
59
|
+
position: absolute; /* 📌 */
|
|
60
|
+
inset: 0; /* 📐 */
|
|
61
|
+
background: var(--ab-scrim-color, rgb(25 18 12 / 0.4)); /* 🌫️ theme 0.3.5: 40% light / 60% dark (John) */
|
|
62
|
+
opacity: 0; /* 🫥 */
|
|
63
|
+
--ab-motion-duration: var(--ab-drawer-duration, 240ms); /* 🎞️ fade = .ab-motion-colors (ab-nextjs-animations) */
|
|
64
|
+
}
|
|
65
|
+
.abDrawerFrame[data-state='open'] > .abDrawerScrim { opacity: 1; } /* 🌫️ */
|
|
66
|
+
|
|
67
|
+
/* 🗄️ the panel */
|
|
68
|
+
.abDrawer {
|
|
69
|
+
--ab-drawer-shift: 100%; /* 🛝 how far it starts off-screen */
|
|
70
|
+
position: absolute; /* 📌 */
|
|
71
|
+
display: flex; /* 📐 header · body · footer */
|
|
72
|
+
flex-direction: column; /* ↕️ */
|
|
73
|
+
box-sizing: border-box; /* 📦 */
|
|
74
|
+
margin: 0; /* 🧹 */
|
|
75
|
+
min-width: 0; /* 🚫 overflow */
|
|
76
|
+
min-height: 0; /* 🚫 overflow */
|
|
77
|
+
background: var(--ab-color-surface-container-low, var(--surface-color, Canvas)); /* 🎨 */
|
|
78
|
+
color: var(--ab-color-on-surface, var(--on-surface-color, CanvasText)); /* 🖋️ */
|
|
79
|
+
box-shadow: 0 8px 32px rgb(0 0 0 / 0.24); /* 🌗 */
|
|
80
|
+
outline: none; /* 🎯 the panel itself is only a fallback focus target */
|
|
81
|
+
overscroll-behavior: contain; /* 🚫 scroll chaining (G54) */
|
|
82
|
+
--ab-motion-duration: var(--ab-drawer-duration, 240ms); /* 🎞️ the panes slide = .ab-motion-transform (ab-nextjs-animations) */
|
|
83
|
+
--ab-motion-easing: cubic-bezier(0.2, 0, 0, 1); /* 🚀 */
|
|
84
|
+
}
|
|
85
|
+
.abDrawer[data-side='bottom'] { inset: auto 0 0 0; max-height: min(85%, var(--ab-drawer-size, 480px)); border-radius: 16px 16px 0 0; padding-bottom: env(safe-area-inset-bottom, 0px); transform: translateY(var(--ab-drawer-shift)); } /* ⬆️ */
|
|
86
|
+
.abDrawer[data-side='top'] { inset: 0 0 auto 0; max-height: min(85%, var(--ab-drawer-size, 480px)); border-radius: 0 0 16px 16px; padding-top: env(safe-area-inset-top, 0px); transform: translateY(calc(-1 * var(--ab-drawer-shift))); } /* ⬇️ */
|
|
87
|
+
.abDrawer[data-side='left'] { inset: 0 auto 0 0; width: min(85%, var(--ab-drawer-size, 360px)); transform: translateX(calc(-1 * var(--ab-drawer-shift))); } /* ➡️ */
|
|
88
|
+
.abDrawer[data-side='right'] { inset: 0 0 0 auto; width: min(85%, var(--ab-drawer-size, 360px)); transform: translateX(var(--ab-drawer-shift)); } /* ⬅️ */
|
|
89
|
+
/* 🌍 logical sides follow the writing direction */
|
|
90
|
+
.abDrawer[data-side='start'] { inset-block: 0; inset-inline-start: 0; width: min(85%, var(--ab-drawer-size, 360px)); transform: translateX(calc(-1 * var(--ab-drawer-shift))); }
|
|
91
|
+
.abDrawer[data-side='end'] { inset-block: 0; inset-inline-end: 0; width: min(85%, var(--ab-drawer-size, 360px)); transform: translateX(var(--ab-drawer-shift)); }
|
|
92
|
+
.abDrawer[data-side='start']:dir(rtl) { transform: translateX(var(--ab-drawer-shift)); } /* 🌍 RTL */
|
|
93
|
+
.abDrawer[data-side='end']:dir(rtl) { transform: translateX(calc(-1 * var(--ab-drawer-shift))); } /* 🌍 RTL */
|
|
94
|
+
.abDrawerFrame[data-state='open'] > .abDrawer { transform: none; } /* 🛬 slid in */
|
|
95
|
+
|
|
96
|
+
.abDrawerHeader { flex: none; display: flex; align-items: center; gap: 8px; padding: 12px 8px 4px 20px; } /* 🔝 */
|
|
97
|
+
.abDrawerTitle { flex: 1 1 auto; min-width: 0; margin: 0; font-size: 1.125rem; font-weight: 600; } /* 🏷️ */
|
|
98
|
+
.abDrawerClose {
|
|
99
|
+
flex: none; /* 📐 */
|
|
100
|
+
inline-size: 40px; /* 📏 */
|
|
101
|
+
block-size: 40px; /* 📏 */
|
|
102
|
+
border: 0; /* 🧹 */
|
|
103
|
+
border-radius: 999px; /* 🟢 */
|
|
104
|
+
background: transparent; /* 🎨 */
|
|
105
|
+
color: inherit; /* 🖋️ */
|
|
106
|
+
font: inherit; /* 🔤 */
|
|
107
|
+
cursor: pointer; /* 🖱️ */
|
|
108
|
+
}
|
|
109
|
+
.abDrawerClose:hover { background: var(--ab-color-surface-container-high, rgb(0 0 0 / 0.06)); } /* 🖱️ */
|
|
110
|
+
.abDrawerClose:focus-visible { outline: var(--ab-focus-ring-width, 2px) solid var(--ab-color-accent, var(--ab-color-primary, #8f4c38)); outline-offset: 2px; } /* ♿ */
|
|
111
|
+
.abDrawerBody { flex: 1 1 auto; min-height: 0; overflow: hidden auto; overscroll-behavior: contain; padding: 8px 20px 16px; } /* 📜 its own scroller */
|
|
112
|
+
.abDrawerFooter { flex: none; display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px 16px; } /* 🔚 */
|
|
113
|
+
|
|
114
|
+
/* ♿ reduced motion: no slide, no fade (handled by ab-nextjs-animations .ab-motion-*) */
|
|
115
|
+
|
|
116
|
+
/* 🧩 D41 no-JS fallback: an in-flow section; hidden as soon as scripting runs (hydration swaps in the drawer) */
|
|
117
|
+
.abDrawerFallback { display: block; margin-block: 16px; border: var(--ab-divider-hairline, 1px solid rgb(0 0 0 / 0.12)); border-radius: 12px; }
|
|
118
|
+
.abDrawerFallback .abDrawerTitle { padding: 12px 20px 0; }
|
|
119
|
+
.abDrawerFallback .abDrawerBody { overflow: visible; }
|
|
120
|
+
@media (scripting: enabled) {
|
|
121
|
+
.abDrawerFallback { display: none; }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/* 📍 the scope box (.AbAppLayout / main / aside pane) is the layer's containing block while a drawer uses it.
|
|
125
|
+
:where() = zero specificity, so a host that is already positioned (fixed / absolute aside) keeps its own.
|
|
126
|
+
The local class inside :has() keeps CSS-modules "pure" mode happy. (A core rule could own this later.) */
|
|
127
|
+
:where([data-ab-drawer-host]:has(> .abDrawerLayer)) { position: relative; }
|
|
@@ -0,0 +1,89 @@
|
|
|
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: useAbDrawer (G53)
|
|
28
|
+
* @file: ab-drawer/use-ab-drawer.ts
|
|
29
|
+
* @type: Client hook
|
|
30
|
+
* @authors: Abraham Ukachi <abrahamukachi@gmail.com>
|
|
31
|
+
*
|
|
32
|
+
* Example usage:
|
|
33
|
+
* 1+|> const drawer = useAbDrawer('app')
|
|
34
|
+
* -|> <button {...drawer.triggerProps}>Language</button>
|
|
35
|
+
* -|> <AbDrawer {...drawer.drawerProps} title="Language">…</AbDrawer>
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/*
|
|
39
|
+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
40
|
+
* MOTTO: We'll always do more 😜!!!
|
|
41
|
+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
'use client';
|
|
46
|
+
|
|
47
|
+
// ⚛️ React
|
|
48
|
+
import { useCallback, useId, useMemo, useState } from 'react';
|
|
49
|
+
|
|
50
|
+
/** 🗺️ which box clips the drawer (G53): the whole app, the main pane or the aside pane */
|
|
51
|
+
export type AbDrawerScope = 'app' | 'main' | 'aside';
|
|
52
|
+
|
|
53
|
+
/** 🎛️ what useAbDrawer returns */
|
|
54
|
+
export interface UseAbDrawer {
|
|
55
|
+
scope: AbDrawerScope; // 🗺️
|
|
56
|
+
id: string; // 🆔 the drawer id (`aria-controls` target)
|
|
57
|
+
open: boolean; // 🚪 is it open?
|
|
58
|
+
show: () => void; // ➕ open it
|
|
59
|
+
close: () => void; // ➖ close it (Escape / scrim / buttons)
|
|
60
|
+
toggle: () => void; // 🔁
|
|
61
|
+
setOpen: (open: boolean) => void; // 🎚️ controlled setter
|
|
62
|
+
/** 🔘 spread on the trigger button: aria-expanded / aria-controls / aria-haspopup / onClick */
|
|
63
|
+
triggerProps: { 'aria-expanded': boolean; 'aria-controls': string; 'aria-haspopup': 'dialog'; onClick: () => void };
|
|
64
|
+
/** 🗄️ spread on the drawer: id / scope / open / onClose */
|
|
65
|
+
drawerProps: { id: string; scope: AbDrawerScope; open: boolean; onClose: () => void };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 🎛️ useAbDrawer(scope) — one drawer's open state + the trigger / drawer prop bags (G53).
|
|
70
|
+
*
|
|
71
|
+
* 🧭 Lives in **components** (next to AbDrawer), not in core: it holds no layout state, only `open`,
|
|
72
|
+
* and components doesn't depend on core. Focus return is handled by AbDrawer itself.
|
|
73
|
+
*/
|
|
74
|
+
export function useAbDrawer(scope: AbDrawerScope = 'app', options: { defaultOpen?: boolean; id?: string } = {}): UseAbDrawer {
|
|
75
|
+
const auto = `ab-drawer-${useId().replace(/[^A-Za-z0-9_-]/g, '')}`; // 🆔 stable across SSR + hydration
|
|
76
|
+
const id = options.id ?? auto; // 🆔
|
|
77
|
+
const [open, setOpen] = useState<boolean>(options.defaultOpen ?? false); // 🚪
|
|
78
|
+
const show = useCallback(() => setOpen(true), []); // ➕
|
|
79
|
+
const close = useCallback(() => setOpen(false), []); // ➖
|
|
80
|
+
const toggle = useCallback(() => setOpen((o) => !o), []); // 🔁
|
|
81
|
+
return useMemo(() => ({
|
|
82
|
+
scope, id, open, show, close, toggle, setOpen,
|
|
83
|
+
triggerProps: { 'aria-expanded': open, 'aria-controls': id, 'aria-haspopup': 'dialog' as const, onClick: toggle },
|
|
84
|
+
drawerProps: { id, scope, open, onClose: close },
|
|
85
|
+
}), [scope, id, open, show, close, toggle]);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 📦 default export
|
|
89
|
+
export default useAbDrawer;
|
|
@@ -0,0 +1,118 @@
|
|
|
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: AbDrawerList (G53 preset)
|
|
28
|
+
* @file: ab-drawer-list/index.tsx
|
|
29
|
+
* @type: Client component
|
|
30
|
+
* @authors: Abraham Ukachi <abrahamukachi@gmail.com>
|
|
31
|
+
*
|
|
32
|
+
* Example usage:
|
|
33
|
+
* 1+|> <AbDrawerList scope="app" side="bottom" open={d.open} onClose={d.close} title="Language"
|
|
34
|
+
* -|> items={[{ value: 'en', label: 'English' }, { value: 'fr', label: 'Français' }]} value="en" onSelect={setLocale} />
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/*
|
|
38
|
+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
39
|
+
* MOTTO: We'll always do more 😜!!!
|
|
40
|
+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
'use client';
|
|
45
|
+
|
|
46
|
+
// ⚛️ React
|
|
47
|
+
import type { ReactElement, ReactNode } from 'react';
|
|
48
|
+
// 🧵 classes
|
|
49
|
+
import clsx from 'clsx';
|
|
50
|
+
// 🗄️ the drawer
|
|
51
|
+
import AbDrawer, { type AbDrawerProps } from '../ab-drawer';
|
|
52
|
+
import type { ComponentType } from 'react';
|
|
53
|
+
// 🔗 links go through AbLinkProvider (locale-aware apps)
|
|
54
|
+
import Link from '../ab-link';
|
|
55
|
+
// 🎨 styles
|
|
56
|
+
import styles from './styles.module.css';
|
|
57
|
+
|
|
58
|
+
/** 📋 one choice */
|
|
59
|
+
export interface AbDrawerListItem {
|
|
60
|
+
value: string; // 🆔 e.g. a locale code
|
|
61
|
+
label: ReactNode; // 🏷️
|
|
62
|
+
description?: ReactNode; // 📝 a second line
|
|
63
|
+
icon?: ReactNode; // 🔣 leading, decorative
|
|
64
|
+
href?: string; // 🔗 render a link (e.g. the same page in another locale) instead of a button
|
|
65
|
+
lang?: string; // 🌍 e.g. 'fr' for "Français" (screen readers pronounce it right)
|
|
66
|
+
disabled?: boolean; // 🚫
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** ⚙️ props: every AbDrawer prop, plus the list */
|
|
70
|
+
export interface AbDrawerListProps extends Omit<AbDrawerProps, 'children'> {
|
|
71
|
+
as?: ComponentType<AbDrawerProps>; // 🧱 the drawer to render in (default AbDrawer; e.g. AbBottomDrawer)
|
|
72
|
+
items: AbDrawerListItem[]; // 📋
|
|
73
|
+
value?: string | null; // 🎯 the selected item (`aria-current="true"` + a check mark)
|
|
74
|
+
onSelect?: (value: string) => void; // 🔔
|
|
75
|
+
closeOnSelect?: boolean; // 🚪 default true
|
|
76
|
+
listLabel?: string; // ♿ the list's name when the title isn't enough
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 📋 AbDrawerList — AbDrawer with one selectable list (languages, sort orders, …). D40: the mobile
|
|
81
|
+
* language picker is `<AbDrawerList scope="app" side="bottom" />`. Items are buttons (or links with
|
|
82
|
+
* `href`), the selected one carries `aria-current="true"`. No-JS: the links still work in the fallback.
|
|
83
|
+
*/
|
|
84
|
+
const AbDrawerList = ({ as: Drawer = AbDrawer, items, value, onSelect, closeOnSelect = true, listLabel, onClose, className, ...drawer }: AbDrawerListProps): ReactElement => {
|
|
85
|
+
const pick = (v: string) => { onSelect?.(v); if (closeOnSelect) onClose(); }; // 🔔 then 🚪
|
|
86
|
+
return (
|
|
87
|
+
<Drawer {...drawer} onClose={onClose} className={clsx('AbDrawerList', className)}>
|
|
88
|
+
<ul className={styles.abDrawerList} aria-label={listLabel} data-ab-part="drawer-list">
|
|
89
|
+
{items.map((it) => {
|
|
90
|
+
const on = it.value === value; // 🎯
|
|
91
|
+
const inner = (
|
|
92
|
+
<>
|
|
93
|
+
{it.icon != null && <span className={styles.abDrawerListIcon} aria-hidden="true">{it.icon}</span>}
|
|
94
|
+
<span className={styles.abDrawerListText}>
|
|
95
|
+
<span lang={it.lang}>{it.label}</span>
|
|
96
|
+
{it.description != null && <span className={styles.abDrawerListDescription}>{it.description}</span>}
|
|
97
|
+
</span>
|
|
98
|
+
<span className={styles.abDrawerListCheck} aria-hidden="true">{on ? '✓' : ''}</span>
|
|
99
|
+
</>
|
|
100
|
+
);
|
|
101
|
+
return (
|
|
102
|
+
<li key={it.value}>
|
|
103
|
+
{it.href && !it.disabled ? (
|
|
104
|
+
<Link href={it.href} className={styles.abDrawerListItem} data-active={on ? 'true' : 'false'} aria-current={on ? 'true' : undefined} hrefLang={it.lang} onClick={() => pick(it.value)}>{inner}</Link>
|
|
105
|
+
) : (
|
|
106
|
+
<button type="button" className={styles.abDrawerListItem} data-active={on ? 'true' : 'false'} aria-current={on ? 'true' : undefined} disabled={it.disabled} onClick={() => pick(it.value)}>{inner}</button>
|
|
107
|
+
)}
|
|
108
|
+
</li>
|
|
109
|
+
);
|
|
110
|
+
})}
|
|
111
|
+
</ul>
|
|
112
|
+
</Drawer>
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// 📦 exports
|
|
117
|
+
export default AbDrawerList;
|
|
118
|
+
export { AbDrawerList };
|