UNPKG

@enact/ui

Version:

A collection of simplified unstyled cross-platform UI components for Enact

163 lines (158 loc) 8.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.Scrim = void 0; var _react = require("react"); var _propTypes = _interopRequireDefault(require("prop-types")); var _ScrimModule = _interopRequireDefault(require("./Scrim.module.css")); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["type"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } 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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : 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 _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } var transparentClassName = _ScrimModule["default"].scrim + ' enact-fit ' + _ScrimModule["default"].transparent; var translucentClassName = _ScrimModule["default"].scrim + ' enact-fit ' + _ScrimModule["default"].translucent; // Stores references to any Scrim instances whose type is translucent to ensure that only the top- // most Scrim is visible to avoid stacking scrims. var scrimStack = []; /** * Pushes a translucent scrim to the top of the stack and hiding the previously top * * @param {ui/FloatingLayer.Scrim} scrim A scrim instance to add * @returns {undefined} * @private */ function pushTranslucentScrim(scrim) { var last = scrimStack.length - 1; if (last >= 0) { // if there are other translucent scrims, hide the topmost one assuming the others have been // hidden correctly by previous calls scrimStack[last].hide(); } scrimStack.push(scrim); } /** * Removes a translucent scrim from the stack. If the scrim was the top-most, removing it will show * the next scrim. If not, it will just be removed * * @param {ui/FloatingLayer.Scrim} scrim A scrim instance to remove * @returns {undefined} * @private */ function removeTranslucentScrim(scrim) { var index = scrimStack.indexOf(scrim); var last = scrimStack.length - 1; if (index === last) { // if scrim is the top of the stack (most likely case), show the one below it then pop it scrimStack.pop(); if (scrimStack.length) { scrimStack[scrimStack.length - 1].show(); } } else { // if it's in the middle of the stack, just remove it scrimStack.splice(index, 1); } } /** * {@link ui/FloatingLayer.Scrim} provides an overlay that will prevent taps from propagating * to the controls that it covers. * * @class Scrim * @memberof ui/FloatingLayer * @ui * @private */ var Scrim = exports.Scrim = /*#__PURE__*/function (_Component) { function Scrim(props) { var _this; _classCallCheck(this, Scrim); _this = _callSuper(this, Scrim, [props]); _this.show = function () { return _this.setState({ visible: true }); }; _this.hide = function () { return _this.setState({ visible: false }); }; _this.state = { visible: true }; if (props.type === 'translucent') { pushTranslucentScrim(_this); } return _this; } _inherits(Scrim, _Component); return _createClass(Scrim, [{ key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (prevProps.type === 'translucent' && this.props.type !== 'translucent') { removeTranslucentScrim(this); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.props.type === 'translucent') { removeTranslucentScrim(this); } } }, { key: "render", value: function render() { if (this.state.visible) { var _this$props = this.props, type = _this$props.type, rest = _objectWithoutProperties(_this$props, _excluded); var className = type === 'transparent' ? transparentClassName : translucentClassName; return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({}, rest), {}, { className: className })); } return null; } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props) { if (props.type !== 'translucent') { return { visible: true }; } return null; } }]); }(_react.Component); Scrim.propTypes = /** @lends ui/FloatingLayer.Scrim.prototype */{ /** * Types of scrim. It can be either `'transparent'` or `'translucent'`. * * @type {String} * @default `translucent` * @public */ type: _propTypes["default"].oneOf(['transparent', 'translucent']) }; Scrim.defaultProps = { type: 'translucent' }; var _default = exports["default"] = Scrim;