flexui-react 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.
@@ -0,0 +1,62 @@
1
+ /**
2
+ * React bindings for `@flexui/runtime`.
3
+ *
4
+ * The hooks own the controller lifecycle: they bind on mount, dispose on
5
+ * unmount, and mirror the controller's open state into React state so a
6
+ * component re-renders when a dialog closes from the keyboard or a click
7
+ * outside it. Component CSS still works without any of this.
8
+ */
9
+ import { type RefObject } from "react";
10
+ import { type DialogOptions } from "@flexui/runtime/dialog";
11
+ import { type DropdownOptions } from "@flexui/runtime/dropdown";
12
+ import { type PopoverOptions } from "@flexui/runtime/popover";
13
+ import { type ResolvedTheme, type ThemeMode } from "@flexui/runtime/theme";
14
+ export interface UseDialogResult {
15
+ /** Attach to the `<dialog>` element. */
16
+ ref: RefObject<HTMLDialogElement | null>;
17
+ open(): void;
18
+ close(reason?: string): void;
19
+ /** Mirrors the dialog's state, including closes from Escape or the backdrop. */
20
+ isOpen: boolean;
21
+ }
22
+ /**
23
+ * Binds a native `<dialog class="fui-dialog" data-fui-dialog>`.
24
+ * Options are read when the dialog is first bound.
25
+ */
26
+ export declare function useDialog(options?: DialogOptions): UseDialogResult;
27
+ export interface UseDropdownResult {
28
+ /** Attach to the trigger button. */
29
+ triggerRef: RefObject<HTMLElement | null>;
30
+ open(focus?: "first" | "last" | "none"): void;
31
+ close(restoreFocus?: boolean): void;
32
+ toggle(): void;
33
+ isOpen: boolean;
34
+ }
35
+ /** Binds a trigger to the menu returned by `menu`, an id or an element. */
36
+ export declare function useDropdown(menu: string | HTMLElement | null, options?: DropdownOptions): UseDropdownResult;
37
+ export interface UsePopoverResult {
38
+ /** Attach to the trigger button. */
39
+ triggerRef: RefObject<HTMLElement | null>;
40
+ open(): void;
41
+ close(): void;
42
+ toggle(): void;
43
+ isOpen: boolean;
44
+ }
45
+ /** Binds a trigger to a `popover` panel returned by `panel`, an id or an element. */
46
+ export declare function usePopover(panel: string | HTMLElement | null, options?: PopoverOptions): UsePopoverResult;
47
+ export interface UseTabsResult {
48
+ /** Attach to the `[data-fui-tabs]` container. */
49
+ ref: RefObject<HTMLElement | null>;
50
+ activeIndex: number;
51
+ activate(index: number, focus?: boolean): void;
52
+ }
53
+ /** Binds an ARIA tablist and tracks its active tab in React state. */
54
+ export declare function useTabs(): UseTabsResult;
55
+ export interface UseThemeResult {
56
+ mode: ThemeMode;
57
+ resolved: ResolvedTheme;
58
+ setMode(mode: ThemeMode): void;
59
+ toggle(): void;
60
+ }
61
+ /** Reads and changes the FlexUI theme mode, re-rendering on `fui:themechange`. */
62
+ export declare function useTheme(): UseThemeResult;
package/dist/index.js ADDED
@@ -0,0 +1,182 @@
1
+ /**
2
+ * React bindings for `@flexui/runtime`.
3
+ *
4
+ * The hooks own the controller lifecycle: they bind on mount, dispose on
5
+ * unmount, and mirror the controller's open state into React state so a
6
+ * component re-renders when a dialog closes from the keyboard or a click
7
+ * outside it. Component CSS still works without any of this.
8
+ */
9
+ import { useCallback, useEffect, useRef, useState } from "react";
10
+ import { bindDialog } from "@flexui/runtime/dialog";
11
+ import { bindDropdown } from "@flexui/runtime/dropdown";
12
+ import { bindPopover } from "@flexui/runtime/popover";
13
+ import { bindTabs } from "@flexui/runtime/tabs";
14
+ import { getResolvedTheme, getTheme, setTheme, toggleTheme, } from "@flexui/runtime/theme";
15
+ /**
16
+ * Mirrors a controller's open state into React state. The ref is read inside
17
+ * the effect, after React has attached it, which is why the element itself is
18
+ * not passed in: on the first render it is still null.
19
+ */
20
+ function useOpenState(ref, openEvent) {
21
+ const [isOpen, setIsOpen] = useState(false);
22
+ const closeEvent = openEvent === "fui:open" ? "fui:close" : "fui:popoverclose";
23
+ useEffect(() => {
24
+ const element = ref.current;
25
+ if (element === null) {
26
+ return;
27
+ }
28
+ const open = () => setIsOpen(true);
29
+ const close = () => setIsOpen(false);
30
+ element.addEventListener(openEvent, open);
31
+ element.addEventListener(closeEvent, close);
32
+ return () => {
33
+ element.removeEventListener(openEvent, open);
34
+ element.removeEventListener(closeEvent, close);
35
+ };
36
+ }, [ref, openEvent, closeEvent]);
37
+ return isOpen;
38
+ }
39
+ /**
40
+ * Binds a native `<dialog class="fui-dialog" data-fui-dialog>`.
41
+ * Options are read when the dialog is first bound.
42
+ */
43
+ export function useDialog(options = {}) {
44
+ const ref = useRef(null);
45
+ const controller = useRef(null);
46
+ const isOpen = useOpenState(ref, "fui:open");
47
+ useEffect(() => {
48
+ const element = ref.current;
49
+ if (element === null) {
50
+ return;
51
+ }
52
+ const instance = bindDialog(element, options);
53
+ controller.current = instance;
54
+ return () => {
55
+ controller.current = null;
56
+ instance.dispose();
57
+ };
58
+ }, []);
59
+ const open = useCallback(() => {
60
+ controller.current?.open();
61
+ }, []);
62
+ const close = useCallback((reason) => {
63
+ controller.current?.close(reason);
64
+ }, []);
65
+ return { ref, open, close, isOpen };
66
+ }
67
+ /** Binds a trigger to the menu returned by `menu`, an id or an element. */
68
+ export function useDropdown(menu, options = {}) {
69
+ const triggerRef = useRef(null);
70
+ const controller = useRef(null);
71
+ const isOpen = useOpenState(triggerRef, "fui:open");
72
+ useEffect(() => {
73
+ const trigger = triggerRef.current;
74
+ if (trigger === null) {
75
+ return;
76
+ }
77
+ const target = typeof menu === "string" ? document.getElementById(menu.replace(/^#/, "")) : menu;
78
+ if (target === null) {
79
+ return;
80
+ }
81
+ const instance = bindDropdown(trigger, target, options);
82
+ controller.current = instance;
83
+ return () => {
84
+ controller.current = null;
85
+ instance.dispose();
86
+ };
87
+ }, [menu]);
88
+ const open = useCallback((focus) => {
89
+ controller.current?.open(focus);
90
+ }, []);
91
+ const close = useCallback((restoreFocus) => {
92
+ controller.current?.close(restoreFocus);
93
+ }, []);
94
+ const toggle = useCallback(() => {
95
+ controller.current?.toggle();
96
+ }, []);
97
+ return { triggerRef, open, close, toggle, isOpen };
98
+ }
99
+ /** Binds a trigger to a `popover` panel returned by `panel`, an id or an element. */
100
+ export function usePopover(panel, options = {}) {
101
+ const triggerRef = useRef(null);
102
+ const controller = useRef(null);
103
+ const isOpen = useOpenState(triggerRef, "fui:popoveropen");
104
+ useEffect(() => {
105
+ const trigger = triggerRef.current;
106
+ if (trigger === null) {
107
+ return;
108
+ }
109
+ const target = typeof panel === "string" ? document.getElementById(panel.replace(/^#/, "")) : panel;
110
+ if (target === null) {
111
+ return;
112
+ }
113
+ const instance = bindPopover(trigger, target, options);
114
+ controller.current = instance;
115
+ return () => {
116
+ controller.current = null;
117
+ instance.dispose();
118
+ };
119
+ }, [panel]);
120
+ const open = useCallback(() => {
121
+ controller.current?.open();
122
+ }, []);
123
+ const close = useCallback(() => {
124
+ controller.current?.close();
125
+ }, []);
126
+ const toggle = useCallback(() => {
127
+ controller.current?.toggle();
128
+ }, []);
129
+ return { triggerRef, open, close, toggle, isOpen };
130
+ }
131
+ /** Binds an ARIA tablist and tracks its active tab in React state. */
132
+ export function useTabs() {
133
+ const ref = useRef(null);
134
+ const controller = useRef(null);
135
+ const [activeIndex, setActiveIndex] = useState(0);
136
+ useEffect(() => {
137
+ const element = ref.current;
138
+ if (element === null) {
139
+ return;
140
+ }
141
+ const instance = bindTabs(element);
142
+ controller.current = instance;
143
+ setActiveIndex(instance.active?.index ?? 0);
144
+ const handle = (event) => {
145
+ const detail = event.detail;
146
+ setActiveIndex(detail.index);
147
+ };
148
+ element.addEventListener("fui:tabchange", handle);
149
+ return () => {
150
+ element.removeEventListener("fui:tabchange", handle);
151
+ controller.current = null;
152
+ instance.dispose();
153
+ };
154
+ }, []);
155
+ const activate = useCallback((index, focus) => {
156
+ controller.current?.activate(index, focus);
157
+ }, []);
158
+ return { ref, activeIndex, activate };
159
+ }
160
+ /** Reads and changes the FlexUI theme mode, re-rendering on `fui:themechange`. */
161
+ export function useTheme() {
162
+ const [mode, setModeState] = useState(() => getTheme());
163
+ const [resolved, setResolved] = useState(() => getResolvedTheme(getTheme()));
164
+ useEffect(() => {
165
+ const handle = (event) => {
166
+ const detail = event.detail;
167
+ setModeState(detail.mode);
168
+ setResolved(detail.resolved);
169
+ };
170
+ document.addEventListener("fui:themechange", handle);
171
+ return () => {
172
+ document.removeEventListener("fui:themechange", handle);
173
+ };
174
+ }, []);
175
+ const setMode = useCallback((next) => {
176
+ setTheme(next);
177
+ }, []);
178
+ const toggle = useCallback(() => {
179
+ toggleTheme();
180
+ }, []);
181
+ return { mode, resolved, setMode, toggle };
182
+ }
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
- {
2
- "name": "flexui-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "flexui-react",
3
+ "version": "2.0.0-alpha.1",
4
+ "description": "React hooks for FlexUI interactive components",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc -p tsconfig.json",
21
+ "typecheck": "tsc -p tsconfig.json --noEmit"
22
+ },
23
+ "dependencies": {
24
+ "flexui-runtime": "2.0.0-alpha.1"
25
+ },
26
+ "peerDependencies": {
27
+ "react": "^19.3.1"
28
+ },
29
+ "keywords": [
30
+ "flexui",
31
+ "react",
32
+ "hooks"
33
+ ],
34
+ "devDependencies": {
35
+ "@types/react": "^19.3.3"
36
+ }
37
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.