UNPKG

wix-style-react

Version:
192 lines (156 loc) • 8.44 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _excluded = ["children", "width", "content", "appearance"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import React from 'react'; import PropTypes from 'prop-types'; import ClosablePopover from './ClosablePopover/ClosablePopover'; import FloatingHelperContent from './FloatingHelperContent/FloatingHelperContent'; import { classes } from './FloatingHelper.st.css'; import { dataHooks, floatingHelperAppearance } from './constants'; import CloseButton from '../CloseButton'; import { SKINS as CloseButtonSkins, SIZES as CloseButtonSizes } from '../CloseButton/constants'; var FloatingHelper = /*#__PURE__*/function (_React$Component) { _inherits(FloatingHelper, _React$Component); var _super = _createSuper(FloatingHelper); function FloatingHelper() { var _this; _classCallCheck(this, FloatingHelper); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "closablePopoverRef", void 0); _defineProperty(_assertThisInitialized(_this), "open", function () { return _this.closablePopoverRef.open(); }); _defineProperty(_assertThisInitialized(_this), "close", function () { return _this.closablePopoverRef.close(); }); return _this; } _createClass(FloatingHelper, [{ key: "_isControlled", value: function _isControlled() { return this.props.opened !== undefined; } }, { key: "_getCloseButtonHandler", value: function _getCloseButtonHandler(closableActions) { return this._isControlled() ? this.props.onClose ? this.props.onClose : function () { return null; } : closableActions.close; } }, { key: "_renderContent", value: function _renderContent(closableActions, _ref) { var _this2 = this; var width = _ref.width, content = _ref.content, appearance = _ref.appearance; return /*#__PURE__*/React.createElement("div", { "data-hook": dataHooks.contentWrapper, style: { width: width } }, /*#__PURE__*/React.createElement("div", { "data-hook": dataHooks.innerContent, className: classes.innerContent }, /*#__PURE__*/React.createElement(CloseButton, { className: classes.closeButton, dataHook: dataHooks.closeButton, onClick: function onClick() { return _this2._getCloseButtonHandler(closableActions)(); }, skin: appearance === floatingHelperAppearance.dark ? CloseButtonSkins.light : CloseButtonSkins.dark, size: CloseButtonSizes.medium }), /*#__PURE__*/React.cloneElement(content, { appearance: appearance }))); } }, { key: "render", value: function render() { var _this3 = this; var _this$props = this.props, children = _this$props.children, width = _this$props.width, content = _this$props.content, appearance = _this$props.appearance, rest = _objectWithoutProperties(_this$props, _excluded); var renderContent = function renderContent(closableActions) { return _this3._renderContent(closableActions, { width: width, content: content, appearance: appearance }); }; var closablePopoverProps = _objectSpread(_objectSpread({}, rest), {}, { appearance: appearance, content: renderContent, showArrow: true, closeOnMouseLeave: false }); return /*#__PURE__*/React.createElement(ClosablePopover, _extends({}, closablePopoverProps, { ref: function ref(_ref2) { return _this3.closablePopoverRef = _ref2; }, className: classes.root })); } }]); return FloatingHelper; }(React.Component); FloatingHelper.displayName = 'FloatingHelper'; FloatingHelper.propTypes = { /** Applied as data-hook HTML attribute that can be used in the tests */ dataHook: PropTypes.string, /** Width HTML attribute of the content. If a number is passed then it defaults to px. e.g width={400} => width="400px" */ width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), /** The target of the popover */ target: PropTypes.node.isRequired, /** A `<FloatingHelper.Content>` component */ content: PropTypes.node.isRequired, /** callback to call when the popover content is requested to be closed (Uncontrolled mode only). NOTE: this callback is called when the close timeout (if exists) starts In Controlled mode - called when the close button is clicked. In Uncontrolled mode - called when the popover is closed */ onClose: PropTypes.func, /** Appearance : `dark` or `light` */ appearance: PropTypes.oneOf(['dark', 'light']), /** Controls whether the popover's content is initially opened (In Uncontrolled mode only) */ initiallyOpened: PropTypes.bool, /** Controls whether the popover's content is shown or not (aka Controlled mode). * When undefined, then the component is Uncontrolled. See open/close behaviour section in docs. */ opened: PropTypes.bool, /** The location to display the content. */ placement: PropTypes.oneOf(['auto-start', 'auto', 'auto-end', 'top-start', 'top', 'top-end', 'right-start', 'right', 'right-end', 'bottom-end', 'bottom', 'bottom-start', 'left-end', 'left', 'left-start']).isRequired, /** Enables calculations in relation to a dom element. */ appendTo: PropTypes.oneOf(['scrollParent', 'viewport', 'window']), /** Callback to call when the popover content is requested to be opened (Uncontrolled mode only)*/ onOpen: PropTypes.func, /** Moves the floating helper relative to the parent by x or y */ moveBy: PropTypes.shape({ x: PropTypes.number, y: PropTypes.number }), /** Floating helper z-index */ zIndex: PropTypes.number, /** Set a delay on closing */ hideDelay: PropTypes.number }; FloatingHelper.defaultProps = { appendTo: 'window', width: '444px', initiallyOpened: true, appearance: 'dark' }; FloatingHelper.Content = FloatingHelperContent; export default FloatingHelper;