UNPKG

wix-style-react

Version:
123 lines (119 loc) 4.46 kB
"use strict"; exports.__esModule = true; exports.getArrowShift = exports.detachClasses = exports.createComponentThatRendersItsChildren = exports.buildChildrenObject = exports.attachClasses = void 0; exports.getParentNode = getParentNode; exports.getScrollParent = getScrollParent; exports.shouldAnimatePopover = exports.noop = exports.isStatelessComponent = exports.isReactElement = void 0; var React = _interopRequireWildcard(require("react")); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } var isTestEnv = process.env.NODE_ENV === 'test'; function getParentNode(element) { if (element.nodeName === 'HTML') { return element; } // @ts-ignore return element.parentNode || element.host; } function getStyleComputedProperty(element) { // NOTE: 1 DOM access here var window = element.ownerDocument.defaultView; // @ts-ignore return window.getComputedStyle(element, null); } // @ts-ignore function getScrollParent(element) { // Return body, `getScroll` will take care to get the correct `scrollTop` from it if (!element) { return document.body; } switch (element.nodeName) { case 'HTML': case 'BODY': return element.ownerDocument.body; case '#document': // @ts-ignore return element.body; default: } // Firefox want us to check `-x` and `-y` variations as well var { overflow, overflowX, overflowY } = getStyleComputedProperty(element); if (/(auto|scroll|overlay)/.test(overflow + overflowY + overflowX)) { return element; } return getScrollParent(getParentNode(element)); } var buildChildrenObject = (children, childrenObject) => { return React.Children.toArray(children).reduce((acc, child) => { if (! /*#__PURE__*/React.isValidElement(child)) { return acc; } if (!child.type || !child.type.displayName) { return acc; } var name = child.type.displayName.split('.').pop(); // @ts-ignore acc[name] = child; return acc; }, childrenObject || {}); }; exports.buildChildrenObject = buildChildrenObject; var createComponentThatRendersItsChildren = displayName => { var Element = _ref => { var { children } = _ref; return typeof children === 'string' ? /*#__PURE__*/React.createElement('div', {}, children) : children; }; Element.displayName = displayName; return Element; }; exports.createComponentThatRendersItsChildren = createComponentThatRendersItsChildren; var noop = () => null; exports.noop = noop; var isReactElement = (child, Element) => { return child && child.type === Element; }; exports.isReactElement = isReactElement; var isStatelessComponent = Component => !(Component.prototype && Component.prototype.render); exports.isStatelessComponent = isStatelessComponent; var attachClasses = (node, classnames) => node && node.classList.add(...classnames.split(' ')); exports.attachClasses = attachClasses; var detachClasses = (node, classnames) => node && node.classList.remove(...classnames.split(' ')); exports.detachClasses = detachClasses; var shouldAnimatePopover = _ref2 => { var { timeout } = _ref2; if (typeof timeout === 'object') { var { enter, exit } = timeout; return typeof enter !== 'undefined' && typeof exit !== 'undefined' && (enter > 0 || exit > 0); } return !!timeout; }; exports.shouldAnimatePopover = shouldAnimatePopover; var getArrowShift = (shift, direction) => { if (!shift && !isTestEnv) { return {}; } if (direction.startsWith('top') || direction.startsWith('bottom')) { return { left: "".concat(shift, "px") }; } if (direction.startsWith('left') || direction.startsWith('right')) { return { top: "".concat(shift, "px") }; } // Arrow can't be shifted when using automatic positioning return {}; }; exports.getArrowShift = getArrowShift; //# sourceMappingURL=utils.js.map