Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 9x 9x 9x 9x 9x 9x 9x 1x 9x 9x 9x 1x 1x 1x 1x 1x 18x | /*
Copyright (c) 2018-2019 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.
*/
// @flow
import * as React from 'react';
import Popper from 'popper.js';
import {toPopperPlacement, parsePopperOffset} from './utils.js';
import {TETHER_PLACEMENT} from './constants.js';
import type {TetherPropsT, TetherStateT, PopperDataObjectT} from './types.js';
class Tether extends React.Component<TetherPropsT, TetherStateT> {
static defaultProps = {
anchorRef: null,
onPopperUpdate: () => null,
placement: TETHER_PLACEMENT.auto,
popperRef: null,
popperOptions: {},
};
popper: ?Popper;
popperHeight = 0;
state = {
isMounted: false,
};
componentDidMount() {
this.setState({isMounted: true});
}
componentDidUpdate(prevProps: TetherPropsT, prevState: TetherStateT) {
// 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.
Eif (this.props.popperRef) {
const {height} = this.props.popperRef.getBoundingClientRect();
Iif (this.popperHeight !== height) {
this.popperHeight = height;
this.popper && this.popper.scheduleUpdate();
}
Eif (this.state.isMounted !== prevState.isMounted) {
Iif (!this.props.anchorRef) {
if (__DEV__) {
// 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
http://baseui.design/components/popover#anchor-ref-handling-example`,
);
}
} else {
this.initializePopper();
}
}
}
}
componentWillUnmount() {
this.destroyPopover();
}
initializePopper() {
const {placement, popperOptions} = this.props;
const {modifiers, ...restOptions} = popperOptions;
this.popper = new Popper(this.props.anchorRef, this.props.popperRef, {
// Recommended placement (popper may ignore if it causes a viewport overflow, etc)
placement: 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,
});
}
onPopperUpdate = (data: PopperDataObjectT) => {
const normalizedOffsets = {
popper: parsePopperOffset(data.offsets.popper),
arrow: data.offsets.arrow
? parsePopperOffset(data.offsets.arrow)
: {top: 0, left: 0},
};
this.props.onPopperUpdate(normalizedOffsets, data);
};
destroyPopover() {
Eif (this.popper) {
this.popper.destroy();
delete this.popper;
}
}
render() {
return this.props.children || null;
}
}
export default Tether;
|