UNPKG

@react-md/menu

Version:

Create menus that auto-position themselves within the viewport and adhere to the accessibility guidelines

78 lines 3.18 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; import { jsx as _jsx } from "react/jsx-runtime"; import { createContext, useContext, useState } from "react"; import { List } from "@react-md/list"; import { useKeyboardFocus } from "@react-md/utils"; import { useMenuBarContext } from "./MenuBarProvider"; /** * This context is used to implement the "roving tab index" behavior * * @internal * @remarks \@since 5.0.0 */ var context = createContext(""); context.displayName = "MenuBarWidgetFocusId"; var Provider = context.Provider; /** @remarks \@since 5.0.0 */ export function useMenuBarWidgetFocusId() { return useContext(context); } /** * This component implements the keyboard focus behavior for the `MenuBar` * component and probably shouldn't be used externally. * * @remarks \@since 5.0.0 */ export function MenuBarWidget(_a) { var children = _a.children, tabIndex = _a.tabIndex, propOnFocus = _a.onFocus, propOnKeyDown = _a.onKeyDown, props = __rest(_a, ["children", "tabIndex", "onFocus", "onKeyDown"]); var setActiveId = useMenuBarContext().setActiveId; var _b = __read(useState(""), 2), focusId = _b[0], setFocusId = _b[1]; var _c = useKeyboardFocus({ onFocus: propOnFocus, onKeyDown: propOnKeyDown, onFocusChange: function (element) { element.focus(); setFocusId(element.id); setActiveId(function (prevActiveId) { return (prevActiveId ? element.id : ""); }); }, }), onFocus = _c.onFocus, onKeyDown = _c.onKeyDown; return (_jsx(Provider, __assign({ value: focusId }, { children: _jsx(List, __assign({}, props, { role: "menubar", horizontal: true, tabIndex: tabIndex !== null && tabIndex !== void 0 ? tabIndex : (focusId ? -1 : 0), onFocus: onFocus, onKeyDown: onKeyDown }, { children: children })) }))); } //# sourceMappingURL=MenuBarWidget.js.map