@loccal/core 0.0.0-stage → 0.1.1
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/file-icons.d.ts +18 -0
- package/dist/file-icons.js +136 -0
- package/dist/highlight.d.ts +16 -0
- package/dist/highlight.js +538 -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 +37 -0
- package/dist/index.js +37 -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 +110 -4
- 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
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type FileIconSpec = {
|
|
2
|
+
/** Identificador estavel do tipo (ex.: "typescript", "image"). */
|
|
3
|
+
id: string;
|
|
4
|
+
/** Sigla de 1 a 3 caracteres desenhada no selo. */
|
|
5
|
+
label: string;
|
|
6
|
+
/** Cor do selo (hex). */
|
|
7
|
+
color: string;
|
|
8
|
+
/** Cor da sigla (hex), escolhida pelo contraste com o selo. */
|
|
9
|
+
ink: string;
|
|
10
|
+
};
|
|
11
|
+
/** Tipo do arquivo pelo nome completo e depois pela extensao; undefined = sem icone especifico. */
|
|
12
|
+
export declare function fileTypeId(name: string): string | undefined;
|
|
13
|
+
/** Especificacao do icone de um arquivo, ou undefined para o icone generico de arquivo. */
|
|
14
|
+
export declare function fileIconFor(name: string): FileIconSpec | undefined;
|
|
15
|
+
/** Todos os tipos com icone (para galerias e testes). */
|
|
16
|
+
export declare const fileIconTypes: readonly string[];
|
|
17
|
+
/** SVG do selo (string). `size` em px. */
|
|
18
|
+
export declare function fileIconSvg(spec: FileIconSpec, size?: number): string;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// Icones de tipo de arquivo (linguagens, formatos e extensoes mais usadas). Vanilla e sem DOM:
|
|
2
|
+
// `fileIconFor` escolhe o icone pelo nome; `fileIconSvg` o desenha como um selo colorido de 16x16 com a sigla do tipo.
|
|
3
|
+
// As formas sao proprias da Loccal UI (selo + sigla), nao reproduzem logotipos de terceiros.
|
|
4
|
+
const DARK = "#1b1b1f";
|
|
5
|
+
const LIGHT = "#ffffff";
|
|
6
|
+
const BY_TYPE = {
|
|
7
|
+
javascript: ["JS", "#f0db4f", DARK],
|
|
8
|
+
typescript: ["TS", "#3178c6"],
|
|
9
|
+
jsx: ["JX", "#f0db4f", DARK],
|
|
10
|
+
tsx: ["TX", "#3178c6"],
|
|
11
|
+
json: ["{}", "#d4a72c", DARK],
|
|
12
|
+
html: ["<>", "#e34f26"],
|
|
13
|
+
xml: ["<>", "#d9822b", DARK],
|
|
14
|
+
svg: ["SV", "#e8912d", DARK],
|
|
15
|
+
css: ["#", "#1572b6"],
|
|
16
|
+
scss: ["S", "#cd6799"],
|
|
17
|
+
less: ["L", "#2b4a7a"],
|
|
18
|
+
vue: ["V", "#41b883", DARK],
|
|
19
|
+
svelte: ["SV", "#ff3e00"],
|
|
20
|
+
go: ["GO", "#00a7c4"],
|
|
21
|
+
rust: ["RS", "#b7410e"],
|
|
22
|
+
c: ["C", "#5560bf"],
|
|
23
|
+
cpp: ["C+", "#00599c"],
|
|
24
|
+
header: ["H", "#7c6fcf"],
|
|
25
|
+
csharp: ["C#", "#8a3f96"],
|
|
26
|
+
java: ["JV", "#d9730d"],
|
|
27
|
+
kotlin: ["KT", "#7f52ff"],
|
|
28
|
+
swift: ["SW", "#f05138"],
|
|
29
|
+
dart: ["DT", "#0175c2"],
|
|
30
|
+
php: ["PH", "#777bb3"],
|
|
31
|
+
python: ["PY", "#3776ab"],
|
|
32
|
+
ruby: ["RB", "#cc342d"],
|
|
33
|
+
lua: ["LU", "#2c2d72"],
|
|
34
|
+
perl: ["PL", "#39457e"],
|
|
35
|
+
elixir: ["EX", "#6e4a7e"],
|
|
36
|
+
haskell: ["HS", "#5e5086"],
|
|
37
|
+
scala: ["SC", "#dc322f"],
|
|
38
|
+
zig: ["ZG", "#f7a41d", DARK],
|
|
39
|
+
r: ["R", "#276dc3"],
|
|
40
|
+
shell: [">_", "#3d9a3a"],
|
|
41
|
+
powershell: ["PS", "#2671be"],
|
|
42
|
+
batch: [">_", "#5a6169"],
|
|
43
|
+
sql: ["DB", "#e38c00", DARK],
|
|
44
|
+
yaml: ["YM", "#c2353a"],
|
|
45
|
+
toml: ["TM", "#9c4221"],
|
|
46
|
+
config: ["CF", "#6b7280"],
|
|
47
|
+
env: ["EN", "#6b7280"],
|
|
48
|
+
markdown: ["MD", "#3b82a0"],
|
|
49
|
+
text: ["TX", "#6b7280"],
|
|
50
|
+
log: ["LG", "#6b7280"],
|
|
51
|
+
docker: ["DK", "#2496ed"],
|
|
52
|
+
make: ["MK", "#6d8086"],
|
|
53
|
+
git: ["GT", "#e0432b"],
|
|
54
|
+
diff: ["±", "#6b7280"],
|
|
55
|
+
license: ["LI", "#6b7280"],
|
|
56
|
+
lock: ["LK", "#7a6f5c"],
|
|
57
|
+
csv: ["CS", "#2e9d6a"],
|
|
58
|
+
database: ["DB", "#7b61a8"],
|
|
59
|
+
pdf: ["PD", "#d63a35"],
|
|
60
|
+
doc: ["DC", "#2b6cb0"],
|
|
61
|
+
sheet: ["XL", "#1f8a4c"],
|
|
62
|
+
slides: ["PP", "#d2562a"],
|
|
63
|
+
image: ["IM", "#1f9e91"],
|
|
64
|
+
video: ["VD", "#9c3fb0"],
|
|
65
|
+
audio: ["AU", "#d6336c"],
|
|
66
|
+
font: ["Aa", "#6b7280"],
|
|
67
|
+
archive: ["ZP", "#8a6d52"],
|
|
68
|
+
binary: ["01", "#4b5563"],
|
|
69
|
+
executable: ["EX", "#4b5563"],
|
|
70
|
+
};
|
|
71
|
+
const EXT = {
|
|
72
|
+
js: "javascript", mjs: "javascript", cjs: "javascript", jsx: "jsx", ts: "typescript", mts: "typescript", cts: "typescript", tsx: "tsx",
|
|
73
|
+
json: "json", jsonc: "json", json5: "json", map: "json", webmanifest: "json", ipynb: "json",
|
|
74
|
+
html: "html", htm: "html", xhtml: "html", xml: "xml", xsd: "xml", xsl: "xml", plist: "xml", csproj: "xml", xaml: "xml", rss: "xml", svg: "svg",
|
|
75
|
+
css: "css", scss: "scss", sass: "scss", less: "less", vue: "vue", svelte: "svelte", astro: "html",
|
|
76
|
+
go: "go", rs: "rust", c: "c", h: "header", hpp: "header", hh: "header", cpp: "cpp", cc: "cpp", cxx: "cpp", ino: "cpp", cs: "csharp",
|
|
77
|
+
java: "java", kt: "kotlin", kts: "kotlin", swift: "swift", dart: "dart", php: "php", py: "python", pyw: "python", pyi: "python",
|
|
78
|
+
rb: "ruby", rake: "ruby", lua: "lua", pl: "perl", pm: "perl", ex: "elixir", exs: "elixir", hs: "haskell", scala: "scala", sc: "scala",
|
|
79
|
+
zig: "zig", r: "r",
|
|
80
|
+
sh: "shell", bash: "shell", zsh: "shell", fish: "shell", ksh: "shell", ps1: "powershell", psm1: "powershell", psd1: "powershell", bat: "batch", cmd: "batch",
|
|
81
|
+
sql: "sql", yaml: "yaml", yml: "yaml", toml: "toml", ini: "config", cfg: "config", conf: "config", properties: "config", editorconfig: "config", env: "env",
|
|
82
|
+
md: "markdown", markdown: "markdown", mdx: "markdown", rst: "text", txt: "text", log: "log", diff: "diff", patch: "diff",
|
|
83
|
+
mk: "make", mak: "make", lock: "lock", csv: "csv", tsv: "csv", db: "database", sqlite: "database", sqlite3: "database", parquet: "database",
|
|
84
|
+
pdf: "pdf", doc: "doc", docx: "doc", odt: "doc", rtf: "doc", xls: "sheet", xlsx: "sheet", ods: "sheet", ppt: "slides", pptx: "slides", odp: "slides",
|
|
85
|
+
png: "image", jpg: "image", jpeg: "image", gif: "image", webp: "image", bmp: "image", ico: "image", avif: "image", tif: "image", tiff: "image", heic: "image", icns: "image",
|
|
86
|
+
mp4: "video", mkv: "video", mov: "video", avi: "video", webm: "video", m4v: "video",
|
|
87
|
+
mp3: "audio", wav: "audio", flac: "audio", ogg: "audio", m4a: "audio", aac: "audio", opus: "audio",
|
|
88
|
+
ttf: "font", otf: "font", woff: "font", woff2: "font", eot: "font",
|
|
89
|
+
zip: "archive", tar: "archive", gz: "archive", tgz: "archive", bz2: "archive", xz: "archive", "7z": "archive", rar: "archive", zst: "archive", iso: "archive", jar: "archive", war: "archive",
|
|
90
|
+
exe: "executable", msi: "executable", app: "executable", apk: "executable", bin: "binary", dll: "binary", so: "binary", dylib: "binary", o: "binary", obj: "binary", class: "binary", wasm: "binary", pyc: "binary",
|
|
91
|
+
};
|
|
92
|
+
const NAME = {
|
|
93
|
+
dockerfile: "docker", ".dockerignore": "docker", "docker-compose.yml": "docker", "docker-compose.yaml": "docker", "compose.yaml": "docker", "compose.yml": "docker",
|
|
94
|
+
makefile: "make", gnumakefile: "make", ".gitignore": "git", ".gitattributes": "git", ".gitmodules": "git", ".gitconfig": "git",
|
|
95
|
+
license: "license", "license.md": "license", "license.txt": "license", copying: "license", notice: "license",
|
|
96
|
+
"package-lock.json": "lock", "pnpm-lock.yaml": "lock", "yarn.lock": "lock", "go.sum": "lock", "cargo.lock": "lock", "poetry.lock": "lock", "composer.lock": "lock", gemfile: "ruby", rakefile: "ruby",
|
|
97
|
+
"go.mod": "go", "go.work": "go", ".env": "env", ".npmrc": "config", ".editorconfig": "config", ".prettierrc": "json", ".eslintrc": "json",
|
|
98
|
+
".bashrc": "shell", ".zshrc": "shell", ".profile": "shell", readme: "markdown",
|
|
99
|
+
};
|
|
100
|
+
/** Tipo do arquivo pelo nome completo e depois pela extensao; undefined = sem icone especifico. */
|
|
101
|
+
export function fileTypeId(name) {
|
|
102
|
+
const base = name.replace(/\\/g, "/").split("/").pop().toLowerCase();
|
|
103
|
+
if (NAME[base])
|
|
104
|
+
return NAME[base];
|
|
105
|
+
if (base.startsWith(".env."))
|
|
106
|
+
return "env";
|
|
107
|
+
const dot = base.lastIndexOf(".");
|
|
108
|
+
if (dot <= 0)
|
|
109
|
+
return undefined;
|
|
110
|
+
const ext = base.slice(dot + 1);
|
|
111
|
+
if (base.endsWith(".d.ts"))
|
|
112
|
+
return "typescript";
|
|
113
|
+
return EXT[ext];
|
|
114
|
+
}
|
|
115
|
+
/** Especificacao do icone de um arquivo, ou undefined para o icone generico de arquivo. */
|
|
116
|
+
export function fileIconFor(name) {
|
|
117
|
+
const id = fileTypeId(name);
|
|
118
|
+
if (!id)
|
|
119
|
+
return undefined;
|
|
120
|
+
const e = BY_TYPE[id];
|
|
121
|
+
if (!e)
|
|
122
|
+
return undefined;
|
|
123
|
+
return { id, label: e[0], color: e[1], ink: e[2] ?? LIGHT };
|
|
124
|
+
}
|
|
125
|
+
/** Todos os tipos com icone (para galerias e testes). */
|
|
126
|
+
export const fileIconTypes = Object.keys(BY_TYPE);
|
|
127
|
+
const escXml = (s) => s.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
|
|
128
|
+
/** SVG do selo (string). `size` em px. */
|
|
129
|
+
export function fileIconSvg(spec, size = 16) {
|
|
130
|
+
const n = [...spec.label].length;
|
|
131
|
+
const fs = n === 1 ? 9.5 : n === 2 ? 7 : 5.5;
|
|
132
|
+
return (`<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 16 16" aria-hidden="true" focusable="false" data-file-type="${spec.id}">` +
|
|
133
|
+
`<rect x="0.75" y="0.75" width="14.5" height="14.5" rx="3.25" fill="${spec.color}"/>` +
|
|
134
|
+
`<text x="8" y="${(8 + fs * 0.36).toFixed(2)}" text-anchor="middle" font-family="ui-monospace,SFMono-Regular,Menlo,Consolas,monospace" font-size="${fs}" font-weight="700" fill="${spec.ink}">${escXml(spec.label)}</text>` +
|
|
135
|
+
`</svg>`);
|
|
136
|
+
}
|