flexui-runtime 0.0.0-stage → 2.0.0-alpha.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/README.md +70 -2
- package/dist/auto.d.ts +15 -0
- package/dist/auto.js +39 -0
- package/dist/command.d.ts +38 -0
- package/dist/command.js +270 -0
- package/dist/dialog.d.ts +44 -0
- package/dist/dialog.js +243 -0
- package/dist/drawer.d.ts +6 -0
- package/dist/drawer.js +23 -0
- package/dist/dropdown.d.ts +32 -0
- package/dist/dropdown.js +209 -0
- package/dist/focus.d.ts +15 -0
- package/dist/focus.js +77 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +36 -0
- package/dist/popover.d.ts +38 -0
- package/dist/popover.js +186 -0
- package/dist/tabs.d.ts +30 -0
- package/dist/tabs.js +160 -0
- package/dist/theme.d.ts +33 -0
- package/dist/theme.js +157 -0
- package/dist/toast.d.ts +18 -0
- package/dist/toast.js +166 -0
- package/dist/types.d.ts +27 -0
- package/dist/types.js +97 -0
- package/package.json +86 -6
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type Cleanup } from "./types.js";
|
|
2
|
+
interface NativePopover extends HTMLElement {
|
|
3
|
+
showPopover: () => void;
|
|
4
|
+
hidePopover: () => void;
|
|
5
|
+
togglePopover: (force?: boolean) => boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface PopoverOptions {
|
|
8
|
+
/** Pixels between trigger and panel. Defaults to 8. */
|
|
9
|
+
offset?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Positions a native popover against its trigger and keeps aria-expanded in
|
|
13
|
+
* sync. Browsers without the popover API get a hidden-attribute fallback with
|
|
14
|
+
* the same keyboard and outside-click behaviour.
|
|
15
|
+
*/
|
|
16
|
+
export declare class PopoverController {
|
|
17
|
+
readonly trigger: HTMLElement;
|
|
18
|
+
readonly panel: NativePopover;
|
|
19
|
+
private readonly options;
|
|
20
|
+
private readonly disposer;
|
|
21
|
+
private readonly native;
|
|
22
|
+
private openState;
|
|
23
|
+
constructor(trigger: HTMLElement, panel: HTMLElement, options?: PopoverOptions);
|
|
24
|
+
get isOpen(): boolean;
|
|
25
|
+
open(): void;
|
|
26
|
+
close(): void;
|
|
27
|
+
toggle(): void;
|
|
28
|
+
dispose(): void;
|
|
29
|
+
private syncOpen;
|
|
30
|
+
private position;
|
|
31
|
+
}
|
|
32
|
+
/** Returns the controller bound to a trigger, if any. */
|
|
33
|
+
export declare function getPopover(trigger: HTMLElement): PopoverController | undefined;
|
|
34
|
+
/** Creates or reuses the controller for a trigger and its panel. */
|
|
35
|
+
export declare function bindPopover(trigger: HTMLElement, panel: HTMLElement, options?: PopoverOptions): PopoverController;
|
|
36
|
+
/** Binds popovers declared with data-fui-popover="<target>". */
|
|
37
|
+
export declare function initPopovers(root?: ParentNode): Cleanup;
|
|
38
|
+
export {};
|
package/dist/popover.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { createDisposer, emit, getElement, on, qsa, uid, } from "./types.js";
|
|
2
|
+
const controllers = new WeakMap();
|
|
3
|
+
/**
|
|
4
|
+
* Positions a native popover against its trigger and keeps aria-expanded in
|
|
5
|
+
* sync. Browsers without the popover API get a hidden-attribute fallback with
|
|
6
|
+
* the same keyboard and outside-click behaviour.
|
|
7
|
+
*/
|
|
8
|
+
export class PopoverController {
|
|
9
|
+
trigger;
|
|
10
|
+
panel;
|
|
11
|
+
options;
|
|
12
|
+
disposer;
|
|
13
|
+
native;
|
|
14
|
+
openState = false;
|
|
15
|
+
constructor(trigger, panel, options = {}) {
|
|
16
|
+
this.trigger = trigger;
|
|
17
|
+
this.panel = panel;
|
|
18
|
+
this.options = options;
|
|
19
|
+
this.disposer = createDisposer();
|
|
20
|
+
this.native = typeof this.panel.showPopover === "function";
|
|
21
|
+
if (!this.panel.id) {
|
|
22
|
+
this.panel.id = uid("fui-popover");
|
|
23
|
+
}
|
|
24
|
+
if (!this.panel.hasAttribute("popover")) {
|
|
25
|
+
this.panel.setAttribute("popover", "auto");
|
|
26
|
+
}
|
|
27
|
+
this.trigger.setAttribute("aria-controls", this.panel.id);
|
|
28
|
+
this.trigger.setAttribute("aria-expanded", "false");
|
|
29
|
+
if (!this.native) {
|
|
30
|
+
this.panel.hidden = true;
|
|
31
|
+
}
|
|
32
|
+
this.disposer.add(on(trigger, "click", (event) => {
|
|
33
|
+
event.preventDefault();
|
|
34
|
+
this.toggle();
|
|
35
|
+
}));
|
|
36
|
+
this.disposer.add(on(this.panel, "toggle", (event) => {
|
|
37
|
+
const state = event.newState;
|
|
38
|
+
this.syncOpen(state === "open");
|
|
39
|
+
}));
|
|
40
|
+
this.disposer.add(on(document, "pointerdown", (event) => {
|
|
41
|
+
const target = event.target;
|
|
42
|
+
if (this.openState &&
|
|
43
|
+
target instanceof Node &&
|
|
44
|
+
!this.panel.contains(target) &&
|
|
45
|
+
!this.trigger.contains(target)) {
|
|
46
|
+
this.close();
|
|
47
|
+
}
|
|
48
|
+
}));
|
|
49
|
+
this.disposer.add(on(document, "keydown", (event) => {
|
|
50
|
+
if (this.openState && event.key === "Escape") {
|
|
51
|
+
this.close();
|
|
52
|
+
}
|
|
53
|
+
}));
|
|
54
|
+
this.disposer.add(on(window, "resize", () => this.position()));
|
|
55
|
+
this.disposer.add(on(window, "scroll", () => this.position(), true));
|
|
56
|
+
for (const closer of qsa(this.panel, "[data-fui-popover-close]")) {
|
|
57
|
+
this.disposer.add(on(closer, "click", (event) => {
|
|
58
|
+
event.preventDefault();
|
|
59
|
+
this.close();
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
get isOpen() {
|
|
64
|
+
return this.openState;
|
|
65
|
+
}
|
|
66
|
+
open() {
|
|
67
|
+
if (this.native) {
|
|
68
|
+
try {
|
|
69
|
+
this.panel.showPopover?.();
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
// InvalidStateError means it is already open.
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
this.panel.hidden = false;
|
|
77
|
+
}
|
|
78
|
+
this.syncOpen(true);
|
|
79
|
+
}
|
|
80
|
+
close() {
|
|
81
|
+
if (this.native) {
|
|
82
|
+
try {
|
|
83
|
+
this.panel.hidePopover?.();
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
// Nothing to hide.
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
this.panel.hidden = true;
|
|
91
|
+
}
|
|
92
|
+
this.syncOpen(false);
|
|
93
|
+
}
|
|
94
|
+
toggle() {
|
|
95
|
+
if (this.openState) {
|
|
96
|
+
this.close();
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
this.open();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
dispose() {
|
|
103
|
+
this.close();
|
|
104
|
+
this.disposer.dispose();
|
|
105
|
+
controllers.delete(this.trigger);
|
|
106
|
+
}
|
|
107
|
+
syncOpen(open) {
|
|
108
|
+
if (this.openState === open) {
|
|
109
|
+
if (open) {
|
|
110
|
+
this.position();
|
|
111
|
+
}
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
this.openState = open;
|
|
115
|
+
this.trigger.setAttribute("aria-expanded", open ? "true" : "false");
|
|
116
|
+
if (open) {
|
|
117
|
+
this.panel.dataset["fuiState"] = "open";
|
|
118
|
+
this.position();
|
|
119
|
+
emit(this.trigger, "fui:popoveropen", { trigger: this.trigger, panel: this.panel });
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
delete this.panel.dataset["fuiState"];
|
|
123
|
+
delete this.panel.dataset["fuiSide"];
|
|
124
|
+
emit(this.trigger, "fui:popoverclose", { trigger: this.trigger, panel: this.panel });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
position() {
|
|
128
|
+
if (!this.openState) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const offset = this.options.offset ?? 8;
|
|
132
|
+
const margin = 8;
|
|
133
|
+
const triggerRect = this.trigger.getBoundingClientRect();
|
|
134
|
+
const panelRect = this.panel.getBoundingClientRect();
|
|
135
|
+
let side = "bottom";
|
|
136
|
+
let top = triggerRect.bottom + offset;
|
|
137
|
+
if (top + panelRect.height > window.innerHeight - margin && triggerRect.top - panelRect.height - offset >= margin) {
|
|
138
|
+
top = triggerRect.top - panelRect.height - offset;
|
|
139
|
+
side = "top";
|
|
140
|
+
}
|
|
141
|
+
const left = Math.min(Math.max(margin, triggerRect.left), Math.max(margin, window.innerWidth - panelRect.width - margin));
|
|
142
|
+
this.panel.style.position = "fixed";
|
|
143
|
+
this.panel.style.left = `${Math.round(left)}px`;
|
|
144
|
+
this.panel.style.top = `${Math.round(top)}px`;
|
|
145
|
+
this.panel.style.right = "auto";
|
|
146
|
+
this.panel.style.bottom = "auto";
|
|
147
|
+
this.panel.style.margin = "0";
|
|
148
|
+
this.panel.dataset["fuiSide"] = side;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Returns the controller bound to a trigger, if any. */
|
|
152
|
+
export function getPopover(trigger) {
|
|
153
|
+
return controllers.get(trigger);
|
|
154
|
+
}
|
|
155
|
+
/** Creates or reuses the controller for a trigger and its panel. */
|
|
156
|
+
export function bindPopover(trigger, panel, options = {}) {
|
|
157
|
+
const existing = controllers.get(trigger);
|
|
158
|
+
if (existing) {
|
|
159
|
+
return existing;
|
|
160
|
+
}
|
|
161
|
+
const controller = new PopoverController(trigger, panel, options);
|
|
162
|
+
controllers.set(trigger, controller);
|
|
163
|
+
return controller;
|
|
164
|
+
}
|
|
165
|
+
/** Binds popovers declared with data-fui-popover="<target>". */
|
|
166
|
+
export function initPopovers(root = document) {
|
|
167
|
+
if (typeof document === "undefined") {
|
|
168
|
+
return () => undefined;
|
|
169
|
+
}
|
|
170
|
+
const disposer = createDisposer();
|
|
171
|
+
for (const trigger of qsa(root, "[data-fui-popover]")) {
|
|
172
|
+
if (controllers.has(trigger)) {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const value = trigger.getAttribute("data-fui-popover") ??
|
|
176
|
+
trigger.getAttribute("aria-controls") ??
|
|
177
|
+
trigger.getAttribute("popovertarget");
|
|
178
|
+
const panel = getElement(value);
|
|
179
|
+
if (!panel) {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const controller = bindPopover(trigger, panel);
|
|
183
|
+
disposer.add(() => controller.dispose());
|
|
184
|
+
}
|
|
185
|
+
return () => disposer.dispose();
|
|
186
|
+
}
|
package/dist/tabs.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Cleanup } from "./types.js";
|
|
2
|
+
export interface TabChangeDetail {
|
|
3
|
+
tabs: Tabs;
|
|
4
|
+
tab: HTMLElement;
|
|
5
|
+
panel: HTMLElement | null;
|
|
6
|
+
index: number;
|
|
7
|
+
}
|
|
8
|
+
/** Accessible tabs with roving tabindex and automatic activation. */
|
|
9
|
+
export declare class Tabs {
|
|
10
|
+
readonly root: HTMLElement;
|
|
11
|
+
private readonly disposer;
|
|
12
|
+
private readonly tabs;
|
|
13
|
+
private readonly panels;
|
|
14
|
+
private activeIndex;
|
|
15
|
+
constructor(root: HTMLElement);
|
|
16
|
+
get active(): {
|
|
17
|
+
tab: HTMLElement;
|
|
18
|
+
panel: HTMLElement | null;
|
|
19
|
+
index: number;
|
|
20
|
+
} | null;
|
|
21
|
+
activate(index: number, focus?: boolean, notify?: boolean): void;
|
|
22
|
+
dispose(): void;
|
|
23
|
+
private handleKeydown;
|
|
24
|
+
}
|
|
25
|
+
/** Returns the controller bound to a tabs container, if any. */
|
|
26
|
+
export declare function getTabs(root: HTMLElement): Tabs | undefined;
|
|
27
|
+
/** Creates or reuses the controller for a tabs container. */
|
|
28
|
+
export declare function bindTabs(root: HTMLElement): Tabs;
|
|
29
|
+
/** Binds every [data-fui-tabs] container under root. */
|
|
30
|
+
export declare function initTabs(root?: ParentNode): Cleanup;
|
package/dist/tabs.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { createDisposer, emit, on, qs, qsa, uid } from "./types.js";
|
|
2
|
+
const controllers = new WeakMap();
|
|
3
|
+
/** Accessible tabs with roving tabindex and automatic activation. */
|
|
4
|
+
export class Tabs {
|
|
5
|
+
root;
|
|
6
|
+
disposer;
|
|
7
|
+
tabs;
|
|
8
|
+
panels;
|
|
9
|
+
activeIndex = 0;
|
|
10
|
+
constructor(root) {
|
|
11
|
+
this.root = root;
|
|
12
|
+
this.disposer = createDisposer();
|
|
13
|
+
const tablist = root.querySelector('[role="tablist"], [data-fui-tablist]') ??
|
|
14
|
+
(root.getAttribute("role") === "tablist" ? root : null);
|
|
15
|
+
this.tabs = tablist ? qsa(tablist, '[role="tab"], [data-fui-tab]') : [];
|
|
16
|
+
this.panels = [];
|
|
17
|
+
if (this.tabs.length === 0) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
for (const [index, tab] of this.tabs.entries()) {
|
|
21
|
+
if (!tab.id) {
|
|
22
|
+
tab.id = uid("fui-tab");
|
|
23
|
+
}
|
|
24
|
+
tab.setAttribute("role", "tab");
|
|
25
|
+
tab.tabIndex = -1;
|
|
26
|
+
const controls = tab.getAttribute("aria-controls");
|
|
27
|
+
const panel = controls ? document.getElementById(controls) : null;
|
|
28
|
+
if (panel) {
|
|
29
|
+
panel.setAttribute("role", "tabpanel");
|
|
30
|
+
panel.setAttribute("aria-labelledby", tab.id);
|
|
31
|
+
if (!panel.id) {
|
|
32
|
+
panel.id = uid("fui-tabpanel");
|
|
33
|
+
}
|
|
34
|
+
if (!controls) {
|
|
35
|
+
tab.setAttribute("aria-controls", panel.id);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
this.panels[index] = panel;
|
|
39
|
+
this.disposer.add(on(tab, "click", () => {
|
|
40
|
+
this.activate(index, true);
|
|
41
|
+
}));
|
|
42
|
+
this.disposer.add(on(tab, "keydown", (event) => {
|
|
43
|
+
this.handleKeydown(event, index, tablist);
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
const selected = this.tabs.findIndex((tab) => tab.getAttribute("aria-selected") === "true");
|
|
47
|
+
this.activate(selected >= 0 ? selected : 0, false, false);
|
|
48
|
+
}
|
|
49
|
+
get active() {
|
|
50
|
+
const tab = this.tabs[this.activeIndex];
|
|
51
|
+
if (!tab) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return { tab, panel: this.panels[this.activeIndex] ?? null, index: this.activeIndex };
|
|
55
|
+
}
|
|
56
|
+
activate(index, focus = false, notify = true) {
|
|
57
|
+
const next = this.tabs[index];
|
|
58
|
+
if (!next) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const changed = index !== this.activeIndex;
|
|
62
|
+
this.activeIndex = index;
|
|
63
|
+
for (const [itemIndex, tab] of this.tabs.entries()) {
|
|
64
|
+
const selected = itemIndex === index;
|
|
65
|
+
tab.setAttribute("aria-selected", selected ? "true" : "false");
|
|
66
|
+
tab.tabIndex = selected ? 0 : -1;
|
|
67
|
+
const panel = this.panels[itemIndex];
|
|
68
|
+
if (panel) {
|
|
69
|
+
panel.hidden = !selected;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (focus) {
|
|
73
|
+
next.focus({ preventScroll: true });
|
|
74
|
+
}
|
|
75
|
+
if (notify && (changed || focus)) {
|
|
76
|
+
emit(this.root, "fui:tabchange", {
|
|
77
|
+
tabs: this,
|
|
78
|
+
tab: next,
|
|
79
|
+
panel: this.panels[index] ?? null,
|
|
80
|
+
index,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
dispose() {
|
|
85
|
+
this.disposer.dispose();
|
|
86
|
+
controllers.delete(this.root);
|
|
87
|
+
}
|
|
88
|
+
handleKeydown(event, index, tablist) {
|
|
89
|
+
const vertical = tablist?.getAttribute("aria-orientation") === "vertical";
|
|
90
|
+
let nextIndex = index;
|
|
91
|
+
switch (event.key) {
|
|
92
|
+
case "ArrowRight":
|
|
93
|
+
if (vertical) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
nextIndex = (index + 1) % this.tabs.length;
|
|
97
|
+
break;
|
|
98
|
+
case "ArrowLeft":
|
|
99
|
+
if (vertical) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
nextIndex = index === 0 ? this.tabs.length - 1 : index - 1;
|
|
103
|
+
break;
|
|
104
|
+
case "ArrowDown":
|
|
105
|
+
if (!vertical) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
nextIndex = (index + 1) % this.tabs.length;
|
|
109
|
+
break;
|
|
110
|
+
case "ArrowUp":
|
|
111
|
+
if (!vertical) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
nextIndex = index === 0 ? this.tabs.length - 1 : index - 1;
|
|
115
|
+
break;
|
|
116
|
+
case "Home":
|
|
117
|
+
nextIndex = 0;
|
|
118
|
+
break;
|
|
119
|
+
case "End":
|
|
120
|
+
nextIndex = this.tabs.length - 1;
|
|
121
|
+
break;
|
|
122
|
+
default:
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
event.preventDefault();
|
|
126
|
+
this.activate(nextIndex, true);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/** Returns the controller bound to a tabs container, if any. */
|
|
130
|
+
export function getTabs(root) {
|
|
131
|
+
return controllers.get(root);
|
|
132
|
+
}
|
|
133
|
+
/** Creates or reuses the controller for a tabs container. */
|
|
134
|
+
export function bindTabs(root) {
|
|
135
|
+
const existing = controllers.get(root);
|
|
136
|
+
if (existing) {
|
|
137
|
+
return existing;
|
|
138
|
+
}
|
|
139
|
+
const controller = new Tabs(root);
|
|
140
|
+
controllers.set(root, controller);
|
|
141
|
+
return controller;
|
|
142
|
+
}
|
|
143
|
+
/** Binds every [data-fui-tabs] container under root. */
|
|
144
|
+
export function initTabs(root = document) {
|
|
145
|
+
if (typeof document === "undefined") {
|
|
146
|
+
return () => undefined;
|
|
147
|
+
}
|
|
148
|
+
const disposer = createDisposer();
|
|
149
|
+
for (const element of qsa(root, "[data-fui-tabs]")) {
|
|
150
|
+
if (controllers.has(element)) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (!qs(element, '[role="tablist"], [data-fui-tablist]') && element.getAttribute("role") !== "tablist") {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const controller = bindTabs(element);
|
|
157
|
+
disposer.add(() => controller.dispose());
|
|
158
|
+
}
|
|
159
|
+
return () => disposer.dispose();
|
|
160
|
+
}
|
package/dist/theme.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Cleanup } from "./types.js";
|
|
2
|
+
export type ThemeMode = "light" | "dark" | "system";
|
|
3
|
+
export type ResolvedTheme = "light" | "dark";
|
|
4
|
+
export interface ThemeChangeDetail {
|
|
5
|
+
mode: ThemeMode;
|
|
6
|
+
resolved: ResolvedTheme;
|
|
7
|
+
}
|
|
8
|
+
export interface ThemeOptions {
|
|
9
|
+
/** Root used for theme controls. Defaults to document. */
|
|
10
|
+
root?: ParentNode;
|
|
11
|
+
/** localStorage key. Defaults to "flexui-theme". */
|
|
12
|
+
storageKey?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function getResolvedTheme(mode?: ThemeMode): ResolvedTheme;
|
|
15
|
+
export declare function getTheme(): ThemeMode;
|
|
16
|
+
/** Applies a mode to <html> and optionally emits fui:themechange. */
|
|
17
|
+
export declare function applyTheme(mode: ThemeMode, silent?: boolean): ResolvedTheme;
|
|
18
|
+
export declare function nextTheme(mode?: ThemeMode): ThemeMode;
|
|
19
|
+
/** Sets and (by default) persists a theme mode. */
|
|
20
|
+
export declare function setTheme(mode: ThemeMode, options?: {
|
|
21
|
+
persist?: boolean;
|
|
22
|
+
storageKey?: string;
|
|
23
|
+
silent?: boolean;
|
|
24
|
+
}): ThemeMode;
|
|
25
|
+
export declare function toggleTheme(): ThemeMode;
|
|
26
|
+
/**
|
|
27
|
+
* Applies the saved/system theme and binds theme controls.
|
|
28
|
+
*
|
|
29
|
+
* Controls:
|
|
30
|
+
* [data-fui-theme-set="light|dark|system"]
|
|
31
|
+
* [data-fui-theme-toggle]
|
|
32
|
+
*/
|
|
33
|
+
export declare function initTheme(options?: ThemeOptions): Cleanup;
|
package/dist/theme.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { createDisposer, emit, on, qsa } from "./types.js";
|
|
2
|
+
const DEFAULT_STORAGE_KEY = "flexui-theme";
|
|
3
|
+
let currentMode = null;
|
|
4
|
+
// Set by initTheme so getTheme()/setTheme()/toggleTheme() read and write the
|
|
5
|
+
// same key as the controls, even when a custom storageKey was configured.
|
|
6
|
+
let activeStorageKey = DEFAULT_STORAGE_KEY;
|
|
7
|
+
function isThemeMode(value) {
|
|
8
|
+
return value === "light" || value === "dark" || value === "system";
|
|
9
|
+
}
|
|
10
|
+
function storage() {
|
|
11
|
+
if (typeof window === "undefined") {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
return window.localStorage;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function mediaQuery() {
|
|
22
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function") {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return window.matchMedia("(prefers-color-scheme: dark)");
|
|
26
|
+
}
|
|
27
|
+
function readStored(key) {
|
|
28
|
+
try {
|
|
29
|
+
const value = storage()?.getItem(key);
|
|
30
|
+
return isThemeMode(value) ? value : null;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function writeStored(key, mode) {
|
|
37
|
+
try {
|
|
38
|
+
storage()?.setItem(key, mode);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Private mode and disabled storage are non-fatal.
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function getResolvedTheme(mode = getTheme()) {
|
|
45
|
+
if (mode === "system") {
|
|
46
|
+
return mediaQuery()?.matches ? "dark" : "light";
|
|
47
|
+
}
|
|
48
|
+
return mode;
|
|
49
|
+
}
|
|
50
|
+
export function getTheme() {
|
|
51
|
+
if (currentMode) {
|
|
52
|
+
return currentMode;
|
|
53
|
+
}
|
|
54
|
+
currentMode = readStored(activeStorageKey) ?? "system";
|
|
55
|
+
return currentMode;
|
|
56
|
+
}
|
|
57
|
+
/** Applies a mode to <html> and optionally emits fui:themechange. */
|
|
58
|
+
export function applyTheme(mode, silent = false) {
|
|
59
|
+
currentMode = mode;
|
|
60
|
+
const resolved = getResolvedTheme(mode);
|
|
61
|
+
if (typeof document !== "undefined") {
|
|
62
|
+
const root = document.documentElement;
|
|
63
|
+
root.classList.toggle("dark", resolved === "dark");
|
|
64
|
+
root.dataset["fuiThemeMode"] = mode;
|
|
65
|
+
root.style.colorScheme = mode === "system" ? "light dark" : mode;
|
|
66
|
+
if (!silent) {
|
|
67
|
+
emit(document, "fui:themechange", {
|
|
68
|
+
mode,
|
|
69
|
+
resolved,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return resolved;
|
|
74
|
+
}
|
|
75
|
+
export function nextTheme(mode = getTheme()) {
|
|
76
|
+
if (mode === "light") {
|
|
77
|
+
return "dark";
|
|
78
|
+
}
|
|
79
|
+
if (mode === "dark") {
|
|
80
|
+
return "system";
|
|
81
|
+
}
|
|
82
|
+
return "light";
|
|
83
|
+
}
|
|
84
|
+
/** Sets and (by default) persists a theme mode. */
|
|
85
|
+
export function setTheme(mode, options = {}) {
|
|
86
|
+
applyTheme(mode, options.silent);
|
|
87
|
+
if (options.storageKey !== undefined) {
|
|
88
|
+
activeStorageKey = options.storageKey;
|
|
89
|
+
}
|
|
90
|
+
if (options.persist !== false) {
|
|
91
|
+
writeStored(activeStorageKey, mode);
|
|
92
|
+
}
|
|
93
|
+
syncThemeControls();
|
|
94
|
+
return mode;
|
|
95
|
+
}
|
|
96
|
+
export function toggleTheme() {
|
|
97
|
+
return setTheme(nextTheme());
|
|
98
|
+
}
|
|
99
|
+
function syncThemeControls(root = document) {
|
|
100
|
+
if (typeof document === "undefined") {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const mode = getTheme();
|
|
104
|
+
for (const button of qsa(root, "[data-fui-theme-set]")) {
|
|
105
|
+
const value = button.getAttribute("data-fui-theme-set");
|
|
106
|
+
button.setAttribute("aria-pressed", value === mode ? "true" : "false");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Applies the saved/system theme and binds theme controls.
|
|
111
|
+
*
|
|
112
|
+
* Controls:
|
|
113
|
+
* [data-fui-theme-set="light|dark|system"]
|
|
114
|
+
* [data-fui-theme-toggle]
|
|
115
|
+
*/
|
|
116
|
+
export function initTheme(options = {}) {
|
|
117
|
+
if (typeof document === "undefined") {
|
|
118
|
+
return () => undefined;
|
|
119
|
+
}
|
|
120
|
+
const disposer = createDisposer();
|
|
121
|
+
const root = options.root ?? document;
|
|
122
|
+
const key = options.storageKey ?? DEFAULT_STORAGE_KEY;
|
|
123
|
+
activeStorageKey = key;
|
|
124
|
+
const saved = readStored(key);
|
|
125
|
+
applyTheme(saved ?? "system", true);
|
|
126
|
+
syncThemeControls(root);
|
|
127
|
+
const query = mediaQuery();
|
|
128
|
+
if (query) {
|
|
129
|
+
const handleChange = () => {
|
|
130
|
+
if (getTheme() === "system") {
|
|
131
|
+
applyTheme("system");
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
query.addEventListener("change", handleChange);
|
|
135
|
+
disposer.add(() => query.removeEventListener("change", handleChange));
|
|
136
|
+
}
|
|
137
|
+
disposer.add(on(root, "click", (event) => {
|
|
138
|
+
const target = event.target;
|
|
139
|
+
if (!(target instanceof Element)) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const control = target.closest("[data-fui-theme-set], [data-fui-theme-toggle]");
|
|
143
|
+
if (!control || !root.contains(control)) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const set = control.getAttribute("data-fui-theme-set");
|
|
147
|
+
if (isThemeMode(set)) {
|
|
148
|
+
setTheme(set, { storageKey: key });
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
setTheme(nextTheme(), { storageKey: key });
|
|
152
|
+
}
|
|
153
|
+
event.preventDefault();
|
|
154
|
+
}));
|
|
155
|
+
disposer.add(on(document, "fui:themechange", () => syncThemeControls(root)));
|
|
156
|
+
return () => disposer.dispose();
|
|
157
|
+
}
|
package/dist/toast.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Cleanup } from "./types.js";
|
|
2
|
+
export type ToastVariant = "info" | "success" | "danger";
|
|
3
|
+
export interface ToastOptions {
|
|
4
|
+
title: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
variant?: ToastVariant;
|
|
7
|
+
/** Milliseconds before dismissal. Use 0 to keep the toast open. */
|
|
8
|
+
duration?: number;
|
|
9
|
+
dismissible?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface ToastHandle {
|
|
12
|
+
readonly element: HTMLElement;
|
|
13
|
+
dismiss(): void;
|
|
14
|
+
}
|
|
15
|
+
/** Creates and announces a toast, creating the live region on first use. */
|
|
16
|
+
export declare function toast(options: ToastOptions): ToastHandle;
|
|
17
|
+
/** Binds static toast dismiss buttons and auto-dismiss timers. */
|
|
18
|
+
export declare function initToasts(root?: ParentNode): Cleanup;
|