@akonwi/mica 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 +81 -0
- package/combobox.js +138 -0
- package/field.js +111 -0
- package/mica.css +1536 -0
- package/package.json +32 -0
- package/select.js +30 -0
- package/tabs.js +82 -0
- package/toast.js +100 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Akonwi Ngoh
|
|
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
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# mica
|
|
2
|
+
|
|
3
|
+
**Custom elements. Native behavior. Nearly no JavaScript.**
|
|
4
|
+
|
|
5
|
+
Mica is a front-end library built from custom element tags, native HTML
|
|
6
|
+
elements, and CSS. No runtime, no build step, no framework. View source —
|
|
7
|
+
there's nothing there but HTML and a stylesheet.
|
|
8
|
+
|
|
9
|
+
```html
|
|
10
|
+
<link rel="stylesheet" href="mica.css">
|
|
11
|
+
|
|
12
|
+
<m-vstack gap="lg">
|
|
13
|
+
<h1>That's it</h1>
|
|
14
|
+
<m-hstack gap="sm">
|
|
15
|
+
<button class="primary">Save</button>
|
|
16
|
+
<button class="ghost">Cancel</button>
|
|
17
|
+
</m-hstack>
|
|
18
|
+
</m-vstack>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`<m-vstack>` is never registered — an unknown custom element is a valid,
|
|
22
|
+
stylable element. The behavior everywhere else (dialogs, accordions, menus,
|
|
23
|
+
form validation) comes from the browser, not from a runtime.
|
|
24
|
+
|
|
25
|
+
## Getting it
|
|
26
|
+
|
|
27
|
+
**Copy the file** — mica is one dependency-free CSS file. Vendoring is a
|
|
28
|
+
first-class distribution channel, not a workaround:
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
curl -O https://raw.githubusercontent.com/akonwi/mica/v0.1.0/mica.css
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**npm** — for toolchains:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
npm install @akonwi/mica
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
import "@akonwi/mica/mica.css";
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**CDN** — for trying it out:
|
|
45
|
+
|
|
46
|
+
```html
|
|
47
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@akonwi/mica@0.1/mica.css">
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## The three tiers
|
|
51
|
+
|
|
52
|
+
Every component states where its behavior comes from
|
|
53
|
+
(see [TIERS.md](TIERS.md)):
|
|
54
|
+
|
|
55
|
+
- **Tier 0 — CSS is the behavior.** Layout primitives: `m-vstack`,
|
|
56
|
+
`m-hstack`, `m-zstack`, `m-center`, `m-box`, `m-grid`, `m-sidecar`,
|
|
57
|
+
`m-switcher`, `m-reel`. Zero JS, work with JS disabled.
|
|
58
|
+
- **Tier 1 — the browser is the behavior.** Styled native elements:
|
|
59
|
+
buttons, forms, `<dialog>`, `<details>` accordions, popover menus,
|
|
60
|
+
tooltips, toasts. Delete the stylesheet and everything still works.
|
|
61
|
+
- **Tier 2 — script, honestly.** Where accessibility genuinely requires
|
|
62
|
+
JS: `tabs.js`, `combobox.js`, `field.js` (declarative validation),
|
|
63
|
+
`select.js`, `toast.js`. Each is a tiny standalone module that enhances
|
|
64
|
+
working markup — never renders it. There is no shared runtime.
|
|
65
|
+
|
|
66
|
+
## Your CSS always wins
|
|
67
|
+
|
|
68
|
+
All of mica ships inside `@layer mica.*`. Unlayered user CSS beats layered
|
|
69
|
+
CSS by spec — overriding mica never requires specificity games. Theming is
|
|
70
|
+
swapping token values (`--hue`, semantic color roles, spacing scale); dark
|
|
71
|
+
mode is `light-dark()` — automatic, no classes.
|
|
72
|
+
|
|
73
|
+
## Docs
|
|
74
|
+
|
|
75
|
+
Read [VISION.md](VISION.md) for the philosophy, [TIERS.md](TIERS.md) for
|
|
76
|
+
the tier system, and the `docs/` pages for every component. Or open
|
|
77
|
+
`demo.html` — the whole library on one page.
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
[MIT](LICENSE)
|
package/combobox.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/* mica/combobox.js — <m-combobox>: filterable input + listbox. Tier 2.
|
|
2
|
+
*
|
|
3
|
+
* The no-JS state is a native <datalist> — real autocomplete, fully
|
|
4
|
+
* functional, just unstylable. The module upgrades it into the ARIA
|
|
5
|
+
* combobox pattern: styled listbox, substring filtering, arrow-key
|
|
6
|
+
* active-descendant navigation. Options come from the author's
|
|
7
|
+
* datalist markup — the module presents them, it doesn't invent them.
|
|
8
|
+
*
|
|
9
|
+
* <m-combobox>
|
|
10
|
+
* <input list="langs" placeholder="Language…" />
|
|
11
|
+
* <datalist id="langs">
|
|
12
|
+
* <option>Ard</option>
|
|
13
|
+
* <option>Go</option>
|
|
14
|
+
* </datalist>
|
|
15
|
+
* </m-combobox>
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
let uid = 0;
|
|
19
|
+
|
|
20
|
+
class MCombobox extends HTMLElement {
|
|
21
|
+
#input;
|
|
22
|
+
#list;
|
|
23
|
+
#options = [];
|
|
24
|
+
#active = -1;
|
|
25
|
+
|
|
26
|
+
connectedCallback() {
|
|
27
|
+
this.#input = this.querySelector("input");
|
|
28
|
+
const datalist = this.querySelector("datalist");
|
|
29
|
+
if (!this.#input || !datalist) return;
|
|
30
|
+
|
|
31
|
+
// disable the native popup; the module takes over
|
|
32
|
+
this.#input.removeAttribute("list");
|
|
33
|
+
this.#input.setAttribute("role", "combobox");
|
|
34
|
+
this.#input.setAttribute("aria-expanded", "false");
|
|
35
|
+
this.#input.setAttribute("aria-autocomplete", "list");
|
|
36
|
+
this.#input.autocomplete = "off";
|
|
37
|
+
|
|
38
|
+
const listId = `m-cb-${++uid}`;
|
|
39
|
+
this.#list = document.createElement("div");
|
|
40
|
+
this.#list.id = listId;
|
|
41
|
+
this.#list.setAttribute("role", "listbox");
|
|
42
|
+
this.#list.hidden = true;
|
|
43
|
+
|
|
44
|
+
this.#options = [...datalist.options].map((o, i) => {
|
|
45
|
+
const d = document.createElement("div");
|
|
46
|
+
d.setAttribute("role", "option");
|
|
47
|
+
d.id = `${listId}-${i}`;
|
|
48
|
+
d.textContent = o.value || o.textContent;
|
|
49
|
+
// pointerdown so the input never loses focus
|
|
50
|
+
d.addEventListener("pointerdown", (e) => {
|
|
51
|
+
e.preventDefault();
|
|
52
|
+
this.#choose(d);
|
|
53
|
+
});
|
|
54
|
+
// hover moves the highlight (cmdk behavior)
|
|
55
|
+
d.addEventListener("pointerenter", () => {
|
|
56
|
+
this.#setActive(this.#visible().indexOf(d));
|
|
57
|
+
});
|
|
58
|
+
this.#list.append(d);
|
|
59
|
+
return d;
|
|
60
|
+
});
|
|
61
|
+
this.append(this.#list);
|
|
62
|
+
this.#input.setAttribute("aria-controls", listId);
|
|
63
|
+
|
|
64
|
+
this.#input.addEventListener("input", (e) => {
|
|
65
|
+
// ignore our own dispatched events from #choose — they'd reopen
|
|
66
|
+
if (e.isTrusted) this.#openAndFilter();
|
|
67
|
+
});
|
|
68
|
+
this.#input.addEventListener("keydown", (e) => this.#onKey(e));
|
|
69
|
+
this.addEventListener("focusout", (e) => {
|
|
70
|
+
if (!this.contains(e.relatedTarget)) this.#close();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#visible() {
|
|
75
|
+
return this.#options.filter((o) => !o.hidden);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
#openAndFilter() {
|
|
79
|
+
const q = this.#input.value.trim().toLowerCase();
|
|
80
|
+
let any = false;
|
|
81
|
+
for (const o of this.#options) {
|
|
82
|
+
o.hidden = q !== "" && !o.textContent.toLowerCase().includes(q);
|
|
83
|
+
if (!o.hidden) any = true;
|
|
84
|
+
}
|
|
85
|
+
this.#list.hidden = !any;
|
|
86
|
+
this.#input.setAttribute("aria-expanded", String(any));
|
|
87
|
+
// first match is pre-highlighted so Enter always has a target
|
|
88
|
+
this.#setActive(any ? 0 : -1);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#close() {
|
|
92
|
+
this.#list.hidden = true;
|
|
93
|
+
this.#input.setAttribute("aria-expanded", "false");
|
|
94
|
+
this.#setActive(-1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#setActive(i) {
|
|
98
|
+
const vis = this.#visible();
|
|
99
|
+
this.#active = i;
|
|
100
|
+
for (const o of this.#options) o.setAttribute("aria-selected", "false");
|
|
101
|
+
if (i >= 0 && vis[i]) {
|
|
102
|
+
vis[i].setAttribute("aria-selected", "true");
|
|
103
|
+
vis[i].scrollIntoView({ block: "nearest" });
|
|
104
|
+
this.#input.setAttribute("aria-activedescendant", vis[i].id);
|
|
105
|
+
} else {
|
|
106
|
+
this.#input.removeAttribute("aria-activedescendant");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#onKey(e) {
|
|
111
|
+
const vis = this.#visible();
|
|
112
|
+
if (e.key === "ArrowDown") {
|
|
113
|
+
e.preventDefault();
|
|
114
|
+
if (this.#list.hidden) this.#openAndFilter();
|
|
115
|
+
if (this.#visible().length) this.#setActive((this.#active + 1) % this.#visible().length);
|
|
116
|
+
} else if (e.key === "ArrowUp") {
|
|
117
|
+
e.preventDefault();
|
|
118
|
+
if (vis.length) this.#setActive((this.#active - 1 + vis.length) % vis.length);
|
|
119
|
+
} else if (e.key === "Enter") {
|
|
120
|
+
if (this.#active >= 0 && !this.#list.hidden) {
|
|
121
|
+
e.preventDefault();
|
|
122
|
+
this.#choose(vis[this.#active]);
|
|
123
|
+
}
|
|
124
|
+
} else if (e.key === "Escape") {
|
|
125
|
+
this.#close();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
#choose(option) {
|
|
130
|
+
this.#input.value = option.textContent;
|
|
131
|
+
this.#close();
|
|
132
|
+
this.#input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
133
|
+
this.#input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
134
|
+
this.#input.focus();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (!customElements.get("m-combobox")) customElements.define("m-combobox", MCombobox);
|
package/field.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/* mica/field.js — <m-field>: declarative validation errors. Tier 2.
|
|
2
|
+
*
|
|
3
|
+
* Enhances working light-DOM markup; never renders it. Without this
|
|
4
|
+
* module, fields inside <m-field> fall back to native bubbles plus the
|
|
5
|
+
* CSS-only generic <m-error> (see mica.css).
|
|
6
|
+
*
|
|
7
|
+
* <m-field>
|
|
8
|
+
* <label for="email">Email</label>
|
|
9
|
+
* <input id="email" type="email" required>
|
|
10
|
+
* <m-error match="value-missing">Email is required.</m-error>
|
|
11
|
+
* <m-error match="type-mismatch">Not a valid email.</m-error>
|
|
12
|
+
* <m-error></m-error> <!-- catch-all; empty = browser's message -->
|
|
13
|
+
* </m-field>
|
|
14
|
+
*
|
|
15
|
+
* What the module does:
|
|
16
|
+
* - preventDefault() on `invalid` — kills the browser bubble while the
|
|
17
|
+
* form still blocks bad submits
|
|
18
|
+
* - focuses the first invalid field on a submit attempt (the browser
|
|
19
|
+
* normally does this; preventDefault suppresses it)
|
|
20
|
+
* - mirrors ValidityState onto <m-field invalid="value-missing ...">
|
|
21
|
+
* - activates the first <m-error> whose `match` applies, else the
|
|
22
|
+
* catch-all; empty catch-alls get the browser's validationMessage
|
|
23
|
+
* - wires aria-invalid + aria-describedby to the active error
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const CAUSES = {
|
|
27
|
+
"value-missing": "valueMissing",
|
|
28
|
+
"type-mismatch": "typeMismatch",
|
|
29
|
+
"pattern-mismatch": "patternMismatch",
|
|
30
|
+
"too-long": "tooLong",
|
|
31
|
+
"too-short": "tooShort",
|
|
32
|
+
"range-underflow": "rangeUnderflow",
|
|
33
|
+
"range-overflow": "rangeOverflow",
|
|
34
|
+
"step-mismatch": "stepMismatch",
|
|
35
|
+
"bad-input": "badInput",
|
|
36
|
+
"custom-error": "customError",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
class MField extends HTMLElement {
|
|
40
|
+
#uid;
|
|
41
|
+
|
|
42
|
+
connectedCallback() {
|
|
43
|
+
this.addEventListener("invalid", this.#onInvalid, true);
|
|
44
|
+
this.addEventListener("blur", this.#onBlur, true);
|
|
45
|
+
this.addEventListener("input", this.#onInput);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get field() {
|
|
49
|
+
return this.querySelector("input, textarea, select");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#onInvalid = (event) => {
|
|
53
|
+
event.preventDefault();
|
|
54
|
+
this.#update(true);
|
|
55
|
+
// focus the first invalid field on a submit attempt (the browser
|
|
56
|
+
// normally does this; preventDefault suppressed it). Checked
|
|
57
|
+
// deterministically — invalid events fire per-field and flags
|
|
58
|
+
// can't survive the interleaved microtask checkpoints.
|
|
59
|
+
const form = event.target.form;
|
|
60
|
+
const first = form?.querySelector(
|
|
61
|
+
"input:invalid, select:invalid, textarea:invalid",
|
|
62
|
+
);
|
|
63
|
+
if (event.target === first) event.target.focus();
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
#onBlur = (event) => {
|
|
67
|
+
if (event.target.matches?.(":user-invalid")) this.#update(true);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
#onInput = () => {
|
|
71
|
+
// once an error is showing, revalidate live so it clears on fix
|
|
72
|
+
if (this.hasAttribute("invalid")) this.#update(true);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
#update(show) {
|
|
76
|
+
const field = this.field;
|
|
77
|
+
if (!field) return;
|
|
78
|
+
const errors = [...this.querySelectorAll("m-error")];
|
|
79
|
+
|
|
80
|
+
if (field.validity.valid) {
|
|
81
|
+
this.removeAttribute("invalid");
|
|
82
|
+
field.removeAttribute("aria-invalid");
|
|
83
|
+
field.removeAttribute("aria-describedby");
|
|
84
|
+
for (const el of errors) el.removeAttribute("active");
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (!show) return;
|
|
88
|
+
|
|
89
|
+
const causes = Object.keys(CAUSES).filter((k) => field.validity[CAUSES[k]]);
|
|
90
|
+
this.setAttribute("invalid", causes.join(" "));
|
|
91
|
+
|
|
92
|
+
const active =
|
|
93
|
+
errors.find((el) => causes.includes(el.getAttribute("match"))) ??
|
|
94
|
+
errors.find((el) => !el.hasAttribute("match"));
|
|
95
|
+
for (const el of errors) el.toggleAttribute("active", el === active);
|
|
96
|
+
|
|
97
|
+
if (active) {
|
|
98
|
+
if ("auto" in active.dataset || !active.textContent.trim()) {
|
|
99
|
+
active.dataset.auto = "";
|
|
100
|
+
active.textContent = field.validationMessage;
|
|
101
|
+
}
|
|
102
|
+
active.id ||=
|
|
103
|
+
`m-error-${(this.#uid ??= Math.random().toString(36).slice(2, 8))}` +
|
|
104
|
+
`-${errors.indexOf(active)}`;
|
|
105
|
+
field.setAttribute("aria-describedby", active.id);
|
|
106
|
+
field.setAttribute("aria-invalid", "true");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!customElements.get("m-field")) customElements.define("m-field", MField);
|