UNPKG

@react-md/menu

Version:

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

83 lines 3.48 kB
"use strict"; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MenuBarWidget = exports.useMenuBarWidgetFocusId = void 0; var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = require("react"); var list_1 = require("@react-md/list"); var utils_1 = require("@react-md/utils"); var MenuBarProvider_1 = require("./MenuBarProvider"); /** * This context is used to implement the "roving tab index" behavior * * @internal * @remarks \@since 5.0.0 */ var context = (0, react_1.createContext)(""); context.displayName = "MenuBarWidgetFocusId"; var Provider = context.Provider; /** @remarks \@since 5.0.0 */ function useMenuBarWidgetFocusId() { return (0, react_1.useContext)(context); } exports.useMenuBarWidgetFocusId = useMenuBarWidgetFocusId; /** * This component implements the keyboard focus behavior for the `MenuBar` * component and probably shouldn't be used externally. * * @remarks \@since 5.0.0 */ 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 = (0, MenuBarProvider_1.useMenuBarContext)().setActiveId; var _b = __read((0, react_1.useState)(""), 2), focusId = _b[0], setFocusId = _b[1]; var _c = (0, utils_1.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 ((0, jsx_runtime_1.jsx)(Provider, __assign({ value: focusId }, { children: (0, jsx_runtime_1.jsx)(list_1.List, __assign({}, props, { role: "menubar", horizontal: true, tabIndex: tabIndex !== null && tabIndex !== void 0 ? tabIndex : (focusId ? -1 : 0), onFocus: onFocus, onKeyDown: onKeyDown }, { children: children })) }))); } exports.MenuBarWidget = MenuBarWidget; //# sourceMappingURL=MenuBarWidget.js.map