tldraw/components/tooltip.tsx

75 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-06-02 21:17:38 +00:00
import * as _Tooltip from '@radix-ui/react-tooltip'
import React from 'react'
import styled from 'styles'
2021-07-14 13:45:24 +00:00
interface TooltipProps {
children: React.ReactNode
label: string
kbd?: string
side?: 'bottom' | 'left' | 'right' | 'top'
}
2021-06-02 21:17:38 +00:00
export default function Tooltip({
children,
label,
2021-07-14 13:45:24 +00:00
kbd,
2021-06-02 21:17:38 +00:00
side = 'top',
2021-07-14 13:45:24 +00:00
}: TooltipProps): JSX.Element {
2021-06-02 21:17:38 +00:00
return (
<_Tooltip.Root>
<_Tooltip.Trigger as="span">{children}</_Tooltip.Trigger>
<StyledContent side={side} sideOffset={8}>
{label}
2021-07-14 13:45:24 +00:00
{kbd ? (
<kbd>
{kbd.split('').map((k, i) => (
<span key={i}>{k}</span>
))}
</kbd>
) : null}
2021-06-02 21:17:38 +00:00
<StyledArrow />
</StyledContent>
</_Tooltip.Root>
)
}
const StyledContent = styled(_Tooltip.Content, {
borderRadius: 3,
2021-07-14 13:45:24 +00:00
padding: '$3 $3 $3 $3',
2021-06-02 21:17:38 +00:00
fontSize: '$1',
2021-07-14 13:45:24 +00:00
backgroundColor: '$tooltipBg',
color: '$tooltipText',
boxShadow: '$3',
display: 'flex',
alignItems: 'center',
'& kbd': {
marginLeft: '$3',
textShadow: '$2',
textAlign: 'center',
fontSize: '$1',
fontFamily: '$ui',
fontWeight: 400,
gap: '$1',
display: 'flex',
alignItems: 'center',
'& > span': {
padding: '$0',
borderRadius: '$0',
background: '$overlayContrast',
boxShadow: '$key',
width: '20px',
height: '20px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
},
2021-06-02 21:17:38 +00:00
})
const StyledArrow = styled(_Tooltip.Arrow, {
2021-07-14 13:45:24 +00:00
fill: '$tooltipBg',
2021-06-02 21:17:38 +00:00
margin: '0 8px',
})