@birdycom/omniaui 0.16.2 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/lib/copy-js.mjs +8 -0
- package/bin/lib/webicons-builtin.json +1 -1
- package/dist/back-link.d.ts +6 -0
- package/dist/back-link.js +54 -0
- package/dist/barcode.d.ts +5 -1
- package/dist/barcode.js +29 -8
- package/dist/calendar.d.ts +17 -0
- package/dist/calendar.js +190 -0
- package/dist/code-block.css +3 -0
- package/dist/combobox.d.ts +27 -4
- package/dist/combobox.js +231 -33
- package/dist/components-extra.css +33 -2
- package/dist/components.css +2096 -163
- package/dist/components.min.css +1 -1
- package/dist/countdown.d.ts +11 -0
- package/dist/countdown.js +76 -0
- package/dist/data-table.d.ts +28 -0
- package/dist/data-table.js +186 -18
- package/dist/docs-ui.css +11 -1
- package/dist/glass-lens.d.ts +41 -0
- package/dist/glass-lens.js +307 -0
- package/dist/input-mask.d.ts +31 -0
- package/dist/input-mask.js +228 -0
- package/dist/nav-overflow.d.ts +11 -0
- package/dist/nav-overflow.js +151 -0
- package/dist/oc-theme.css +1 -1
- package/dist/parts/app-0160-apps.css +6 -0
- package/dist/parts/app-compound-overlays.css +68 -0
- package/dist/parts/app-countdown.css +66 -0
- package/dist/parts/app-data-table-footer.css +6 -3
- package/dist/parts/app-data-table-server.css +47 -0
- package/dist/parts/app-data-table.css +27 -1
- package/dist/parts/app-header-settings.css +9 -0
- package/dist/parts/app-mode-badge.css +12 -0
- package/dist/parts/app-module-color.css +152 -0
- package/dist/parts/app-name-card.css +5 -0
- package/dist/parts/app-page-header.css +1 -1
- package/dist/parts/app-recovery-codes-extras.css +13 -0
- package/dist/parts/app-sidebar-meta.css +14 -3
- package/dist/parts/app-sidebar-provider.css +98 -0
- package/dist/parts/app-table-actions.css +16 -0
- package/dist/parts/app-tree.css +6 -0
- package/dist/parts/app-type-picker.css +272 -0
- package/dist/parts/data-dl-numbered.css +17 -0
- package/dist/parts/data-graph.css +12 -4
- package/dist/parts/forms-combobox-catalog.css +142 -0
- package/dist/parts/forms-combobox-icons.css +40 -2
- package/dist/parts/forms-copy-field-extras.css +47 -0
- package/dist/parts/forms-field-extras.css +55 -0
- package/dist/parts/forms-form-section.css +39 -1
- package/dist/parts/forms-ledger.css +98 -0
- package/dist/parts/media-aspect-flush.css +6 -0
- package/dist/parts/media-barcode.css +25 -4
- package/dist/parts/media-doc-viewer.css +2 -0
- package/dist/parts/media-entity-card.css +5 -0
- package/dist/parts/media-glass-liquid.css +10 -5
- package/dist/parts/media-glass-options.css +37 -5
- package/dist/parts/media-glass.css +85 -35
- package/dist/parts/media-scrim.css +51 -0
- package/dist/parts/nav-topbar-variants.css +12 -4
- package/dist/parts/site-header-variants.css +40 -4
- package/dist/parts/site-hero-media.css +88 -0
- package/dist/parts/site-price-card.css +3 -1
- package/dist/parts/site-split-reverse.css +11 -0
- package/dist/parts/site-tint-card.css +5 -4
- package/dist/parts/utility-hover.css +6 -3
- package/dist/showcase.css +73 -31
- package/dist/sidebar.d.ts +22 -0
- package/dist/sidebar.js +161 -0
- package/dist/tabs.d.ts +9 -0
- package/dist/tabs.js +104 -0
- package/dist/theme.js +20 -4
- package/manifest.json +24 -0
- package/package.json +10 -2
- package/snippets/accordion-accent.html +1 -1
- package/snippets/copy-field.html +44 -6
- package/snippets/countdown.html +12 -0
- package/snippets/dropdown-menu.html +2 -0
- package/snippets/glass.html +3 -3
- package/snippets/hero-media.html +20 -0
- package/snippets/page-header.html +1 -1
- package/snippets/recovery-codes.html +55 -6
- package/snippets/scrim.html +19 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
// File: packages/ui/src/glass-lens.js
|
|
2
|
+
// Map: browser check (Chromium only) -> displacement map (SVG data URI, size of the sheet) -> filter store (one hidden
|
|
3
|
+
// <svg>, one <filter> per size, counted) -> bind one sheet (ResizeObserver, --oc-glass-lens, data-oc-lens) ->
|
|
4
|
+
// init every sheet -> drag (optional: data-oc-glass-drag, pointer + keyboard, clamped to its area) -> boot
|
|
5
|
+
// The lens of the glass (.oc-glass, .oc-glass-bar; parts/media-glass.css): the backdrop stays sharp and bends at the
|
|
6
|
+
// edge, as through a thick piece of glass. Recipe: a map the exact size of the sheet with red running left to right and
|
|
7
|
+
// blue top to bottom (screen-blended), and a blurred purple (half red, half blue = no shift) rectangle over the middle,
|
|
8
|
+
// so only the edge moves; an SVG filter feImage + feDisplacementMap reads it, backdrop-filter: url(#…) applies it.
|
|
9
|
+
// Only Chromium (Chrome, Edge, Opera, Electron) applies url() in backdrop-filter. Elsewhere the script adds
|
|
10
|
+
// html.oc-no-lens and does nothing else: the CSS keeps frosted glass (blur 12px, saturate 160%), as without the script.
|
|
11
|
+
// Skips .oc-glass--flat, Pro's .oc-glass--lens (its own #oc-glass-lens filter) and [data-oc-glass-lens="off"].
|
|
12
|
+
// Per sheet: data-oc-glass-lens-scale="-80" (stronger with a bigger number, the minus sign turns the direction); a sheet
|
|
13
|
+
// with a short side under 160px gets a proportionally smaller scale (lensScale).
|
|
14
|
+
|
|
15
|
+
const NS = "http://www.w3.org/2000/svg";
|
|
16
|
+
const DEFS_ID = "oc-lens-defs";
|
|
17
|
+
export const LENS_SCALE = -80;
|
|
18
|
+
/** Sheets the auto-start (and initGlassLens) gives a lens. */
|
|
19
|
+
/** Never a lens: a desktop window (the OS draws the blur), Pro's own lens, or switched off. */
|
|
20
|
+
const SKIP = ".oc-glass--flat, .oc-glass--lens, [data-oc-glass-lens=\"off\"]";
|
|
21
|
+
export const LENS_SELECTOR = `:is(.oc-glass, .oc-glass-bar, [data-oc-glass-lens]):not(${SKIP})`;
|
|
22
|
+
|
|
23
|
+
/** True in Chromium browsers, the only ones that apply an SVG filter in backdrop-filter. */
|
|
24
|
+
export function supportsGlassLens(nav = typeof navigator === "undefined" ? undefined : navigator) {
|
|
25
|
+
const brands = nav?.userAgentData?.brands;
|
|
26
|
+
return Array.isArray(brands) && brands.some((b) => /Chromium/i.test(b.brand));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Width of the bending edge for a sheet of w x h (26px, less on a small sheet such as a bar). */
|
|
30
|
+
const edgeOf = (w, h) => Math.max(4, Math.min(26, Math.round(Math.min(w, h) * 0.3)));
|
|
31
|
+
|
|
32
|
+
/** The displacement map for a sheet of w x h px, as an SVG data URI. radius = corner radius of the neutral middle. */
|
|
33
|
+
export function lensMap(w, h, radius = 28) {
|
|
34
|
+
const e = edgeOf(w, h);
|
|
35
|
+
const blur = Math.max(2, Math.round(e * 0.55));
|
|
36
|
+
const iw = Math.max(0, w - 2 * e);
|
|
37
|
+
const ih = Math.max(0, h - 2 * e);
|
|
38
|
+
const r = Math.max(0, Math.min(radius - e / 2, iw / 2, ih / 2));
|
|
39
|
+
const svg = `<svg xmlns="${NS}" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">`
|
|
40
|
+
+ `<defs><linearGradient id="x" x2="1" y2="0"><stop offset="0" stop-color="#000"/><stop offset="1" stop-color="#f00"/></linearGradient>`
|
|
41
|
+
+ `<linearGradient id="y" x2="0" y2="1"><stop offset="0" stop-color="#000"/><stop offset="1" stop-color="#00f"/></linearGradient>`
|
|
42
|
+
+ `<filter id="b" x="-50%" y="-50%" width="200%" height="200%"><feGaussianBlur stdDeviation="${blur}"/></filter></defs>`
|
|
43
|
+
+ `<rect width="${w}" height="${h}" fill="url(#x)"/>`
|
|
44
|
+
+ `<rect width="${w}" height="${h}" fill="url(#y)" style="mix-blend-mode:screen"/>`
|
|
45
|
+
+ `<rect x="${e}" y="${e}" width="${iw}" height="${ih}" rx="${r}" fill="#800080" filter="url(#b)"/></svg>`;
|
|
46
|
+
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The scale used on a sheet of w x h: the full scale from 160px (the short side) up, less on a smaller sheet, so a
|
|
50
|
+
* thin bar or a small card bends as much as its edge allows instead of smearing. */
|
|
51
|
+
export function lensScale(w, h, scale = LENS_SCALE) {
|
|
52
|
+
return Math.round(scale * Math.min(1, Math.min(w, h) / 160));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Id of the filter for a sheet of w x h with this scale ("oc-lens-320x180", "oc-lens-320x180-s40"). */
|
|
56
|
+
export function lensId(w, h, scale = LENS_SCALE) {
|
|
57
|
+
return `oc-lens-${w}x${h}${scale === LENS_SCALE ? "" : `-s${String(scale).replace("-", "m")}`}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Filters in use: id -> number of sheets on it. A filter goes away when no sheet uses it any more.
|
|
61
|
+
const uses = new Map();
|
|
62
|
+
|
|
63
|
+
function defs(doc) {
|
|
64
|
+
let svg = doc.getElementById(DEFS_ID);
|
|
65
|
+
if (!svg) {
|
|
66
|
+
svg = doc.createElementNS(NS, "svg");
|
|
67
|
+
svg.id = DEFS_ID;
|
|
68
|
+
svg.setAttribute("class", "oc-lens-defs");
|
|
69
|
+
svg.setAttribute("width", "0");
|
|
70
|
+
svg.setAttribute("height", "0");
|
|
71
|
+
svg.setAttribute("aria-hidden", "true");
|
|
72
|
+
svg.setAttribute("focusable", "false");
|
|
73
|
+
svg.style.cssText = "position:absolute;inline-size:0;block-size:0;overflow:hidden;pointer-events:none";
|
|
74
|
+
doc.body.append(svg);
|
|
75
|
+
}
|
|
76
|
+
return svg;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Makes (or reuses) the filter for w x h and counts one more user. Returns its id. */
|
|
80
|
+
export function acquireLens(doc, w, h, scale = LENS_SCALE, radius = 28) {
|
|
81
|
+
const id = lensId(w, h, scale);
|
|
82
|
+
if (!uses.has(id) || !doc.getElementById(id)) {
|
|
83
|
+
const filter = doc.createElementNS(NS, "filter");
|
|
84
|
+
filter.id = id;
|
|
85
|
+
for (const [k, v] of Object.entries({ x: "0", y: "0", width: "100%", height: "100%", "color-interpolation-filters": "sRGB" })) filter.setAttribute(k, v);
|
|
86
|
+
const img = doc.createElementNS(NS, "feImage");
|
|
87
|
+
for (const [k, v] of Object.entries({ href: lensMap(w, h, radius), x: "0", y: "0", width: String(w), height: String(h), preserveAspectRatio: "none", result: "map" })) img.setAttribute(k, v);
|
|
88
|
+
const move = doc.createElementNS(NS, "feDisplacementMap");
|
|
89
|
+
for (const [k, v] of Object.entries({ in: "SourceGraphic", in2: "map", scale: String(lensScale(w, h, scale)), xChannelSelector: "R", yChannelSelector: "B" })) move.setAttribute(k, v);
|
|
90
|
+
filter.append(img, move);
|
|
91
|
+
defs(doc).append(filter);
|
|
92
|
+
if (!uses.has(id)) uses.set(id, 0);
|
|
93
|
+
}
|
|
94
|
+
uses.set(id, uses.get(id) + 1);
|
|
95
|
+
return id;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** One user less; the filter is removed when nobody uses it. */
|
|
99
|
+
export function releaseLens(doc, id) {
|
|
100
|
+
if (!id || !uses.has(id)) return;
|
|
101
|
+
const n = uses.get(id) - 1;
|
|
102
|
+
if (n > 0) { uses.set(id, n); return; }
|
|
103
|
+
uses.delete(id);
|
|
104
|
+
doc.getElementById(id)?.remove();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Number of lens filters on the page (for tests and debugging). */
|
|
108
|
+
export const lensCount = () => uses.size;
|
|
109
|
+
|
|
110
|
+
let observer;
|
|
111
|
+
const bound = new WeakMap();
|
|
112
|
+
|
|
113
|
+
function update(el) {
|
|
114
|
+
const state = bound.get(el);
|
|
115
|
+
if (!state) return;
|
|
116
|
+
const w = Math.round(el.clientWidth);
|
|
117
|
+
const h = Math.round(el.clientHeight);
|
|
118
|
+
const scale = Number(el.dataset.ocGlassLensScale) || LENS_SCALE;
|
|
119
|
+
const id = w > 0 && h > 0 ? lensId(w, h, scale) : "";
|
|
120
|
+
if (id === state.id) return;
|
|
121
|
+
const doc = el.ownerDocument;
|
|
122
|
+
const radius = parseFloat(doc.defaultView?.getComputedStyle(el).borderTopLeftRadius) || 28;
|
|
123
|
+
const next = id ? acquireLens(doc, w, h, scale, radius) : "";
|
|
124
|
+
releaseLens(doc, state.id);
|
|
125
|
+
state.id = next;
|
|
126
|
+
if (next) {
|
|
127
|
+
el.style.setProperty("--oc-glass-lens", `url(#${next})`);
|
|
128
|
+
el.dataset.ocLens = `${w}x${h}`;
|
|
129
|
+
} else {
|
|
130
|
+
el.style.removeProperty("--oc-glass-lens");
|
|
131
|
+
delete el.dataset.ocLens;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Gives one sheet the lens and keeps it the size of the sheet. Returns a function that removes it again.
|
|
136
|
+
* Outside Chromium it only marks html.oc-no-lens. */
|
|
137
|
+
export function bindGlassLens(el, { force = false } = {}) {
|
|
138
|
+
const doc = el?.ownerDocument;
|
|
139
|
+
if (!doc || bound.has(el) || el.matches(SKIP)) return () => {};
|
|
140
|
+
if (!force && !supportsGlassLens(doc.defaultView?.navigator)) {
|
|
141
|
+
doc.documentElement.classList.add("oc-no-lens");
|
|
142
|
+
return () => {};
|
|
143
|
+
}
|
|
144
|
+
doc.documentElement.classList.remove("oc-no-lens");
|
|
145
|
+
bound.set(el, { id: "" });
|
|
146
|
+
const RO = doc.defaultView?.ResizeObserver;
|
|
147
|
+
if (RO) {
|
|
148
|
+
observer ??= new RO((entries) => { for (const entry of entries) update(entry.target); });
|
|
149
|
+
observer.observe(el);
|
|
150
|
+
}
|
|
151
|
+
update(el);
|
|
152
|
+
return () => {
|
|
153
|
+
const state = bound.get(el);
|
|
154
|
+
if (!state) return;
|
|
155
|
+
observer?.unobserve(el);
|
|
156
|
+
releaseLens(doc, state.id);
|
|
157
|
+
bound.delete(el);
|
|
158
|
+
el.style.removeProperty("--oc-glass-lens");
|
|
159
|
+
delete el.dataset.ocLens;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Gives every glass sheet under `scope` the lens (see LENS_SELECTOR). */
|
|
164
|
+
export function initGlassLens(scope = document, options) {
|
|
165
|
+
const els = [...scope.querySelectorAll(LENS_SELECTOR)];
|
|
166
|
+
if (scope.nodeType === 1 && scope.matches(LENS_SELECTOR)) els.unshift(scope);
|
|
167
|
+
return els.map((el) => bindGlassLens(el, options));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ---- Drag (optional): data-oc-glass-drag on a sheet lets people move it over its backdrop ------------------------------
|
|
171
|
+
// Mouse, pen and touch (pointer events, touch-action: none in the CSS) and the keyboard (the sheet takes focus; arrow keys
|
|
172
|
+
// move it 16px, with Shift 64px, Home puts it back). It stays inside its area: the closest [data-oc-glass-drag-area], else
|
|
173
|
+
// the parent. It moves with `translate`, so the size and the lens filter stay the same; when the area resizes the sheet is
|
|
174
|
+
// clamped again. Clicks on links, buttons and fields inside still work. Fires "oc-glass-move" with { x, y }.
|
|
175
|
+
|
|
176
|
+
/** Sheets initGlassDrag (and the auto-start) make draggable. */
|
|
177
|
+
export const DRAG_SELECTOR = "[data-oc-glass-drag]";
|
|
178
|
+
/** Keyboard step in px (Shift: four times as far). */
|
|
179
|
+
export const DRAG_STEP = 16;
|
|
180
|
+
const INTERACTIVE = "a[href], button, input, select, textarea, label, summary, [contenteditable=\"\"], [contenteditable=\"true\"], [data-oc-glass-drag-ignore]";
|
|
181
|
+
const KEYS = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] };
|
|
182
|
+
const dragged = new WeakMap();
|
|
183
|
+
|
|
184
|
+
/** The area a sheet moves in: the closest [data-oc-glass-drag-area] above it, else its parent. */
|
|
185
|
+
const areaOf = (el) => el.parentElement?.closest("[data-oc-glass-drag-area]") ?? el.parentElement;
|
|
186
|
+
|
|
187
|
+
/** Keeps x/y so the sheet stays inside its area (the inner box: inside the area's border). */
|
|
188
|
+
export function clampGlassDrag(el, x, y, state = dragged.get(el) ?? { x: 0, y: 0 }) {
|
|
189
|
+
const area = areaOf(el);
|
|
190
|
+
if (!area) return [x, y];
|
|
191
|
+
const a = area.getBoundingClientRect();
|
|
192
|
+
const r = el.getBoundingClientRect();
|
|
193
|
+
// the box without the current move (the computed translate, which is also right halfway a keyboard glide)
|
|
194
|
+
const t = el.ownerDocument.defaultView?.getComputedStyle(el).translate;
|
|
195
|
+
const [nowX, nowY = 0] = t ? (t === "none" ? [0, 0] : t.split(" ").map((v) => parseFloat(v) || 0)) : [state.x, state.y];
|
|
196
|
+
const left = r.left - nowX;
|
|
197
|
+
const top = r.top - nowY;
|
|
198
|
+
const minX = a.left + area.clientLeft - left;
|
|
199
|
+
const minY = a.top + area.clientTop - top;
|
|
200
|
+
const maxX = Math.max(minX, a.left + area.clientLeft + area.clientWidth - (left + r.width));
|
|
201
|
+
const maxY = Math.max(minY, a.top + area.clientTop + area.clientHeight - (top + r.height));
|
|
202
|
+
return [Math.round(Math.min(maxX, Math.max(minX, x))), Math.round(Math.min(maxY, Math.max(minY, y)))];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Moves a draggable sheet to x/y (px from where the layout put it), clamped to its area. */
|
|
206
|
+
export function moveGlass(el, x, y) {
|
|
207
|
+
const state = dragged.get(el);
|
|
208
|
+
if (!state) return;
|
|
209
|
+
[state.x, state.y] = clampGlassDrag(el, x, y, state);
|
|
210
|
+
el.style.translate = state.x || state.y ? `${state.x}px ${state.y}px` : "";
|
|
211
|
+
el.dispatchEvent(new el.ownerDocument.defaultView.CustomEvent("oc-glass-move", { detail: { x: state.x, y: state.y } }));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Makes one sheet draggable (mouse, touch, keyboard). Returns a function that undoes it. */
|
|
215
|
+
export function bindGlassDrag(el, { label = "Drag to move" } = {}) {
|
|
216
|
+
const doc = el?.ownerDocument;
|
|
217
|
+
if (!doc || dragged.has(el)) return () => {};
|
|
218
|
+
const state = { x: 0, y: 0, pointer: null };
|
|
219
|
+
dragged.set(el, state);
|
|
220
|
+
const added = [];
|
|
221
|
+
const set = (k, v) => { if (!el.hasAttribute(k)) { el.setAttribute(k, v); added.push(k); } };
|
|
222
|
+
set("data-oc-glass-drag", "");
|
|
223
|
+
set("tabindex", "0");
|
|
224
|
+
set("role", "group");
|
|
225
|
+
set("aria-roledescription", "draggable");
|
|
226
|
+
if (!el.hasAttribute("aria-labelledby")) set("aria-label", label);
|
|
227
|
+
|
|
228
|
+
const down = (e) => {
|
|
229
|
+
if (e.button !== undefined && e.button !== 0) return;
|
|
230
|
+
const hit = e.target !== el && e.target.closest?.(INTERACTIVE);
|
|
231
|
+
if (hit && el.contains(hit)) return;
|
|
232
|
+
state.pointer = { id: e.pointerId, sx: e.clientX - state.x, sy: e.clientY - state.y };
|
|
233
|
+
el.setPointerCapture?.(e.pointerId);
|
|
234
|
+
el.setAttribute("data-oc-dragging", "");
|
|
235
|
+
// no text selection or native image drag; the sheet takes focus for the arrow keys, without a ring until a key is used
|
|
236
|
+
e.preventDefault();
|
|
237
|
+
el.setAttribute("data-oc-pointer", "");
|
|
238
|
+
el.focus?.({ preventScroll: true });
|
|
239
|
+
};
|
|
240
|
+
const noRing = () => el.removeAttribute("data-oc-pointer");
|
|
241
|
+
const move = (e) => {
|
|
242
|
+
const p = state.pointer;
|
|
243
|
+
if (!p || (p.id !== undefined && e.pointerId !== undefined && e.pointerId !== p.id)) return;
|
|
244
|
+
moveGlass(el, e.clientX - p.sx, e.clientY - p.sy);
|
|
245
|
+
};
|
|
246
|
+
const up = (e) => {
|
|
247
|
+
if (!state.pointer) return;
|
|
248
|
+
el.releasePointerCapture?.(e.pointerId);
|
|
249
|
+
state.pointer = null;
|
|
250
|
+
el.removeAttribute("data-oc-dragging");
|
|
251
|
+
};
|
|
252
|
+
const key = (e) => {
|
|
253
|
+
if (e.target !== el) return;
|
|
254
|
+
noRing();
|
|
255
|
+
if (e.key === "Home") { e.preventDefault(); moveGlass(el, 0, 0); return; }
|
|
256
|
+
const dir = KEYS[e.key];
|
|
257
|
+
if (!dir) return;
|
|
258
|
+
e.preventDefault();
|
|
259
|
+
const step = DRAG_STEP * (e.shiftKey ? 4 : 1);
|
|
260
|
+
moveGlass(el, state.x + dir[0] * step, state.y + dir[1] * step);
|
|
261
|
+
};
|
|
262
|
+
const on = [["pointerdown", down], ["pointermove", move], ["pointerup", up], ["pointercancel", up], ["keydown", key], ["blur", noRing]];
|
|
263
|
+
for (const [t, f] of on) el.addEventListener(t, f);
|
|
264
|
+
|
|
265
|
+
// the area got smaller (window, sidebar): keep the sheet inside
|
|
266
|
+
const RO = doc.defaultView?.ResizeObserver;
|
|
267
|
+
const area = areaOf(el);
|
|
268
|
+
const ro = RO && area ? new RO(() => { if (state.x || state.y) moveGlass(el, state.x, state.y); }) : null;
|
|
269
|
+
ro?.observe(area);
|
|
270
|
+
|
|
271
|
+
return () => {
|
|
272
|
+
if (!dragged.has(el)) return;
|
|
273
|
+
for (const [t, f] of on) el.removeEventListener(t, f);
|
|
274
|
+
ro?.disconnect();
|
|
275
|
+
dragged.delete(el);
|
|
276
|
+
for (const k of added) el.removeAttribute(k);
|
|
277
|
+
el.removeAttribute("data-oc-dragging");
|
|
278
|
+
noRing();
|
|
279
|
+
el.style.translate = "";
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Makes every [data-oc-glass-drag] under `scope` draggable. */
|
|
284
|
+
export function initGlassDrag(scope = document) {
|
|
285
|
+
const els = [...scope.querySelectorAll(DRAG_SELECTOR)];
|
|
286
|
+
if (scope.nodeType === 1 && scope.matches(DRAG_SELECTOR)) els.unshift(scope);
|
|
287
|
+
return els.map((el) => bindGlassDrag(el));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Starts on its own as /js/oc-glass-lens.js (`npx omniaui copy-js glass-lens`), with ?auto in the URL or data-oc-auto
|
|
291
|
+
// on the tag. Dragging works in every browser; the lens only in Chromium.
|
|
292
|
+
function autoStart() {
|
|
293
|
+
if (typeof document === "undefined") return false;
|
|
294
|
+
const url = String(import.meta.url);
|
|
295
|
+
if (url.includes("oc-glass-lens") || /[?&]auto(=|&|$)/.test(url)) return true;
|
|
296
|
+
const file = url.split(/[?#]/)[0];
|
|
297
|
+
return [...document.querySelectorAll("script[data-oc-auto][src]")].some((s) => s.src.split(/[?#]/)[0] === file);
|
|
298
|
+
}
|
|
299
|
+
if (autoStart()) {
|
|
300
|
+
const boot = () => {
|
|
301
|
+
if (!supportsGlassLens()) document.documentElement.classList.add("oc-no-lens");
|
|
302
|
+
else initGlassLens(document);
|
|
303
|
+
initGlassDrag(document);
|
|
304
|
+
};
|
|
305
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot);
|
|
306
|
+
else boot();
|
|
307
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// File: packages/ui/src/input-mask.d.ts
|
|
2
|
+
// Types for the input mask and validation helpers (live formatting, blur validation) of a plain .oc-input.
|
|
3
|
+
|
|
4
|
+
/** Named masks; any other string is a pattern (9 = digit, A = letter, * = any). */
|
|
5
|
+
export type NamedMask = "iban" | "bic" | "vat" | "phone";
|
|
6
|
+
/** Checks `validate()` knows. */
|
|
7
|
+
export type ValidationType = "iban" | "bic" | "vat" | "email" | "phone";
|
|
8
|
+
|
|
9
|
+
/** Length of an IBAN per country (ISO 3166 code). */
|
|
10
|
+
export const IBAN_LENGTHS: Record<string, number>;
|
|
11
|
+
export const NAMED_MASKS: NamedMask[];
|
|
12
|
+
/** Formats a value with a pattern: 9 = digit, A = letter, * = any; other characters are literals. */
|
|
13
|
+
export function applyPattern(pattern: string, value: string): string;
|
|
14
|
+
/** Formats a value while typing (named mask or pattern). */
|
|
15
|
+
export function applyMask(type: NamedMask | (string & {}) | undefined, value: string): string;
|
|
16
|
+
/** Caret position in `formatted` after `count` letters/digits. */
|
|
17
|
+
export function caretFor(formatted: string, count: number): number;
|
|
18
|
+
/** Number of letters/digits before `caret` in `value`. */
|
|
19
|
+
export function countBefore(value: string, caret: number): number;
|
|
20
|
+
/** IBAN check: country length + mod-97. */
|
|
21
|
+
export function validIban(value: string): boolean;
|
|
22
|
+
/** VAT number: BE (mod-97), NL (format), other EU countries (format). */
|
|
23
|
+
export function validVat(value: string): boolean;
|
|
24
|
+
/** True when the value is valid for the type; an empty value is always valid. */
|
|
25
|
+
export function validate(type: ValidationType | (string & {}) | undefined, value: string): boolean;
|
|
26
|
+
/** Formats an input in place and keeps the caret behind the same character. Returns the new value. */
|
|
27
|
+
export function maskInput(input: HTMLInputElement, type: string): string;
|
|
28
|
+
/** Starts one input (data-oc-mask, data-oc-validate, data-oc-validation-message). Event: oc-input-validate { valid, value, type }. */
|
|
29
|
+
export function bindInputMask(input: HTMLInputElement): () => void;
|
|
30
|
+
/** Starts every input[data-oc-mask] / input[data-oc-validate] in scope. */
|
|
31
|
+
export function initInputMask(scope?: ParentNode): () => void;
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// File: packages/ui/src/input-mask.js
|
|
2
|
+
// Map: named masks (iban, bic, vat, phone) + pattern masks (9 = digit, A = letter, * = any) -> applyMask -> caret position
|
|
3
|
+
// after formatting -> validate (iban mod-97 + country length, bic, BE/NL vat, email, phone) -> bind one input
|
|
4
|
+
// -> init every input[data-oc-mask] / input[data-oc-validate] -> boot
|
|
5
|
+
// Live formatting and blur validation for a plain .oc-input, shared by the React and Vue wrappers (pure functions) and by
|
|
6
|
+
// Blade/HTML (data attributes + this script, `npx omniaui copy-js input-mask`).
|
|
7
|
+
// Markup: <input class="oc-input" data-oc-mask="iban" data-oc-validate="iban" data-oc-validation-message="Not a valid IBAN">
|
|
8
|
+
// On blur an invalid value gets aria-invalid="true" + .oc-input--error; inside an .oc-field the message is written into a
|
|
9
|
+
// .oc-field-error[data-oc-validation] (made when missing, removed again when the value is valid). A server error that is
|
|
10
|
+
// already in the field is left alone.
|
|
11
|
+
// Event on the input: oc-input-validate { valid, value, type }.
|
|
12
|
+
|
|
13
|
+
/** Length of an IBAN per country (ISO 3166 code). */
|
|
14
|
+
export const IBAN_LENGTHS = {
|
|
15
|
+
AD: 24, AE: 23, AL: 28, AT: 20, AZ: 28, BA: 20, BE: 16, BG: 22, BH: 22, BR: 29, CH: 21, CR: 22, CY: 28, CZ: 24,
|
|
16
|
+
DE: 22, DK: 18, DO: 28, EE: 20, EG: 29, ES: 24, FI: 18, FO: 18, FR: 27, GB: 22, GE: 22, GI: 23, GL: 18, GR: 27,
|
|
17
|
+
GT: 28, HR: 21, HU: 28, IE: 22, IL: 23, IS: 26, IT: 27, JO: 30, KW: 30, KZ: 20, LB: 28, LI: 21, LT: 20, LU: 20,
|
|
18
|
+
LV: 21, MC: 27, MD: 24, ME: 22, MK: 19, MR: 27, MT: 31, MU: 30, NL: 18, NO: 15, PK: 24, PL: 28, PS: 29, PT: 25,
|
|
19
|
+
QA: 29, RO: 24, RS: 22, SA: 24, SE: 24, SI: 19, SK: 24, SM: 27, TN: 24, TR: 26, UA: 29, VG: 24, XK: 20,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const NAMED = new Set(["iban", "bic", "vat", "phone"]);
|
|
23
|
+
const alnum = (s) => String(s ?? "").toUpperCase().replace(/[^0-9A-Z]/g, "");
|
|
24
|
+
|
|
25
|
+
/** True for the characters a mask keeps and counts (letters and digits; for a pattern mask also its * slots). */
|
|
26
|
+
function significant(ch) {
|
|
27
|
+
return /[0-9A-Za-z]/.test(ch);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Formats `value` with a pattern: 9 = digit, A = letter, * = any character; other pattern characters are literals. */
|
|
31
|
+
export function applyPattern(pattern, value) {
|
|
32
|
+
const raw = String(value ?? "");
|
|
33
|
+
const literals = new Set([...pattern].filter((c) => c !== "9" && c !== "A" && c !== "*"));
|
|
34
|
+
const chars = [...raw].filter((c) => !literals.has(c));
|
|
35
|
+
let out = "";
|
|
36
|
+
let i = 0;
|
|
37
|
+
for (const slot of pattern) {
|
|
38
|
+
if (i >= chars.length) break;
|
|
39
|
+
if (slot === "9" || slot === "A" || slot === "*") {
|
|
40
|
+
// Skip characters that do not fit this slot.
|
|
41
|
+
while (i < chars.length) {
|
|
42
|
+
const c = chars[i++];
|
|
43
|
+
if (slot === "*" || (slot === "9" && /\d/.test(c)) || (slot === "A" && /[A-Za-z]/.test(c))) { out += slot === "A" ? c.toUpperCase() : c; break; }
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
out += slot;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Formats a value while typing. Named masks: "iban" (capitals, groups of four), "bic" (capitals, 11 at most), "vat"
|
|
54
|
+
* (capitals; BE0123.456.789, NL123456789B01, other countries compact), "phone" (+, digits and single spaces). Any other
|
|
55
|
+
* string is a pattern (9 = digit, A = letter, * = any).
|
|
56
|
+
*/
|
|
57
|
+
export function applyMask(type, value) {
|
|
58
|
+
const v = String(value ?? "");
|
|
59
|
+
if (!type) return v;
|
|
60
|
+
if (type === "iban") return alnum(v).slice(0, 34).replace(/(.{4})(?=.)/g, "$1 ");
|
|
61
|
+
if (type === "bic") return alnum(v).slice(0, 11);
|
|
62
|
+
if (type === "vat") {
|
|
63
|
+
const c = alnum(v);
|
|
64
|
+
if (/^BE\d/.test(c)) {
|
|
65
|
+
const d = c.slice(2).replace(/\D/g, "").slice(0, 10);
|
|
66
|
+
return `BE${d.slice(0, 4)}${d.length > 4 ? "." + d.slice(4, 7) : ""}${d.length > 7 ? "." + d.slice(7, 10) : ""}`;
|
|
67
|
+
}
|
|
68
|
+
return c.slice(0, 14);
|
|
69
|
+
}
|
|
70
|
+
if (type === "phone") {
|
|
71
|
+
const plus = v.trimStart().startsWith("+") ? "+" : "";
|
|
72
|
+
return plus + v.replace(/[^\d ]/g, "").replace(/ {2,}/g, " ").replace(/^ /, "").slice(0, 20);
|
|
73
|
+
}
|
|
74
|
+
return applyPattern(type, v);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Caret position in `formatted` after `count` significant characters (letters/digits) of the raw value. */
|
|
78
|
+
export function caretFor(formatted, count) {
|
|
79
|
+
if (count <= 0) return 0;
|
|
80
|
+
let seen = 0;
|
|
81
|
+
for (let i = 0; i < formatted.length; i++) {
|
|
82
|
+
if (significant(formatted[i])) seen++;
|
|
83
|
+
if (seen >= count) return i + 1;
|
|
84
|
+
}
|
|
85
|
+
return formatted.length;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Number of significant characters (letters/digits) before `caret` in `value`. */
|
|
89
|
+
export function countBefore(value, caret) {
|
|
90
|
+
return [...String(value).slice(0, caret)].filter(significant).length;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** IBAN check: country length + mod-97. */
|
|
94
|
+
export function validIban(value) {
|
|
95
|
+
const c = alnum(value);
|
|
96
|
+
if (!/^[A-Z]{2}\d{2}[0-9A-Z]+$/.test(c)) return false;
|
|
97
|
+
const len = IBAN_LENGTHS[c.slice(0, 2)];
|
|
98
|
+
if (len && c.length !== len) return false;
|
|
99
|
+
if (!len && (c.length < 15 || c.length > 34)) return false;
|
|
100
|
+
const moved = (c.slice(4) + c.slice(0, 4)).replace(/[A-Z]/g, (ch) => String(ch.charCodeAt(0) - 55));
|
|
101
|
+
let rest = 0;
|
|
102
|
+
for (const digit of moved) rest = (rest * 10 + Number(digit)) % 97;
|
|
103
|
+
return rest === 1;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** VAT number: BE (10 digits, mod-97 check), NL (9 digits + B + 2 digits), other EU countries by format only. */
|
|
107
|
+
export function validVat(value) {
|
|
108
|
+
const c = alnum(value);
|
|
109
|
+
if (c.startsWith("BE")) {
|
|
110
|
+
const d = c.slice(2);
|
|
111
|
+
if (!/^[01]\d{9}$/.test(d)) return false;
|
|
112
|
+
return 97 - (Number(d.slice(0, 8)) % 97) === Number(d.slice(8));
|
|
113
|
+
}
|
|
114
|
+
if (c.startsWith("NL")) return /^NL\d{9}B\d{2}$/.test(c);
|
|
115
|
+
return /^(AT|BG|CY|CZ|DE|DK|EE|EL|ES|FI|FR|HR|HU|IE|IT|LT|LU|LV|MT|PL|PT|RO|SE|SI|SK|XI)[0-9A-Z]{2,13}$/.test(c);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* True when `value` is valid for `type` ("iban" | "bic" | "vat" | "email" | "phone"). An empty value is valid: whether a
|
|
120
|
+
* field may be empty is `required`, not this check. An unknown type is valid.
|
|
121
|
+
*/
|
|
122
|
+
export function validate(type, value) {
|
|
123
|
+
const v = String(value ?? "").trim();
|
|
124
|
+
if (!v || !type) return true;
|
|
125
|
+
if (type === "iban") return validIban(v);
|
|
126
|
+
if (type === "bic") return /^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/.test(alnum(v));
|
|
127
|
+
if (type === "vat") return validVat(v);
|
|
128
|
+
if (type === "email") return /^[^\s@]+@[^\s@.]+(\.[^\s@.]+)+$/.test(v);
|
|
129
|
+
if (type === "phone") return /^\+?[\d ()./-]+$/.test(v) && v.replace(/\D/g, "").length >= 6 && v.replace(/\D/g, "").length <= 15;
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Formats an input in place and keeps the caret behind the same character. Returns the new value. */
|
|
134
|
+
export function maskInput(input, type) {
|
|
135
|
+
const before = input.value;
|
|
136
|
+
const caret = input.selectionStart ?? before.length;
|
|
137
|
+
const count = countBefore(before, caret);
|
|
138
|
+
const next = applyMask(type, before);
|
|
139
|
+
if (next !== before) {
|
|
140
|
+
input.value = next;
|
|
141
|
+
const at = caret >= before.length ? next.length : caretFor(next, count);
|
|
142
|
+
try { input.setSelectionRange(at, at); } catch { /* type=email has no selection API */ }
|
|
143
|
+
}
|
|
144
|
+
return next;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Writes or removes the validation message of an input inside its .oc-field. */
|
|
148
|
+
function showMessage(input, valid, message) {
|
|
149
|
+
const field = input.closest(".oc-field");
|
|
150
|
+
if (!field) return;
|
|
151
|
+
const own = field.querySelector(":scope > .oc-field-error[data-oc-validation]");
|
|
152
|
+
const server = field.querySelector(":scope > .oc-field-error:not([data-oc-validation])");
|
|
153
|
+
const ids = (input.getAttribute("aria-describedby") || "").split(/\s+/).filter(Boolean);
|
|
154
|
+
if (valid || server) {
|
|
155
|
+
if (own) {
|
|
156
|
+
input.setAttribute("aria-describedby", ids.filter((id) => id !== own.id).join(" "));
|
|
157
|
+
if (!input.getAttribute("aria-describedby")) input.removeAttribute("aria-describedby");
|
|
158
|
+
own.remove();
|
|
159
|
+
}
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const p = own ?? field.ownerDocument.createElement("p");
|
|
163
|
+
if (!own) {
|
|
164
|
+
p.className = "oc-field-error";
|
|
165
|
+
p.dataset.ocValidation = "";
|
|
166
|
+
p.setAttribute("role", "alert");
|
|
167
|
+
p.id = `${input.id || "oc-input"}-validation`;
|
|
168
|
+
field.append(p);
|
|
169
|
+
}
|
|
170
|
+
p.textContent = message || "Invalid value";
|
|
171
|
+
if (!ids.includes(p.id)) input.setAttribute("aria-describedby", [...ids, p.id].join(" "));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Starts one input: data-oc-mask formats while typing, data-oc-validate checks on blur. Returns a stop function. */
|
|
175
|
+
export function bindInputMask(input) {
|
|
176
|
+
if (!input || input.dataset.ocMaskBound != null) return () => {};
|
|
177
|
+
input.dataset.ocMaskBound = "";
|
|
178
|
+
const mask = input.dataset.ocMask;
|
|
179
|
+
const type = input.dataset.ocValidate;
|
|
180
|
+
let checked = false;
|
|
181
|
+
const check = () => {
|
|
182
|
+
const valid = validate(type, input.value);
|
|
183
|
+
checked = true;
|
|
184
|
+
input.toggleAttribute("aria-invalid", !valid);
|
|
185
|
+
if (!valid) input.setAttribute("aria-invalid", "true");
|
|
186
|
+
input.classList.toggle("oc-input--error", !valid);
|
|
187
|
+
showMessage(input, valid, input.dataset.ocValidationMessage);
|
|
188
|
+
input.dispatchEvent(new (input.ownerDocument.defaultView ?? globalThis).CustomEvent("oc-input-validate", { bubbles: true, detail: { valid, value: input.value, type } }));
|
|
189
|
+
return valid;
|
|
190
|
+
};
|
|
191
|
+
const onInput = () => {
|
|
192
|
+
if (mask) maskInput(input, mask);
|
|
193
|
+
// Once a value was found wrong, the message goes away as soon as it is right.
|
|
194
|
+
if (type && checked && input.getAttribute("aria-invalid") === "true" && validate(type, input.value)) check();
|
|
195
|
+
};
|
|
196
|
+
const onBlur = () => { if (type) check(); };
|
|
197
|
+
input.addEventListener("input", onInput);
|
|
198
|
+
input.addEventListener("blur", onBlur);
|
|
199
|
+
if (mask && input.value) input.value = applyMask(mask, input.value);
|
|
200
|
+
return () => {
|
|
201
|
+
input.removeEventListener("input", onInput);
|
|
202
|
+
input.removeEventListener("blur", onBlur);
|
|
203
|
+
delete input.dataset.ocMaskBound;
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Starts every input[data-oc-mask] and input[data-oc-validate] in scope. Returns a function that stops them. */
|
|
208
|
+
export function initInputMask(scope = document) {
|
|
209
|
+
const stops = [...scope.querySelectorAll("input[data-oc-mask], input[data-oc-validate]")].map(bindInputMask);
|
|
210
|
+
return () => stops.forEach((stop) => stop());
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export const NAMED_MASKS = [...NAMED];
|
|
214
|
+
|
|
215
|
+
// Starts on its own as /js/oc-input-mask.js (`npx omniaui copy-js input-mask`), with ?auto in the URL or data-oc-auto on
|
|
216
|
+
// the tag.
|
|
217
|
+
function autoStart() {
|
|
218
|
+
if (typeof document === "undefined") return false;
|
|
219
|
+
const url = String(import.meta.url);
|
|
220
|
+
if (url.includes("oc-input-mask") || /[?&]auto(=|&|$)/.test(url)) return true;
|
|
221
|
+
const file = url.split(/[?#]/)[0];
|
|
222
|
+
return [...document.querySelectorAll("script[data-oc-auto][src]")].some((s) => s.src.split(/[?#]/)[0] === file);
|
|
223
|
+
}
|
|
224
|
+
if (autoStart()) {
|
|
225
|
+
const boot = () => initInputMask(document);
|
|
226
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot);
|
|
227
|
+
else boot();
|
|
228
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// File: packages/ui/src/nav-overflow.d.ts
|
|
2
|
+
// Types for the app header "More" overflow ([data-oc-nav-overflow] around a <ul> of links).
|
|
3
|
+
|
|
4
|
+
/** How many items fit: all when they fit in `available`, otherwise as many as fit next to the More button. */
|
|
5
|
+
export function fitNavItems(widths: number[], available: number, moreWidth: number, gap?: number): number;
|
|
6
|
+
/** Shows the first `visible` <li> items of `list`, hides the rest (data-oc-hidden) and copies their links into the More menu. Returns the hidden count. */
|
|
7
|
+
export function applyNavOverflow(list: HTMLElement, more: HTMLElement, visible: number): number;
|
|
8
|
+
/** Starts one [data-oc-nav-overflow] nav (ResizeObserver); returns a function that stops it. Event: oc-nav-overflow ({ visible, hidden }). */
|
|
9
|
+
export function bindNavOverflow(nav: HTMLElement): () => void;
|
|
10
|
+
/** Starts every [data-oc-nav-overflow] nav in scope. */
|
|
11
|
+
export function initNavOverflow(scope?: ParentNode): Array<() => void>;
|