UNPKG

@momentum-ui/react-collaboration

Version:

Cisco Momentum UI Framework for React Collaboration Applications

73 lines 3.93 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; }; import React, { useRef, useState, useEffect, useCallback } from 'react'; import classnames from 'classnames'; import { DEFAULTS, STYLE } from './AriaToolbar.constants'; import ButtonGroup from '../ButtonGroup'; import { AriaToolbarContext } from './AriaToolbar.utils'; import { getKeyboardFocusableElements } from '../../utils/navigation'; /** * The AriaToolbar component. A style-less by default or button-group styled component implementing the Aria Toolbar pattern * see https://www.w3.org/WAI/ARIA/apg/patterns/toolbar */ var AriaToolbar = function (props) { var className = props.className, id = props.id, style = props.style, children = props.children, _a = props.orientation, orientation = _a === void 0 ? DEFAULTS.ORIENTATION : _a, _b = props.shouldRenderAsButtonGroup, shouldRenderAsButtonGroup = _b === void 0 ? DEFAULTS.SHOULD_RENDER_AS_BUTTON_GROUP : _b, onTabPress = props.onTabPress, buttonGroupProps = props.buttonGroupProps, ariaToolbarItemsSize = props.ariaToolbarItemsSize, rest = __rest(props, ["className", "id", "style", "children", "orientation", "shouldRenderAsButtonGroup", "onTabPress", "buttonGroupProps", "ariaToolbarItemsSize"]); var _c = useState(undefined), currentFocus = _c[0], setCurrentFocus = _c[1]; var buttonRefs = useRef({}); var ref = useRef(null); useEffect(function () { var _a; (_a = buttonRefs.current[currentFocus]) === null || _a === void 0 ? void 0 : _a.focus(); }, [currentFocus]); var getContext = useCallback(function () { return ({ currentFocus: currentFocus, setCurrentFocus: setCurrentFocus, orientation: orientation, onTabPress: onTabPress, ariaToolbarItemsSize: ariaToolbarItemsSize, buttonRefs: buttonRefs, }); }, [currentFocus, setCurrentFocus, orientation, onTabPress, ariaToolbarItemsSize, buttonRefs]); var commonProps = __assign({ className: classnames(className, STYLE.wrapper), id: id, style: style, role: 'toolbar', 'aria-orientation': orientation }, rest); var renderBody = function () { if (shouldRenderAsButtonGroup) { return (React.createElement(ButtonGroup, __assign({ ref: ref }, buttonGroupProps, commonProps, { orientation: orientation }), children)); } else { return (React.createElement("div", __assign({ ref: ref }, commonProps), children)); } }; useEffect(function () { // When the toolbar is rendered inside a list, only the first item in the toolbar // should be focusable. This is to preserve the tab order as the // List uses a roving tab index. getKeyboardFocusableElements(ref.current, { includeTabbableOnly: false }).forEach(function (el, index) { if (index === 0) { return; } el.setAttribute('data-exclude-focus', 'true'); }); }, [ref]); return (React.createElement(AriaToolbarContext.Provider, { value: getContext() }, renderBody())); }; export default AriaToolbar; //# sourceMappingURL=AriaToolbar.js.map