UNPKG

@enact/ui

Version:

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

165 lines (160 loc) 9.4 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(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 _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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } 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 _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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) { _inherits(Scrim, _Component); var _super = _createSuper(Scrim); function Scrim(props) { var _this; _classCallCheck(this, Scrim); _this = _super.call(this, 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(_assertThisInitialized(_this)); } return _this; } _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; } }]); return Scrim; }(_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;