@open-pioneer/react-utils 4.9.0-dev.20260930055642 → 4.9.0-dev.20261002130116
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 +8 -1
- package/README.md +2 -1
- package/TitledSection.d.ts +2 -2
- package/TitledSection.js.map +1 -1
- package/package.json +3 -3
- package/roving-menu/RovingMenuRoot.d.ts +1 -0
- package/roving-menu/RovingMenuRoot.js.map +1 -1
- package/roving-menu/RovingMenuState.js +2 -2
- package/roving-menu/RovingMenuState.js.map +1 -1
- package/roving-menu/useRovingMenu.d.ts +1 -1
- package/roving-menu/useRovingMenu.js.map +1 -1
- package/roving-menu/useRovingMenuItem.d.ts +3 -1
- package/roving-menu/useRovingMenuItem.js +14 -8
- package/roving-menu/useRovingMenuItem.js.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
# @open-pioneer/react-utils
|
|
2
2
|
|
|
3
|
-
## 4.9.0-dev.
|
|
3
|
+
## 4.9.0-dev.20261002130116
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 6b160f3: Fix three roving menu issues:
|
|
8
|
+
- Pressing `End` in a menu whose current item is unknown now moves to the last item instead of the first.
|
|
9
|
+
- Keyboard navigation uses `getAttribute("aria-disabled")` to skip disabled nodes instead of reading the `ariaDisabled` property.
|
|
10
|
+
- `useRovingMenuItem({ disabled: true })` now returns `itemProps` with `aria-disabled="true"`. Previously the item element had to carry the attribute itself to be skipped during keyboard navigation.
|
|
4
11
|
|
|
5
12
|
## 4.8.0
|
|
6
13
|
|
package/README.md
CHANGED
|
@@ -54,7 +54,7 @@ import { FormattedMessage } from "@open-pioneer/react-utils";
|
|
|
54
54
|
<FormattedRichMessage intl={intl} id="message.id" />
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
`intl` can be either a plain `PackageIntl` object, or a signal (`ReadonlyReactive<PackageIntl
|
|
57
|
+
`intl` can be either a plain `PackageIntl` object, or a signal (`ReadonlyReactive<PackageIntl>`), or a reactive getter (e.g. `() => currentIntl.value`).
|
|
58
58
|
|
|
59
59
|
`FormattedMessage` and `FormattedRichMessage` support `values`, too:
|
|
60
60
|
|
|
@@ -124,6 +124,7 @@ function MenuItem(props: { value: string }) {
|
|
|
124
124
|
|
|
125
125
|
Use the `disabled` prop of `useRovingMenuItem` to indicate that your menu item is currently disabled.
|
|
126
126
|
Disabled items will lose focus and will be skipped during keyboard navigation.
|
|
127
|
+
The `itemProps` of a disabled item carry `aria-disabled="true"`, so the DOM element is marked as disabled for assistive technology as well.
|
|
127
128
|
|
|
128
129
|
To mark your items as disabled for your users, prefer `aria-disabled` over HTML's `disabled` attribute.
|
|
129
130
|
The `disabled` attribute completely disables focus and any browser events, which makes moving the focus to a neighboring item difficult to implement.
|
package/TitledSection.d.ts
CHANGED
|
@@ -104,7 +104,7 @@ export interface SectionHeadingProps extends HeadingProps, RefAttributes<HTMLHea
|
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
106
|
* Renders an appropriate heading tag for the current heading level (`h1` through `h6`).
|
|
107
|
-
*
|
|
107
|
+
* Use this component in the `title` prop of a {@link TitledSection} or once among its children.
|
|
108
108
|
*
|
|
109
109
|
* Headings are rendered as [Chakra Headings](https://chakra-ui.com/docs/components/heading).
|
|
110
110
|
*
|
|
@@ -133,7 +133,7 @@ export interface ConfigureTitledSectionProps {
|
|
|
133
133
|
*
|
|
134
134
|
* If your application is embedded into another site, it should often not use the `h1` tag
|
|
135
135
|
* but start with a higher heading level instead. To achieve that, simply wrap your application
|
|
136
|
-
* with `<
|
|
136
|
+
* with `<ConfigureTitledSection>`. No other code changes are necessary:
|
|
137
137
|
*
|
|
138
138
|
* ```jsx
|
|
139
139
|
* <ConfigureTitledSection level={2}>
|
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\n\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 *
|
|
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\n\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 * Use this component in the `title` prop of a {@link TitledSection} or once among its children.\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 `<ConfigureTitledSection>`. 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 * If you use it outside of a TitledSection, it will throw an error by default.\n * You can set `required: false` to make it return `undefined` instead of throwing an error\n *\n * @group Headings\n */\nexport function useHeadingLevel(): HeadingLevel;\nexport function useHeadingLevel(props: { required: false }): HeadingLevel | undefined;\nexport function useHeadingLevel(props: { required?: true }): HeadingLevel;\nexport function useHeadingLevel(props?: { required?: boolean }): HeadingLevel | undefined {\n const level = useContext(LevelContext);\n if (level == null) {\n if (props?.required === false) {\n return undefined;\n }\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":";;;;AAMA,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,CA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version": "4.9.0-dev.20261002130116",
|
|
5
5
|
"description": "Contains common react utilities used by the open pioneer trails project",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"open-pioneer-trails"
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@chakra-ui/react": "^3.37.0",
|
|
18
18
|
"@conterra/reactivity-core": "^0.8.8",
|
|
19
|
-
"@open-pioneer/core": "4.9.0-dev.
|
|
20
|
-
"@open-pioneer/reactivity": "4.9.0-dev.
|
|
19
|
+
"@open-pioneer/core": "4.9.0-dev.20261002130116",
|
|
20
|
+
"@open-pioneer/reactivity": "4.9.0-dev.20261002130116",
|
|
21
21
|
"react": "^19.3.0"
|
|
22
22
|
},
|
|
23
23
|
"publishConfig": {
|
|
@@ -1 +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\n\nimport { FC, memo, ReactNode } from \"react\";\nimport { RovingMenuState, MenuStateContext } from \"./RovingMenuState\";\n// oxlint-disable-next-line @typescript-eslint/no-unused-vars\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":";;;;
|
|
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\n\nimport { FC, memo, ReactNode } from \"react\";\nimport { RovingMenuState, MenuStateContext } from \"./RovingMenuState\";\n// oxlint-disable-next-line @typescript-eslint/no-unused-vars\nimport { type useRovingMenu } from \"./useRovingMenu\";\n\n/**\n * Properties of the {@link RovingMenuRoot} component.\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":";;;;AA2BO,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;;"}
|
|
@@ -198,7 +198,7 @@ function getFocusTarget(items, current, direction, wrap) {
|
|
|
198
198
|
} else if (typeof current === "string") {
|
|
199
199
|
currentIndex = findItemIndex(items, current);
|
|
200
200
|
}
|
|
201
|
-
if (currentIndex === -1
|
|
201
|
+
if (direction === "home" || currentIndex === -1 && direction !== "end") {
|
|
202
202
|
const index = items.findIndex((item) => !isDisabled(item));
|
|
203
203
|
const el = items[index];
|
|
204
204
|
return el;
|
|
@@ -283,7 +283,7 @@ function getItemValue(item) {
|
|
|
283
283
|
return item?.dataset[MENU_VALUE_JS] ?? void 0;
|
|
284
284
|
}
|
|
285
285
|
function isDisabled(item) {
|
|
286
|
-
return !!item.disabled || item.
|
|
286
|
+
return !!item.disabled || item.getAttribute("aria-disabled") === "true";
|
|
287
287
|
}
|
|
288
288
|
|
|
289
289
|
export { InternalMenuState, MENU_ID_ATTR, MENU_OWNER_ATTR, MENU_VALUE_ATTR, MenuStateContext, getInternalState };
|
|
@@ -1 +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\n\nimport { Reactive, reactive } from \"@conterra/reactivity-core\";\nimport { createLogger } from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { createContext, KeyboardEvent, FocusEvent, 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\nconst NOT_DISABLED_SELECTOR = \":not([disabled],[aria-disabled='true'])\";\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\nexport interface MenuStateOptions {\n menuId: string;\n orientation: \"horizontal\" | \"vertical\";\n menuRef: RefObject<HTMLElement | null>;\n wrap: boolean;\n active: boolean;\n isActiveInParent?: () => boolean;\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 #active: Reactive<boolean>;\n\n // True if the menu is currently active in its parent menu (used for nesting).\n // Note: reactive function\n #isActiveInParent: () => boolean;\n\n #menuRef: RefObject<HTMLElement | null>;\n\n // Value of the currently active item\n #current = reactive<string | undefined>();\n\n readonly menuId: string;\n readonly orientation: \"horizontal\" | \"vertical\";\n readonly wrap: boolean;\n\n constructor(options: MenuStateOptions) {\n this.menuId = options.menuId;\n this.wrap = options.wrap;\n this.orientation = options.orientation;\n this.#active = reactive(options.active);\n this.#menuRef = options.menuRef;\n this.#isActiveInParent = options.isActiveInParent ?? (() => true);\n }\n\n get active(): boolean {\n return this.#active.value;\n }\n\n set active(value: boolean) {\n this.#active.value = value;\n }\n\n get #currentValue(): string | undefined {\n return this.#current.value;\n }\n\n /**\n * Returns true if the menu item with the given value is currently active.\n *\n * The active menu item can receive the focus via keyboard navigation (tab).\n */\n isActive(value: string): boolean {\n return this.active && this.#isActiveInParent() && this.#currentValue === value;\n }\n\n /**\n * Called when the menu itself is focused.\n * The focus handler moves focus to the active child.\n *\n * This is used to implement nested menus.\n */\n onFocus(e: FocusEvent) {\n if (e.target !== e.currentTarget) {\n return; // Focus event from child\n }\n\n const activeValue = this.#currentValue;\n if (activeValue != null) {\n const item = getMenuItem(this.#menuRef, this.menuId, activeValue);\n if (item) {\n LOG.debug(\"Forwarding menu focus to item\", item);\n item.focus();\n } else {\n LOG.debug(\"Failed to find menu item for active value\");\n }\n }\n }\n\n /**\n * Called when a key is pressed on the menu root element.\n */\n onKeyDown(event: KeyboardEvent): void {\n if (event.defaultPrevented) {\n return; // Keyboard event from child\n }\n\n const direction = getNavDirection(event, this.orientation);\n if (!direction) {\n return;\n }\n\n const items = getMenuItems(this.#menuRef, this.menuId);\n const target = getFocusTarget(items, this.#currentValue, direction, this.wrap);\n if (!target) {\n // No candidate in the requested direction.\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.#currentValue) {\n this.#activateItem(value);\n }\n }\n\n /**\n * Called by items when they unmount.\n * Also used to handle the case when an item becomes disabled while focused.\n *\n * This will move the active value to another item to ensure that one item is focusable.\n */\n onItemUnmount(value: string, hadFocus: boolean): void {\n if (!this.isActive(value)) {\n return;\n }\n\n this.#navigateToNextFocusableItem(value, hadFocus);\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 #navigateToNextFocusableItem(disabledValue: string, hadFocus: boolean): void {\n const items = getMenuItems(this.#menuRef, this.menuId, true, false);\n const disabledIndex = findItemIndex(items, disabledValue);\n\n let target;\n if (disabledIndex === -1) {\n target = getFocusTarget(items, -1, \"home\", false);\n } else {\n // Attempt to move focus to something sensible\n target =\n getFocusTarget(items, disabledIndex, \"forward\", false) ??\n getFocusTarget(items, disabledIndex, \"backward\", false);\n }\n\n if (target) {\n this.#navigateToItem(target);\n if (hadFocus) {\n LOG.debug(\"Restoring focus within menu to\", target);\n requestAnimationFrame(() => target.focus());\n }\n return;\n }\n this.#activateItem(undefined);\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 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 | number | undefined,\n direction: NavDirection,\n wrap: boolean\n): HTMLElement | undefined {\n if (items.length === 0) {\n return undefined;\n }\n\n let currentIndex = -1;\n if (typeof current === \"number\") {\n currentIndex = current;\n } else if (typeof current === \"string\") {\n currentIndex = findItemIndex(items, current);\n }\n\n if (currentIndex === -1 || direction === \"home\") {\n const index = items.findIndex((item) => !isDisabled(item));\n const el = items[index];\n return el;\n }\n if (direction === \"end\") {\n const index = items.findLastIndex((item) => !isDisabled(item));\n const el = items[index];\n return el;\n }\n\n if (direction === \"forward\") {\n for (let nextIndex = currentIndex + 1; ;) {\n if (nextIndex >= items.length) {\n if (!wrap) {\n break;\n }\n nextIndex = 0;\n }\n if (nextIndex === currentIndex) {\n break;\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion\n const item = items[nextIndex]!;\n if (!isDisabled(item)) {\n return item;\n }\n nextIndex += 1;\n }\n } else {\n for (let nextIndex = currentIndex - 1; ;) {\n if (nextIndex < 0) {\n if (!wrap) {\n break;\n }\n nextIndex = items.length - 1;\n }\n if (nextIndex === currentIndex) {\n break;\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion\n const item = items[nextIndex]!;\n if (!isDisabled(item)) {\n return item;\n }\n nextIndex -= 1;\n }\n }\n return undefined;\n}\n\nfunction findItemIndex(items: HTMLElement[], value: string) {\n return items.findIndex((item) => getItemValue(item) === value);\n}\n\nfunction getMenuItems(\n menuRef: RefObject<HTMLElement | null>,\n menuId: string,\n unmount = false,\n excludeDisabled = true\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 let selector = `[${MENU_OWNER_ATTR}=${escapedId}]`;\n if (excludeDisabled) {\n selector += NOT_DISABLED_SELECTOR;\n }\n const children = owner.querySelectorAll(selector);\n return Array.from(children) as HTMLElement[];\n}\n\nfunction getMenuItem(\n menuRef: RefObject<HTMLElement | null>,\n menuId: string,\n value: string\n): HTMLElement | undefined {\n const owner = menuRef.current;\n if (!owner) {\n return undefined;\n }\n\n const escapedMenuId = CSS.escape(menuId);\n const escapedValue = CSS.escape(value);\n const node = owner.querySelector(\n `[${MENU_OWNER_ATTR}=${escapedMenuId}][${MENU_VALUE_ATTR}=${escapedValue}]${NOT_DISABLED_SELECTOR}`\n );\n return (node ?? undefined) as HTMLElement | undefined;\n}\n\nfunction getItemValue(item: HTMLElement | undefined): string | undefined {\n return item?.dataset[MENU_VALUE_JS] ?? undefined;\n}\n\nfunction isDisabled(item: HTMLElement): boolean {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n return !!(item as any).disabled || item.ariaDisabled === \"true\";\n}\n"],"names":[],"mappings":";;;;;AAQA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AAG1B,MAAM,YAAA,GAAe;AAGrB,MAAM,eAAA,GAAkB;AAGxB,MAAM,eAAA,GAAkB;AAGxB,MAAM,aAAA,GAAgB,iBAAA;AAE7B,MAAM,qBAAA,GAAwB,yCAAA;AAOvB,MAAM,gBAAA,GAAmB,cAA2C,MAAS;AAmB7E,SAAS,iBAAiB,SAAA,EAA+C;AAC5E,EAAA,IAAI,EAAE,qBAAqB,iBAAA,CAAA,EAAoB;AAC3C,IAAA,MAAM,IAAI,MAAM,qBAAqB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,EAAA,MAAM,QAAQ,OAAA,CAAQ,OAAA;AACtB,EAAA,IAAI,CAAC,KAAA,EAAO;AACR,IAAA,OAAO,MAAA;AAAA,EACX;AAEA,EAAA,MAAM,aAAA,GAAgB,GAAA,CAAI,MAAA,CAAO,MAAM,CAAA;AACvC,EAAA,MAAM,YAAA,GAAe,GAAA,CAAI,MAAA,CAAO,KAAK,CAAA;AACrC,EAAA,MAAM,OAAO,KAAA,CAAM,aAAA;AAAA,IACf,CAAA,CAAA,EAAI,eAAe,CAAA,CAAA,EAAI,aAAa,KAAK,eAAe,CAAA,CAAA,EAAI,YAAY,CAAA,CAAA,EAAI,qBAAqB,CAAA;AAAA,GACrG;AACA,EAAA,OAAQ,IAAA,IAAQ,MAAA;AACpB;AAEA,SAAS,aAAa,IAAA,EAAmD;AACrE,EAAA,OAAO,IAAA,EAAM,OAAA,CAAQ,aAAa,CAAA,IAAK,MAAA;AAC3C;AAEA,SAAS,WAAW,IAAA,EAA4B;AAE5C,EAAA,OAAO,CAAC,CAAE,IAAA,CAAa,QAAA,IAAY,KAAK,YAAA,KAAiB,MAAA;AAC7D;;"}
|
|
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\n\nimport { Reactive, reactive } from \"@conterra/reactivity-core\";\nimport { createLogger } from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { createContext, KeyboardEvent, FocusEvent, 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\nconst NOT_DISABLED_SELECTOR = \":not([disabled],[aria-disabled='true'])\";\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\nexport interface MenuStateOptions {\n menuId: string;\n orientation: \"horizontal\" | \"vertical\";\n menuRef: RefObject<HTMLElement | null>;\n wrap: boolean;\n active: boolean;\n isActiveInParent?: () => boolean;\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 #active: Reactive<boolean>;\n\n // True if the menu is currently active in its parent menu (used for nesting).\n // Note: reactive function\n #isActiveInParent: () => boolean;\n\n #menuRef: RefObject<HTMLElement | null>;\n\n // Value of the currently active item\n #current = reactive<string | undefined>();\n\n readonly menuId: string;\n readonly orientation: \"horizontal\" | \"vertical\";\n readonly wrap: boolean;\n\n constructor(options: MenuStateOptions) {\n this.menuId = options.menuId;\n this.wrap = options.wrap;\n this.orientation = options.orientation;\n this.#active = reactive(options.active);\n this.#menuRef = options.menuRef;\n this.#isActiveInParent = options.isActiveInParent ?? (() => true);\n }\n\n get active(): boolean {\n return this.#active.value;\n }\n\n set active(value: boolean) {\n this.#active.value = value;\n }\n\n get #currentValue(): string | undefined {\n return this.#current.value;\n }\n\n /**\n * Returns true if the menu item with the given value is currently active.\n *\n * The active menu item can receive the focus via keyboard navigation (tab).\n */\n isActive(value: string): boolean {\n return this.active && this.#isActiveInParent() && this.#currentValue === value;\n }\n\n /**\n * Called when the menu itself is focused.\n * The focus handler moves focus to the active child.\n *\n * This is used to implement nested menus.\n */\n onFocus(e: FocusEvent) {\n if (e.target !== e.currentTarget) {\n return; // Focus event from child\n }\n\n const activeValue = this.#currentValue;\n if (activeValue != null) {\n const item = getMenuItem(this.#menuRef, this.menuId, activeValue);\n if (item) {\n LOG.debug(\"Forwarding menu focus to item\", item);\n item.focus();\n } else {\n LOG.debug(\"Failed to find menu item for active value\");\n }\n }\n }\n\n /**\n * Called when a key is pressed on the menu root element.\n */\n onKeyDown(event: KeyboardEvent): void {\n if (event.defaultPrevented) {\n return; // Keyboard event from child\n }\n\n const direction = getNavDirection(event, this.orientation);\n if (!direction) {\n return;\n }\n\n const items = getMenuItems(this.#menuRef, this.menuId);\n const target = getFocusTarget(items, this.#currentValue, direction, this.wrap);\n if (!target) {\n // No candidate in the requested direction.\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.#currentValue) {\n this.#activateItem(value);\n }\n }\n\n /**\n * Called by items when they unmount.\n * Also used to handle the case when an item becomes disabled while focused.\n *\n * This will move the active value to another item to ensure that one item is focusable.\n */\n onItemUnmount(value: string, hadFocus: boolean): void {\n if (!this.isActive(value)) {\n return;\n }\n\n this.#navigateToNextFocusableItem(value, hadFocus);\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 #navigateToNextFocusableItem(disabledValue: string, hadFocus: boolean): void {\n const items = getMenuItems(this.#menuRef, this.menuId, true, false);\n const disabledIndex = findItemIndex(items, disabledValue);\n\n let target;\n if (disabledIndex === -1) {\n target = getFocusTarget(items, -1, \"home\", false);\n } else {\n // Attempt to move focus to something sensible\n target =\n getFocusTarget(items, disabledIndex, \"forward\", false) ??\n getFocusTarget(items, disabledIndex, \"backward\", false);\n }\n\n if (target) {\n this.#navigateToItem(target);\n if (hadFocus) {\n LOG.debug(\"Restoring focus within menu to\", target);\n requestAnimationFrame(() => target.focus());\n }\n return;\n }\n this.#activateItem(undefined);\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 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 | number | undefined,\n direction: NavDirection,\n wrap: boolean\n): HTMLElement | undefined {\n if (items.length === 0) {\n return undefined;\n }\n\n let currentIndex = -1;\n if (typeof current === \"number\") {\n currentIndex = current;\n } else if (typeof current === \"string\") {\n currentIndex = findItemIndex(items, current);\n }\n\n if (direction === \"home\" || (currentIndex === -1 && direction !== \"end\")) {\n const index = items.findIndex((item) => !isDisabled(item));\n const el = items[index];\n return el;\n }\n if (direction === \"end\") {\n const index = items.findLastIndex((item) => !isDisabled(item));\n const el = items[index];\n return el;\n }\n\n if (direction === \"forward\") {\n for (let nextIndex = currentIndex + 1; ;) {\n if (nextIndex >= items.length) {\n if (!wrap) {\n break;\n }\n nextIndex = 0;\n }\n if (nextIndex === currentIndex) {\n break;\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion\n const item = items[nextIndex]!;\n if (!isDisabled(item)) {\n return item;\n }\n nextIndex += 1;\n }\n } else {\n for (let nextIndex = currentIndex - 1; ;) {\n if (nextIndex < 0) {\n if (!wrap) {\n break;\n }\n nextIndex = items.length - 1;\n }\n if (nextIndex === currentIndex) {\n break;\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion\n const item = items[nextIndex]!;\n if (!isDisabled(item)) {\n return item;\n }\n nextIndex -= 1;\n }\n }\n return undefined;\n}\n\nfunction findItemIndex(items: HTMLElement[], value: string) {\n return items.findIndex((item) => getItemValue(item) === value);\n}\n\nfunction getMenuItems(\n menuRef: RefObject<HTMLElement | null>,\n menuId: string,\n unmount = false,\n excludeDisabled = true\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 let selector = `[${MENU_OWNER_ATTR}=${escapedId}]`;\n if (excludeDisabled) {\n selector += NOT_DISABLED_SELECTOR;\n }\n const children = owner.querySelectorAll(selector);\n return Array.from(children) as HTMLElement[];\n}\n\nfunction getMenuItem(\n menuRef: RefObject<HTMLElement | null>,\n menuId: string,\n value: string\n): HTMLElement | undefined {\n const owner = menuRef.current;\n if (!owner) {\n return undefined;\n }\n\n const escapedMenuId = CSS.escape(menuId);\n const escapedValue = CSS.escape(value);\n const node = owner.querySelector(\n `[${MENU_OWNER_ATTR}=${escapedMenuId}][${MENU_VALUE_ATTR}=${escapedValue}]${NOT_DISABLED_SELECTOR}`\n );\n return (node ?? undefined) as HTMLElement | undefined;\n}\n\nfunction getItemValue(item: HTMLElement | undefined): string | undefined {\n return item?.dataset[MENU_VALUE_JS] ?? undefined;\n}\n\nfunction isDisabled(item: HTMLElement): boolean {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n return !!(item as any).disabled || item.getAttribute(\"aria-disabled\") === \"true\";\n}\n"],"names":[],"mappings":";;;;;AAQA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AAG1B,MAAM,YAAA,GAAe;AAGrB,MAAM,eAAA,GAAkB;AAGxB,MAAM,eAAA,GAAkB;AAGxB,MAAM,aAAA,GAAgB,iBAAA;AAE7B,MAAM,qBAAA,GAAwB,yCAAA;AAOvB,MAAM,gBAAA,GAAmB,cAA2C,MAAS;AAmB7E,SAAS,iBAAiB,SAAA,EAA+C;AAC5E,EAAA,IAAI,EAAE,qBAAqB,iBAAA,CAAA,EAAoB;AAC3C,IAAA,MAAM,IAAI,MAAM,qBAAqB,CAAA;AAAA,EACzC;AACA,EAAA,OAAO,SAAA;AACX;AAgBO,MAAM,iBAAA,CAAkB;AAAA,EAC3B,OAAA;AAAA;AAAA;AAAA,EAIA,iBAAA;AAAA,EAEA,QAAA;AAAA;AAAA,EAGA,WAAW,QAAA,EAA6B;AAAA,EAE/B,MAAA;AAAA,EACA,WAAA;AAAA,EACA,IAAA;AAAA,EAET,YAAY,OAAA,EAA2B;AACnC,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,OAAO,OAAA,CAAQ,IAAA;AACpB,IAAA,IAAA,CAAK,cAAc,OAAA,CAAQ,WAAA;AAC3B,IAAA,IAAA,CAAK,OAAA,GAAU,QAAA,CAAS,OAAA,CAAQ,MAAM,CAAA;AACtC,IAAA,IAAA,CAAK,WAAW,OAAA,CAAQ,OAAA;AACxB,IAAA,IAAA,CAAK,iBAAA,GAAoB,OAAA,CAAQ,gBAAA,KAAqB,MAAM,IAAA,CAAA;AAAA,EAChE;AAAA,EAEA,IAAI,MAAA,GAAkB;AAClB,IAAA,OAAO,KAAK,OAAA,CAAQ,KAAA;AAAA,EACxB;AA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AC9E;;"}
|
|
@@ -88,7 +88,7 @@ export interface NestedRovingMenuResult extends RovingMenuResult {
|
|
|
88
88
|
/**
|
|
89
89
|
* Like {@link useRovingMenu}, but suitable for nested menus.
|
|
90
90
|
*
|
|
91
|
-
* Only a single level of
|
|
91
|
+
* Only a single level of nesting is supported at this time (i.e. horizontal in vertical, or the other way around).
|
|
92
92
|
*
|
|
93
93
|
* @group Roving menu
|
|
94
94
|
* @expandType NestedRovingMenuProps
|
|
@@ -1 +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\n\nimport {\n KeyboardEvent,\n FocusEvent,\n RefObject,\n useCallback,\n useId,\n useMemo,\n useRef,\n useEffect\n} from \"react\";\nimport { useEvent } from \"../useEvent\";\n// oxlint-disable-next-line @typescript-eslint/no-unused-vars\nimport { type RovingMenuRoot } from \"./RovingMenuRoot\";\nimport { InternalMenuState, MENU_ID_ATTR, RovingMenuState } from \"./RovingMenuState\";\nimport { useMenuState } from \"./useMenuState\";\nimport { RovingMenuItemDomProps, useRovingMenuItemImpl } from \"./useRovingMenuItem\";\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 * Wrap around when pressing an arrow key at the start or the end of the menu.\n *\n * Default: `true`.\n */\n wrap?: boolean;\n\n /**\n * Whether the menu is active.\n * Only active menus are focusable by the user through keyboard navigation.\n *\n * Default: `true`\n */\n active?: boolean;\n}\n\n/** @internal */\nexport interface RovingMenuDomProps {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ref: RefObject<any>;\n role: string;\n tabIndex: number;\n [MENU_ID_ATTR]: string;\n \"aria-orientation\": \"horizontal\" | \"vertical\";\n onKeyDown: (event: KeyboardEvent) => void;\n onFocus: (event: FocusEvent) => void;\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: RovingMenuDomProps;\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 return useRovingMenuImpl(props);\n}\n\n/**\n * Properties supported when creating a new nested menu via {@link useNestedRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface NestedRovingMenuProps extends RovingMenuProps {\n /**\n * The value of the menu within its parent menu.\n */\n value: string;\n}\n\n/**\n * The return value of {@link useNestedRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface NestedRovingMenuResult extends RovingMenuResult {\n menuProps: RovingMenuDomProps & RovingMenuItemDomProps;\n}\n\n/**\n * Like {@link useRovingMenu}, but suitable for nested menus.\n *\n * Only a single level of
|
|
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\n\nimport {\n KeyboardEvent,\n FocusEvent,\n RefObject,\n useCallback,\n useId,\n useMemo,\n useRef,\n useEffect\n} from \"react\";\nimport { useEvent } from \"../useEvent\";\n// oxlint-disable-next-line @typescript-eslint/no-unused-vars\nimport { type RovingMenuRoot } from \"./RovingMenuRoot\";\nimport { InternalMenuState, MENU_ID_ATTR, RovingMenuState } from \"./RovingMenuState\";\nimport { useMenuState } from \"./useMenuState\";\nimport { RovingMenuItemDomProps, useRovingMenuItemImpl } from \"./useRovingMenuItem\";\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 * Wrap around when pressing an arrow key at the start or the end of the menu.\n *\n * Default: `true`.\n */\n wrap?: boolean;\n\n /**\n * Whether the menu is active.\n * Only active menus are focusable by the user through keyboard navigation.\n *\n * Default: `true`\n */\n active?: boolean;\n}\n\n/** @internal */\nexport interface RovingMenuDomProps {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ref: RefObject<any>;\n role: string;\n tabIndex: number;\n [MENU_ID_ATTR]: string;\n \"aria-orientation\": \"horizontal\" | \"vertical\";\n onKeyDown: (event: KeyboardEvent) => void;\n onFocus: (event: FocusEvent) => void;\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: RovingMenuDomProps;\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 return useRovingMenuImpl(props);\n}\n\n/**\n * Properties supported when creating a new nested menu via {@link useNestedRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface NestedRovingMenuProps extends RovingMenuProps {\n /**\n * The value of the menu within its parent menu.\n */\n value: string;\n}\n\n/**\n * The return value of {@link useNestedRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface NestedRovingMenuResult extends RovingMenuResult {\n menuProps: RovingMenuDomProps & RovingMenuItemDomProps;\n}\n\n/**\n * Like {@link useRovingMenu}, but suitable for nested menus.\n *\n * Only a single level of nesting is supported at this time (i.e. horizontal in vertical, or the other way around).\n *\n * @group Roving menu\n * @expandType NestedRovingMenuProps\n */\nexport function useNestedRovingMenu(props: NestedRovingMenuProps): NestedRovingMenuResult {\n const parentMenuState = useMenuState();\n const { menuProps, menuState } = useRovingMenuImpl(\n props,\n useCallback(() => parentMenuState.isActive(props.value), [parentMenuState, props.value])\n );\n const { itemProps } = useRovingMenuItemImpl(props, \"nested\");\n\n return useMemo(() => {\n return {\n menuProps: {\n ...menuProps,\n ...itemProps,\n onFocus(e: FocusEvent) {\n // Tracks active state in parent\n itemProps.onFocus(e);\n\n // Focuses the correct child\n menuProps.onFocus(e);\n }\n },\n menuState\n };\n }, [menuProps, menuState, itemProps]);\n}\n\nfunction useRovingMenuImpl(\n props: RovingMenuProps,\n isActiveInParent?: () => boolean\n): RovingMenuResult {\n const { orientation = \"horizontal\", active = true, wrap = true } = props;\n const menuId = useId();\n const menuRef = useRef<HTMLElement>(null);\n\n const initialActive = useRef(active);\n\n // Shared state between items and root\n const state = useMemo(\n () =>\n // oxlint-disable-next-line react/refs\n new InternalMenuState({\n menuId,\n orientation,\n menuRef,\n wrap,\n // oxlint-disable-next-line react/refs\n active: initialActive.current,\n isActiveInParent\n }),\n [menuRef, orientation, menuId, wrap, isActiveInParent]\n );\n\n // Fine grained updates without losing state\n useEffect(() => {\n state.active = active;\n }, [state, active]);\n\n const onKeyDown = useEvent((event: KeyboardEvent) => {\n state.onKeyDown(event);\n });\n const onFocus = useEvent((event: FocusEvent) => {\n state.onFocus(event);\n });\n const result = useMemo(\n (): RovingMenuResult => ({\n menuProps: {\n ref: menuRef,\n role: \"toolbar\",\n \"aria-orientation\": orientation,\n tabIndex: -1,\n [MENU_ID_ATTR]: menuId,\n onKeyDown,\n onFocus\n },\n menuState: state as unknown as RovingMenuState\n }),\n [menuId, orientation, onKeyDown, onFocus, state]\n );\n return result;\n}\n"],"names":[],"mappings":";;;;;;AA2FO,SAAS,aAAA,CAAc,KAAA,GAAyB,EAAC,EAAqB;AACzE,EAAA,OAAO,kBAAkB,KAAK,CAAA;AAClC;AA+BO,SAAS,oBAAoB,KAAA,EAAsD;AACtF,EAAA,MAAM,kBAAkB,YAAA,EAAa;AACrC,EAAA,MAAM,EAAE,SAAA,EAAW,SAAA,EAAU,GAAI,iBAAA;AAAA,IAC7B,KAAA;AAAA,IACA,WAAA,CAAY,MAAM,eAAA,CAAgB,QAAA,CAAS,KAAA,CAAM,KAAK,CAAA,EAAG,CAAC,eAAA,EAAiB,KAAA,CAAM,KAAK,CAAC;AAAA,GAC3F;AACA,EAAA,MAAM,EAAE,SAAA,EAAU,GAAI,qBAAA,CAAsB,OAAO,QAAQ,CAAA;AAE3D,EAAA,OAAO,QAAQ,MAAM;AACjB,IAAA,OAAO;AAAA,MACH,SAAA,EAAW;AAAA,QACP,GAAG,SAAA;AAAA,QACH,GAAG,SAAA;AAAA,QACH,QAAQ,CAAA,EAAe;AAEnB,UAAA,SAAA,CAAU,QAAQ,CAAC,CAAA;AAGnB,UAAA,SAAA,CAAU,QAAQ,CAAC,CAAA;AAAA,QACvB;AAAA,OACJ;AAAA,MACA;AAAA,KACJ;AAAA,EACJ,CAAA,EAAG,CAAC,SAAA,EAAW,SAAA,EAAW,SAAS,CAAC,CAAA;AACxC;AAEA,SAAS,iBAAA,CACL,OACA,gBAAA,EACgB;AAChB,EAAA,MAAM,EAAE,WAAA,GAAc,YAAA,EAAc,SAAS,IAAA,EAAM,IAAA,GAAO,MAAK,GAAI,KAAA;AACnE,EAAA,MAAM,SAAS,KAAA,EAAM;AACrB,EAAA,MAAM,OAAA,GAAU,OAAoB,IAAI,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} from "react";
|
|
2
2
|
import { MENU_OWNER_ATTR, MENU_VALUE_ATTR } from "./RovingMenuState";
|
|
3
3
|
/**
|
|
4
|
-
* Properties
|
|
4
|
+
* Properties supported when creating a new menu item via {@link useRovingMenuItem}.
|
|
5
5
|
*
|
|
6
6
|
* @group Roving menu
|
|
7
7
|
*/
|
|
@@ -14,6 +14,7 @@ export interface RovingMenuItemProps {
|
|
|
14
14
|
* Set this to true if your item is disabled.
|
|
15
15
|
*
|
|
16
16
|
* This tells the menu that the item cannot be focused.
|
|
17
|
+
* The returned `itemProps` then carry `aria-disabled`, which keyboard navigation uses to skip the item.
|
|
17
18
|
*/
|
|
18
19
|
disabled?: boolean;
|
|
19
20
|
/**
|
|
@@ -31,6 +32,7 @@ export interface RovingMenuItemProps {
|
|
|
31
32
|
export interface RovingMenuItemDomProps {
|
|
32
33
|
[MENU_OWNER_ATTR]: string;
|
|
33
34
|
[MENU_VALUE_ATTR]: string;
|
|
35
|
+
"aria-disabled"?: true;
|
|
34
36
|
tabIndex: number;
|
|
35
37
|
onFocus: FocusEventHandler;
|
|
36
38
|
onBlur: FocusEventHandler;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useReactiveSnapshot } from '@open-pioneer/reactivity';
|
|
2
2
|
import { useRef, useLayoutEffect, useMemo } from 'react';
|
|
3
|
+
import { useEvent } from '../useEvent.js';
|
|
3
4
|
import { MENU_VALUE_ATTR, MENU_OWNER_ATTR } from './RovingMenuState.js';
|
|
4
5
|
import { useMenuState } from './useMenuState.js';
|
|
5
6
|
|
|
@@ -19,6 +20,13 @@ function useRovingMenuItemImpl(props, context) {
|
|
|
19
20
|
}, [state, value, disabled]);
|
|
20
21
|
const isActiveValue = useReactiveSnapshot(() => state?.isActive(value), [state, value]);
|
|
21
22
|
const isActive = !disabled && isActiveValue;
|
|
23
|
+
const onFocus = useEvent((_event) => {
|
|
24
|
+
hasFocus.current = true;
|
|
25
|
+
state?.onItemFocus(value);
|
|
26
|
+
});
|
|
27
|
+
const onBlur = useEvent((_event) => {
|
|
28
|
+
hasFocus.current = false;
|
|
29
|
+
});
|
|
22
30
|
const result = useMemo(() => {
|
|
23
31
|
if (!state) {
|
|
24
32
|
return void 0;
|
|
@@ -28,18 +36,16 @@ function useRovingMenuItemImpl(props, context) {
|
|
|
28
36
|
itemProps: {
|
|
29
37
|
[MENU_OWNER_ATTR]: menuId,
|
|
30
38
|
[MENU_VALUE_ATTR]: value,
|
|
39
|
+
// Only set when disabled, so a consumer's own aria-disabled survives the spread
|
|
40
|
+
...disabled ? { "aria-disabled": true } : void 0,
|
|
31
41
|
// items can be reached via user tab, nested menus can't
|
|
32
42
|
tabIndex: context === "item" && isActive ? 0 : -1,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
},
|
|
37
|
-
onBlur: (_event) => {
|
|
38
|
-
hasFocus.current = false;
|
|
39
|
-
}
|
|
43
|
+
// Dom events
|
|
44
|
+
onFocus,
|
|
45
|
+
onBlur
|
|
40
46
|
}
|
|
41
47
|
};
|
|
42
|
-
}, [state, value, isActive, context]);
|
|
48
|
+
}, [state, value, isActive, disabled, context, onFocus, onBlur]);
|
|
43
49
|
return result;
|
|
44
50
|
}
|
|
45
51
|
|
|
@@ -1 +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\n\nimport { useReactiveSnapshot } from \"@open-pioneer/reactivity\";\nimport { FocusEventHandler, useLayoutEffect, useMemo, useRef } from \"react\";\nimport { MENU_OWNER_ATTR, MENU_VALUE_ATTR } from \"./RovingMenuState\";\nimport { useMenuState } from \"./useMenuState\";\n\n/**\n * Properties
|
|
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\n\nimport { useReactiveSnapshot } from \"@open-pioneer/reactivity\";\nimport { FocusEventHandler, useLayoutEffect, useMemo, useRef, FocusEvent } from \"react\";\nimport { useEvent } from \"../useEvent\";\nimport { MENU_OWNER_ATTR, MENU_VALUE_ATTR } from \"./RovingMenuState\";\nimport { useMenuState } from \"./useMenuState\";\n\n/**\n * Properties supported 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 * The returned `itemProps` then carry `aria-disabled`, which keyboard navigation uses to skip the item.\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/** @internal */\nexport interface RovingMenuItemDomProps {\n [MENU_OWNER_ATTR]: string;\n [MENU_VALUE_ATTR]: string;\n \"aria-disabled\"?: true;\n tabIndex: number;\n onFocus: FocusEventHandler;\n onBlur: FocusEventHandler;\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: RovingMenuItemDomProps;\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 return useRovingMenuItemImpl(props, \"item\");\n}\n\n/**\n * @internal\n */\nexport function useRovingMenuItemImpl(\n props: RovingMenuItemProps & { required?: true },\n context: \"item\" | \"nested\"\n): RovingMenuItemResult;\nexport function useRovingMenuItemImpl(\n props: RovingMenuItemProps,\n context: \"item\" | \"nested\"\n): RovingMenuItemResult | undefined;\nexport function useRovingMenuItemImpl(\n props: RovingMenuItemProps,\n context: \"item\" | \"nested\"\n): RovingMenuItemResult | undefined {\n const { value, disabled = false, required = true } = props;\n const state = useMenuState(required);\n\n const hasFocus = useRef(false);\n\n useLayoutEffect(() => {\n if (!state || disabled) {\n return;\n }\n state.onItemMount(value);\n return () => state.onItemUnmount(value, hasFocus.current);\n }, [state, value, disabled]);\n\n const isActiveValue = useReactiveSnapshot(() => state?.isActive(value), [state, value]);\n const isActive = !disabled && isActiveValue;\n\n const onFocus = useEvent((_event: FocusEvent) => {\n hasFocus.current = true;\n state?.onItemFocus(value);\n });\n const onBlur = useEvent((_event: FocusEvent) => {\n hasFocus.current = false;\n });\n\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\n // Only set when disabled, so a consumer's own aria-disabled survives the spread\n ...(disabled ? { \"aria-disabled\": true as const } : undefined),\n\n // items can be reached via user tab, nested menus can't\n tabIndex: context === \"item\" && isActive ? 0 : -1,\n\n // Dom events\n onFocus,\n onBlur\n }\n };\n }, [state, value, isActive, disabled, context, onFocus, onBlur]);\n return result;\n}\n"],"names":[],"mappings":";;;;;;AA4EO,SAAS,kBAAkB,KAAA,EAA8D;AAC5F,EAAA,OAAO,qBAAA,CAAsB,OAAO,MAAM,CAAA;AAC9C;AAaO,SAAS,qBAAA,CACZ,OACA,OAAA,EACgC;AAChC,EAAA,MAAM,EAAE,KAAA,EAAO,QAAA,GAAW,KAAA,EAAO,QAAA,GAAW,MAAK,GAAI,KAAA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