UNPKG

@enact/sandstone

Version:

Large-screen/TV support library for Enact, containing a variety of UI components.

97 lines (94 loc) 6.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNavigableFilter = exports["default"] = exports.RefocusDecorator = void 0; exports.getTabsSpotlightId = getTabsSpotlightId; var _spotlight = _interopRequireDefault(require("@enact/spotlight")); var _IdProvider = require("@enact/ui/internal/IdProvider"); var _propTypes = _interopRequireDefault(require("prop-types")); var _react = require("react"); var _TabGroupModule = _interopRequireDefault(require("./TabGroup.module.css")); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["collapsed", "index", "onTabAnimationEnd", "orientation", "spotlightId"]; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function getTabsSpotlightId(spotlightId, collapsed) { return "".concat(spotlightId, "-tabs-").concat(collapsed ? 'collapsed' : 'expanded'); } function getContainerNode(containerId) { return document.querySelector("[data-spotlight-id='".concat(containerId, "']")); } var getNavigableFilter = exports.getNavigableFilter = function getNavigableFilter(spotlightId, collapsed) { return function (elem) { return _spotlight["default"].getPointerMode() || !elem.classList.contains(_TabGroupModule["default"].tab) && elem.dataset.spotlightId !== getTabsSpotlightId(spotlightId, collapsed); }; }; var RefocusDecorator = exports.RefocusDecorator = function RefocusDecorator(Wrapped) { // eslint-disable-next-line no-shadow function RefocusDecorator(_ref) { var collapsed = _ref.collapsed, index = _ref.index, onTabAnimationEnd = _ref.onTabAnimationEnd, orientation = _ref.orientation, spotlightId = _ref.spotlightId, rest = _objectWithoutProperties(_ref, _excluded); var _useId = (0, _IdProvider.useId)({ prefix: 'sand-tablayout-' }), generateId = _useId.generateId; // generate an id for the component (and a derived id for the tabs) so we can refocus them // generating a different ID by orientation so swapping orientations doesn't clear container // config before the new one is mounted spotlightId = spotlightId || generateId(orientation || 'vertical'); (0, _react.useLayoutEffect)(function () { if (!_spotlight["default"].getPointerMode() && !_spotlight["default"].isPaused()) { var current = _spotlight["default"].getCurrent(), tabsSpotlightId = getTabsSpotlightId(spotlightId, collapsed), containerNode = getContainerNode(tabsSpotlightId); if (!current || containerNode && containerNode.querySelector(".".concat(_TabGroupModule["default"].selected)) !== current) { _spotlight["default"].focus(spotlightId); } } }, [index]); // eslint-disable-line react-hooks/exhaustive-deps (0, _react.useEffect)(function () { _spotlight["default"].set(spotlightId, { navigableFilter: collapsed && orientation !== 'horizontal' ? getNavigableFilter(spotlightId, collapsed) : null }); }, [collapsed, orientation, spotlightId]); var handleTabAnimationEnd = (0, _react.useCallback)(function (ev) { if (onTabAnimationEnd) { onTabAnimationEnd(ev); } if (!collapsed && !_spotlight["default"].getPointerMode() && !_spotlight["default"].isPaused()) { var tabsSpotlightId = getTabsSpotlightId(spotlightId, collapsed); var containerNode = getContainerNode(tabsSpotlightId); if (containerNode && !containerNode.contains(_spotlight["default"].getCurrent())) { _spotlight["default"].focus(tabsSpotlightId); } } }, [collapsed, onTabAnimationEnd, spotlightId]); return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread(_objectSpread({}, rest), {}, { collapsed: collapsed, index: index, onTabAnimationEnd: handleTabAnimationEnd, orientation: orientation, spotlightId: spotlightId })); } RefocusDecorator.propTypes = { collapsed: _propTypes["default"].bool, index: _propTypes["default"].number, onTabAnimationEnd: _propTypes["default"].func, orientation: _propTypes["default"].string, spotlightId: _propTypes["default"].string }; return RefocusDecorator; }; var _default = exports["default"] = RefocusDecorator;