@enact/sandstone
Version:
Large-screen/TV support library for Enact, containing a variety of UI components.
150 lines (148 loc) • 7.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PageViewsRouter = PageViewsRouter;
exports["default"] = void 0;
var _propTypes = _interopRequireDefault(require("@enact/core/internal/prop-types"));
var _useChainRefs = _interopRequireDefault(require("@enact/core/useChainRefs"));
var _propTypes2 = _interopRequireDefault(require("prop-types"));
var _react = require("react");
var _Panels = require("../internal/Panels");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["autoFocus", "children", "componentRef", "data-spotlight-id", "index", "onTransition", "onWillTransition", "rtl"],
_excluded2 = ["onWillTransition"]; // single-index ViewManagers need some help knowing when the transition direction needs to change
// because the index is always 0 from its perspective.
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 useReverseTransition(index, rtl) {
var prevIndex = (0, _react.useRef)(index);
var reverse = false;
if (prevIndex.current !== index) {
reverse = rtl ? index > prevIndex.current : index < prevIndex.current;
}
prevIndex.current = index;
return {
reverseTransition: reverse
};
}
/**
* PageViewsRouter passes children, index and transition handlers
*
* @class PageViewsRouter
* @memberof sandstone/PageViews
* @private
*/
function PageViewsRouter(Wrapped) {
var PageViewsProvider = function PageViewsProvider(_ref) {
var autoFocus = _ref.autoFocus,
children = _ref.children,
componentRef = _ref.componentRef,
spotlightId = _ref['data-spotlight-id'],
_ref$index = _ref.index,
index = _ref$index === void 0 ? 0 : _ref$index,
onTransition = _ref.onTransition,
onWillTransition = _ref.onWillTransition,
rtl = _ref.rtl,
rest = _objectWithoutProperties(_ref, _excluded);
var totalIndex = _react.Children.count(children);
var _useToggleRole = (0, _Panels.useToggleRole)(),
a11yRef = _useToggleRole.ref,
a11yOnWillTransition = _useToggleRole.onWillTransition;
var autoFocusRef = (0, _Panels.useAutoFocus)({
autoFocus: autoFocus
});
var ref = (0, _useChainRefs["default"])(autoFocusRef, a11yRef, componentRef);
var _useReverseTransition = useReverseTransition(index, rtl),
reverseTransition = _useReverseTransition.reverseTransition;
var _useFocusOnTransition = (0, _Panels.useFocusOnTransition)({
onTransition: onTransition,
onWillTransition: onWillTransition,
spotlightId: spotlightId
}),
focusOnWillTransition = _useFocusOnTransition.onWillTransition,
transition = _objectWithoutProperties(_useFocusOnTransition, _excluded2);
var handleWillTransition = (0, _react.useCallback)(function (ev) {
focusOnWillTransition(ev);
a11yOnWillTransition(ev);
}, [a11yOnWillTransition, focusOnWillTransition]);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread(_objectSpread(_objectSpread({}, rest), transition), {}, {
componentRef: ref,
"data-spotlight-id": spotlightId,
index: index,
totalIndex: totalIndex,
onWillTransition: handleWillTransition,
reverseTransition: reverseTransition,
children: children
}));
};
PageViewsProvider.propTypes = /** @lends sandstone/PageViews.PageViewsRouter.prototype */{
/**
* Sets the strategy used to automatically focus an element within the PageViews upon render.
* When set to 'none', focus is not set only on the first render.
*
* @type {('default-element'|'last-focused'|'none'|String)}
* @default 'last-focused'
* @private
*/
autoFocus: _propTypes2["default"].string,
/**
* Obtains a reference to the root node.
*
* @type {Function|Object}
* @private
*/
componentRef: _propTypes["default"].ref,
/**
* The spotlight id for the panel.
*
* @type {String}
* @private
*/
'data-spotlight-id': _propTypes2["default"].string,
/**
* The currently selected step.
*
* @type {Number}
* @default 0
* @private
*/
index: _propTypes2["default"].number,
/**
* Disables panel transitions.
*
* @type {Boolean}
* @public
*/
noAnimation: _propTypes2["default"].bool,
/**
* Called when a transition completes.
*
* @type {Function}
* @private
*/
onTransition: _propTypes2["default"].func,
/**
* Called when a transition begins.
*
* @type {Function}
* @private
*/
onWillTransition: _propTypes2["default"].func,
/**
* Used to determine the transition direction.
*
* @type {Boolean}
* @private
*/
rtl: _propTypes2["default"].bool
};
return PageViewsProvider;
}
var _default = exports["default"] = PageViewsRouter;