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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | 50x 50x 88x 88x 79x 17x 17x 17x 62x 15x 15x 50x 1x 1x 32x 160x 4x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 15x 15x 15x 134x 174x 174x 6x 138x 138x 312x 138x 138x 138x 138x 138x 134x 134x 134x 134x 4x 4x 4x 138x 15x 15x 15x 15x 123x 138x 36x 36x 36x 34x 2x 2x 2x 36x 8x 8x 36x 36x 36x 36x 138x 138x 138x 138x 138x 138x 138x 138x 138x 120x 18x 36x 36x 36x 36x 36x 36x 36x 36x 138x 138x 138x 36x 17x 1x 138x | /*
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
/* global document */
/* eslint-disable react/no-find-dom-node */
import * as React from 'react';
import {getOverride, getOverrideProps} from '../helpers/overrides.js';
import getBuiId from '../utils/get-bui-id.js';
import {
ACCESSIBILITY_TYPE,
PLACEMENT,
TRIGGER_TYPE,
ANIMATE_OUT_TIME,
ANIMATE_IN_TIME,
} from './constants.js';
import {Layer, TetherBehavior} from '../layer/index.js';
import {
Arrow as StyledArrow,
Body as StyledBody,
Inner as StyledInner,
} from './styled-components.js';
import {fromPopperPlacement} from './utils.js';
import defaultProps from './default-props.js';
import type {
AnchorPropsT,
PopoverPropsT,
PopoverPrivateStateT,
SharedStylePropsArgT,
} from './types.js';
import type {PopperDataObjectT, NormalizedOffsetsT} from '../layer/types.js';
class Popover extends React.Component<PopoverPropsT, PopoverPrivateStateT> {
static defaultProps: $Shape<PopoverPropsT> = defaultProps;
/* eslint-disable react/sort-comp */
animateInTimer: ?TimeoutID;
animateOutTimer: ?TimeoutID;
animateOutCompleteTimer: ?TimeoutID;
onMouseEnterTimer: ?TimeoutID;
onMouseLeaveTimer: ?TimeoutID;
generatedId: string = '';
anchorRef = (React.createRef(): {current: *});
popperRef = (React.createRef(): {current: *});
arrowRef = (React.createRef(): {current: *});
/* eslint-enable react/sort-comp */
/**
* Yes our "Stateless" popover still has state. This is private state that
* customers shouldn't have to manage themselves, such as positioning and
* other internal flags for managing animation states.
*/
state = this.getDefaultState(this.props);
componentDidMount() {
this.generatedId = getBuiId();
this.setState({isMounted: true});
}
componentDidUpdate(
prevProps: PopoverPropsT,
prevState: PopoverPrivateStateT,
) {
this.init(prevProps, prevState);
}
init(prevProps: PopoverPropsT, prevState: PopoverPrivateStateT) {
if (
this.props.isOpen !== prevProps.isOpen ||
this.state.isMounted !== prevState.isMounted ||
this.state.isLayerMounted !== prevState.isLayerMounted
) {
// Transition from closed to open.
if (this.props.isOpen && this.state.isLayerMounted) {
// Clear any existing timers (like previous animateOutCompleteTimer)
this.clearTimers();
this.addDomEvents();
return;
}
// Transition from open to closed.
Iif (!this.props.isOpen && prevProps.isOpen) {
this.removeDomEvents();
this.animateOutTimer = setTimeout(this.animateOut, 20);
return;
}
}
}
componentWillUnmount() {
this.removeDomEvents();
this.clearTimers();
}
getDefaultState(props: PopoverPropsT) {
return {
isAnimating: false,
arrowOffset: {left: 0, top: 0},
popoverOffset: {left: 0, top: 0},
placement: props.placement,
isMounted: false,
isLayerMounted: false,
};
}
animateIn = () => {
Eif (this.props.isOpen) {
this.setState({isAnimating: true});
}
};
animateOut = () => {
if (!this.props.isOpen) {
this.setState({isAnimating: true});
// Remove the popover from the DOM after animation finishes
this.animateOutCompleteTimer = setTimeout(() => {
this.setState({
isAnimating: false,
// Reset to ideal placement specified in props
placement: this.props.placement,
});
}, this.props.animateOutTime || ANIMATE_OUT_TIME);
}
};
clearTimers() {
[
this.animateInTimer,
this.animateOutTimer,
this.animateOutCompleteTimer,
this.onMouseEnterTimer,
this.onMouseLeaveTimer,
].forEach(timerId => {
if (timerId) {
clearTimeout(timerId);
}
});
}
onAnchorClick = (e: Event) => {
Eif (this.props.onClick) {
this.props.onClick(e);
}
};
onAnchorMouseEnter = () => {
// First clear any existing close timers, this ensures that the user can
// move their mouse from the popover back to anchor without it hiding
Iif (this.onMouseLeaveTimer) {
clearTimeout(this.onMouseLeaveTimer);
}
this.triggerOnMouseEnterWithDelay();
};
onAnchorMouseLeave = () => {
// Clear any existing open timer, otherwise popover could be stuck open
if (this.onMouseEnterTimer) {
clearTimeout(this.onMouseEnterTimer);
}
this.triggerOnMouseLeaveWithDelay();
};
onPopoverMouseEnter = () => {
if (this.onMouseLeaveTimer) {
clearTimeout(this.onMouseLeaveTimer);
}
};
onPopoverMouseLeave = () => {
this.triggerOnMouseLeaveWithDelay();
};
onKeyPress = (evt: KeyboardEvent) => {
Eif (evt.key === 'Escape' && this.props.onEsc) {
this.props.onEsc();
}
};
onPopperUpdate = (
normalizedOffsets: NormalizedOffsetsT,
data: PopperDataObjectT,
) => {
const placement = fromPopperPlacement(data.placement) || PLACEMENT.top;
this.setState({
arrowOffset: normalizedOffsets.arrow,
popoverOffset: normalizedOffsets.popper,
placement,
});
// Now that element has been positioned, we can animate it in
this.animateInTimer = setTimeout(this.animateIn, ANIMATE_IN_TIME);
return data;
};
triggerOnMouseLeaveWithDelay() {
const {onMouseLeaveDelay} = this.props;
Eif (onMouseLeaveDelay) {
this.onMouseLeaveTimer = setTimeout(
this.triggerOnMouseLeave,
onMouseLeaveDelay,
);
return;
}
this.triggerOnMouseLeave();
}
triggerOnMouseLeave = () => {
Eif (this.props.onMouseLeave) {
this.props.onMouseLeave();
}
};
triggerOnMouseEnterWithDelay() {
const {onMouseEnterDelay} = this.props;
Eif (onMouseEnterDelay) {
this.onMouseEnterTimer = setTimeout(
this.triggerOnMouseEnter,
onMouseEnterDelay,
);
return;
}
this.triggerOnMouseEnter();
}
triggerOnMouseEnter = () => {
Eif (this.props.onMouseEnter) {
this.props.onMouseEnter();
}
};
addDomEvents() {
Eif (__BROWSER__) {
// using mousedown event so that callback runs before events on children inside of the popover
document.addEventListener('mousedown', this.onDocumentClick);
document.addEventListener('keyup', this.onKeyPress);
}
}
removeDomEvents() {
Eif (__BROWSER__) {
document.removeEventListener('mousedown', this.onDocumentClick);
document.removeEventListener('keyup', this.onKeyPress);
}
}
onDocumentClick = (evt: MouseEvent) => {
const target = evt.target;
const popper = this.popperRef.current;
const anchor = this.anchorRef.current;
// Ignore document click if it came from popover or anchor
if (!popper || popper === target || popper.contains(target)) {
return;
}
if (!anchor || anchor === target || anchor.contains(target)) {
return;
}
if (this.props.onClickOutside) {
this.props.onClickOutside(evt);
}
};
isClickTrigger() {
return this.props.triggerType === TRIGGER_TYPE.click;
}
isHoverTrigger() {
return this.props.triggerType === TRIGGER_TYPE.hover;
}
isAccessibilityTypeMenu() {
return this.props.accessibilityType === ACCESSIBILITY_TYPE.menu;
}
isAccessibilityTypeTooltip() {
return this.props.accessibilityType === ACCESSIBILITY_TYPE.tooltip;
}
getAnchorIdAttr() {
const popoverId = this.getPopoverIdAttr();
return popoverId ? `${popoverId}__anchor` : null;
}
getPopoverIdAttr() {
return this.props.id || this.generatedId || null;
}
getAnchorProps() {
const {isOpen} = this.props;
const anchorProps: AnchorPropsT = {
key: 'popover-anchor',
ref: this.anchorRef,
};
const anchorId = this.getAnchorIdAttr();
const popoverId = this.getPopoverIdAttr();
if (this.isAccessibilityTypeMenu()) {
anchorProps['aria-haspopup'] = 'true';
anchorProps['aria-expanded'] = isOpen ? 'true' : 'false';
const relationAttr = this.isClickTrigger()
? 'aria-controls'
: 'aria-owns';
anchorProps[relationAttr] = isOpen ? popoverId : null;
} else Eif (this.isAccessibilityTypeTooltip()) {
anchorProps.id = anchorId;
anchorProps['aria-describedby'] = isOpen ? popoverId : null;
}
if (this.isHoverTrigger()) {
anchorProps.onMouseEnter = this.onAnchorMouseEnter;
anchorProps.onMouseLeave = this.onAnchorMouseLeave;
// Make it focusable too
anchorProps.onBlur = this.props.onBlur;
anchorProps.onFocus = this.props.onFocus;
} else {
anchorProps.onClick = this.onAnchorClick;
}
return anchorProps;
}
getPopoverBodyProps() {
const bodyProps = {};
const popoverId = this.getPopoverIdAttr();
if (this.isAccessibilityTypeMenu()) {
bodyProps.id = popoverId;
} else Eif (this.isAccessibilityTypeTooltip()) {
bodyProps.id = popoverId;
bodyProps.role = 'tooltip';
}
if (this.isHoverTrigger()) {
bodyProps.onMouseEnter = this.onPopoverMouseEnter;
bodyProps.onMouseLeave = this.onPopoverMouseLeave;
}
return bodyProps;
}
getSharedProps(): $Diff<SharedStylePropsArgT, {children?: React.Node}> {
const {isOpen, showArrow} = this.props;
const {isAnimating, arrowOffset, popoverOffset, placement} = this.state;
return {
$showArrow: !!showArrow,
$arrowOffset: arrowOffset,
$popoverOffset: popoverOffset,
$placement: placement,
$isAnimating: isAnimating,
$isOpen: isOpen,
};
}
getAnchorFromChildren() {
const {children} = this.props;
const childArray = React.Children.toArray(children);
Iif (childArray.length !== 1) {
// eslint-disable-next-line no-console
console.error(
`[baseui] Exactly 1 child must be passed to Popover/Tooltip, found ${
childArray.length
} children`,
);
}
return childArray[0];
}
renderAnchor() {
const anchor = this.getAnchorFromChildren();
Iif (!anchor) {
return null;
}
const isValidElement = React.isValidElement(anchor);
const anchorProps = this.getAnchorProps();
if (typeof anchor === 'object' && isValidElement) {
return React.cloneElement(anchor, anchorProps);
}
return <span {...anchorProps}>{anchor}</span>;
}
renderPopover() {
const {showArrow, overrides = {}, content} = this.props;
const {
Arrow: ArrowOverride,
Body: BodyOverride,
Inner: InnerOverride,
} = overrides;
const Arrow = getOverride(ArrowOverride) || StyledArrow;
const Body = getOverride(BodyOverride) || StyledBody;
const Inner = getOverride(InnerOverride) || StyledInner;
const sharedProps = this.getSharedProps();
const bodyProps = this.getPopoverBodyProps();
return (
<Body
key="popover-body"
ref={this.popperRef}
data-baseweb={this.props['data-baseweb'] || 'popover'}
{...bodyProps}
{...sharedProps}
{...getOverrideProps(BodyOverride)}
>
{showArrow ? (
<Arrow
key="popover-arrow"
ref={this.arrowRef}
{...sharedProps}
{...getOverrideProps(ArrowOverride)}
/>
) : null}
<Inner
key="popover-inner"
{...sharedProps}
{...getOverrideProps(InnerOverride)}
>
{typeof content === 'function' ? content() : content}
</Inner>
</Body>
);
}
render() {
const rendered = [this.renderAnchor()];
// Only render popover on the browser (portals aren't supported server-side)
Eif (__BROWSER__) {
if (this.state.isMounted && this.props.isOpen) {
rendered.push(
<Layer
key={'new-layer'}
mountNode={this.props.mountNode}
onMount={() => this.setState({isLayerMounted: true})}
onUnmount={() => this.setState({isLayerMounted: false})}
>
<TetherBehavior
anchorRef={this.anchorRef.current}
arrowRef={this.arrowRef.current}
popperRef={this.popperRef.current}
// Remove the `ignoreBoundary` prop in the next major version
// and have it replaced with the TetherBehavior props overrides
popperOptions={{
modifiers: {
preventOverflow: {enabled: !this.props.ignoreBoundary},
},
}}
onPopperUpdate={this.onPopperUpdate}
placement={this.state.placement}
>
{this.renderPopover()}
</TetherBehavior>
</Layer>,
);
}
}
return rendered;
}
}
export default Popover;
/* eslint-enable react/no-find-dom-node */
|