From ca4f734b016893ecb110b13f16713ef0a6fc3432 Mon Sep 17 00:00:00 2001 From: atanasster Date: Sun, 25 Oct 2020 20:40:31 -0400 Subject: [PATCH] fix: themable popup arrow --- ui/components/src/Popover/Popover.tsx | 12 +- ui/components/src/Popover/PopoverUtils.tsx | 139 +++++++++++++-------- 2 files changed, 92 insertions(+), 59 deletions(-) diff --git a/ui/components/src/Popover/Popover.tsx b/ui/components/src/Popover/Popover.tsx index 0b458d4da..887a69f70 100644 --- a/ui/components/src/Popover/Popover.tsx +++ b/ui/components/src/Popover/Popover.tsx @@ -2,7 +2,7 @@ import { FC } from 'react'; import TooltipTrigger from 'react-popper-tooltip'; import { TooltipTriggerProps } from 'react-popper-tooltip/dist/types'; -import { jsx, Box } from 'theme-ui'; +import { jsx, Box, SxStyleProp } from 'theme-ui'; import { Arrow, Wrapper } from './PopoverUtils'; export interface PopoverOwnProps { @@ -52,15 +52,19 @@ export const Popover: FC = ({ borderColor={borderColor} hidden={hidden} ref={tooltipRef as any} - {...containerProps} - sx={{ backgroundColor: 'background' }} + sx={{ + ...(containerProps.style as SxStyleProp), + backgroundColor: 'background', + }} > {arrowVisible && ( )} {typeof tooltip === 'function' ? tooltip(tooltipProps) : tooltip} diff --git a/ui/components/src/Popover/PopoverUtils.tsx b/ui/components/src/Popover/PopoverUtils.tsx index d3a4e318c..4b33a1e45 100644 --- a/ui/components/src/Popover/PopoverUtils.tsx +++ b/ui/components/src/Popover/PopoverUtils.tsx @@ -1,4 +1,6 @@ -import styled from '@emotion/styled'; +/** @jsx jsx */ +import { FC, forwardRef, Ref } from 'react'; +import { jsx, Box, BoxProps } from 'theme-ui'; const SPACING = 8; @@ -9,68 +11,95 @@ const match = ( fallback: number | string = 0, ) => (actual.split('-')[0] === requested ? value : fallback); -export const Arrow = styled.div<{ placement: string; borderColor: string }>( - ({ placement, borderColor }) => ({ - position: 'absolute', - borderStyle: 'solid', - background: 'white', - marginBottom: `${match('top', placement, '0', SPACING)}px`, - marginTop: `${match('bottom', placement, '0', SPACING)}px`, - marginRight: `${match('left', placement, '0', SPACING)}px`, - marginLeft: `${match('right', placement, '0', SPACING)}px`, +const matchPx = ( + requested: string, + actual: string, + value: number | string, + fallback: number | string = 0, +) => { + const result = match(requested, actual, value, fallback); + return typeof result === 'number' ? `${result}px` : result; +}; - bottom: `${match('top', placement, SPACING * -1, 'auto')}px`, - top: `${match('bottom', placement, SPACING * -1, 'auto')}px`, - right: `${match('left', placement, SPACING * -1, 'auto')}px`, - left: `${match('right', placement, SPACING * -1, 'auto')}px`, +export const Arrow: FC<{ + placement: string; + borderColor: string; +} & BoxProps> = forwardRef( + ({ placement, borderColor, ...rest }, ref: Ref) => ( + + ), ); -export const Wrapper = styled.div<{ +export const Wrapper: FC<{ placement: string; borderColor: string; hidden: boolean; -}>(({ placement, borderColor, hidden }) => ({ - display: hidden ? 'none' : 'inline-block', - background: 'white', - marginTop: `${match('bottom', placement, SPACING + 2, 0)}px`, - marginLeft: `${match('right', placement, SPACING + 2, 0)}px`, - marginRight: `${match('left', placement, SPACING + 2, 0)}px`, - filter: ` +} & BoxProps> = forwardRef( + ({ placement, borderColor, hidden, ...rest }, ref: Ref) => ( +