UNPKG

baseui

Version:

A React Component library implementing the Base design language

154 lines (151 loc) • 6.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _popper = _interopRequireDefault(require("popper.js")); var _utils = require("./utils"); var _constants = require("./constants"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } 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); } /* Copyright (c) Uber Technologies, Inc. This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree. */ class Tether extends React.Component { constructor(...args) { super(...args); _defineProperty(this, "popper", void 0); _defineProperty(this, "popperHeight", 0); _defineProperty(this, "popperWidth", 0); _defineProperty(this, "anchorHeight", 0); _defineProperty(this, "anchorWidth", 0); _defineProperty(this, "state", { isMounted: false }); _defineProperty(this, "onPopperUpdate", data => { const normalizedOffsets = { popper: (0, _utils.parsePopperOffset)(data.offsets.popper), arrow: data.offsets.arrow ? (0, _utils.parsePopperOffset)(data.offsets.arrow) : { top: 0, left: 0 } }; this.props.onPopperUpdate(normalizedOffsets, data); }); } componentDidMount() { this.setState({ isMounted: true }); } componentDidUpdate(prevProps, prevState) { // Handles the case where popover content changes size and creates a gap between the anchor and // the popover. Popper.js only schedules updates on resize and scroll events. In the case of // the Select component, when options were filtered in the dropdown menu it creates a gap // between it and the input element. if (this.props.anchorRef) { const { height, width } = this.props.anchorRef.getBoundingClientRect(); if (this.anchorHeight !== height || this.anchorWidth !== width) { this.anchorHeight = height; this.anchorWidth = width; this.popper && this.popper.scheduleUpdate(); } } if (this.props.popperRef) { const { height, width } = this.props.popperRef.getBoundingClientRect(); if (this.popperHeight !== height || this.popperWidth !== width) { this.popperHeight = height; this.popperWidth = width; this.popper && this.popper.scheduleUpdate(); } if (this.state.isMounted !== prevState.isMounted) { if (!this.props.anchorRef) { if (process.env.NODE_ENV !== "production") { // eslint-disable-next-line no-console console.warn(`[baseui][TetherBehavior] ref has not been passed to the Popper's anchor element. See how to pass the ref to an anchor element in the Popover example https://baseweb.design/components/popover/#anchor-ref-handling-example`); } } else { this.initializePopper(); } } } } componentWillUnmount() { this.destroyPopover(); } initializePopper() { const { placement, popperOptions } = this.props; const { modifiers, ...restOptions } = popperOptions; if (!this.props.anchorRef || !this.props.popperRef) return; this.popper = new _popper.default(this.props.anchorRef, this.props.popperRef, { // Recommended placement (popper may ignore if it causes a viewport overflow, etc) placement: (0, _utils.toPopperPlacement)(placement), modifiers: { // Passing the arrow ref will measure the arrow when calculating styles arrow: { element: this.props.arrowRef, enabled: !!this.props.arrowRef }, computeStyle: { // Make popper use top/left instead of transform translate, this is because // we use transform for animations and we dont want them to conflict gpuAcceleration: false }, applyStyle: { // Disable default styling modifier, we'll apply styles on our own enabled: false }, applyReactStyle: { enabled: true, fn: this.onPopperUpdate, order: 900 }, preventOverflow: { enabled: true }, ...modifiers }, ...restOptions }); } destroyPopover() { if (this.popper) { this.popper.destroy(); delete this.popper; } } render() { return this.props.children || null; } } _defineProperty(Tether, "defaultProps", { // @ts-ignore anchorRef: null, // @ts-ignore onPopperUpdate: () => null, placement: _constants.TETHER_PLACEMENT.auto, // @ts-ignore popperRef: null, popperOptions: {} }); var _default = exports.default = Tether;