All files / popover styled-components.js

77.78% Statements 7/9
91.67% Branches 11/12
66.67% Functions 2/3
77.78% Lines 7/9

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                                                          34x   34x                                       28x                                   28x                 34x             28x             28x            
/*
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 {styled} from '../styles/index.js';
import {ARROW_WIDTH} from './constants.js';
import {
  getPopoverMarginStyles,
  getArrowPositionStyles,
  getStartPosition,
  getEndPosition,
} from './utils.js';
import type {SharedStylePropsArgT} from './types.js';
import type {ThemeT} from '../styles/types.js';
 
/**
 * Main popover container element that gets positioned next to the anchor
 */
export function getBodyStyles(props: SharedStylePropsArgT & {$theme: ThemeT}) {
  const {
    $isOpen,
    $isAnimating,
    $placement,
    $popoverOffset,
    $showArrow,
    $theme,
  } = props;
 
  return {
    position: 'absolute',
    top: 0,
    left: 0,
    backgroundColor: $theme.colors.background,
    boxShadow: $theme.lighting.shadow600,
    transitionProperty: 'opacity,transform',
    transitionDuration: $isAnimating ? '0.1s' : '0s',
    transitionTimingFunction: $isOpen
      ? $theme.animation.easeOutCurve
      : $theme.animation.easeInCurve,
    opacity: $isAnimating && $isOpen ? 1 : 0,
    transform:
      $isAnimating && $isOpen
        ? getEndPosition($popoverOffset)
        : getStartPosition($popoverOffset, $placement, $showArrow),
    ...getPopoverMarginStyles($showArrow, $placement),
  };
}
 
export const Body = styled<SharedStylePropsArgT>('div', getBodyStyles);
 
/**
 * Arrow shown between the popover and the anchor element
 */
export function getArrowStyles(props: SharedStylePropsArgT & {$theme: ThemeT}) {
  const {$arrowOffset, $placement, $theme} = props;
  return {
    backgroundColor: $theme.colors.backgroundAlt,
    boxShadow: $theme.lighting.shadow600,
    width: `${ARROW_WIDTH}px`,
    height: `${ARROW_WIDTH}px`,
    transform: 'rotate(45deg)',
    position: 'absolute',
    ...getArrowPositionStyles($arrowOffset, $placement),
  };
}
 
export const Arrow = styled<SharedStylePropsArgT>('div', getArrowStyles);
 
/**
 * Extra div that holds the popover content. This extra element
 * is needed for the arrow–the arrow is just a 45deg rotated div,
 * and rendering this extra element on top with a solid background
 * clips the part of the arrow that extends into the popover.
 */
export function getInnerStyles({$theme}: {$theme: ThemeT}) {
  return {
    backgroundColor: $theme.colors.backgroundAlt,
    color: $theme.colors.foreground,
    position: 'relative',
  };
}
 
export const Inner = styled<SharedStylePropsArgT>('div', getInnerStyles);
 
/**
 * A drop-in component that provides the recommended padding
 * for popovers. Mostly a convenience for users so they don't
 * have to define this themselves.
 */
export const Padding = styled('div', {
  paddingLeft: '12px',
  paddingTop: '12px',
  paddingRight: '12px',
  paddingBottom: '12px',
});