@loccal/core 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +34 -3
- package/dist/a11y/dismiss.d.ts +8 -0
- package/dist/a11y/dismiss.js +30 -0
- package/dist/a11y/focus.d.ts +11 -0
- package/dist/a11y/focus.js +89 -0
- package/dist/a11y/ids.d.ts +2 -0
- package/dist/a11y/ids.js +6 -0
- package/dist/a11y/menu.d.ts +19 -0
- package/dist/a11y/menu.js +65 -0
- package/dist/a11y/menubar.d.ts +2 -0
- package/dist/a11y/menubar.js +70 -0
- package/dist/a11y/position.d.ts +35 -0
- package/dist/a11y/position.js +66 -0
- package/dist/dom.d.ts +2 -0
- package/dist/dom.js +8 -0
- package/dist/i18n.d.ts +21 -0
- package/dist/i18n.js +64 -0
- package/dist/icon-shapes.d.ts +14 -0
- package/dist/icon-shapes.js +47 -0
- package/dist/icons.d.ts +11 -0
- package/dist/icons.js +14 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +35 -0
- package/dist/labels.d.ts +186 -0
- package/dist/labels.js +61 -0
- package/dist/machine.d.ts +18 -0
- package/dist/machine.js +35 -0
- package/dist/machines/button.d.ts +30 -0
- package/dist/machines/button.js +56 -0
- package/dist/machines/calendar.d.ts +132 -0
- package/dist/machines/calendar.js +251 -0
- package/dist/machines/choice.d.ts +27 -0
- package/dist/machines/choice.js +88 -0
- package/dist/machines/command.d.ts +128 -0
- package/dist/machines/command.js +148 -0
- package/dist/machines/data-table.d.ts +127 -0
- package/dist/machines/data-table.js +219 -0
- package/dist/machines/dialog.d.ts +40 -0
- package/dist/machines/dialog.js +127 -0
- package/dist/machines/disclosure.d.ts +26 -0
- package/dist/machines/disclosure.js +74 -0
- package/dist/machines/dropzone.d.ts +68 -0
- package/dist/machines/dropzone.js +108 -0
- package/dist/machines/file-tree.d.ts +82 -0
- package/dist/machines/file-tree.js +120 -0
- package/dist/machines/floating.d.ts +38 -0
- package/dist/machines/floating.js +184 -0
- package/dist/machines/input-otp.d.ts +42 -0
- package/dist/machines/input-otp.js +85 -0
- package/dist/machines/locale-switch.d.ts +19 -0
- package/dist/machines/locale-switch.js +49 -0
- package/dist/machines/navigation-menu.d.ts +72 -0
- package/dist/machines/navigation-menu.js +191 -0
- package/dist/machines/number-field.d.ts +53 -0
- package/dist/machines/number-field.js +117 -0
- package/dist/machines/resizable.d.ts +38 -0
- package/dist/machines/resizable.js +128 -0
- package/dist/machines/select.d.ts +77 -0
- package/dist/machines/select.js +221 -0
- package/dist/machines/status-bar.d.ts +26 -0
- package/dist/machines/status-bar.js +31 -0
- package/dist/machines/tab-list.d.ts +86 -0
- package/dist/machines/tab-list.js +285 -0
- package/dist/machines/tabs.d.ts +42 -0
- package/dist/machines/tabs.js +138 -0
- package/dist/machines/toast.d.ts +66 -0
- package/dist/machines/toast.js +92 -0
- package/dist/machines/toggle.d.ts +37 -0
- package/dist/machines/toggle.js +78 -0
- package/dist/machines/tree.d.ts +48 -0
- package/dist/machines/tree.js +141 -0
- package/dist/shell-meta.d.ts +12 -0
- package/dist/shell-meta.js +48 -0
- package/dist/static.d.ts +795 -0
- package/dist/static.js +194 -0
- package/package.json +112 -6
- package/src/tokens/tokens.css +106 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Marcelo Matzembacher
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @loccal/core
|
|
2
|
+
|
|
3
|
+
Nucleo headless do Loccal UI: maquinas de estado, teclado, ARIA, foco, i18n e tokens CSS. Sem dependencias e sem acesso a DOM no import.
|
|
4
|
+
|
|
5
|
+
## Instalacao
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @loccal/core
|
|
9
|
+
pnpm add @loccal/core
|
|
10
|
+
yarn add @loccal/core
|
|
11
|
+
bun add @loccal/core
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Nenhum peer necessario. O nucleo e usado pelos adaptadores `@loccal/react`, `@loccal/vue`, `@loccal/svelte` e pelos custom elements de `@loccal/ui/wc`.
|
|
15
|
+
|
|
16
|
+
## Uso
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { createDialog } from "@loccal/core/machines/dialog"
|
|
20
|
+
import "@loccal/core/tokens.css"
|
|
21
|
+
|
|
22
|
+
const dialog = createDialog({ open: false }, (open) => console.log("aberto:", open))
|
|
23
|
+
dialog.send({ type: "OPEN" })
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Cada componente e um subpath, sem barrel.
|
|
27
|
+
|
|
28
|
+
## Versionamento
|
|
29
|
+
|
|
30
|
+
Semver. Veja o [changelog](../ui/CHANGELOG.md) e a documentacao do Loccal UI.
|
|
31
|
+
|
|
32
|
+
## Licenca
|
|
33
|
+
|
|
34
|
+
MIT.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface DismissOptions {
|
|
2
|
+
/** Elementos considerados "dentro" (a camada e o gatilho). */
|
|
3
|
+
inside: HTMLElement[];
|
|
4
|
+
onDismiss(reason: "escape" | "outside"): void;
|
|
5
|
+
escape?: boolean;
|
|
6
|
+
outside?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare function dismissable(o: DismissOptions): () => void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Fechamento de camadas flutuantes: Escape e clique/foco fora. Pilha: so a camada do topo reage ao Escape.
|
|
2
|
+
const stack = [];
|
|
3
|
+
export function dismissable(o) {
|
|
4
|
+
const doc = o.inside[0].ownerDocument;
|
|
5
|
+
const token = {};
|
|
6
|
+
stack.push(token);
|
|
7
|
+
const onKey = (e) => {
|
|
8
|
+
if (o.escape === false || e.key !== "Escape" || e.defaultPrevented || stack[stack.length - 1] !== token)
|
|
9
|
+
return;
|
|
10
|
+
e.stopPropagation();
|
|
11
|
+
o.onDismiss("escape");
|
|
12
|
+
};
|
|
13
|
+
const onDown = (e) => {
|
|
14
|
+
if (o.outside === false)
|
|
15
|
+
return;
|
|
16
|
+
const t = e.target;
|
|
17
|
+
if (t && o.inside.some((el) => el.contains(t)))
|
|
18
|
+
return;
|
|
19
|
+
o.onDismiss("outside");
|
|
20
|
+
};
|
|
21
|
+
doc.addEventListener("keydown", onKey, true);
|
|
22
|
+
doc.addEventListener("pointerdown", onDown, true);
|
|
23
|
+
return () => {
|
|
24
|
+
doc.removeEventListener("keydown", onKey, true);
|
|
25
|
+
doc.removeEventListener("pointerdown", onDown, true);
|
|
26
|
+
const i = stack.indexOf(token);
|
|
27
|
+
if (i >= 0)
|
|
28
|
+
stack.splice(i, 1);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare function getTabbables(root: Element): HTMLElement[];
|
|
2
|
+
/** Mantem Tab/Shift+Tab dentro de `container`. Devolve a funcao de limpeza. */
|
|
3
|
+
export declare function trapFocus(container: HTMLElement): () => void;
|
|
4
|
+
/** Foca o elemento inicial: `[data-autofocus]`, senao o primeiro focavel, senao o proprio container. */
|
|
5
|
+
export declare function focusInitial(container: HTMLElement): void;
|
|
6
|
+
/** Guarda o elemento focado e o restaura depois. */
|
|
7
|
+
export declare function captureFocus(doc: Document): () => void;
|
|
8
|
+
/** Trava a rolagem do body (com contagem, para dialogos empilhados). */
|
|
9
|
+
export declare function lockScroll(doc: Document): () => void;
|
|
10
|
+
/** Marca como `inert` tudo fora de `content` (irmaos de cada ancestral ate o body). */
|
|
11
|
+
export declare function inertOutside(content: HTMLElement): () => void;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Foco: elementos focaveis, armadilha de foco, retorno de foco, scroll lock e inert.
|
|
2
|
+
const TABBABLE = [
|
|
3
|
+
"a[href]",
|
|
4
|
+
"button:not([disabled])",
|
|
5
|
+
"input:not([disabled]):not([type=hidden])",
|
|
6
|
+
"select:not([disabled])",
|
|
7
|
+
"textarea:not([disabled])",
|
|
8
|
+
"[tabindex]:not([tabindex='-1'])",
|
|
9
|
+
"[contenteditable='true']",
|
|
10
|
+
].join(",");
|
|
11
|
+
export function getTabbables(root) {
|
|
12
|
+
return Array.from(root.querySelectorAll(TABBABLE)).filter((el) => !el.hasAttribute("hidden") && !el.closest("[hidden],[inert]") && el.getAttribute("aria-hidden") !== "true");
|
|
13
|
+
}
|
|
14
|
+
/** Mantem Tab/Shift+Tab dentro de `container`. Devolve a funcao de limpeza. */
|
|
15
|
+
export function trapFocus(container) {
|
|
16
|
+
const onKeyDown = (e) => {
|
|
17
|
+
if (e.key !== "Tab")
|
|
18
|
+
return;
|
|
19
|
+
const items = getTabbables(container);
|
|
20
|
+
if (items.length === 0) {
|
|
21
|
+
e.preventDefault();
|
|
22
|
+
container.focus();
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const first = items[0];
|
|
26
|
+
const last = items[items.length - 1];
|
|
27
|
+
const active = container.ownerDocument.activeElement;
|
|
28
|
+
if (e.shiftKey && (active === first || active === container || !container.contains(active))) {
|
|
29
|
+
e.preventDefault();
|
|
30
|
+
last.focus();
|
|
31
|
+
}
|
|
32
|
+
else if (!e.shiftKey && (active === last || !container.contains(active))) {
|
|
33
|
+
e.preventDefault();
|
|
34
|
+
first.focus();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
container.addEventListener("keydown", onKeyDown);
|
|
38
|
+
return () => container.removeEventListener("keydown", onKeyDown);
|
|
39
|
+
}
|
|
40
|
+
/** Foca o elemento inicial: `[data-autofocus]`, senao o primeiro focavel, senao o proprio container. */
|
|
41
|
+
export function focusInitial(container) {
|
|
42
|
+
const target = container.querySelector("[data-autofocus]") ?? getTabbables(container)[0] ?? container;
|
|
43
|
+
if (target === container && !container.hasAttribute("tabindex"))
|
|
44
|
+
container.setAttribute("tabindex", "-1");
|
|
45
|
+
target.focus();
|
|
46
|
+
}
|
|
47
|
+
/** Guarda o elemento focado e o restaura depois. */
|
|
48
|
+
export function captureFocus(doc) {
|
|
49
|
+
const previous = doc.activeElement;
|
|
50
|
+
return () => {
|
|
51
|
+
if (previous && previous.isConnected && typeof previous.focus === "function")
|
|
52
|
+
previous.focus();
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
let lockCount = 0;
|
|
56
|
+
let savedOverflow = "";
|
|
57
|
+
/** Trava a rolagem do body (com contagem, para dialogos empilhados). */
|
|
58
|
+
export function lockScroll(doc) {
|
|
59
|
+
if (lockCount === 0) {
|
|
60
|
+
savedOverflow = doc.body.style.overflow;
|
|
61
|
+
doc.body.style.overflow = "hidden";
|
|
62
|
+
}
|
|
63
|
+
lockCount += 1;
|
|
64
|
+
let released = false;
|
|
65
|
+
return () => {
|
|
66
|
+
if (released)
|
|
67
|
+
return;
|
|
68
|
+
released = true;
|
|
69
|
+
lockCount -= 1;
|
|
70
|
+
if (lockCount === 0)
|
|
71
|
+
doc.body.style.overflow = savedOverflow;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** Marca como `inert` tudo fora de `content` (irmaos de cada ancestral ate o body). */
|
|
75
|
+
export function inertOutside(content) {
|
|
76
|
+
const changed = [];
|
|
77
|
+
let node = content;
|
|
78
|
+
while (node && node.parentElement && node !== node.ownerDocument.body) {
|
|
79
|
+
const parent = node.parentElement;
|
|
80
|
+
for (const sibling of Array.from(parent.children)) {
|
|
81
|
+
if (sibling !== node && sibling instanceof HTMLElement && !sibling.hasAttribute("inert") && sibling.tagName !== "SCRIPT" && sibling.dataset.part !== "backdrop") {
|
|
82
|
+
sibling.setAttribute("inert", "");
|
|
83
|
+
changed.push(sibling);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
node = parent;
|
|
87
|
+
}
|
|
88
|
+
return () => changed.forEach((el) => el.removeAttribute("inert"));
|
|
89
|
+
}
|
package/dist/a11y/ids.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
let counter = 0;
|
|
2
|
+
/** Id estavel por instancia (equivalente vanilla de useId). Nao usar para markup de servidor: os adaptadores usam o id nativo do framework. */
|
|
3
|
+
export function uid(prefix = "loccal") {
|
|
4
|
+
counter += 1;
|
|
5
|
+
return `${prefix}-${counter.toString(36)}`;
|
|
6
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Attrs } from "../machine.ts";
|
|
2
|
+
export interface MenuItem {
|
|
3
|
+
label: string;
|
|
4
|
+
value?: string;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
/** Item de acao destrutiva (estilo). */
|
|
7
|
+
destructive?: boolean;
|
|
8
|
+
separator?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare function getMenuItemProps(item: MenuItem): Attrs;
|
|
11
|
+
export declare function focusFirstItem(root: HTMLElement, last?: boolean): void;
|
|
12
|
+
export interface MenuBindOptions {
|
|
13
|
+
/** Ativacao (clique, Enter ou Espaco) de um item habilitado. */
|
|
14
|
+
onSelect(item: HTMLElement): void;
|
|
15
|
+
/** Tab ou Escape dentro do menu. */
|
|
16
|
+
onClose?(reason: "tab"): void;
|
|
17
|
+
}
|
|
18
|
+
/** Liga o teclado ao conteudo do menu. Retorna a limpeza. */
|
|
19
|
+
export declare function bindMenuKeys(content: HTMLElement, o: MenuBindOptions): () => void;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export function getMenuItemProps(item) {
|
|
2
|
+
if (item.separator)
|
|
3
|
+
return { "data-part": "separator", role: "separator" };
|
|
4
|
+
return { "data-part": "item", role: "menuitem", tabindex: -1, disabled: item.disabled || undefined, "aria-disabled": item.disabled ? "true" : undefined, "data-value": item.value ?? item.label, "data-variant": item.destructive ? "destructive" : undefined };
|
|
5
|
+
}
|
|
6
|
+
const enabledItems = (root) => Array.from(root.querySelectorAll("[role=menuitem]")).filter((i) => !i.hasAttribute("disabled") && i.getAttribute("aria-disabled") !== "true");
|
|
7
|
+
export function focusFirstItem(root, last = false) {
|
|
8
|
+
const list = enabledItems(root);
|
|
9
|
+
(last ? list[list.length - 1] : list[0])?.focus();
|
|
10
|
+
}
|
|
11
|
+
/** Liga o teclado ao conteudo do menu. Retorna a limpeza. */
|
|
12
|
+
export function bindMenuKeys(content, o) {
|
|
13
|
+
let buffer = "";
|
|
14
|
+
let timer;
|
|
15
|
+
const onKey = (e) => {
|
|
16
|
+
const items = enabledItems(content);
|
|
17
|
+
if (!items.length)
|
|
18
|
+
return;
|
|
19
|
+
const cur = items.indexOf(content.ownerDocument.activeElement);
|
|
20
|
+
const go = (i) => {
|
|
21
|
+
e.preventDefault();
|
|
22
|
+
items[(i + items.length) % items.length].focus();
|
|
23
|
+
};
|
|
24
|
+
switch (e.key) {
|
|
25
|
+
case "ArrowDown": return go(cur + 1);
|
|
26
|
+
case "ArrowUp": return go(cur < 0 ? items.length - 1 : cur - 1);
|
|
27
|
+
case "Home": return go(0);
|
|
28
|
+
case "End": return go(items.length - 1);
|
|
29
|
+
case "Tab": return o.onClose?.("tab");
|
|
30
|
+
case "Enter":
|
|
31
|
+
case " ":
|
|
32
|
+
if (cur >= 0) {
|
|
33
|
+
e.preventDefault();
|
|
34
|
+
o.onSelect(items[cur]);
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
default:
|
|
38
|
+
if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
|
|
39
|
+
buffer += e.key.toLowerCase();
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
timer = setTimeout(() => (buffer = ""), 500);
|
|
42
|
+
const order = [...items.slice(cur + 1), ...items.slice(0, cur + 1)];
|
|
43
|
+
// Mesma letra repetida avanca para o proximo item que comeca com ela.
|
|
44
|
+
const needle = /^(.)\1*$/.test(buffer) ? buffer[0] : buffer;
|
|
45
|
+
const hit = order.find((i) => (i.textContent ?? "").trim().toLowerCase().startsWith(needle));
|
|
46
|
+
if (hit) {
|
|
47
|
+
e.preventDefault();
|
|
48
|
+
hit.focus();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const onClick = (e) => {
|
|
54
|
+
const item = e.target.closest?.("[role=menuitem]");
|
|
55
|
+
if (item && content.contains(item) && !item.hasAttribute("disabled") && item.getAttribute("aria-disabled") !== "true")
|
|
56
|
+
o.onSelect(item);
|
|
57
|
+
};
|
|
58
|
+
content.addEventListener("keydown", onKey);
|
|
59
|
+
content.addEventListener("click", onClick);
|
|
60
|
+
return () => {
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
content.removeEventListener("keydown", onKey);
|
|
63
|
+
content.removeEventListener("click", onClick);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Barra de menus (WAI-ARIA menubar) sobre dropdowns: roving nos gatilhos, setas trocam de menu, hover troca com um aberto.
|
|
2
|
+
const TRIGGER = "[data-part=trigger]";
|
|
3
|
+
/** Liga o comportamento de menubar a um container que tem varios menus (gatilhos `[data-part=trigger]`). */
|
|
4
|
+
export function bindMenubar(root) {
|
|
5
|
+
const triggers = () => Array.from(root.querySelectorAll(TRIGGER));
|
|
6
|
+
const isOpen = (t) => t.getAttribute("aria-expanded") === "true";
|
|
7
|
+
const openOne = () => triggers().find(isOpen);
|
|
8
|
+
const sync = (active) => {
|
|
9
|
+
const list = triggers();
|
|
10
|
+
const cur = active ?? list.find((t) => t.tabIndex === 0) ?? list[0];
|
|
11
|
+
list.forEach((t) => {
|
|
12
|
+
t.setAttribute("role", "menuitem");
|
|
13
|
+
t.tabIndex = t === cur ? 0 : -1;
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
const triggerOf = (t) => {
|
|
17
|
+
const el = t;
|
|
18
|
+
const own = el?.closest?.(TRIGGER);
|
|
19
|
+
if (own && root.contains(own))
|
|
20
|
+
return own;
|
|
21
|
+
// De dentro de um menu aberto: o gatilho aberto e o dono.
|
|
22
|
+
return el && root.contains(el) ? openOne() : undefined;
|
|
23
|
+
};
|
|
24
|
+
const move = (from, dir) => {
|
|
25
|
+
const list = triggers();
|
|
26
|
+
const next = list[(list.indexOf(from) + dir + list.length) % list.length];
|
|
27
|
+
const wasOpen = isOpen(from);
|
|
28
|
+
if (wasOpen)
|
|
29
|
+
from.click();
|
|
30
|
+
sync(next);
|
|
31
|
+
next.focus();
|
|
32
|
+
if (wasOpen)
|
|
33
|
+
next.click();
|
|
34
|
+
};
|
|
35
|
+
const onKey = (e) => {
|
|
36
|
+
if (e.key !== "ArrowRight" && e.key !== "ArrowLeft")
|
|
37
|
+
return;
|
|
38
|
+
const from = triggerOf(e.target);
|
|
39
|
+
if (!from)
|
|
40
|
+
return;
|
|
41
|
+
e.preventDefault();
|
|
42
|
+
move(from, e.key === "ArrowRight" ? 1 : -1);
|
|
43
|
+
};
|
|
44
|
+
const onEnter = (e) => {
|
|
45
|
+
const t = e.target.closest?.(TRIGGER);
|
|
46
|
+
const open = openOne();
|
|
47
|
+
if (t && open && t !== open && root.contains(t)) {
|
|
48
|
+
open.click();
|
|
49
|
+
sync(t);
|
|
50
|
+
t.click();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const onFocusIn = (e) => {
|
|
54
|
+
const t = e.target.closest?.(TRIGGER);
|
|
55
|
+
if (t && root.contains(t))
|
|
56
|
+
sync(t);
|
|
57
|
+
};
|
|
58
|
+
sync();
|
|
59
|
+
const mo = new MutationObserver(() => sync());
|
|
60
|
+
mo.observe(root, { childList: true, subtree: true });
|
|
61
|
+
root.addEventListener("keydown", onKey);
|
|
62
|
+
root.addEventListener("pointerover", onEnter);
|
|
63
|
+
root.addEventListener("focusin", onFocusIn);
|
|
64
|
+
return () => {
|
|
65
|
+
mo.disconnect();
|
|
66
|
+
root.removeEventListener("keydown", onKey);
|
|
67
|
+
root.removeEventListener("pointerover", onEnter);
|
|
68
|
+
root.removeEventListener("focusin", onFocusIn);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type Side = "top" | "right" | "bottom" | "left";
|
|
2
|
+
export type Align = "start" | "center" | "end";
|
|
3
|
+
export interface Box {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
}
|
|
9
|
+
export interface PositionOptions {
|
|
10
|
+
side?: Side;
|
|
11
|
+
align?: Align;
|
|
12
|
+
/** Distancia entre ancora e camada, em px. */
|
|
13
|
+
offset?: number;
|
|
14
|
+
/** Inverte o lado quando nao cabe. */
|
|
15
|
+
flip?: boolean;
|
|
16
|
+
/** Mantem a camada dentro da viewport (a partir de `padding`). */
|
|
17
|
+
shift?: boolean;
|
|
18
|
+
padding?: number;
|
|
19
|
+
}
|
|
20
|
+
export interface PositionResult {
|
|
21
|
+
x: number;
|
|
22
|
+
y: number;
|
|
23
|
+
side: Side;
|
|
24
|
+
}
|
|
25
|
+
/** Calcula x/y (coordenadas da viewport) e o lado efetivo. */
|
|
26
|
+
export declare function computePosition(anchor: Box, floating: Box, viewport: Box, o?: PositionOptions): PositionResult;
|
|
27
|
+
/** Posiciona `floating` junto de `anchor` (position: fixed) e reposiciona em scroll/resize. Retorna a limpeza. */
|
|
28
|
+
export declare function autoPosition(anchor: {
|
|
29
|
+
getBoundingClientRect(): {
|
|
30
|
+
left: number;
|
|
31
|
+
top: number;
|
|
32
|
+
width: number;
|
|
33
|
+
height: number;
|
|
34
|
+
};
|
|
35
|
+
}, floating: HTMLElement, o?: PositionOptions | (() => PositionOptions)): () => void;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const OPPOSITE = { top: "bottom", bottom: "top", left: "right", right: "left" };
|
|
2
|
+
function place(a, f, side, align, offset) {
|
|
3
|
+
const horizontal = side === "top" || side === "bottom";
|
|
4
|
+
let x;
|
|
5
|
+
let y;
|
|
6
|
+
if (horizontal) {
|
|
7
|
+
y = side === "top" ? a.y - f.height - offset : a.y + a.height + offset;
|
|
8
|
+
x = align === "start" ? a.x : align === "end" ? a.x + a.width - f.width : a.x + a.width / 2 - f.width / 2;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
x = side === "left" ? a.x - f.width - offset : a.x + a.width + offset;
|
|
12
|
+
y = align === "start" ? a.y : align === "end" ? a.y + a.height - f.height : a.y + a.height / 2 - f.height / 2;
|
|
13
|
+
}
|
|
14
|
+
return { x, y };
|
|
15
|
+
}
|
|
16
|
+
const fits = (p, f, v, side) => {
|
|
17
|
+
switch (side) {
|
|
18
|
+
case "top": return p.y >= v.y;
|
|
19
|
+
case "bottom": return p.y + f.height <= v.y + v.height;
|
|
20
|
+
case "left": return p.x >= v.x;
|
|
21
|
+
case "right": return p.x + f.width <= v.x + v.width;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
/** Calcula x/y (coordenadas da viewport) e o lado efetivo. */
|
|
25
|
+
export function computePosition(anchor, floating, viewport, o = {}) {
|
|
26
|
+
const { align = "center", offset = 4, flip = true, shift = true, padding = 4 } = o;
|
|
27
|
+
let side = o.side ?? "bottom";
|
|
28
|
+
let pos = place(anchor, floating, side, align, offset);
|
|
29
|
+
if (flip && !fits(pos, floating, viewport, side)) {
|
|
30
|
+
const alt = place(anchor, floating, OPPOSITE[side], align, offset);
|
|
31
|
+
if (fits(alt, floating, viewport, OPPOSITE[side])) {
|
|
32
|
+
side = OPPOSITE[side];
|
|
33
|
+
pos = alt;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (shift) {
|
|
37
|
+
const clamp = (n, lo, hi) => Math.max(lo, Math.min(n, Math.max(lo, hi)));
|
|
38
|
+
pos = {
|
|
39
|
+
x: clamp(pos.x, viewport.x + padding, viewport.x + viewport.width - floating.width - padding),
|
|
40
|
+
y: clamp(pos.y, viewport.y + padding, viewport.y + viewport.height - floating.height - padding),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return { ...pos, side };
|
|
44
|
+
}
|
|
45
|
+
const box = (r) => ({ x: r.left, y: r.top, width: r.width, height: r.height });
|
|
46
|
+
/** Posiciona `floating` junto de `anchor` (position: fixed) e reposiciona em scroll/resize. Retorna a limpeza. */
|
|
47
|
+
export function autoPosition(anchor, floating, o = {}) {
|
|
48
|
+
const win = floating.ownerDocument.defaultView ?? window;
|
|
49
|
+
const update = () => {
|
|
50
|
+
const opts = typeof o === "function" ? o() : o;
|
|
51
|
+
const vp = { x: 0, y: 0, width: win.innerWidth, height: win.innerHeight };
|
|
52
|
+
const r = computePosition(box(anchor.getBoundingClientRect()), box(floating.getBoundingClientRect()), vp, opts);
|
|
53
|
+
floating.style.position = "fixed";
|
|
54
|
+
floating.style.left = "0px";
|
|
55
|
+
floating.style.top = "0px";
|
|
56
|
+
floating.style.transform = `translate(${Math.round(r.x)}px, ${Math.round(r.y)}px)`;
|
|
57
|
+
floating.setAttribute("data-placement", r.side);
|
|
58
|
+
};
|
|
59
|
+
update();
|
|
60
|
+
win.addEventListener("resize", update);
|
|
61
|
+
win.addEventListener("scroll", update, true);
|
|
62
|
+
return () => {
|
|
63
|
+
win.removeEventListener("resize", update);
|
|
64
|
+
win.removeEventListener("scroll", update, true);
|
|
65
|
+
};
|
|
66
|
+
}
|
package/dist/dom.d.ts
ADDED
package/dist/dom.js
ADDED
package/dist/i18n.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type Locale = "pt-BR" | "en-US" | "es-ES";
|
|
2
|
+
export declare const LOCALES: {
|
|
3
|
+
id: Locale;
|
|
4
|
+
short: string;
|
|
5
|
+
label: string;
|
|
6
|
+
}[];
|
|
7
|
+
export declare const LOCALE_STORAGE_KEY = "loccal-locale";
|
|
8
|
+
export declare const LOCALE_EVENT = "loccal:locale-changed";
|
|
9
|
+
export declare const DEFAULT_LOCALE: Locale;
|
|
10
|
+
export declare function isLocale(value: unknown): value is Locale;
|
|
11
|
+
export declare function detectLocale(stored?: string | null, navigatorLanguage?: string): Locale;
|
|
12
|
+
export declare function pick<T>(locale: Locale, values: Partial<Record<Locale, T>> & {
|
|
13
|
+
"pt-BR": T;
|
|
14
|
+
}): T;
|
|
15
|
+
/** Idioma atual (valor salvo, depois navegador). No servidor devolve o padrao. */
|
|
16
|
+
export declare function getLocale(): Locale;
|
|
17
|
+
export declare function setLocale(locale: Locale): void;
|
|
18
|
+
/** Observa trocas de idioma. Devolve a funcao de limpeza. */
|
|
19
|
+
export declare function onLocaleChange(fn: (locale: Locale) => void): () => void;
|
|
20
|
+
/** Somente para testes. */
|
|
21
|
+
export declare function resetLocale(): void;
|
package/dist/i18n.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// i18n do Loccal Core (pt-BR, en-US, es-ES). Mesmo contrato de `@loccal/ui/lib/i18n-core`.
|
|
2
|
+
export const LOCALES = [
|
|
3
|
+
{ id: "pt-BR", short: "PT", label: "Português (Brasil)" },
|
|
4
|
+
{ id: "en-US", short: "EN", label: "English (US)" },
|
|
5
|
+
{ id: "es-ES", short: "ES", label: "Español" },
|
|
6
|
+
];
|
|
7
|
+
export const LOCALE_STORAGE_KEY = "loccal-locale";
|
|
8
|
+
export const LOCALE_EVENT = "loccal:locale-changed";
|
|
9
|
+
export const DEFAULT_LOCALE = "pt-BR";
|
|
10
|
+
export function isLocale(value) {
|
|
11
|
+
return value === "pt-BR" || value === "en-US" || value === "es-ES";
|
|
12
|
+
}
|
|
13
|
+
export function detectLocale(stored, navigatorLanguage) {
|
|
14
|
+
if (isLocale(stored))
|
|
15
|
+
return stored;
|
|
16
|
+
const lang = (navigatorLanguage ?? "").toLowerCase();
|
|
17
|
+
if (lang.startsWith("pt"))
|
|
18
|
+
return "pt-BR";
|
|
19
|
+
if (lang.startsWith("es"))
|
|
20
|
+
return "es-ES";
|
|
21
|
+
if (lang.startsWith("en"))
|
|
22
|
+
return "en-US";
|
|
23
|
+
return DEFAULT_LOCALE;
|
|
24
|
+
}
|
|
25
|
+
export function pick(locale, values) {
|
|
26
|
+
return values[locale] ?? values["pt-BR"];
|
|
27
|
+
}
|
|
28
|
+
let current = null;
|
|
29
|
+
/** Idioma atual (valor salvo, depois navegador). No servidor devolve o padrao. */
|
|
30
|
+
export function getLocale() {
|
|
31
|
+
if (current)
|
|
32
|
+
return current;
|
|
33
|
+
if (typeof window === "undefined")
|
|
34
|
+
return DEFAULT_LOCALE;
|
|
35
|
+
let stored = null;
|
|
36
|
+
try {
|
|
37
|
+
stored = window.localStorage.getItem(LOCALE_STORAGE_KEY);
|
|
38
|
+
}
|
|
39
|
+
catch { }
|
|
40
|
+
current = detectLocale(stored, window.navigator?.language);
|
|
41
|
+
return current;
|
|
42
|
+
}
|
|
43
|
+
export function setLocale(locale) {
|
|
44
|
+
current = locale;
|
|
45
|
+
if (typeof window === "undefined")
|
|
46
|
+
return;
|
|
47
|
+
try {
|
|
48
|
+
window.localStorage.setItem(LOCALE_STORAGE_KEY, locale);
|
|
49
|
+
}
|
|
50
|
+
catch { }
|
|
51
|
+
window.dispatchEvent(new CustomEvent(LOCALE_EVENT, { detail: locale }));
|
|
52
|
+
}
|
|
53
|
+
/** Observa trocas de idioma. Devolve a funcao de limpeza. */
|
|
54
|
+
export function onLocaleChange(fn) {
|
|
55
|
+
if (typeof window === "undefined")
|
|
56
|
+
return () => { };
|
|
57
|
+
const handler = (e) => fn(e.detail ?? getLocale());
|
|
58
|
+
window.addEventListener(LOCALE_EVENT, handler);
|
|
59
|
+
return () => window.removeEventListener(LOCALE_EVENT, handler);
|
|
60
|
+
}
|
|
61
|
+
/** Somente para testes. */
|
|
62
|
+
export function resetLocale() {
|
|
63
|
+
current = null;
|
|
64
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type ShapeAttrs = {
|
|
2
|
+
tag: "path" | "circle" | "rect" | "line";
|
|
3
|
+
attrs: Record<string, string | number>;
|
|
4
|
+
};
|
|
5
|
+
export declare const ICON_GRID = 24;
|
|
6
|
+
export declare const ICON_STROKE = 1.75;
|
|
7
|
+
export declare function parseShape(shape: string): ShapeAttrs;
|
|
8
|
+
export declare function shapeToSvg(shape: string): string;
|
|
9
|
+
export declare function iconToSvg(shapes: readonly string[], opts?: {
|
|
10
|
+
size?: number;
|
|
11
|
+
strokeWidth?: number;
|
|
12
|
+
}): string;
|
|
13
|
+
/** "git-branch" -> "GitBranchIcon" */
|
|
14
|
+
export declare function iconComponentName(name: string): string;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Gramatica compacta das formas dos icones loccal (grade 24x24, traco, currentColor).
|
|
2
|
+
// Cada forma e uma string:
|
|
3
|
+
// "M..." caminho SVG (atributo d)
|
|
4
|
+
// "c cx cy r" circulo
|
|
5
|
+
// "r x y w h [rx]" retangulo
|
|
6
|
+
// "l x1 y1 x2 y2" linha
|
|
7
|
+
// Mantido sem dependencias para ser testavel com node --test.
|
|
8
|
+
export const ICON_GRID = 24;
|
|
9
|
+
export const ICON_STROKE = 1.75;
|
|
10
|
+
export function parseShape(shape) {
|
|
11
|
+
const s = shape.trim();
|
|
12
|
+
if (s.startsWith("M"))
|
|
13
|
+
return { tag: "path", attrs: { d: s } };
|
|
14
|
+
const [kind, ...rest] = s.split(/\s+/);
|
|
15
|
+
const n = rest.map(Number);
|
|
16
|
+
if (n.some((v) => Number.isNaN(v)))
|
|
17
|
+
throw new Error(`forma invalida: "${shape}"`);
|
|
18
|
+
switch (kind) {
|
|
19
|
+
case "c":
|
|
20
|
+
if (n.length !== 3)
|
|
21
|
+
throw new Error(`circulo precisa de 3 numeros: "${shape}"`);
|
|
22
|
+
return { tag: "circle", attrs: { cx: n[0], cy: n[1], r: n[2] } };
|
|
23
|
+
case "r":
|
|
24
|
+
if (n.length !== 4 && n.length !== 5)
|
|
25
|
+
throw new Error(`retangulo precisa de 4 ou 5 numeros: "${shape}"`);
|
|
26
|
+
return { tag: "rect", attrs: n.length === 5 ? { x: n[0], y: n[1], width: n[2], height: n[3], rx: n[4] } : { x: n[0], y: n[1], width: n[2], height: n[3] } };
|
|
27
|
+
case "l":
|
|
28
|
+
if (n.length !== 4)
|
|
29
|
+
throw new Error(`linha precisa de 4 numeros: "${shape}"`);
|
|
30
|
+
return { tag: "line", attrs: { x1: n[0], y1: n[1], x2: n[2], y2: n[3] } };
|
|
31
|
+
default:
|
|
32
|
+
throw new Error(`tipo de forma desconhecido: "${shape}"`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export function shapeToSvg(shape) {
|
|
36
|
+
const { tag, attrs } = parseShape(shape);
|
|
37
|
+
return `<${tag} ${Object.entries(attrs).map(([k, v]) => `${k}="${v}"`).join(" ")}/>`;
|
|
38
|
+
}
|
|
39
|
+
export function iconToSvg(shapes, opts = {}) {
|
|
40
|
+
const size = opts.size ?? ICON_GRID;
|
|
41
|
+
const sw = opts.strokeWidth ?? ICON_STROKE;
|
|
42
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 ${ICON_GRID} ${ICON_GRID}" fill="none" stroke="currentColor" stroke-width="${sw}" stroke-linecap="round" stroke-linejoin="round">${shapes.map(shapeToSvg).join("")}</svg>`;
|
|
43
|
+
}
|
|
44
|
+
/** "git-branch" -> "GitBranchIcon" */
|
|
45
|
+
export function iconComponentName(name) {
|
|
46
|
+
return name.split("-").map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join("") + "Icon";
|
|
47
|
+
}
|
package/dist/icons.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare const POC_ICONS: {
|
|
2
|
+
readonly x: readonly ["M6 6l12 12", "M18 6L6 18"];
|
|
3
|
+
readonly plus: readonly ["M12 5v14", "M5 12h14"];
|
|
4
|
+
readonly minus: readonly ["M5 12h14"];
|
|
5
|
+
readonly "chevron-down": readonly ["M6 9l6 6 6-6"];
|
|
6
|
+
readonly check: readonly ["M5 12.5l4.5 4.5L19 7"];
|
|
7
|
+
};
|
|
8
|
+
export type PocIconName = keyof typeof POC_ICONS;
|
|
9
|
+
/** Forma funcional: loccalIcon("x", 16) devolve a string SVG. */
|
|
10
|
+
export declare function loccalIcon(name: PocIconName, size?: number): string;
|
|
11
|
+
export {};
|