All files / src/bubble Bubble.tsx

61.54% Statements 8/13
64.71% Branches 11/17
75% Functions 3/4
61.54% Lines 8/13

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                                                                                                                                                                                                                                  192x   192x 192x 190x 190x               192x           7x                                 192x                                                                
import React, { useRef, useLayoutEffect } from "react";
 
import { BubbleContent, BubbleContentProps } from "./BubbleContent";
import { Popover, PopoverProps } from "../popover";
import { isChildOfType } from "../_util/is-child-of-type";
import { Button } from "../button";
 
export interface BubbleProps extends BubbleContentProps {
  /**
   * 气泡内容
   *
   * 如果气泡内容传入 `null` 或者 `undefined`,则不会出现气泡
   */
  content?: React.ReactNode;
 
  /**
   * 触发方式
   * @default "hover"
   */
  trigger?: PopoverProps["trigger"];
 
  /**
   * 传入 `visible` 则表示使用受控模式来控制弹出气泡的显示,请处理 `onVisibleChange` 方法
   */
  visible?: PopoverProps["visible"];
 
  /**
   * 处理 `visible` 变化的情况
   */
  onVisibleChange?: PopoverProps["onVisibleChange"];
 
  /**
   * 覆盖层自定义类名
   */
  overlayClassName?: PopoverProps["overlayClassName"];
 
  /**
   * 覆盖层自定义样式
   */
  overlayStyle?: PopoverProps["overlayStyle"];
 
  /**
   * 打开延时
   */
  openDelay?: PopoverProps["openDelay"];
 
  /**
   * 关闭延时
   */
  closeDelay?: PopoverProps["closeDelay"];
 
  /**
   * 是否在父容器滚动时关闭
   * @default false
   */
  closeOnScroll?: PopoverProps["closeOnScroll"];
 
  /**
   * 是否在 resize 和 scroll 发生时更新位置
   */
  updateOnDimensionChange?: PopoverProps["updateOnDimensionChange"];
 
  /**
   * 自定义定位参考信息
   * @see https://github.com/FezVrasta/react-popper#usage-without-a-reference-htmlelement
   */
  referenceElement?: PopoverProps["referenceElement"];
 
  /**
   * 弹出位置距离参考位置的偏移量
   * @default 10
   */
  placementOffset?: PopoverProps["placementOffset"];
 
  /**
   * 出现动画滑动距离
   */
  animationScaleFrom?: PopoverProps["animationScaleFrom"];
 
  /**
   * 动画时长
   */
  transitionTimeout?: PopoverProps["transitionTimeout"];
 
  /**
   * 导致位置更新的依赖列表,可用于性能优化
   *
   * @default [content]
   */
  updateDeps?: any[];
}
 
export function Bubble({
  visible,
  onVisibleChange,
  content,
  trigger,
  overlayClassName,
  overlayStyle,
  openDelay,
  closeDelay,
  closeOnScroll,
  updateOnDimensionChange,
  transitionTimeout,
  referenceElement,
  placement,
  placementOffset,
  animationScaleFrom,
  children,
  updateDeps = [content],
  ...bubbleProps
}: BubbleProps) {
  // 内容变化时更新位置
  const scheduleUpdateRef = useRef(null);
 
  const deps = Array.isArray(updateDeps) ? updateDeps : [content];
  useLayoutEffect(
    () => () => {
      Iif (scheduleUpdateRef.current) {
        scheduleUpdateRef.current();
      }
    },
    deps // eslint-disable-line react-hooks/exhaustive-deps
  );
 
  // issue: https://github.com/facebook/react/issues/4251
  if (
    React.Children.count(children) === 1 &&
    (isChildOfType(children, Button) || isChildOfType(children, "button")) &&
    children.props.disabled
  ) {
    // eslint-disable-next-line no-param-reassign
    children = (
      <span
        style={{
          display: "inline-block",
          cursor: "not-allowed",
        }}
      >
        {React.cloneElement(children as React.ReactElement, {
          style: {
            ...(children.props.style || {}),
            pointerEvents: "none",
          },
        })}
      </span>
    );
  }
 
  return (
    <Popover
      visible={visible}
      onVisibleChange={onVisibleChange}
      closeOnScroll={closeOnScroll}
      trigger={trigger}
      placement={placement}
      placementOffset={placementOffset}
      animationScaleFrom={animationScaleFrom}
      referenceElement={referenceElement}
      overlay={({ scheduleUpdate, placement }) => {
        scheduleUpdateRef.current = scheduleUpdate;
        if (!content && content !== 0) {
          return null;
        }
        return (
          <BubbleContent {...bubbleProps} placement={placement}>
            {content}
          </BubbleContent>
        );
      }}
      overlayClassName={overlayClassName}
      overlayStyle={overlayStyle}
      openDelay={openDelay}
      closeDelay={closeDelay}
      updateOnDimensionChange={updateOnDimensionChange}
      transitionTimeout={transitionTimeout}
    >
      {children}
    </Popover>
  );
}