1 | import * as React from "react";
|
2 | import {
|
3 | SealedInitialState,
|
4 | useSealedState,
|
5 | } from "reakit-utils/useSealedState";
|
6 | import {
|
7 | CompositeState,
|
8 | CompositeActions,
|
9 | CompositeInitialState,
|
10 | useCompositeState,
|
11 | } from "../Composite";
|
12 |
|
13 | export type MenuBarState = CompositeState & {
|
14 | |
15 |
|
16 |
|
17 | unstable_values: Record<string, any>;
|
18 | };
|
19 |
|
20 | export type MenuBarActions = CompositeActions & {
|
21 | |
22 |
|
23 |
|
24 | unstable_setValue: (name: string, value?: any) => void;
|
25 | };
|
26 |
|
27 | export type MenuBarInitialState = CompositeInitialState &
|
28 | Partial<Pick<MenuBarState, "unstable_values">>;
|
29 |
|
30 | export type MenuBarStateReturn = MenuBarState & MenuBarActions;
|
31 |
|
32 | export function useMenuBarState(
|
33 | initialState: SealedInitialState<MenuBarInitialState> = {}
|
34 | ): MenuBarStateReturn {
|
35 | const {
|
36 | orientation = "horizontal",
|
37 | unstable_values: initialValues = {},
|
38 | ...sealed
|
39 | } = useSealedState(initialState);
|
40 |
|
41 | const [values, setValues] = React.useState(initialValues);
|
42 | const composite = useCompositeState({ ...sealed, orientation });
|
43 |
|
44 | return {
|
45 | ...composite,
|
46 | unstable_values: values,
|
47 | unstable_setValue: React.useCallback((name, value) => {
|
48 | setValues((vals) => ({
|
49 | ...vals,
|
50 | [name]: typeof value === "function" ? value(vals) : value,
|
51 | }));
|
52 | }, []),
|
53 | };
|
54 | }
|