@enact/sandstone
Version:
Large-screen/TV support library for Enact, containing a variety of UI components.
95 lines (91 loc) • 6.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.AutoFocusDecorator = void 0;
exports.useAutoFocus = useAutoFocus;
var _hoc = _interopRequireDefault(require("@enact/core/hoc"));
var _propTypes = _interopRequireDefault(require("@enact/core/internal/prop-types"));
var _useChainRefs = _interopRequireDefault(require("@enact/core/useChainRefs"));
var _spotlight = _interopRequireDefault(require("@enact/spotlight"));
var _propTypes2 = _interopRequireDefault(require("prop-types"));
var _react = require("react");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["autoFocus", "componentRef", "hideChildren"];
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; }
var isSelector = function isSelector(autoFocus) {
return autoFocus && autoFocus !== 'last-focused' && autoFocus !== 'default-element' && autoFocus !== 'none';
};
function configureContainer(ref, autoFocus, spotlightId) {
if (ref.current.id === spotlightId && ref.current.autoFocus === autoFocus) return;
ref.current.id = spotlightId;
ref.current.autoFocus = autoFocus;
// If autoFocus is a selector, we're using default-element but need to update the selector
// for that element in the container config
if (isSelector(autoFocus)) {
_spotlight["default"].set(spotlightId, {
defaultElement: autoFocus
});
}
}
function useAutoFocus(_ref) {
var _ref$autoFocus = _ref.autoFocus,
autoFocus = _ref$autoFocus === void 0 ? 'last-focused' : _ref$autoFocus,
hideChildren = _ref.hideChildren;
var ref = (0, _react.useRef)({
id: null,
autoFocus: null
});
return (0, _react.useCallback)(function (node) {
if (!node) return;
// FIXME: This is a candidate to move to the decorator once hooks have been fully
// adopted and we can configure SpotlightContainerDecorator with the current props
var spotlightId = node.dataset.spotlightId;
configureContainer(ref, autoFocus, spotlightId);
// In order to spot the body components, we defer spotting until !hideChildren. If the
// Panel opts out of hideChildren support by explicitly setting it to false, it'll spot
// on first render.
if (!hideChildren && autoFocus !== 'none' && !_spotlight["default"].getCurrent() && !_spotlight["default"].isPaused()) {
// For the purpose of imperatively focusing the Panel contents, we find the target
// within the panel using a (currently) private Spotlight API with the enterTo parameter
// to influence which configuration is used to find said target.
var enterTo = isSelector(autoFocus) || autoFocus === 'default-element' ? 'default-element' : 'last-focused';
_spotlight["default"].focus(spotlightId, {
enterTo: enterTo
});
}
}, [autoFocus, hideChildren, ref]);
}
var AutoFocusDecorator = exports.AutoFocusDecorator = (0, _hoc["default"])(function (config, Wrapped) {
// eslint-disable-next-line no-shadow
function AutoFocusDecorator(_ref2) {
var _ref2$autoFocus = _ref2.autoFocus,
autoFocus = _ref2$autoFocus === void 0 ? 'last-focused' : _ref2$autoFocus,
componentRef = _ref2.componentRef,
hideChildren = _ref2.hideChildren,
rest = _objectWithoutProperties(_ref2, _excluded);
var hook = useAutoFocus({
autoFocus: autoFocus,
hideChildren: hideChildren
});
var ref = (0, _useChainRefs["default"])(componentRef, hook);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread(_objectSpread({}, rest), {}, {
componentRef: ref,
hideChildren: hideChildren
}));
}
AutoFocusDecorator.propTypes = {
autoFocus: _propTypes2["default"].string,
componentRef: _propTypes["default"].ref,
hideChildren: _propTypes2["default"].bool
};
return AutoFocusDecorator;
});
var _default = exports["default"] = useAutoFocus;