diff --git a/components/tooltip/styles.ts b/components/tooltip/styles.ts new file mode 100644 index 0000000..c7669f7 --- /dev/null +++ b/components/tooltip/styles.ts @@ -0,0 +1,28 @@ +import { SnippetTypes } from '../utils/prop-types' +import { ZeitUIThemesPalette } from '../styles/themes' + +export type TooltipColors = { + bgColor: string + color: string +} + +export const getColors = ( + type: SnippetTypes, + palette: ZeitUIThemesPalette, +): TooltipColors => { + const colors: { [key in SnippetTypes ]: string } = { + default: palette.background, + success: palette.success, + warning: palette.warning, + error: palette.error, + secondary: palette.secondary, + dark: palette.foreground, + lite: palette.background + } + const color = type === 'lite' || type === 'default' ? palette.foreground : palette.background + + return { + color, + bgColor: colors[type] + } +} diff --git a/components/tooltip/tooltip-content.tsx b/components/tooltip/tooltip-content.tsx index 2c82f0c..a674959 100644 --- a/components/tooltip/tooltip-content.tsx +++ b/components/tooltip/tooltip-content.tsx @@ -1,20 +1,23 @@ -import React, { MutableRefObject, useRef, useState } from 'react' +import React, { MutableRefObject, useMemo, useRef, useState } from 'react' import { createPortal } from 'react-dom' import useTheme from '../styles/use-theme' import usePortal from '../utils/use-portal' import useResize from '../utils/use-resize' import CSSTransition from '../shared/css-transition' import useClickAnyWhere from '../utils/use-click-anywhere' +import { getColors } from './styles' import { getPosition, TooltipPosition, defaultTooltipPosition } from './placement' import TooltipIcon from './tooltip-icon' -import { Placement } from '../utils/prop-types' +import { Placement, SnippetTypes } from '../utils/prop-types' interface Props { parent?: MutableRefObject | undefined placement: Placement + type: SnippetTypes visible: boolean + hideArrow: boolean offset: number - bgColor: string + className?: string } interface ReactiveDomReact { @@ -50,12 +53,14 @@ const getRect = (ref: MutableRefObject): ReactiveDomReact => } const TooltipContent: React.FC> = React.memo(({ - children, parent, visible, offset, placement, bgColor, + children, parent, visible, offset, placement, type, className, hideArrow, }) => { const theme = useTheme() const el = usePortal('tooltip') const selfRef = useRef(null) const [rect, setRect] = useState(defaultTooltipPosition) + const colors = useMemo(() => getColors(type, theme.palette), [type, theme.palette]) + const hasShadow = type === 'default' if (!parent) return null const updateRect = () => { @@ -75,12 +80,10 @@ const TooltipContent: React.FC> = React.memo(({ if (!el) return null return createPortal(( -
+
- + {!hideArrow && } {children}