@momentum-design/components 0.134.9 → 0.134.11
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/dist/browser/index.js +2 -2
- package/dist/browser/index.js.map +3 -3
- package/dist/components/popover/popover.component.d.ts +25 -5
- package/dist/components/popover/popover.component.js +40 -19
- package/dist/components/spatialnavigationprovider/spatialnavigationprovider.component.d.ts +59 -21
- package/dist/components/spatialnavigationprovider/spatialnavigationprovider.component.js +96 -31
- package/dist/custom-elements.json +70 -8
- package/dist/react/spatialnavigationprovider/index.d.ts +44 -20
- package/dist/react/spatialnavigationprovider/index.js +44 -20
- package/package.json +1 -1
|
@@ -44925,7 +44925,7 @@
|
|
|
44925
44925
|
"declarations": [
|
|
44926
44926
|
{
|
|
44927
44927
|
"kind": "class",
|
|
44928
|
-
"description": "
|
|
44928
|
+
"description": "Spatial navigation focus manager\n\n[Spatial navigation](https://en.wikipedia.org/wiki/Spatial_navigation) lets users move focus among\nelements on a 2D plane, common on TVs and game consoles with remotes or gamepads.\n\nIt should have only one instance and it should placed at the root of the application.\n\n## Focus management\n\nThe provider listens to keyboard events and moves focus among elements based on arrow key input.\nYou can influence or override this behavior.\n\n### Steps\n\nSpatial navigation goes trough the following steps after each keydown:\n\n1. Handle `keydown` in the capture phase.\n When active element has `data-spatial-{direction}` attribute then prevent all component navigation and call the\n provider own `keydown` handler (see step 3).\n2. Component own `keydown` handler executed (bubble phase) (e.g., list moves focus internally) it it was not\n prevented.\n3. Spatial Navigation Provider's `keydown` handler executed (bubble phase)\n - If key event was not prevented in step 1. emit `navbeforeprocess` to check if any component want to handle\n the key event itself. If `navbeforeprocess` event is prevented, stop here.\n - If the component did not handle `keydown`, it calculate the next focusable item\n - if the active element has `data-spatial-{direction}` attribute, it will try to focus the element with the id.\n - Otherwise calculate the next focused item based on the direction and distances.\n - If there is no next item, it emits `navnotarget` event\n - Otherwise emit `navbeforefocus`,\n - If this event prevented, nothing happens\n - Otherwise the focus moves to the next element\n\n### Determine next focus\n\nThe provider use multiple ways to determine the next focused element. The order defined in the \"Steps\" section.\n\n#### Calculated focus\n\nBy default, the next focus target is computed from element positions:\n\n1. Find the nearest focus area (scrollable container or active focus trap) relative to the current element.\n2. Collect focusable elements in that area.\n3. Compute distances from the current element to candidates using the W3C \"find the shortest\n distance\" algorithm: https://www.w3.org/TR/css-nav-1/#find-the-shortest-distance\n4. If no candidates are found, repeat from step 1, skipping areas already checked.\n5. Focus the closest candidate.\n\nElements with `data-spatial-focusable` are treated as focusable even if they would otherwise not be\n(e.g., `tabindex=\"-1\"`).\n\nElements with `data-spatial-exclude` are excluded (with its subtree) from the navigation, even if they\nare focusable.\n\nNote: The algorithm is distance-based, so the UI should be designed to focusable elements are\npredictably reachable. Relative element positions should remain stable; responsive layouts can\nmake navigation unpredictable. This is less of an issue on fixed-size TV UIs but can show unexpected\nbehavior in Storybook when resizing. See the \"Limitations\" section.\n\n#### Overwrite next element\n\nOverride calculated navigation by adding one of these attributes to a focusable element:\n\n- `data-spatial-up`\n- `data-spatial-down`\n- `data-spatial-left`\n- `data-spatial-right`\n\nEach attribute value must be the id of the element to focus when the corresponding key is pressed.\n\n#### Element internal navigation\n\nComplex components (List, Combobox, Tree, etc.) may handle their own navigation. For example, a List moves\nfocus internally on Down until the last item, after which Down should fall back to provider navigation.\n\nTo prevent the provider from handling a key, listen to `navbeforeprocess` and call `event.preventDefault()`.\nThis event fires after the component handles `keydown`.\n\n### Cancel focus change\n\nBefore focusing a computed target, the provider dispatches `navbeforefocus` on the current element. Call\n`event.preventDefault()` on this event to cancel the focus change.\n\n## Enter action\n\nPressing Enter triggers `.click()` on the currently focused element.\n\nYou can prevent this by listening to `navbeforeprocess` and calling `event.preventDefault()`.\n\n## Escape/Back action\n\nPressing Escape tries to find a focusable element with `data-spatial-go-back` and clicks it. If none exists,\nthe provider calls `history.back()`.\n\nYou can prevent this by listening to `navbeforeprocess` and calling `event.preventDefault()`.\n\nYou can also intercept the back click by listening to `navback` and calling `event.preventDefault()`.\n\n## Control data attributes\n\nSupported data attributes:\n\n| Attribute | Value | Default | Description |\n|--------------------------|---------------------------|---------|-------------------------------------------------------------------------------|\n| `data-spatial-left` | empty string / element id | N/A | Prevent native navigation in Left direction and focus element if exists |\n| `data-spatial-up` | empty string / element id | N/A | Prevent native navigation in Up direction and focus element if exists |\n| `data-spatial-right` | empty string / element id | N/A | Prevent native navigation in Right direction and focus element if exists |\n| `data-spatial-down` | empty string / element id | N/A | Prevent native navigation in Down direction and focus element if exists |\n| `data-spatial-go-back` | N/A | N/A | First focusable element with this attribute is clicked on Back/Escape |\n| `data-spatial-focusable` | N/A | N/A | Treat element as focusable even if it normally is not (e.g., `tabindex=\"-1\"`) |\n| `data-spatial-exclude` | N/A | N/A | Exclude focusable element (and its subtree) from the navigation |\n\n## Event emitting order\n\nOn a navigation key press, events fire in this order:\n\n1. `navbeforeprocess` on the currently focused element.\n2. If not prevented:\na. Arrow keys: `navbeforefocus` on the currently focused element.\nb. Enter: `.click()` on the currently focused element.\nc. Escape/Back: `navback` on the provider, then `.click()` on the go-back element or `history.back()`.\n3. If no target is found in the requested direction: `navnotarget` on the provider.\n\n## Handle complex components\n\n### Generic components\n\nComponents that handle navigation internally should prevent the provider from acting. Handle `navbeforeprocess`\nand call `event.preventDefault()` for keys you process yourself.\n\n### Form inputs\n\nNative inputs often submit on Enter, which is not desirable here. Enter should toggle or activate the control\n(e.g., check/uncheck). Provide a dedicated submit button users can navigate to and press Enter on.\n\n### Utilities for complex components\n\n#### KeyToActionMixin\n\nMaps key events to action names. Call `getActionForKeyEvent` to get the action for a keyboard event. Also provides\n`getKeyboardNavMode` to check whether navigation is spatial or default.\n\n#### KeyDownHandledMixin\n\nNotify the provider when a component handled `keydown` internally. Call `keyDownEventHandled` whenever you process\nkeydown yourself.\n\n## Platform specific behaviors\n\nConsider remote/gamepad constraints. Often focus alone is not enough and users press Enter to \"enter\" an interactive mode:\n- Select: Enter opens options rather than arrow keys opening a popover.\n- Text inputs: see the next section.\n- Slider: Enter to start adjusting, arrow keys to change value, Enter/Escape to stop.\n\n### Text inputs\n\nOn TV-like platforms without physical keyboards, Enter/focus on an input should open a virtual keyboard instead of submitting\nthe form. Users must close the keyboard (Escape) to continue spatial navigation.\n\nIf navigation keys are mapped to letters (e.g., `w/a/s/d`), they should navigate, not change input values. Inputs should\nbe edited via the virtual keyboard.\n\nNote: Stories do not emulate virtual keyboards, so letter-based navigation may change input values in Storybook.\n\n## Debugging\n\n### Storybook toolbar\n\nEnable \"Spatial navigation\" in the toolbar. Key mapping:\n- Up - ArrowUp\n- Left - ArrowLeft\n- Down - ArrowDown\n- Right - ArrowRight\n- Enter - Enter\n- Escape - Escape\n\nWith wrapper: wraps the component in a 3x3 grid with surrounding buttons for testing.\nWithout wrapper: renders the component alone.\n\n### Visual debugger\n\nWith spatial navigation enabled, press Shift + navigation key to visualize calculations:\n\n- Star: next active element\n- `#{number}`: candidate order by distance\n- `D: {distance}`: computed distance\n\n## Limitations\n\n### Completeness\n\nThe algorithm cannot guarantee reachability to all elements using the four directions. Some components can be isolated.\n\nWorkarounds:\n- Use data attributes to explicitly link navigation targets.\n- Arrange DOM to improve spatial consistency:\n- Group focusable elements using dedicated components (lists, menus, etc.).\n- Avoid complex grid-like layouts with variable-sized items.\n- Avoid overlap along horizontal or vertical axes.\n- Avoid nested focusable elements where possible.\n- Tune algorithm weights to match your UI layout.\n\n### Scrollable containers\n\nContent scrolling is not supported yet, e.g.:\n- Focused element larger than the viewport.\n- Scrollable content without interactive children.\n\n### Nested providers\n\nOnly one provider instance is supported in the application at a time.",
|
|
44929
44929
|
"name": "SpatialNavigationProvider",
|
|
44930
44930
|
"members": [
|
|
44931
44931
|
{
|
|
@@ -44950,6 +44950,33 @@
|
|
|
44950
44950
|
"privacy": "public",
|
|
44951
44951
|
"description": "Weights used in the distance calculation algorithm"
|
|
44952
44952
|
},
|
|
44953
|
+
{
|
|
44954
|
+
"kind": "method",
|
|
44955
|
+
"name": "getElementIdForDirectionAttr",
|
|
44956
|
+
"privacy": "private",
|
|
44957
|
+
"return": {
|
|
44958
|
+
"type": {
|
|
44959
|
+
"text": "string | undefined"
|
|
44960
|
+
}
|
|
44961
|
+
},
|
|
44962
|
+
"parameters": [
|
|
44963
|
+
{
|
|
44964
|
+
"name": "currentDomActiveElement",
|
|
44965
|
+
"type": {
|
|
44966
|
+
"text": "HTMLElement | null"
|
|
44967
|
+
},
|
|
44968
|
+
"description": "The current active element in the DOM"
|
|
44969
|
+
},
|
|
44970
|
+
{
|
|
44971
|
+
"name": "direction",
|
|
44972
|
+
"type": {
|
|
44973
|
+
"text": "Direction"
|
|
44974
|
+
},
|
|
44975
|
+
"description": "Direction"
|
|
44976
|
+
}
|
|
44977
|
+
],
|
|
44978
|
+
"description": "Check if the current active element has instruction to find the next focusable\nWe look for the element in all the shadow DOMs in the composed path of the active element,\nso mdc component can use this feature as well."
|
|
44979
|
+
},
|
|
44953
44980
|
{
|
|
44954
44981
|
"kind": "method",
|
|
44955
44982
|
"name": "goBack",
|
|
@@ -44961,6 +44988,47 @@
|
|
|
44961
44988
|
},
|
|
44962
44989
|
"description": "Handle back action\n\nEither trigger click on goBack element if any\notherwise call default go back handler"
|
|
44963
44990
|
},
|
|
44991
|
+
{
|
|
44992
|
+
"kind": "field",
|
|
44993
|
+
"name": "handleKeyDownBefore",
|
|
44994
|
+
"privacy": "private"
|
|
44995
|
+
},
|
|
44996
|
+
{
|
|
44997
|
+
"kind": "method",
|
|
44998
|
+
"name": "isDirectionKey",
|
|
44999
|
+
"return": {
|
|
45000
|
+
"type": {
|
|
45001
|
+
"text": "boolean"
|
|
45002
|
+
}
|
|
45003
|
+
},
|
|
45004
|
+
"parameters": [
|
|
45005
|
+
{
|
|
45006
|
+
"name": "key",
|
|
45007
|
+
"type": {
|
|
45008
|
+
"text": "string"
|
|
45009
|
+
}
|
|
45010
|
+
}
|
|
45011
|
+
],
|
|
45012
|
+
"description": "List of navigation keys"
|
|
45013
|
+
},
|
|
45014
|
+
{
|
|
45015
|
+
"kind": "method",
|
|
45016
|
+
"name": "isNavigationKey",
|
|
45017
|
+
"return": {
|
|
45018
|
+
"type": {
|
|
45019
|
+
"text": "boolean"
|
|
45020
|
+
}
|
|
45021
|
+
},
|
|
45022
|
+
"parameters": [
|
|
45023
|
+
{
|
|
45024
|
+
"name": "key",
|
|
45025
|
+
"type": {
|
|
45026
|
+
"text": "string"
|
|
45027
|
+
}
|
|
45028
|
+
}
|
|
45029
|
+
],
|
|
45030
|
+
"description": "List of navigation keys"
|
|
45031
|
+
},
|
|
44964
45032
|
{
|
|
44965
45033
|
"kind": "field",
|
|
44966
45034
|
"name": "navigationKeyMapping",
|
|
@@ -44970,12 +45038,6 @@
|
|
|
44970
45038
|
"privacy": "public",
|
|
44971
45039
|
"description": "Key mapping for spatial navigation actions\nIt is possible to map left/right/up/down/enter/escape actions to any valid key"
|
|
44972
45040
|
},
|
|
44973
|
-
{
|
|
44974
|
-
"kind": "field",
|
|
44975
|
-
"name": "navigationKeys",
|
|
44976
|
-
"description": "List of navigation keys",
|
|
44977
|
-
"readonly": true
|
|
44978
|
-
},
|
|
44979
45041
|
{
|
|
44980
45042
|
"kind": "method",
|
|
44981
45043
|
"name": "pressEnter",
|
|
@@ -45025,7 +45087,7 @@
|
|
|
45025
45087
|
"module": "/src/models"
|
|
45026
45088
|
},
|
|
45027
45089
|
"tagName": "mdc-spatialnavigationprovider",
|
|
45028
|
-
"jsDoc": "/**\n * This component manages focus using spatial navigation and provides context for child components.\n *\n * Place it at the root of the application.\n *\n * [Spatial navigation](https://en.wikipedia.org/wiki/Spatial_navigation) lets users move focus among\n * elements on a 2D plane, common on TVs and game consoles with remotes or gamepads.\n *\n * ## Focus management\n *\n * The provider listens to keyboard events and moves focus among elements based on arrow key input.\n * You can influence or override this behavior.\n *\n * Note: The algorithm is distance-based, so the UI should be designed so focusable elements are\n * predictably reachable. Relative element positions should remain stable; responsive layouts can\n * make navigation unpredictable. This is less of an issue on fixed-size TV UIs but can show unexpected\n * behavior in Storybook when resizing. See the \"Limitations\" section.\n *\n * ### Automatic\n *\n * By default, the next focus target is computed from element positions:\n *\n * 1. Find the nearest focus area (scrollable container or active focus trap) relative to the current element.\n * 2. Collect focusable elements in that area.\n * 3. Compute distances from the current element to candidates using the W3C \"find the shortest\n * distance\" algorithm: https://www.w3.org/TR/css-nav-1/#find-the-shortest-distance\n * 4. If no candidates are found, repeat from step 1, skipping areas already checked.\n * 5. Focus the closest candidate.\n *\n * Elements with `data-spatial-focusable` are treated as focusable even if they would otherwise not be\n * (e.g., `tabindex=\"-1\"`).\n *\n * Elements with `data-spatial-exclude` are excluded (with its subtree) from the navigation, even if they\n * are focusable.\n *\n * ### Overwrite next element\n *\n * Override automatic navigation by adding one of these attributes to a focusable element:\n *\n * - `data-spatial-up`\n * - `data-spatial-down`\n * - `data-spatial-left`\n * - `data-spatial-right`\n *\n * Each attribute value must be the id of the element to focus when the corresponding key is pressed.\n *\n * ### Element internal navigation\n *\n * Complex components (List, Combobox, Tree, etc.) may handle their own navigation. For example, a List moves\n * focus internally on Down until the last item, after which Down should fall back to provider navigation.\n *\n * To prevent the provider from handling a key, listen to `navbeforeprocess` and call `event.preventDefault()`.\n * This event fires after the component handles `keydown`.\n *\n * ### Cancel focus change\n *\n * Before focusing a computed target, the provider dispatches `navbeforefocus` on the current element. Call\n * `event.preventDefault()` on this event to cancel the focus change.\n *\n * ## Enter action\n *\n * Pressing Enter triggers `.click()` on the currently focused element.\n *\n * You can prevent this by listening to `navbeforeprocess` and calling `event.preventDefault()`.\n *\n * ## Escape/Back action\n *\n * Pressing Escape tries to find a focusable element with `data-spatial-go-back` and clicks it. If none exists,\n * the provider calls `history.back()`.\n *\n * You can prevent this by listening to `navbeforeprocess` and calling `event.preventDefault()`.\n *\n * You can also intercept the back click by listening to `navback` and calling `event.preventDefault()`.\n *\n * ## Control data attributes\n *\n * Supported data attributes:\n *\n * | Attribute | Value | Default | Description |\n * |------------------------|-------------|---------|-------------------------------------------------------------------------------------|\n * | `data-spatial-left` | element id | N/A | Focus this element when Left is pressed |\n * | `data-spatial-up` | element id | N/A | Focus this element when Up is pressed |\n * | `data-spatial-right` | element id | N/A | Focus this element when Right is pressed |\n * | `data-spatial-down` | element id | N/A | Focus this element when Down is pressed |\n * | `data-spatial-go-back` | N/A | N/A | First focusable element with this attribute is clicked on Back/Escape |\n * | `data-spatial-focusable` | N/A | N/A | Treat element as focusable even if it normally is not (e.g., `tabindex=\"-1\"`) |\n * | `data-spatial-exclude` | N/A | N/A | Exclude focusable element (and its subtree) from the navigation |\n *\n * ## Event emitting order\n *\n * On a navigation key press, events fire in this order:\n *\n * 1. `navbeforeprocess` on the currently focused element.\n * 2. If not prevented:\n * a. Arrow keys: `navbeforefocus` on the currently focused element.\n * b. Enter: `.click()` on the currently focused element.\n * c. Escape/Back: `navback` on the provider, then `.click()` on the go-back element or `history.back()`.\n * 3. If no target is found in the requested direction: `navnotarget` on the provider.\n *\n * ## Handle complex components\n *\n * ### Generic components\n *\n * Components that handle navigation internally should prevent the provider from acting. Handle `navbeforeprocess`\n * and call `event.preventDefault()` for keys you process yourself.\n *\n * ### Form inputs\n *\n * Native inputs often submit on Enter, which is not desirable here. Enter should toggle or activate the control\n * (e.g., check/uncheck). Provide a dedicated submit button users can navigate to and press Enter on.\n *\n * ### Utilities for complex components\n *\n * #### KeyToActionMixin\n *\n * Maps key events to action names. Call `getActionForKeyEvent` to get the action for a keyboard event. Also provides\n * `getKeyboardNavMode` to check whether navigation is spatial or default.\n *\n * #### KeyDownHandledMixin\n *\n * Notify the provider when a component handled `keydown` internally. Call `keyDownEventHandled` whenever you process\n * keydown yourself.\n *\n * ## Platform specific behaviors\n *\n * Consider remote/gamepad constraints. Often focus alone is not enough and users press Enter to \"enter\" an interactive mode:\n * - Select: Enter opens options rather than arrow keys opening a popover.\n * - Text inputs: see the next section.\n * - Slider: Enter to start adjusting, arrow keys to change value, Enter/Escape to stop.\n *\n * ### Text inputs\n *\n * On TV-like platforms without physical keyboards, Enter/focus on an input should open a virtual keyboard instead of submitting\n * the form. Users must close the keyboard (Escape) to continue spatial navigation.\n *\n * If navigation keys are mapped to letters (e.g., `w/a/s/d`), they should navigate, not change input values. Inputs should\n * be edited via the virtual keyboard.\n *\n * Note: Stories do not emulate virtual keyboards, so letter-based navigation may change input values in Storybook.\n *\n * ## Debugging\n *\n * ### Storybook toolbar\n *\n * Enable \"Spatial navigation\" in the toolbar. Key mapping:\n * - Up - ArrowUp\n * - Left - ArrowLeft\n * - Down - ArrowDown\n * - Right - ArrowRight\n * - Enter - Enter\n * - Escape - Escape\n *\n * With wrapper: wraps the component in a 3x3 grid with surrounding buttons for testing.\n * Without wrapper: renders the component alone.\n *\n * ### Visual debugger\n *\n * With spatial navigation enabled, press Shift + navigation key to visualize calculations:\n *\n * - Star: next active element\n * - `#{number}`: candidate order by distance\n * - `D: {distance}`: computed distance\n *\n * ## Limitations\n *\n * ### Completeness\n *\n * The algorithm cannot guarantee reachability to all elements using the four directions. Some components can be isolated.\n *\n * Workarounds:\n * - Use data attributes to explicitly link navigation targets.\n * - Arrange DOM to improve spatial consistency:\n * - Group focusable elements using dedicated components (lists, menus, etc.).\n * - Avoid complex grid-like layouts with variable-sized items.\n * - Avoid overlap along horizontal or vertical axes.\n * - Avoid nested focusable elements where possible.\n * - Tune algorithm weights to match your UI layout.\n *\n * ### Scrollable containers\n *\n * Content scrolling is not supported yet, e.g.:\n * - Focused element larger than the viewport.\n * - Scrollable content without interactive children.\n *\n * ### Nested providers\n *\n * Only one provider instance is supported in the application at a time.\n *\n * @event navbeforeprocess - (React: onNavBeforeProcess) This event dispatched before spatial navigation process any key event.\n * It can be canceled to prevent any action from spatial navigation, e.g.: back, click or calculating the next candidate.\n * @event navbeforefocus - (React: onNavBeforeFocus) This event is dispatched before the focus is changing to the next element.\n * It can be canceled to prevent the focus change. @see https://www.w3.org/TR/css-nav-1/#event-type-navbeforefocus\n * @event navback - (React: onNavBack) This event dispatched a back navigation triggered by the user.\n * The event's detail contains the goBackElement if any. It is cancelable to prevent click\n * action on the goBackElement.\n * @event navnotarget - (React: onNavNoTarget) This event is dispatched when there is no target to focus in the current focus area and\n * in the given direction .\n *\n * @tagname mdc-spatialnavigationprovider\n */",
|
|
45090
|
+
"jsDoc": "/**\n * Spatial navigation focus manager\n *\n * [Spatial navigation](https://en.wikipedia.org/wiki/Spatial_navigation) lets users move focus among\n * elements on a 2D plane, common on TVs and game consoles with remotes or gamepads.\n *\n * It should have only one instance and it should placed at the root of the application.\n *\n * ## Focus management\n *\n * The provider listens to keyboard events and moves focus among elements based on arrow key input.\n * You can influence or override this behavior.\n *\n * ### Steps\n *\n * Spatial navigation goes trough the following steps after each keydown:\n *\n * 1. Handle `keydown` in the capture phase.\n * When active element has `data-spatial-{direction}` attribute then prevent all component navigation and call the\n * provider own `keydown` handler (see step 3).\n * 2. Component own `keydown` handler executed (bubble phase) (e.g., list moves focus internally) it it was not\n * prevented.\n * 3. Spatial Navigation Provider's `keydown` handler executed (bubble phase)\n * - If key event was not prevented in step 1. emit `navbeforeprocess` to check if any component want to handle\n * the key event itself. If `navbeforeprocess` event is prevented, stop here.\n * - If the component did not handle `keydown`, it calculate the next focusable item\n * - if the active element has `data-spatial-{direction}` attribute, it will try to focus the element with the id.\n * - Otherwise calculate the next focused item based on the direction and distances.\n * - If there is no next item, it emits `navnotarget` event\n * - Otherwise emit `navbeforefocus`,\n * - If this event prevented, nothing happens\n * - Otherwise the focus moves to the next element\n *\n * ### Determine next focus\n *\n * The provider use multiple ways to determine the next focused element. The order defined in the \"Steps\" section.\n *\n * #### Calculated focus\n *\n * By default, the next focus target is computed from element positions:\n *\n * 1. Find the nearest focus area (scrollable container or active focus trap) relative to the current element.\n * 2. Collect focusable elements in that area.\n * 3. Compute distances from the current element to candidates using the W3C \"find the shortest\n * distance\" algorithm: https://www.w3.org/TR/css-nav-1/#find-the-shortest-distance\n * 4. If no candidates are found, repeat from step 1, skipping areas already checked.\n * 5. Focus the closest candidate.\n *\n * Elements with `data-spatial-focusable` are treated as focusable even if they would otherwise not be\n * (e.g., `tabindex=\"-1\"`).\n *\n * Elements with `data-spatial-exclude` are excluded (with its subtree) from the navigation, even if they\n * are focusable.\n *\n * Note: The algorithm is distance-based, so the UI should be designed to focusable elements are\n * predictably reachable. Relative element positions should remain stable; responsive layouts can\n * make navigation unpredictable. This is less of an issue on fixed-size TV UIs but can show unexpected\n * behavior in Storybook when resizing. See the \"Limitations\" section.\n *\n * #### Overwrite next element\n *\n * Override calculated navigation by adding one of these attributes to a focusable element:\n *\n * - `data-spatial-up`\n * - `data-spatial-down`\n * - `data-spatial-left`\n * - `data-spatial-right`\n *\n * Each attribute value must be the id of the element to focus when the corresponding key is pressed.\n *\n * #### Element internal navigation\n *\n * Complex components (List, Combobox, Tree, etc.) may handle their own navigation. For example, a List moves\n * focus internally on Down until the last item, after which Down should fall back to provider navigation.\n *\n * To prevent the provider from handling a key, listen to `navbeforeprocess` and call `event.preventDefault()`.\n * This event fires after the component handles `keydown`.\n *\n * ### Cancel focus change\n *\n * Before focusing a computed target, the provider dispatches `navbeforefocus` on the current element. Call\n * `event.preventDefault()` on this event to cancel the focus change.\n *\n * ## Enter action\n *\n * Pressing Enter triggers `.click()` on the currently focused element.\n *\n * You can prevent this by listening to `navbeforeprocess` and calling `event.preventDefault()`.\n *\n * ## Escape/Back action\n *\n * Pressing Escape tries to find a focusable element with `data-spatial-go-back` and clicks it. If none exists,\n * the provider calls `history.back()`.\n *\n * You can prevent this by listening to `navbeforeprocess` and calling `event.preventDefault()`.\n *\n * You can also intercept the back click by listening to `navback` and calling `event.preventDefault()`.\n *\n * ## Control data attributes\n *\n * Supported data attributes:\n *\n * | Attribute | Value | Default | Description |\n * |--------------------------|---------------------------|---------|-------------------------------------------------------------------------------|\n * | `data-spatial-left` | empty string / element id | N/A | Prevent native navigation in Left direction and focus element if exists |\n * | `data-spatial-up` | empty string / element id | N/A | Prevent native navigation in Up direction and focus element if exists |\n * | `data-spatial-right` | empty string / element id | N/A | Prevent native navigation in Right direction and focus element if exists |\n * | `data-spatial-down` | empty string / element id | N/A | Prevent native navigation in Down direction and focus element if exists |\n * | `data-spatial-go-back` | N/A | N/A | First focusable element with this attribute is clicked on Back/Escape |\n * | `data-spatial-focusable` | N/A | N/A | Treat element as focusable even if it normally is not (e.g., `tabindex=\"-1\"`) |\n * | `data-spatial-exclude` | N/A | N/A | Exclude focusable element (and its subtree) from the navigation |\n *\n * ## Event emitting order\n *\n * On a navigation key press, events fire in this order:\n *\n * 1. `navbeforeprocess` on the currently focused element.\n * 2. If not prevented:\n * a. Arrow keys: `navbeforefocus` on the currently focused element.\n * b. Enter: `.click()` on the currently focused element.\n * c. Escape/Back: `navback` on the provider, then `.click()` on the go-back element or `history.back()`.\n * 3. If no target is found in the requested direction: `navnotarget` on the provider.\n *\n * ## Handle complex components\n *\n * ### Generic components\n *\n * Components that handle navigation internally should prevent the provider from acting. Handle `navbeforeprocess`\n * and call `event.preventDefault()` for keys you process yourself.\n *\n * ### Form inputs\n *\n * Native inputs often submit on Enter, which is not desirable here. Enter should toggle or activate the control\n * (e.g., check/uncheck). Provide a dedicated submit button users can navigate to and press Enter on.\n *\n * ### Utilities for complex components\n *\n * #### KeyToActionMixin\n *\n * Maps key events to action names. Call `getActionForKeyEvent` to get the action for a keyboard event. Also provides\n * `getKeyboardNavMode` to check whether navigation is spatial or default.\n *\n * #### KeyDownHandledMixin\n *\n * Notify the provider when a component handled `keydown` internally. Call `keyDownEventHandled` whenever you process\n * keydown yourself.\n *\n * ## Platform specific behaviors\n *\n * Consider remote/gamepad constraints. Often focus alone is not enough and users press Enter to \"enter\" an interactive mode:\n * - Select: Enter opens options rather than arrow keys opening a popover.\n * - Text inputs: see the next section.\n * - Slider: Enter to start adjusting, arrow keys to change value, Enter/Escape to stop.\n *\n * ### Text inputs\n *\n * On TV-like platforms without physical keyboards, Enter/focus on an input should open a virtual keyboard instead of submitting\n * the form. Users must close the keyboard (Escape) to continue spatial navigation.\n *\n * If navigation keys are mapped to letters (e.g., `w/a/s/d`), they should navigate, not change input values. Inputs should\n * be edited via the virtual keyboard.\n *\n * Note: Stories do not emulate virtual keyboards, so letter-based navigation may change input values in Storybook.\n *\n * ## Debugging\n *\n * ### Storybook toolbar\n *\n * Enable \"Spatial navigation\" in the toolbar. Key mapping:\n * - Up - ArrowUp\n * - Left - ArrowLeft\n * - Down - ArrowDown\n * - Right - ArrowRight\n * - Enter - Enter\n * - Escape - Escape\n *\n * With wrapper: wraps the component in a 3x3 grid with surrounding buttons for testing.\n * Without wrapper: renders the component alone.\n *\n * ### Visual debugger\n *\n * With spatial navigation enabled, press Shift + navigation key to visualize calculations:\n *\n * - Star: next active element\n * - `#{number}`: candidate order by distance\n * - `D: {distance}`: computed distance\n *\n * ## Limitations\n *\n * ### Completeness\n *\n * The algorithm cannot guarantee reachability to all elements using the four directions. Some components can be isolated.\n *\n * Workarounds:\n * - Use data attributes to explicitly link navigation targets.\n * - Arrange DOM to improve spatial consistency:\n * - Group focusable elements using dedicated components (lists, menus, etc.).\n * - Avoid complex grid-like layouts with variable-sized items.\n * - Avoid overlap along horizontal or vertical axes.\n * - Avoid nested focusable elements where possible.\n * - Tune algorithm weights to match your UI layout.\n *\n * ### Scrollable containers\n *\n * Content scrolling is not supported yet, e.g.:\n * - Focused element larger than the viewport.\n * - Scrollable content without interactive children.\n *\n * ### Nested providers\n *\n * Only one provider instance is supported in the application at a time.\n *\n * @event navbeforeprocess - (React: onNavBeforeProcess) This event dispatched before spatial navigation process any key event.\n * It can be canceled to prevent any action from spatial navigation, e.g.: back, click or calculating the next candidate.\n * @event navbeforefocus - (React: onNavBeforeFocus) This event is dispatched before the focus is changing to the next element.\n * It can be canceled to prevent the focus change. @see https://www.w3.org/TR/css-nav-1/#event-type-navbeforefocus\n * @event navback - (React: onNavBack) This event dispatched a back navigation triggered by the user.\n * The event's detail contains the goBackElement if any. It is cancelable to prevent click\n * action on the goBackElement.\n * @event navnotarget - (React: onNavNoTarget) This event is dispatched when there is no target to focus in the current focus area and\n * in the given direction .\n *\n * @tagname mdc-spatialnavigationprovider\n */",
|
|
45029
45091
|
"customElement": true
|
|
45030
45092
|
}
|
|
45031
45093
|
],
|
|
@@ -2,24 +2,43 @@ import { type EventName } from '@lit/react';
|
|
|
2
2
|
import Component from '../../components/spatialnavigationprovider';
|
|
3
3
|
import type { Events } from '../../components/spatialnavigationprovider/spatialnavigationprovider.types';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* Place it at the root of the application.
|
|
5
|
+
* Spatial navigation focus manager
|
|
8
6
|
*
|
|
9
7
|
* [Spatial navigation](https://en.wikipedia.org/wiki/Spatial_navigation) lets users move focus among
|
|
10
8
|
* elements on a 2D plane, common on TVs and game consoles with remotes or gamepads.
|
|
11
9
|
*
|
|
10
|
+
* It should have only one instance and it should placed at the root of the application.
|
|
11
|
+
*
|
|
12
12
|
* ## Focus management
|
|
13
13
|
*
|
|
14
14
|
* The provider listens to keyboard events and moves focus among elements based on arrow key input.
|
|
15
15
|
* You can influence or override this behavior.
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
17
|
+
* ### Steps
|
|
18
|
+
*
|
|
19
|
+
* Spatial navigation goes trough the following steps after each keydown:
|
|
20
|
+
*
|
|
21
|
+
* 1. Handle `keydown` in the capture phase.
|
|
22
|
+
* When active element has `data-spatial-{direction}` attribute then prevent all component navigation and call the
|
|
23
|
+
* provider own `keydown` handler (see step 3).
|
|
24
|
+
* 2. Component own `keydown` handler executed (bubble phase) (e.g., list moves focus internally) it it was not
|
|
25
|
+
* prevented.
|
|
26
|
+
* 3. Spatial Navigation Provider's `keydown` handler executed (bubble phase)
|
|
27
|
+
* - If key event was not prevented in step 1. emit `navbeforeprocess` to check if any component want to handle
|
|
28
|
+
* the key event itself. If `navbeforeprocess` event is prevented, stop here.
|
|
29
|
+
* - If the component did not handle `keydown`, it calculate the next focusable item
|
|
30
|
+
* - if the active element has `data-spatial-{direction}` attribute, it will try to focus the element with the id.
|
|
31
|
+
* - Otherwise calculate the next focused item based on the direction and distances.
|
|
32
|
+
* - If there is no next item, it emits `navnotarget` event
|
|
33
|
+
* - Otherwise emit `navbeforefocus`,
|
|
34
|
+
* - If this event prevented, nothing happens
|
|
35
|
+
* - Otherwise the focus moves to the next element
|
|
21
36
|
*
|
|
22
|
-
* ###
|
|
37
|
+
* ### Determine next focus
|
|
38
|
+
*
|
|
39
|
+
* The provider use multiple ways to determine the next focused element. The order defined in the "Steps" section.
|
|
40
|
+
*
|
|
41
|
+
* #### Calculated focus
|
|
23
42
|
*
|
|
24
43
|
* By default, the next focus target is computed from element positions:
|
|
25
44
|
*
|
|
@@ -36,9 +55,14 @@ import type { Events } from '../../components/spatialnavigationprovider/spatialn
|
|
|
36
55
|
* Elements with `data-spatial-exclude` are excluded (with its subtree) from the navigation, even if they
|
|
37
56
|
* are focusable.
|
|
38
57
|
*
|
|
39
|
-
*
|
|
58
|
+
* Note: The algorithm is distance-based, so the UI should be designed to focusable elements are
|
|
59
|
+
* predictably reachable. Relative element positions should remain stable; responsive layouts can
|
|
60
|
+
* make navigation unpredictable. This is less of an issue on fixed-size TV UIs but can show unexpected
|
|
61
|
+
* behavior in Storybook when resizing. See the "Limitations" section.
|
|
62
|
+
*
|
|
63
|
+
* #### Overwrite next element
|
|
40
64
|
*
|
|
41
|
-
* Override
|
|
65
|
+
* Override calculated navigation by adding one of these attributes to a focusable element:
|
|
42
66
|
*
|
|
43
67
|
* - `data-spatial-up`
|
|
44
68
|
* - `data-spatial-down`
|
|
@@ -47,7 +71,7 @@ import type { Events } from '../../components/spatialnavigationprovider/spatialn
|
|
|
47
71
|
*
|
|
48
72
|
* Each attribute value must be the id of the element to focus when the corresponding key is pressed.
|
|
49
73
|
*
|
|
50
|
-
*
|
|
74
|
+
* #### Element internal navigation
|
|
51
75
|
*
|
|
52
76
|
* Complex components (List, Combobox, Tree, etc.) may handle their own navigation. For example, a List moves
|
|
53
77
|
* focus internally on Down until the last item, after which Down should fall back to provider navigation.
|
|
@@ -79,15 +103,15 @@ import type { Events } from '../../components/spatialnavigationprovider/spatialn
|
|
|
79
103
|
*
|
|
80
104
|
* Supported data attributes:
|
|
81
105
|
*
|
|
82
|
-
* | Attribute
|
|
83
|
-
*
|
|
84
|
-
* | `data-spatial-left`
|
|
85
|
-
* | `data-spatial-up`
|
|
86
|
-
* | `data-spatial-right`
|
|
87
|
-
* | `data-spatial-down`
|
|
88
|
-
* | `data-spatial-go-back`
|
|
89
|
-
* | `data-spatial-focusable` | N/A
|
|
90
|
-
* | `data-spatial-exclude`
|
|
106
|
+
* | Attribute | Value | Default | Description |
|
|
107
|
+
* |--------------------------|---------------------------|---------|-------------------------------------------------------------------------------|
|
|
108
|
+
* | `data-spatial-left` | empty string / element id | N/A | Prevent native navigation in Left direction and focus element if exists |
|
|
109
|
+
* | `data-spatial-up` | empty string / element id | N/A | Prevent native navigation in Up direction and focus element if exists |
|
|
110
|
+
* | `data-spatial-right` | empty string / element id | N/A | Prevent native navigation in Right direction and focus element if exists |
|
|
111
|
+
* | `data-spatial-down` | empty string / element id | N/A | Prevent native navigation in Down direction and focus element if exists |
|
|
112
|
+
* | `data-spatial-go-back` | N/A | N/A | First focusable element with this attribute is clicked on Back/Escape |
|
|
113
|
+
* | `data-spatial-focusable` | N/A | N/A | Treat element as focusable even if it normally is not (e.g., `tabindex="-1"`) |
|
|
114
|
+
* | `data-spatial-exclude` | N/A | N/A | Exclude focusable element (and its subtree) from the navigation |
|
|
91
115
|
*
|
|
92
116
|
* ## Event emitting order
|
|
93
117
|
*
|
|
@@ -3,24 +3,43 @@ import { createComponent } from '@lit/react';
|
|
|
3
3
|
import Component from '../../components/spatialnavigationprovider';
|
|
4
4
|
import { TAG_NAME } from '../../components/spatialnavigationprovider/spatialnavigationprovider.constants';
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* Place it at the root of the application.
|
|
6
|
+
* Spatial navigation focus manager
|
|
9
7
|
*
|
|
10
8
|
* [Spatial navigation](https://en.wikipedia.org/wiki/Spatial_navigation) lets users move focus among
|
|
11
9
|
* elements on a 2D plane, common on TVs and game consoles with remotes or gamepads.
|
|
12
10
|
*
|
|
11
|
+
* It should have only one instance and it should placed at the root of the application.
|
|
12
|
+
*
|
|
13
13
|
* ## Focus management
|
|
14
14
|
*
|
|
15
15
|
* The provider listens to keyboard events and moves focus among elements based on arrow key input.
|
|
16
16
|
* You can influence or override this behavior.
|
|
17
17
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
18
|
+
* ### Steps
|
|
19
|
+
*
|
|
20
|
+
* Spatial navigation goes trough the following steps after each keydown:
|
|
21
|
+
*
|
|
22
|
+
* 1. Handle `keydown` in the capture phase.
|
|
23
|
+
* When active element has `data-spatial-{direction}` attribute then prevent all component navigation and call the
|
|
24
|
+
* provider own `keydown` handler (see step 3).
|
|
25
|
+
* 2. Component own `keydown` handler executed (bubble phase) (e.g., list moves focus internally) it it was not
|
|
26
|
+
* prevented.
|
|
27
|
+
* 3. Spatial Navigation Provider's `keydown` handler executed (bubble phase)
|
|
28
|
+
* - If key event was not prevented in step 1. emit `navbeforeprocess` to check if any component want to handle
|
|
29
|
+
* the key event itself. If `navbeforeprocess` event is prevented, stop here.
|
|
30
|
+
* - If the component did not handle `keydown`, it calculate the next focusable item
|
|
31
|
+
* - if the active element has `data-spatial-{direction}` attribute, it will try to focus the element with the id.
|
|
32
|
+
* - Otherwise calculate the next focused item based on the direction and distances.
|
|
33
|
+
* - If there is no next item, it emits `navnotarget` event
|
|
34
|
+
* - Otherwise emit `navbeforefocus`,
|
|
35
|
+
* - If this event prevented, nothing happens
|
|
36
|
+
* - Otherwise the focus moves to the next element
|
|
22
37
|
*
|
|
23
|
-
* ###
|
|
38
|
+
* ### Determine next focus
|
|
39
|
+
*
|
|
40
|
+
* The provider use multiple ways to determine the next focused element. The order defined in the "Steps" section.
|
|
41
|
+
*
|
|
42
|
+
* #### Calculated focus
|
|
24
43
|
*
|
|
25
44
|
* By default, the next focus target is computed from element positions:
|
|
26
45
|
*
|
|
@@ -37,9 +56,14 @@ import { TAG_NAME } from '../../components/spatialnavigationprovider/spatialnavi
|
|
|
37
56
|
* Elements with `data-spatial-exclude` are excluded (with its subtree) from the navigation, even if they
|
|
38
57
|
* are focusable.
|
|
39
58
|
*
|
|
40
|
-
*
|
|
59
|
+
* Note: The algorithm is distance-based, so the UI should be designed to focusable elements are
|
|
60
|
+
* predictably reachable. Relative element positions should remain stable; responsive layouts can
|
|
61
|
+
* make navigation unpredictable. This is less of an issue on fixed-size TV UIs but can show unexpected
|
|
62
|
+
* behavior in Storybook when resizing. See the "Limitations" section.
|
|
63
|
+
*
|
|
64
|
+
* #### Overwrite next element
|
|
41
65
|
*
|
|
42
|
-
* Override
|
|
66
|
+
* Override calculated navigation by adding one of these attributes to a focusable element:
|
|
43
67
|
*
|
|
44
68
|
* - `data-spatial-up`
|
|
45
69
|
* - `data-spatial-down`
|
|
@@ -48,7 +72,7 @@ import { TAG_NAME } from '../../components/spatialnavigationprovider/spatialnavi
|
|
|
48
72
|
*
|
|
49
73
|
* Each attribute value must be the id of the element to focus when the corresponding key is pressed.
|
|
50
74
|
*
|
|
51
|
-
*
|
|
75
|
+
* #### Element internal navigation
|
|
52
76
|
*
|
|
53
77
|
* Complex components (List, Combobox, Tree, etc.) may handle their own navigation. For example, a List moves
|
|
54
78
|
* focus internally on Down until the last item, after which Down should fall back to provider navigation.
|
|
@@ -80,15 +104,15 @@ import { TAG_NAME } from '../../components/spatialnavigationprovider/spatialnavi
|
|
|
80
104
|
*
|
|
81
105
|
* Supported data attributes:
|
|
82
106
|
*
|
|
83
|
-
* | Attribute
|
|
84
|
-
*
|
|
85
|
-
* | `data-spatial-left`
|
|
86
|
-
* | `data-spatial-up`
|
|
87
|
-
* | `data-spatial-right`
|
|
88
|
-
* | `data-spatial-down`
|
|
89
|
-
* | `data-spatial-go-back`
|
|
90
|
-
* | `data-spatial-focusable` | N/A
|
|
91
|
-
* | `data-spatial-exclude`
|
|
107
|
+
* | Attribute | Value | Default | Description |
|
|
108
|
+
* |--------------------------|---------------------------|---------|-------------------------------------------------------------------------------|
|
|
109
|
+
* | `data-spatial-left` | empty string / element id | N/A | Prevent native navigation in Left direction and focus element if exists |
|
|
110
|
+
* | `data-spatial-up` | empty string / element id | N/A | Prevent native navigation in Up direction and focus element if exists |
|
|
111
|
+
* | `data-spatial-right` | empty string / element id | N/A | Prevent native navigation in Right direction and focus element if exists |
|
|
112
|
+
* | `data-spatial-down` | empty string / element id | N/A | Prevent native navigation in Down direction and focus element if exists |
|
|
113
|
+
* | `data-spatial-go-back` | N/A | N/A | First focusable element with this attribute is clicked on Back/Escape |
|
|
114
|
+
* | `data-spatial-focusable` | N/A | N/A | Treat element as focusable even if it normally is not (e.g., `tabindex="-1"`) |
|
|
115
|
+
* | `data-spatial-exclude` | N/A | N/A | Exclude focusable element (and its subtree) from the navigation |
|
|
92
116
|
*
|
|
93
117
|
* ## Event emitting order
|
|
94
118
|
*
|