@open-pioneer/react-utils 4.4.0 → 4.5.0-dev-roving-menu.20260129100701
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/CHANGELOG.md +12 -0
- package/README.md +63 -0
- package/TitledSection.d.ts +23 -2
- package/TitledSection.js.map +1 -1
- package/_virtual/source-info.js +4 -0
- package/_virtual/source-info.js.map +1 -0
- package/index.d.ts +20 -0
- package/index.js +3 -0
- package/index.js.map +1 -1
- package/package.json +4 -2
- package/roving-menu/RovingMenuRoot.d.ts +20 -0
- package/roving-menu/RovingMenuRoot.js +11 -0
- package/roving-menu/RovingMenuRoot.js.map +1 -0
- package/roving-menu/RovingMenuState.d.ts +65 -0
- package/roving-menu/RovingMenuState.js +177 -0
- package/roving-menu/RovingMenuState.js.map +1 -0
- package/roving-menu/useRovingMenu.d.ts +50 -0
- package/roving-menu/useRovingMenu.js +36 -0
- package/roving-menu/useRovingMenu.js.map +1 -0
- package/roving-menu/useRovingMenuItem.d.ts +59 -0
- package/roving-menu/useRovingMenuItem.js +48 -0
- package/roving-menu/useRovingMenuItem.js.map +1 -0
- package/useCommonComponentProps.d.ts +5 -0
- package/useCommonComponentProps.js.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @open-pioneer/react-utils
|
|
2
2
|
|
|
3
|
+
## 4.5.0-dev-roving-menu.20260129100701
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 707e240: Provide an implementation of the roving tab index pattern for menus.
|
|
8
|
+
This roving menu can be used to implement accessible menus with support for keyboard navigation.
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
11
|
+
|
|
12
|
+
- @open-pioneer/core@4.5.0-dev-roving-menu.20260129100701
|
|
13
|
+
- @open-pioneer/reactivity@4.5.0-dev-roving-menu.20260129100701
|
|
14
|
+
|
|
3
15
|
## 4.4.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -43,6 +43,69 @@ Example:
|
|
|
43
43
|
In the preceding example the topmost heading(s) in `TheRestOfYourApplication` start at level 2, and nested headings use increasing levels as usual.
|
|
44
44
|
For more details, see the API documentation.
|
|
45
45
|
|
|
46
|
+
### Roving menu
|
|
47
|
+
|
|
48
|
+
The roving menu is a render-less component that can be used to implement an accessible menu with keyboard navigation.
|
|
49
|
+
It implements focus management and supports navigation using the arrow keys and home/end buttons.
|
|
50
|
+
|
|
51
|
+
To create a roving menu, use the `useRovingMenu` hook in combination with the `RovingMenuRoot`.
|
|
52
|
+
You must apply the `menuProps` to some container element (a `div`, a list, etc.) and the `menuState` to the `<RovingMenuRoot />`, which acts as a context provider.
|
|
53
|
+
|
|
54
|
+
For example:
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { useRovingMenu, RovingMenuRoot } from "@open-pioneer/react-utils";
|
|
58
|
+
|
|
59
|
+
function HorizontalMenu() {
|
|
60
|
+
const { menuProps, menuState } = useRovingMenu({
|
|
61
|
+
orientation: "horizontal"
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<HStack {...menuProps} justify="center" gap={5} padding={2}>
|
|
66
|
+
<RovingMenuRoot menuState={menuState}>
|
|
67
|
+
<MenuItem value="1" />
|
|
68
|
+
<MenuItem value="2" />
|
|
69
|
+
<MenuItem value="3" />
|
|
70
|
+
<MenuItem value="4" />
|
|
71
|
+
</RovingMenuRoot>
|
|
72
|
+
</HStack>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
To implement a menu item, use the `useRovingMenuItem` hook.
|
|
78
|
+
You must provide a unique value for the item (unique within the menu).
|
|
79
|
+
You must also make certain to apply the resulting `itemProps` to the menu item component (which can be any focusable DOM element).
|
|
80
|
+
These props implement keyboard navigation and focus handling.
|
|
81
|
+
|
|
82
|
+
For example:
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
function MenuItem(props: { value: string }) {
|
|
86
|
+
const { value } = props;
|
|
87
|
+
const { itemProps } = useRovingMenuItem({
|
|
88
|
+
value
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<Button {...itemProps} onClick={() => console.log(`Button ${value} triggered.`)}>
|
|
93
|
+
{value}
|
|
94
|
+
</Button>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Disabled menu items
|
|
100
|
+
|
|
101
|
+
Use the `disabled` prop of `useRovingMenuItem` to indicate that your menu item is currently disabled.
|
|
102
|
+
Disabled items will lose focus and will be skipped during keyboard navigation.
|
|
103
|
+
|
|
104
|
+
#### Limitations
|
|
105
|
+
|
|
106
|
+
- Items must be focusable HTML elements, such as buttons.
|
|
107
|
+
Composed components are not supported at this time.
|
|
108
|
+
|
|
46
109
|
### Hooks
|
|
47
110
|
|
|
48
111
|
#### useCommonComponentProps()
|
package/TitledSection.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { HeadingProps } from "@chakra-ui/react";
|
|
2
2
|
import { FC, ReactNode, RefAttributes } from "react";
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Properties of the {@link TitledSection} component.
|
|
5
|
+
*
|
|
6
|
+
* @group Headings
|
|
7
|
+
**/
|
|
4
8
|
export interface TitledSectionProps {
|
|
5
9
|
/**
|
|
6
10
|
* The title component.
|
|
@@ -83,12 +87,17 @@ export interface TitledSectionProps {
|
|
|
83
87
|
* Content
|
|
84
88
|
* </TitledSection>
|
|
85
89
|
* ```
|
|
90
|
+
*
|
|
91
|
+
* @group Headings
|
|
92
|
+
* @expandType TitledSectionProps
|
|
86
93
|
*/
|
|
87
94
|
export declare function TitledSection(props: TitledSectionProps): ReactNode;
|
|
88
95
|
/**
|
|
89
96
|
* Properties supported by the {@link SectionHeading} component.
|
|
90
97
|
*
|
|
91
98
|
* All chakra properties are forwarded to the [Heading](https://chakra-ui.com/docs/components/heading) component.
|
|
99
|
+
*
|
|
100
|
+
* @group Headings
|
|
92
101
|
*/
|
|
93
102
|
export interface SectionHeadingProps extends HeadingProps, RefAttributes<HTMLHeadingElement> {
|
|
94
103
|
children?: ReactNode | undefined;
|
|
@@ -100,10 +109,15 @@ export interface SectionHeadingProps extends HeadingProps, RefAttributes<HTMLHea
|
|
|
100
109
|
* Headings are rendered as [Chakra Headings](https://chakra-ui.com/docs/components/heading).
|
|
101
110
|
*
|
|
102
111
|
* Heading levels are managed by nesting {@link TitledSection}.
|
|
112
|
+
*
|
|
113
|
+
* @group Headings
|
|
114
|
+
* @expandType SectionHeadingProps
|
|
103
115
|
*/
|
|
104
116
|
export declare function SectionHeading(props: SectionHeadingProps): ReactNode;
|
|
105
117
|
/**
|
|
106
118
|
* Properties for the {@link ConfigureTitledSection} component.
|
|
119
|
+
*
|
|
120
|
+
* @group Headings
|
|
107
121
|
*/
|
|
108
122
|
export interface ConfigureTitledSectionProps {
|
|
109
123
|
level: HeadingLevel;
|
|
@@ -142,13 +156,20 @@ export interface ConfigureTitledSectionProps {
|
|
|
142
156
|
*
|
|
143
157
|
* The headings used by `Widget` will start with `h5`.
|
|
144
158
|
*
|
|
159
|
+
* @group Headings
|
|
160
|
+
* @expandType ConfigureTitledSectionProps
|
|
145
161
|
*/
|
|
146
162
|
export declare const ConfigureTitledSection: FC<ConfigureTitledSectionProps>;
|
|
147
|
-
/**
|
|
163
|
+
/**
|
|
164
|
+
* The level of a html heading.
|
|
165
|
+
* @group Headings
|
|
166
|
+
**/
|
|
148
167
|
export type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
|
|
149
168
|
/**
|
|
150
169
|
* Returns the current heading level.
|
|
151
170
|
*
|
|
152
171
|
* This hook should be used in the `title` property of a {@link TitledSection} or in that component's children.
|
|
172
|
+
*
|
|
173
|
+
* @group Headings
|
|
153
174
|
*/
|
|
154
175
|
export declare function useHeadingLevel(): HeadingLevel;
|
package/TitledSection.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TitledSection.js","sources":["TitledSection.tsx"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { Heading, HeadingProps } from \"@chakra-ui/react\";\nimport { FC, ReactNode, RefAttributes, createContext, useContext } from \"react\";\n\nconst LevelContext = createContext<number | undefined>(undefined);\nLevelContext.displayName = \"LevelContext\";\n\n
|
|
1
|
+
{"version":3,"file":"TitledSection.js","sources":["TitledSection.tsx"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { Heading, HeadingProps } from \"@chakra-ui/react\";\nimport { FC, ReactNode, RefAttributes, createContext, useContext } from \"react\";\n\nconst LevelContext = createContext<number | undefined>(undefined);\nLevelContext.displayName = \"LevelContext\";\n\n/**\n * Properties of the {@link TitledSection} component.\n *\n * @group Headings\n **/\nexport interface TitledSectionProps {\n /**\n * The title component.\n *\n * Strings are automatically wrapped in a {@link SectionHeading}.\n * More complex nodes should use the {@link SectionHeading} internally.\n */\n title?: string | ReactNode | undefined;\n\n /**\n * Properties for the automatically generated section heading.\n *\n * NOTE: This is only applied if `title` is a string.\n */\n sectionHeadingProps?: SectionHeadingProps | undefined;\n\n /**\n * Children are rendered without any modifications.\n */\n children?: ReactNode | undefined;\n}\n\n/**\n * Automatically manages the level of html headings such as `h1` and `h2`.\n *\n * Use this component's `title` property or `<SectionHeading />` components in the children\n * of this component instead of manual heading tags.\n * Nested sections will automatically use the correct level for their current location.\n *\n * Titles are rendered as [Chakra Headings](https://chakra-ui.com/docs/components/heading) by default.\n *\n * **Example:** Simple string headings (1, 2 and 3 will be rendered as h1, h2 and h3):\n *\n * ```jsx\n * <TitledSection title=\"1\">\n * Top level content\n *\n * <TitledSection title=\"2\" sectionHeadingProps={...}>\n * Nested content\n *\n * <TitledSection title=\"3\">\n * Deeply nested content\n * </TitledSection>\n * </TitledSection>\n * </TitledSection>\n * ```\n *\n * When using using a string for the `title` prop, you can specify the `sectionHeadingProps` to\n * customize the properties of the automatically generated heading component.\n *\n * **Example:** custom section headings as children\n *\n * ```jsx\n * <TitledSection>\n * <SectionHeading>Top level heading (H1)</SectionHeading>\n *\n * .. Some Content ..\n *\n * <TitledSection>\n * <SectionHeading>Other Heading (H2)</SectionHeading>\n *\n * .. More Content ..\n * </TitledSection>\n *\n * </TitledSection>\n * ```\n *\n * > NOTE: You should use exactly one `<SectionHeading />` for each `<TitledSection />`.\n * > The SectionHeading can appear anywhere, for example wrapped in other layout components.\n *\n * **Example:** custom title rendering\n *\n * You can also use completely custom components for the title.\n * In this case, you should ensure that you use the `SectionHeading` component within the `title` in some place.\n * The section heading does not need to be the immediate child, you can wrap it any way you want.\n *\n * ```jsx\n * <TitledSection\n * title={\n * <SectionHeading size=\"4xl\">Heading</SectionHeading>\n * }\n * >\n * Content\n * </TitledSection>\n * ```\n *\n * @group Headings\n * @expandType TitledSectionProps\n */\nexport function TitledSection(props: TitledSectionProps): ReactNode {\n const { title, sectionHeadingProps, children } = props;\n const currentLevel = useContext(LevelContext) ?? 0;\n const heading =\n typeof title === \"string\" ? (\n <SectionHeading {...sectionHeadingProps}>{title}</SectionHeading>\n ) : (\n title\n );\n\n return (\n <LevelContext value={currentLevel + 1}>\n {heading}\n {children}\n </LevelContext>\n );\n}\n\n/**\n * Properties supported by the {@link SectionHeading} component.\n *\n * All chakra properties are forwarded to the [Heading](https://chakra-ui.com/docs/components/heading) component.\n *\n * @group Headings\n */\nexport interface SectionHeadingProps extends HeadingProps, RefAttributes<HTMLHeadingElement> {\n children?: ReactNode | undefined;\n}\n\n/**\n * Renders an appropriate heading tag for the current heading level (`h1` through `h6`).\n * This component should only be used as part of the `title` prop of the {@link TitledSection}.\n *\n * Headings are rendered as [Chakra Headings](https://chakra-ui.com/docs/components/heading).\n *\n * Heading levels are managed by nesting {@link TitledSection}.\n *\n * @group Headings\n * @expandType SectionHeadingProps\n */\nexport function SectionHeading(props: SectionHeadingProps): ReactNode {\n const { children, ref, ...rest } = props;\n const level = useHeadingLevel();\n const tag = getHeadingTag(level);\n return (\n <Heading as={tag} ref={ref} {...rest}>\n {children}\n </Heading>\n );\n}\n\n/**\n * Properties for the {@link ConfigureTitledSection} component.\n *\n * @group Headings\n */\nexport interface ConfigureTitledSectionProps {\n level: HeadingLevel;\n children?: ReactNode;\n}\n\n/**\n * Overrides the heading level for all children.\n *\n * TitledSection components nested in `children` will start with the configured level.\n * Nested TitledSections will continue to use the next appropriate heading level.\n *\n * Example: Changing the global heading level for your application\n *\n * If your application is embedded into another site, it should often not use the `h1` tag\n * but start with a higher heading level instead. To achieve that, simply wrap your application\n * with `<ConfigureTitledSectionProps>`. No other code changes are necessary:\n *\n * ```jsx\n * <ConfigureTitledSection level={2}>\n * <TheRestOfYourApplication />\n * </ConfigureTitledSection>\n * ```\n *\n * Example: Embedding a widget\n *\n * Given a `<Widget />` (that we cannot easily change) that internally uses `TitledSection`,\n * this is how we can embed it with a custom heading level.\n * This can be appropriate in some circumstances, for example when the react component tree does not match the DOM\n * tree (portals etc.).\n *\n * ```jsx\n * <ConfigureTitledSection level={5}>\n * <Widget />\n * </ConfigureTitledSection>\n * ```\n *\n * The headings used by `Widget` will start with `h5`.\n *\n * @group Headings\n * @expandType ConfigureTitledSectionProps\n */\nexport const ConfigureTitledSection: FC<ConfigureTitledSectionProps> = (props) => {\n const level = props.level - 1; // TitledSection increments by 1.\n return <LevelContext value={level}>{props.children}</LevelContext>;\n};\n\n/**\n * The level of a html heading.\n * @group Headings\n **/\nexport type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;\n\n/**\n * Returns the current heading level.\n *\n * This hook should be used in the `title` property of a {@link TitledSection} or in that component's children.\n *\n * @group Headings\n */\nexport function useHeadingLevel(): HeadingLevel {\n const level = useContext(LevelContext);\n if (level == null) {\n throw new Error(\"useHeadingLevel() must be used within a <TitledSection />\");\n }\n return Math.max(1, Math.min(level, 6)) as HeadingLevel;\n}\n\nfunction getHeadingTag(level: HeadingLevel): `h${HeadingLevel}` {\n return `h${level}`;\n}\n"],"names":[],"mappings":";;;;AAKA,MAAM,YAAA,GAAe,cAAkC,MAAS,CAAA;AAChE,YAAA,CAAa,WAAA,GAAc,cAAA;AAgGpB,SAAS,cAAc,KAAA,EAAsC;AAChE,EAAA,MAAM,EAAE,KAAA,EAAO,mBAAA,EAAqB,QAAA,EAAS,GAAI,KAAA;AACjD,EAAA,MAAM,YAAA,GAAe,UAAA,CAAW,YAAY,CAAA,IAAK,CAAA;AACjD,EAAA,MAAM,OAAA,GACF,OAAO,KAAA,KAAU,QAAA,uBACZ,cAAA,EAAA,EAAgB,GAAG,mBAAA,EAAsB,QAAA,EAAA,KAAA,EAAM,CAAA,GAEhD,KAAA;AAGR,EAAA,uBACI,IAAA,CAAC,YAAA,EAAA,EAAa,KAAA,EAAO,YAAA,GAAe,CAAA,EAC/B,QAAA,EAAA;AAAA,IAAA,OAAA;AAAA,IACA;AAAA,GAAA,EACL,CAAA;AAER;AAwBO,SAAS,eAAe,KAAA,EAAuC;AAClE,EAAA,MAAM,EAAE,QAAA,EAAU,GAAA,EAAK,GAAG,MAAK,GAAI,KAAA;AACnC,EAAA,MAAM,QAAQ,eAAA,EAAgB;AAC9B,EAAA,MAAM,GAAA,GAAM,cAAc,KAAK,CAAA;AAC/B,EAAA,2BACK,OAAA,EAAA,EAAQ,EAAA,EAAI,KAAK,GAAA,EAAW,GAAG,MAC3B,QAAA,EACL,CAAA;AAER;AAgDO,MAAM,sBAAA,GAA0D,CAAC,KAAA,KAAU;AAC9E,EAAA,MAAM,KAAA,GAAQ,MAAM,KAAA,GAAQ,CAAA;AAC5B,EAAA,uBAAO,GAAA,CAAC,YAAA,EAAA,EAAa,KAAA,EAAO,KAAA,EAAQ,gBAAM,QAAA,EAAS,CAAA;AACvD;AAeO,SAAS,eAAA,GAAgC;AAC5C,EAAA,MAAM,KAAA,GAAQ,WAAW,YAAY,CAAA;AACrC,EAAA,IAAI,SAAS,IAAA,EAAM;AACf,IAAA,MAAM,IAAI,MAAM,2DAA2D,CAAA;AAAA,EAC/E;AACA,EAAA,OAAO,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,KAAA,EAAO,CAAC,CAAC,CAAA;AACzC;AAEA,SAAS,cAAc,KAAA,EAAyC;AAC5D,EAAA,OAAO,IAAI,KAAK,CAAA,CAAA;AACpB;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source-info.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
*
|
|
4
|
+
* @groupDescription Headings
|
|
5
|
+
*
|
|
6
|
+
* Implements automatic heading levels. Use {@link TitledSection} and {@link SectionHeading} instead of raw `h1`, `h2`, ... tags.
|
|
7
|
+
*
|
|
8
|
+
* @groupDescription Roving menu
|
|
9
|
+
*
|
|
10
|
+
* A (render-less) implementation of the roving tab index pattern.
|
|
11
|
+
* Useful as a building block for custom accessible menus.
|
|
12
|
+
*
|
|
13
|
+
* @groupDescription Common component props
|
|
14
|
+
*
|
|
15
|
+
* A basic hook that enforces some common properties and patterns shared by all major trails UI components.
|
|
16
|
+
*/
|
|
1
17
|
export { useEvent } from "./useEvent";
|
|
2
18
|
export { type TitledSectionProps, TitledSection, type SectionHeadingProps, SectionHeading, type ConfigureTitledSectionProps, ConfigureTitledSection, type HeadingLevel, useHeadingLevel } from "./TitledSection";
|
|
3
19
|
export { type CommonComponentProps, useCommonComponentProps } from "./useCommonComponentProps";
|
|
20
|
+
export { RovingMenuRoot, type RovingMenuRootProps } from "./roving-menu/RovingMenuRoot";
|
|
21
|
+
export { useRovingMenu, type RovingMenuProps, type RovingMenuResult } from "./roving-menu/useRovingMenu";
|
|
22
|
+
export { useRovingMenuItem, type RovingMenuItemProps, type RovingMenuItemResult } from "./roving-menu/useRovingMenuItem";
|
|
23
|
+
export { type RovingMenuState } from "./roving-menu/RovingMenuState";
|
package/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export { useEvent } from './useEvent.js';
|
|
2
2
|
export { ConfigureTitledSection, SectionHeading, TitledSection, useHeadingLevel } from './TitledSection.js';
|
|
3
3
|
export { useCommonComponentProps } from './useCommonComponentProps.js';
|
|
4
|
+
export { RovingMenuRoot } from './roving-menu/RovingMenuRoot.js';
|
|
5
|
+
export { useRovingMenu } from './roving-menu/useRovingMenu.js';
|
|
6
|
+
export { useRovingMenuItem } from './roving-menu/useRovingMenuItem.js';
|
|
4
7
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@open-pioneer/react-utils",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.5.0-dev-roving-menu.20260129100701",
|
|
5
5
|
"description": "Contains common react utilities used by the open pioneer trails project",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"open-pioneer-trails"
|
|
@@ -15,9 +15,11 @@
|
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@chakra-ui/react": "^3.31.0",
|
|
18
|
+
"@conterra/reactivity-core": "^0.8.1",
|
|
18
19
|
"classnames": "^2.5.1",
|
|
19
20
|
"react": "^19.2.3",
|
|
20
|
-
"@open-pioneer/core": "4.
|
|
21
|
+
"@open-pioneer/core": "4.5.0-dev-roving-menu.20260129100701",
|
|
22
|
+
"@open-pioneer/reactivity": "4.5.0-dev-roving-menu.20260129100701"
|
|
21
23
|
},
|
|
22
24
|
"exports": {
|
|
23
25
|
"./package.json": "./package.json",
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { FC, ReactNode } from "react";
|
|
2
|
+
import { RovingMenuState } from "./RovingMenuState";
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @group Roving menu
|
|
6
|
+
*/
|
|
7
|
+
export interface RovingMenuRootProps {
|
|
8
|
+
/**
|
|
9
|
+
* The menu state provided by {@link useRovingMenu}.
|
|
10
|
+
*/
|
|
11
|
+
menuState: RovingMenuState;
|
|
12
|
+
children?: ReactNode;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* This component must be the parent of all roving menu items.
|
|
16
|
+
*
|
|
17
|
+
* @group Roving menu
|
|
18
|
+
* @expandType RovingMenuRootProps
|
|
19
|
+
*/
|
|
20
|
+
export declare const RovingMenuRoot: FC<RovingMenuRootProps>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { memo } from 'react';
|
|
3
|
+
import { MenuStateContext } from './RovingMenuState.js';
|
|
4
|
+
|
|
5
|
+
const RovingMenuRoot = memo(function RovingMenuRoot2(props) {
|
|
6
|
+
const { menuState, children } = props;
|
|
7
|
+
return /* @__PURE__ */ jsx(MenuStateContext.Provider, { value: menuState, children });
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export { RovingMenuRoot };
|
|
11
|
+
//# sourceMappingURL=RovingMenuRoot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RovingMenuRoot.js","sources":["RovingMenuRoot.tsx"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { FC, memo, ReactNode } from \"react\";\nimport { RovingMenuState, MenuStateContext } from \"./RovingMenuState\";\nimport { type useRovingMenu } from \"./useRovingMenu\";\n\n/**\n *\n * @group Roving menu\n */\nexport interface RovingMenuRootProps {\n /**\n * The menu state provided by {@link useRovingMenu}.\n */\n menuState: RovingMenuState;\n children?: ReactNode;\n}\n\n/**\n * This component must be the parent of all roving menu items.\n *\n * @group Roving menu\n * @expandType RovingMenuRootProps\n */\nexport const RovingMenuRoot: FC<RovingMenuRootProps> = memo(function RovingMenuRoot(\n props: RovingMenuRootProps\n) {\n const { menuState, children } = props;\n return <MenuStateContext.Provider value={menuState}>{children}</MenuStateContext.Provider>;\n});\n"],"names":["RovingMenuRoot"],"mappings":";;;;AAwBO,MAAM,cAAA,GAA0C,IAAA,CAAK,SAASA,eAAAA,CACjE,KAAA,EACF;AACE,EAAA,MAAM,EAAE,SAAA,EAAW,QAAA,EAAS,GAAI,KAAA;AAChC,EAAA,2BAAQ,gBAAA,CAAiB,QAAA,EAAjB,EAA0B,KAAA,EAAO,WAAY,QAAA,EAAS,CAAA;AAClE,CAAC;;;;"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { KeyboardEvent, RefObject } from "react";
|
|
2
|
+
export declare const MENU_ID_ATTR = "data-roving-menu-id";
|
|
3
|
+
export declare const MENU_OWNER_ATTR = "data-roving-menu-parent";
|
|
4
|
+
export declare const MENU_VALUE_ATTR = "data-roving-menu-value";
|
|
5
|
+
export declare const MENU_VALUE_JS = "rovingMenuValue";
|
|
6
|
+
/**
|
|
7
|
+
* Shared context for Menu <--> MenuItem coordination.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare const MenuStateContext: import("react").Context<RovingMenuState | undefined>;
|
|
12
|
+
declare const MENU_STATE_BRAND: unique symbol;
|
|
13
|
+
/**
|
|
14
|
+
* The menu state represents the internal state of the menu.
|
|
15
|
+
*
|
|
16
|
+
* The internals are currently hidden.
|
|
17
|
+
*
|
|
18
|
+
* @group Roving menu
|
|
19
|
+
*/
|
|
20
|
+
export interface RovingMenuState {
|
|
21
|
+
[MENU_STATE_BRAND]: void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Checked cast for the internal menu state.
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export declare function getInternalState(menuState: RovingMenuState): InternalMenuState;
|
|
28
|
+
/**
|
|
29
|
+
* Internal model class; created by the menu and accessible by the menu items.
|
|
30
|
+
*
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export declare class InternalMenuState {
|
|
34
|
+
#private;
|
|
35
|
+
readonly menuId: string;
|
|
36
|
+
readonly orientation: "horizontal" | "vertical";
|
|
37
|
+
constructor(menuId: string, orientation: "horizontal" | "vertical", menuRef: RefObject<HTMLElement | null>);
|
|
38
|
+
get current(): string | undefined;
|
|
39
|
+
isActive(value: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Called when a key is pressed on the menu root element.
|
|
42
|
+
*/
|
|
43
|
+
onKeyDown(event: KeyboardEvent): void;
|
|
44
|
+
/**
|
|
45
|
+
* Called by items when they mount.
|
|
46
|
+
*
|
|
47
|
+
* This will activate the first item to ensure that at least one item is focusable.
|
|
48
|
+
*/
|
|
49
|
+
onItemMount(value: string): void;
|
|
50
|
+
/**
|
|
51
|
+
* Called by items when they unmount.
|
|
52
|
+
*
|
|
53
|
+
* This will move the active value to another item to ensure that one item is focusable.
|
|
54
|
+
*/
|
|
55
|
+
onItemUnmount(value: string): void;
|
|
56
|
+
/**
|
|
57
|
+
* Called by items when they receive focus.
|
|
58
|
+
*
|
|
59
|
+
* This is redundant when using arrow key navigation, but its important if
|
|
60
|
+
* the item receives focus from the outside (e.g. programmatically) to sync it
|
|
61
|
+
* with the menu's state.
|
|
62
|
+
*/
|
|
63
|
+
onItemFocus(value: string): void;
|
|
64
|
+
}
|
|
65
|
+
export {};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { reactive } from '@conterra/reactivity-core';
|
|
2
|
+
import { createLogger } from '@open-pioneer/core';
|
|
3
|
+
import { sourceId } from '../_virtual/source-info.js';
|
|
4
|
+
import { createContext } from 'react';
|
|
5
|
+
|
|
6
|
+
const LOG = createLogger(sourceId);
|
|
7
|
+
const MENU_ID_ATTR = "data-roving-menu-id";
|
|
8
|
+
const MENU_OWNER_ATTR = "data-roving-menu-parent";
|
|
9
|
+
const MENU_VALUE_ATTR = "data-roving-menu-value";
|
|
10
|
+
const MENU_VALUE_JS = "rovingMenuValue";
|
|
11
|
+
const MenuStateContext = createContext(void 0);
|
|
12
|
+
function getInternalState(menuState) {
|
|
13
|
+
if (!(menuState instanceof InternalMenuState)) {
|
|
14
|
+
throw new Error("Invalid menu state.");
|
|
15
|
+
}
|
|
16
|
+
return menuState;
|
|
17
|
+
}
|
|
18
|
+
class InternalMenuState {
|
|
19
|
+
#menuRef;
|
|
20
|
+
#current = reactive();
|
|
21
|
+
menuId;
|
|
22
|
+
orientation;
|
|
23
|
+
constructor(menuId, orientation, menuRef) {
|
|
24
|
+
this.#menuRef = menuRef;
|
|
25
|
+
this.menuId = menuId;
|
|
26
|
+
this.orientation = orientation;
|
|
27
|
+
}
|
|
28
|
+
get current() {
|
|
29
|
+
return this.#current.value;
|
|
30
|
+
}
|
|
31
|
+
isActive(value) {
|
|
32
|
+
return this.current === value;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Called when a key is pressed on the menu root element.
|
|
36
|
+
*/
|
|
37
|
+
onKeyDown(event) {
|
|
38
|
+
const direction = getNavDirection(event, this.orientation);
|
|
39
|
+
if (!direction) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const items = getFocusableItems(this.#menuRef, this.menuId);
|
|
43
|
+
const target = getFocusTarget(items, this.current, direction);
|
|
44
|
+
if (!target) {
|
|
45
|
+
LOG.warn("Failed to identify focus target for keyboard navigation");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
this.#navigateToItem(target);
|
|
50
|
+
target.focus();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Called by items when they mount.
|
|
54
|
+
*
|
|
55
|
+
* This will activate the first item to ensure that at least one item is focusable.
|
|
56
|
+
*/
|
|
57
|
+
onItemMount(value) {
|
|
58
|
+
if (!this.current) {
|
|
59
|
+
this.#activateItem(value);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Called by items when they unmount.
|
|
64
|
+
*
|
|
65
|
+
* This will move the active value to another item to ensure that one item is focusable.
|
|
66
|
+
*/
|
|
67
|
+
onItemUnmount(value) {
|
|
68
|
+
if (this.isActive(value)) {
|
|
69
|
+
const items = getFocusableItems(this.#menuRef, this.menuId, true);
|
|
70
|
+
const target = items[0];
|
|
71
|
+
if (target) {
|
|
72
|
+
this.#navigateToItem(target);
|
|
73
|
+
} else {
|
|
74
|
+
this.#activateItem(void 0);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Called by items when they receive focus.
|
|
80
|
+
*
|
|
81
|
+
* This is redundant when using arrow key navigation, but its important if
|
|
82
|
+
* the item receives focus from the outside (e.g. programmatically) to sync it
|
|
83
|
+
* with the menu's state.
|
|
84
|
+
*/
|
|
85
|
+
onItemFocus(value) {
|
|
86
|
+
this.#activateItem(value);
|
|
87
|
+
}
|
|
88
|
+
#navigateToItem(target) {
|
|
89
|
+
const value = getItemValue(target);
|
|
90
|
+
if (!value) {
|
|
91
|
+
LOG.warn("Menu item without a value", value);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.#activateItem(value);
|
|
95
|
+
}
|
|
96
|
+
#activateItem(value) {
|
|
97
|
+
this.#current.value = value;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function getNavDirection(event, orientation) {
|
|
101
|
+
switch (event.key) {
|
|
102
|
+
case "ArrowUp": {
|
|
103
|
+
if (orientation === "vertical") {
|
|
104
|
+
return "backward";
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case "ArrowDown": {
|
|
109
|
+
if (orientation === "vertical") {
|
|
110
|
+
return "forward";
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "ArrowLeft": {
|
|
115
|
+
if (orientation === "horizontal") {
|
|
116
|
+
return "backward";
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case "ArrowRight": {
|
|
121
|
+
if (orientation === "horizontal") {
|
|
122
|
+
return "forward";
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case "Home":
|
|
127
|
+
return "home";
|
|
128
|
+
case "End":
|
|
129
|
+
return "end";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function getFocusTarget(items, current, direction) {
|
|
133
|
+
const currentIndex = items.findIndex((item) => getItemValue(item) === current);
|
|
134
|
+
if (currentIndex === -1) {
|
|
135
|
+
return items.at(0);
|
|
136
|
+
}
|
|
137
|
+
if (direction === "home") {
|
|
138
|
+
return items.at(0);
|
|
139
|
+
}
|
|
140
|
+
if (direction === "end") {
|
|
141
|
+
return items.at(-1);
|
|
142
|
+
}
|
|
143
|
+
let nextIndex;
|
|
144
|
+
if (direction === "forward") {
|
|
145
|
+
nextIndex = currentIndex + 1;
|
|
146
|
+
if (nextIndex >= items.length) {
|
|
147
|
+
nextIndex = 0;
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
nextIndex = currentIndex - 1;
|
|
151
|
+
if (nextIndex < 0) {
|
|
152
|
+
nextIndex = items.length - 1;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return items.at(nextIndex);
|
|
156
|
+
}
|
|
157
|
+
function getFocusableItems(menuRef, menuId, unmount = false) {
|
|
158
|
+
const owner = menuRef.current;
|
|
159
|
+
if (!owner) {
|
|
160
|
+
if (unmount) {
|
|
161
|
+
return [];
|
|
162
|
+
} else {
|
|
163
|
+
throw new Error(
|
|
164
|
+
"Menu parent not found in DOM. Was the 'ref' prop applied correctly to the container?"
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const escapedId = CSS.escape(menuId);
|
|
169
|
+
const children = owner.querySelectorAll(`[${MENU_OWNER_ATTR}=${escapedId}]:not([disabled])`);
|
|
170
|
+
return Array.from(children);
|
|
171
|
+
}
|
|
172
|
+
function getItemValue(item) {
|
|
173
|
+
return item?.dataset[MENU_VALUE_JS] ?? void 0;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export { InternalMenuState, MENU_ID_ATTR, MENU_OWNER_ATTR, MENU_VALUE_ATTR, MenuStateContext, getInternalState };
|
|
177
|
+
//# sourceMappingURL=RovingMenuState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RovingMenuState.js","sources":["RovingMenuState.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { reactive } from \"@conterra/reactivity-core\";\nimport { createLogger } from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { createContext, KeyboardEvent, RefObject } from \"react\";\n\nconst LOG = createLogger(sourceId);\n\n// Mostly for debugging parent/child links. Not used for lookups.\nexport const MENU_ID_ATTR = \"data-roving-menu-id\";\n\n// Children of the menu have this attribute to signal which menu they belong to.\nexport const MENU_OWNER_ATTR = \"data-roving-menu-parent\";\n\n// Children of the menu use this as their unique id (in the context of the menu).\nexport const MENU_VALUE_ATTR = \"data-roving-menu-value\";\n\n// Same as above, but for access via node.dataset\nexport const MENU_VALUE_JS = \"rovingMenuValue\";\n\n/**\n * Shared context for Menu <--> MenuItem coordination.\n *\n * @internal\n */\nexport const MenuStateContext = createContext<RovingMenuState | undefined>(undefined);\n\ndeclare const MENU_STATE_BRAND: unique symbol;\n\n/**\n * The menu state represents the internal state of the menu.\n *\n * The internals are currently hidden.\n *\n * @group Roving menu\n */\nexport interface RovingMenuState {\n [MENU_STATE_BRAND]: void;\n}\n\n/**\n * Checked cast for the internal menu state.\n * @internal\n */\nexport function getInternalState(menuState: RovingMenuState): InternalMenuState {\n if (!(menuState instanceof InternalMenuState)) {\n throw new Error(\"Invalid menu state.\");\n }\n return menuState as unknown as InternalMenuState;\n}\n\n/**\n * Internal model class; created by the menu and accessible by the menu items.\n *\n * @internal\n */\nexport class InternalMenuState {\n #menuRef: RefObject<HTMLElement | null>;\n #current = reactive<string | undefined>();\n\n readonly menuId: string;\n readonly orientation: \"horizontal\" | \"vertical\";\n\n constructor(\n menuId: string,\n orientation: \"horizontal\" | \"vertical\",\n menuRef: RefObject<HTMLElement | null>\n ) {\n this.#menuRef = menuRef;\n this.menuId = menuId;\n this.orientation = orientation;\n }\n\n get current(): string | undefined {\n return this.#current.value;\n }\n\n isActive(value: string): boolean {\n return this.current === value;\n }\n\n /**\n * Called when a key is pressed on the menu root element.\n */\n onKeyDown(event: KeyboardEvent): void {\n const direction = getNavDirection(event, this.orientation);\n if (!direction) {\n return;\n }\n\n const items = getFocusableItems(this.#menuRef, this.menuId);\n const target = getFocusTarget(items, this.current, direction);\n if (!target) {\n LOG.warn(\"Failed to identify focus target for keyboard navigation\");\n return;\n }\n\n event.preventDefault();\n this.#navigateToItem(target);\n target.focus();\n }\n\n /**\n * Called by items when they mount.\n *\n * This will activate the first item to ensure that at least one item is focusable.\n */\n onItemMount(value: string): void {\n if (!this.current) {\n this.#activateItem(value);\n }\n }\n\n /**\n * Called by items when they unmount.\n *\n * This will move the active value to another item to ensure that one item is focusable.\n */\n onItemUnmount(value: string): void {\n if (this.isActive(value)) {\n const items = getFocusableItems(this.#menuRef, this.menuId, true);\n const target = items[0];\n if (target) {\n this.#navigateToItem(target);\n } else {\n this.#activateItem(undefined);\n }\n }\n }\n\n /**\n * Called by items when they receive focus.\n *\n * This is redundant when using arrow key navigation, but its important if\n * the item receives focus from the outside (e.g. programmatically) to sync it\n * with the menu's state.\n */\n onItemFocus(value: string): void {\n this.#activateItem(value);\n }\n\n #navigateToItem(target: HTMLElement) {\n const value = getItemValue(target);\n if (!value) {\n LOG.warn(\"Menu item without a value\", value);\n return;\n }\n\n this.#activateItem(value);\n }\n\n #activateItem(value: string | undefined): void {\n this.#current.value = value;\n }\n}\n\ntype NavDirection = \"backward\" | \"forward\" | \"home\" | \"end\";\n\nfunction getNavDirection(\n event: KeyboardEvent,\n orientation: \"horizontal\" | \"vertical\"\n): NavDirection | undefined {\n switch (event.key) {\n case \"ArrowUp\": {\n if (orientation === \"vertical\") {\n return \"backward\";\n }\n break;\n }\n case \"ArrowDown\": {\n if (orientation === \"vertical\") {\n return \"forward\";\n }\n break;\n }\n case \"ArrowLeft\": {\n if (orientation === \"horizontal\") {\n return \"backward\";\n }\n break;\n }\n case \"ArrowRight\": {\n if (orientation === \"horizontal\") {\n return \"forward\";\n }\n break;\n }\n case \"Home\":\n return \"home\";\n case \"End\":\n return \"end\";\n }\n}\n\nfunction getFocusTarget(\n items: HTMLElement[],\n current: string | undefined,\n direction: NavDirection\n): HTMLElement | undefined {\n const currentIndex = items.findIndex((item) => getItemValue(item) === current);\n if (currentIndex === -1) {\n // Some keyboard event from an unknown child?\n return items.at(0);\n }\n\n if (direction === \"home\") {\n return items.at(0);\n }\n if (direction === \"end\") {\n return items.at(-1);\n }\n\n let nextIndex;\n if (direction === \"forward\") {\n nextIndex = currentIndex + 1;\n if (nextIndex >= items.length) {\n nextIndex = 0;\n }\n } else {\n nextIndex = currentIndex - 1;\n if (nextIndex < 0) {\n nextIndex = items.length - 1;\n }\n }\n return items.at(nextIndex);\n}\n\nfunction getFocusableItems(\n menuRef: RefObject<HTMLElement | null>,\n menuId: string,\n unmount = false\n): HTMLElement[] {\n const owner = menuRef.current;\n if (!owner) {\n if (unmount) {\n // List may no longer be in the DOM\n return [];\n } else {\n throw new Error(\n \"Menu parent not found in DOM. Was the 'ref' prop applied correctly to the container?\"\n );\n }\n }\n\n const escapedId = CSS.escape(menuId);\n const children = owner.querySelectorAll(`[${MENU_OWNER_ATTR}=${escapedId}]:not([disabled])`);\n return Array.from(children) as HTMLElement[];\n}\n\nfunction getItemValue(item: HTMLElement | undefined): string | undefined {\n return item?.dataset[MENU_VALUE_JS] ?? undefined;\n}\n"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { KeyboardEvent, RefObject } from "react";
|
|
2
|
+
import { MENU_ID_ATTR, RovingMenuState } from "./RovingMenuState";
|
|
3
|
+
/**
|
|
4
|
+
* Properties supported when creating a new menu via {@link useRovingMenu}.
|
|
5
|
+
*
|
|
6
|
+
* @group Roving menu
|
|
7
|
+
*/
|
|
8
|
+
export interface RovingMenuProps {
|
|
9
|
+
/**
|
|
10
|
+
* Configures the orientation of the menu (default: `horizontal`).
|
|
11
|
+
*
|
|
12
|
+
* Horizontal menus can be navigated using left/right, vertical menus use up/down.
|
|
13
|
+
*/
|
|
14
|
+
orientation?: "vertical" | "horizontal";
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The return value of {@link useRovingMenu}.
|
|
18
|
+
*
|
|
19
|
+
* @group Roving menu
|
|
20
|
+
*/
|
|
21
|
+
export interface RovingMenuResult {
|
|
22
|
+
/**
|
|
23
|
+
* DOM-Properties that must be applied to the menu's dom element.
|
|
24
|
+
*
|
|
25
|
+
* Note that other properties may be added in a future release.
|
|
26
|
+
*/
|
|
27
|
+
menuProps: {
|
|
28
|
+
ref: RefObject<any>;
|
|
29
|
+
role: string;
|
|
30
|
+
[MENU_ID_ATTR]: string;
|
|
31
|
+
"aria-orientation": "horizontal" | "vertical";
|
|
32
|
+
onKeyDown: (event: KeyboardEvent) => void;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Properties that should be passed to the {@link RovingMenuRoot}.
|
|
36
|
+
*
|
|
37
|
+
* The specific internals of this object are an implementation detail.
|
|
38
|
+
*/
|
|
39
|
+
menuState: RovingMenuState;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The roving menu is a render-less component for lists that support keyboard navigation.
|
|
43
|
+
* It implements the basics for an accessible, keyboard navigable menu using the "roving tab index" pattern.
|
|
44
|
+
*
|
|
45
|
+
* Items can be navigated using arrow keys and home/end keys.
|
|
46
|
+
*
|
|
47
|
+
* @group Roving menu
|
|
48
|
+
* @expandType RovingMenuProps
|
|
49
|
+
*/
|
|
50
|
+
export declare function useRovingMenu(props?: RovingMenuProps): RovingMenuResult;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useId, useRef, useMemo } from 'react';
|
|
2
|
+
import { useEvent } from '../useEvent.js';
|
|
3
|
+
import { InternalMenuState, MENU_ID_ATTR } from './RovingMenuState.js';
|
|
4
|
+
|
|
5
|
+
function useRovingMenu(props = {}) {
|
|
6
|
+
const { orientation = "horizontal" } = props;
|
|
7
|
+
const menuId = useId();
|
|
8
|
+
const menuRef = useRef(null);
|
|
9
|
+
const state = useMemo(
|
|
10
|
+
() => new InternalMenuState(menuId, orientation, menuRef),
|
|
11
|
+
[menuRef, orientation, menuId]
|
|
12
|
+
);
|
|
13
|
+
const onKeyDown = useEvent((event) => {
|
|
14
|
+
if (event.defaultPrevented) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
state.onKeyDown(event);
|
|
18
|
+
});
|
|
19
|
+
const result = useMemo(
|
|
20
|
+
() => ({
|
|
21
|
+
menuProps: {
|
|
22
|
+
ref: menuRef,
|
|
23
|
+
role: "toolbar",
|
|
24
|
+
"aria-orientation": orientation,
|
|
25
|
+
[MENU_ID_ATTR]: menuId,
|
|
26
|
+
onKeyDown
|
|
27
|
+
},
|
|
28
|
+
menuState: state
|
|
29
|
+
}),
|
|
30
|
+
[menuId, orientation, onKeyDown, state]
|
|
31
|
+
);
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { useRovingMenu };
|
|
36
|
+
//# sourceMappingURL=useRovingMenu.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRovingMenu.js","sources":["useRovingMenu.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { KeyboardEvent, RefObject, useId, useMemo, useRef } from \"react\";\nimport { useEvent } from \"../useEvent\";\nimport { type RovingMenuRoot } from \"./RovingMenuRoot\";\nimport { InternalMenuState, MENU_ID_ATTR, RovingMenuState } from \"./RovingMenuState\";\n\n/**\n * Properties supported when creating a new menu via {@link useRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface RovingMenuProps {\n /**\n * Configures the orientation of the menu (default: `horizontal`).\n *\n * Horizontal menus can be navigated using left/right, vertical menus use up/down.\n */\n orientation?: \"vertical\" | \"horizontal\";\n}\n\n/**\n * The return value of {@link useRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface RovingMenuResult {\n /**\n * DOM-Properties that must be applied to the menu's dom element.\n *\n * Note that other properties may be added in a future release.\n */\n menuProps: {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ref: RefObject<any>;\n role: string;\n [MENU_ID_ATTR]: string;\n \"aria-orientation\": \"horizontal\" | \"vertical\";\n onKeyDown: (event: KeyboardEvent) => void;\n };\n\n /**\n * Properties that should be passed to the {@link RovingMenuRoot}.\n *\n * The specific internals of this object are an implementation detail.\n */\n menuState: RovingMenuState;\n}\n\n/**\n * The roving menu is a render-less component for lists that support keyboard navigation.\n * It implements the basics for an accessible, keyboard navigable menu using the \"roving tab index\" pattern.\n *\n * Items can be navigated using arrow keys and home/end keys.\n *\n * @group Roving menu\n * @expandType RovingMenuProps\n */\nexport function useRovingMenu(props: RovingMenuProps = {}): RovingMenuResult {\n const { orientation = \"horizontal\" } = props;\n const menuId = useId();\n const menuRef = useRef<HTMLElement>(null);\n\n // Shared state between items and root\n const state = useMemo(\n () => new InternalMenuState(menuId, orientation, menuRef),\n [menuRef, orientation, menuId]\n );\n\n // Key handler on the menu\n const onKeyDown = useEvent((event: KeyboardEvent) => {\n if (event.defaultPrevented) {\n return;\n }\n\n state.onKeyDown(event);\n });\n\n const result = useMemo(\n (): RovingMenuResult => ({\n menuProps: {\n ref: menuRef,\n role: \"toolbar\",\n \"aria-orientation\": orientation,\n [MENU_ID_ATTR]: menuId,\n onKeyDown: onKeyDown\n },\n menuState: state as unknown as RovingMenuState\n }),\n [menuId, orientation, onKeyDown, state]\n );\n return result;\n}\n"],"names":[],"mappings":";;;;AA0DO,SAAS,aAAA,CAAc,KAAA,GAAyB,EAAC,EAAqB;AACzE,EAAA,MAAM,EAAE,WAAA,GAAc,YAAA,EAAa,GAAI,KAAA;AACvC,EAAA,MAAM,SAAS,KAAA,EAAM;AACrB,EAAA,MAAM,OAAA,GAAU,OAAoB,IAAI,CAAA;AAGxC,EAAA,MAAM,KAAA,GAAQ,OAAA;AAAA,IACV,MAAM,IAAI,iBAAA,CAAkB,MAAA,EAAQ,aAAa,OAAO,CAAA;AAAA,IACxD,CAAC,OAAA,EAAS,WAAA,EAAa,MAAM;AAAA,GACjC;AAGA,EAAA,MAAM,SAAA,GAAY,QAAA,CAAS,CAAC,KAAA,KAAyB;AACjD,IAAA,IAAI,MAAM,gBAAA,EAAkB;AACxB,MAAA;AAAA,IACJ;AAEA,IAAA,KAAA,CAAM,UAAU,KAAK,CAAA;AAAA,EACzB,CAAC,CAAA;AAED,EAAA,MAAM,MAAA,GAAS,OAAA;AAAA,IACX,OAAyB;AAAA,MACrB,SAAA,EAAW;AAAA,QACP,GAAA,EAAK,OAAA;AAAA,QACL,IAAA,EAAM,SAAA;AAAA,QACN,kBAAA,EAAoB,WAAA;AAAA,QACpB,CAAC,YAAY,GAAG,MAAA;AAAA,QAChB;AAAA,OACJ;AAAA,MACA,SAAA,EAAW;AAAA,KACf,CAAA;AAAA,IACA,CAAC,MAAA,EAAQ,WAAA,EAAa,SAAA,EAAW,KAAK;AAAA,GAC1C;AACA,EAAA,OAAO,MAAA;AACX;;;;"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { FocusEventHandler } from "react";
|
|
2
|
+
import { MENU_OWNER_ATTR, MENU_VALUE_ATTR } from "./RovingMenuState";
|
|
3
|
+
/**
|
|
4
|
+
* Properties support when creating a new menu item via {@link useRovingMenuItem}.
|
|
5
|
+
*
|
|
6
|
+
* @group Roving menu
|
|
7
|
+
*/
|
|
8
|
+
export interface RovingMenuItemProps {
|
|
9
|
+
/**
|
|
10
|
+
* A unique value for the current item (in the context of this menu).
|
|
11
|
+
*/
|
|
12
|
+
value: string;
|
|
13
|
+
/**
|
|
14
|
+
* Set this to true if your item is disabled.
|
|
15
|
+
*
|
|
16
|
+
* This tells the menu that the item cannot be focused.
|
|
17
|
+
*/
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* By default, a menu item parent is required.
|
|
21
|
+
*
|
|
22
|
+
* Set this to `false` to make the parent optional.
|
|
23
|
+
* The hook will become inactive and will return `undefined` if no parent is present.
|
|
24
|
+
* This is useful for components that may or may not be used within a menu.
|
|
25
|
+
*
|
|
26
|
+
* Default: `true`
|
|
27
|
+
*/
|
|
28
|
+
required?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The return value of {@link useRovingMenuItem}.
|
|
32
|
+
*
|
|
33
|
+
* @group Roving menu
|
|
34
|
+
*/
|
|
35
|
+
export interface RovingMenuItemResult {
|
|
36
|
+
/**
|
|
37
|
+
* DOM-Properties that must be applied to the menu item's dom element.
|
|
38
|
+
*
|
|
39
|
+
* Most importantly, this will apply the item's tab index and data attributes
|
|
40
|
+
* that allow the menu to find the item in the DOM.
|
|
41
|
+
*
|
|
42
|
+
* Note that other properties may be added in a future release.
|
|
43
|
+
*/
|
|
44
|
+
itemProps: {
|
|
45
|
+
[MENU_OWNER_ATTR]: string;
|
|
46
|
+
[MENU_VALUE_ATTR]: string;
|
|
47
|
+
tabIndex: number;
|
|
48
|
+
onFocus: FocusEventHandler;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Declares a new menu item using the given `props`.
|
|
53
|
+
* @group Roving menu
|
|
54
|
+
* @expandType RovingMenuItemProps
|
|
55
|
+
*/
|
|
56
|
+
export declare function useRovingMenuItem(props: RovingMenuItemProps & {
|
|
57
|
+
required?: true;
|
|
58
|
+
}): RovingMenuItemResult;
|
|
59
|
+
export declare function useRovingMenuItem(props: RovingMenuItemProps): RovingMenuItemResult | undefined;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useReactiveSnapshot } from '@open-pioneer/reactivity';
|
|
2
|
+
import { useEffect, useMemo, useContext } from 'react';
|
|
3
|
+
import { MENU_VALUE_ATTR, MENU_OWNER_ATTR, MenuStateContext, getInternalState } from './RovingMenuState.js';
|
|
4
|
+
|
|
5
|
+
function useRovingMenuItem(props) {
|
|
6
|
+
const { value, disabled = false, required = true } = props;
|
|
7
|
+
const state = useMenuState(required);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (!state || disabled) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
state.onItemMount(value);
|
|
13
|
+
return () => state.onItemUnmount(value);
|
|
14
|
+
}, [state, value, disabled]);
|
|
15
|
+
const isActive = useReactiveSnapshot(() => state?.isActive(value), [state, value]);
|
|
16
|
+
const result = useMemo(() => {
|
|
17
|
+
if (!state) {
|
|
18
|
+
return void 0;
|
|
19
|
+
}
|
|
20
|
+
const menuId = state.menuId;
|
|
21
|
+
return {
|
|
22
|
+
itemProps: {
|
|
23
|
+
[MENU_OWNER_ATTR]: menuId,
|
|
24
|
+
[MENU_VALUE_ATTR]: value,
|
|
25
|
+
tabIndex: isActive ? 0 : -1,
|
|
26
|
+
onFocus: (_event) => {
|
|
27
|
+
state?.onItemFocus(value);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}, [state, value, isActive]);
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
function useMenuState(required) {
|
|
35
|
+
const state = useContext(MenuStateContext);
|
|
36
|
+
if (!state) {
|
|
37
|
+
if (required) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
"Failed to find the outer menu. Is the menu item surrounded by the RovingMenuRoot?"
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return void 0;
|
|
43
|
+
}
|
|
44
|
+
return getInternalState(state);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export { useRovingMenuItem };
|
|
48
|
+
//# sourceMappingURL=useRovingMenuItem.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRovingMenuItem.js","sources":["useRovingMenuItem.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { useReactiveSnapshot } from \"@open-pioneer/reactivity\";\nimport { FocusEventHandler, useContext, useEffect, useMemo } from \"react\";\nimport {\n getInternalState,\n InternalMenuState,\n MENU_OWNER_ATTR,\n MENU_VALUE_ATTR,\n MenuStateContext\n} from \"./RovingMenuState\";\n\n/**\n * Properties support when creating a new menu item via {@link useRovingMenuItem}.\n *\n * @group Roving menu\n */\nexport interface RovingMenuItemProps {\n /**\n * A unique value for the current item (in the context of this menu).\n */\n value: string;\n\n /**\n * Set this to true if your item is disabled.\n *\n * This tells the menu that the item cannot be focused.\n */\n disabled?: boolean;\n\n /**\n * By default, a menu item parent is required.\n *\n * Set this to `false` to make the parent optional.\n * The hook will become inactive and will return `undefined` if no parent is present.\n * This is useful for components that may or may not be used within a menu.\n *\n * Default: `true`\n */\n required?: boolean;\n}\n\n/**\n * The return value of {@link useRovingMenuItem}.\n *\n * @group Roving menu\n */\nexport interface RovingMenuItemResult {\n /**\n * DOM-Properties that must be applied to the menu item's dom element.\n *\n * Most importantly, this will apply the item's tab index and data attributes\n * that allow the menu to find the item in the DOM.\n *\n * Note that other properties may be added in a future release.\n */\n itemProps: {\n [MENU_OWNER_ATTR]: string;\n [MENU_VALUE_ATTR]: string;\n tabIndex: number;\n onFocus: FocusEventHandler;\n };\n}\n\n/**\n * Declares a new menu item using the given `props`.\n * @group Roving menu\n * @expandType RovingMenuItemProps\n */\nexport function useRovingMenuItem(\n props: RovingMenuItemProps & { required?: true }\n): RovingMenuItemResult;\nexport function useRovingMenuItem(props: RovingMenuItemProps): RovingMenuItemResult | undefined;\nexport function useRovingMenuItem(props: RovingMenuItemProps): RovingMenuItemResult | undefined {\n const { value, disabled = false, required = true } = props;\n const state = useMenuState(required);\n\n useEffect(() => {\n if (!state || disabled) {\n return;\n }\n\n state.onItemMount(value);\n return () => state.onItemUnmount(value);\n }, [state, value, disabled]);\n\n const isActive = useReactiveSnapshot(() => state?.isActive(value), [state, value]);\n const result = useMemo((): RovingMenuItemResult | undefined => {\n if (!state) {\n return undefined;\n }\n const menuId = state.menuId;\n return {\n itemProps: {\n [MENU_OWNER_ATTR]: menuId,\n [MENU_VALUE_ATTR]: value,\n tabIndex: isActive ? 0 : -1,\n onFocus: (_event) => {\n state?.onItemFocus(value);\n }\n }\n };\n }, [state, value, isActive]);\n return result;\n}\n\nfunction useMenuState(required: boolean): InternalMenuState | undefined {\n const state = useContext(MenuStateContext);\n if (!state) {\n if (required) {\n throw new Error(\n \"Failed to find the outer menu. Is the menu item surrounded by the RovingMenuRoot?\"\n );\n }\n return undefined;\n }\n return getInternalState(state);\n}\n"],"names":[],"mappings":";;;;AAyEO,SAAS,kBAAkB,KAAA,EAA8D;AAC5F,EAAA,MAAM,EAAE,KAAA,EAAO,QAAA,GAAW,KAAA,EAAO,QAAA,GAAW,MAAK,GAAI,KAAA;AACrD,EAAA,MAAM,KAAA,GAAQ,aAAa,QAAQ,CAAA;AAEnC,EAAA,SAAA,CAAU,MAAM;AACZ,IAAA,IAAI,CAAC,SAAS,QAAA,EAAU;AACpB,MAAA;AAAA,IACJ;AAEA,IAAA,KAAA,CAAM,YAAY,KAAK,CAAA;AACvB,IAAA,OAAO,MAAM,KAAA,CAAM,aAAA,CAAc,KAAK,CAAA;AAAA,EAC1C,CAAA,EAAG,CAAC,KAAA,EAAO,KAAA,EAAO,QAAQ,CAAC,CAAA;AAE3B,EAAA,MAAM,QAAA,GAAW,mBAAA,CAAoB,MAAM,KAAA,EAAO,QAAA,CAAS,KAAK,CAAA,EAAG,CAAC,KAAA,EAAO,KAAK,CAAC,CAAA;AACjF,EAAA,MAAM,MAAA,GAAS,QAAQ,MAAwC;AAC3D,IAAA,IAAI,CAAC,KAAA,EAAO;AACR,MAAA,OAAO,MAAA;AAAA,IACX;AACA,IAAA,MAAM,SAAS,KAAA,CAAM,MAAA;AACrB,IAAA,OAAO;AAAA,MACH,SAAA,EAAW;AAAA,QACP,CAAC,eAAe,GAAG,MAAA;AAAA,QACnB,CAAC,eAAe,GAAG,KAAA;AAAA,QACnB,QAAA,EAAU,WAAW,CAAA,GAAI,EAAA;AAAA,QACzB,OAAA,EAAS,CAAC,MAAA,KAAW;AACjB,UAAA,KAAA,EAAO,YAAY,KAAK,CAAA;AAAA,QAC5B;AAAA;AACJ,KACJ;AAAA,EACJ,CAAA,EAAG,CAAC,KAAA,EAAO,KAAA,EAAO,QAAQ,CAAC,CAAA;AAC3B,EAAA,OAAO,MAAA;AACX;AAEA,SAAS,aAAa,QAAA,EAAkD;AACpE,EAAA,MAAM,KAAA,GAAQ,WAAW,gBAAgB,CAAA;AACzC,EAAA,IAAI,CAAC,KAAA,EAAO;AACR,IAAA,IAAI,QAAA,EAAU;AACV,MAAA,MAAM,IAAI,KAAA;AAAA,QACN;AAAA,OACJ;AAAA,IACJ;AACA,IAAA,OAAO,MAAA;AAAA,EACX;AACA,EAAA,OAAO,iBAAiB,KAAK,CAAA;AACjC;;;;"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { HTMLAttributes } from "react";
|
|
2
2
|
/**
|
|
3
3
|
* Common properties supported by all public react components.
|
|
4
|
+
*
|
|
5
|
+
* @group Common component props
|
|
4
6
|
*/
|
|
5
7
|
export interface CommonComponentProps {
|
|
6
8
|
/**
|
|
@@ -24,6 +26,9 @@ export interface CommonComponentProps {
|
|
|
24
26
|
* return <Box {...containerProps}>Content</Box>;
|
|
25
27
|
* }
|
|
26
28
|
* ```
|
|
29
|
+
*
|
|
30
|
+
* @group Common component props
|
|
31
|
+
* @expandType CommonComponentProps
|
|
27
32
|
*/
|
|
28
33
|
export declare function useCommonComponentProps(componentClassName: string, props: CommonComponentProps): {
|
|
29
34
|
containerProps: HTMLAttributes<HTMLElement>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useCommonComponentProps.js","sources":["useCommonComponentProps.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport classNames from \"classnames\";\nimport { HTMLAttributes } from \"react\";\n\n/**\n * Common properties supported by all public react components.\n */\nexport interface CommonComponentProps {\n /**\n * Additional class name(s).\n */\n \"className\"?: string;\n\n /**\n * Used for testing.\n */\n \"data-testid\"?: string;\n}\n\n/**\n * A helper hook that computes react properties for the topmost container in a public react component.\n *\n * Example:\n *\n * ```jsx\n * function MyComponent(props) {\n * const { containerProps } = useCommonComponentProps(\"my-component\", props);\n * // automatically applies css classes and testid\n * return <Box {...containerProps}>Content</Box>;\n * }\n * ```\n */\nexport function useCommonComponentProps(\n componentClassName: string,\n props: CommonComponentProps\n): { containerProps: HTMLAttributes<HTMLElement> } {\n const containerProps = {\n \"className\": classNames(componentClassName, props.className),\n \"data-testid\": props[\"data-testid\"]\n };\n return {\n containerProps\n };\n}\n"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"useCommonComponentProps.js","sources":["useCommonComponentProps.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport classNames from \"classnames\";\nimport { HTMLAttributes } from \"react\";\n\n/**\n * Common properties supported by all public react components.\n *\n * @group Common component props\n */\nexport interface CommonComponentProps {\n /**\n * Additional class name(s).\n */\n \"className\"?: string;\n\n /**\n * Used for testing.\n */\n \"data-testid\"?: string;\n}\n\n/**\n * A helper hook that computes react properties for the topmost container in a public react component.\n *\n * Example:\n *\n * ```jsx\n * function MyComponent(props) {\n * const { containerProps } = useCommonComponentProps(\"my-component\", props);\n * // automatically applies css classes and testid\n * return <Box {...containerProps}>Content</Box>;\n * }\n * ```\n *\n * @group Common component props\n * @expandType CommonComponentProps\n */\nexport function useCommonComponentProps(\n componentClassName: string,\n props: CommonComponentProps\n): { containerProps: HTMLAttributes<HTMLElement> } {\n const containerProps = {\n \"className\": classNames(componentClassName, props.className),\n \"data-testid\": props[\"data-testid\"]\n };\n return {\n containerProps\n };\n}\n"],"names":[],"mappings":";;AAsCO,SAAS,uBAAA,CACZ,oBACA,KAAA,EAC+C;AAC/C,EAAA,MAAM,cAAA,GAAiB;AAAA,IACnB,WAAA,EAAa,UAAA,CAAW,kBAAA,EAAoB,KAAA,CAAM,SAAS,CAAA;AAAA,IAC3D,aAAA,EAAe,MAAM,aAAa;AAAA,GACtC;AACA,EAAA,OAAO;AAAA,IACH;AAAA,GACJ;AACJ;;;;"}
|