tldraw/components/shared.tsx

728 lines
14 KiB
TypeScript
Raw Normal View History

2021-07-14 13:45:24 +00:00
import React from 'react'
import Tooltip from 'components/tooltip'
2021-07-10 16:14:49 +00:00
import * as ContextMenu from '@radix-ui/react-context-menu'
2021-06-03 12:06:39 +00:00
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'
import * as RadioGroup from '@radix-ui/react-radio-group'
import * as Panel from './panel'
import styled from 'styles'
2021-07-10 20:39:29 +00:00
import { CheckIcon, ChevronRightIcon } from '@radix-ui/react-icons'
2021-07-10 16:14:49 +00:00
import { isMobile } from 'utils'
2021-05-17 10:01:11 +00:00
export const breakpoints: any = { '@initial': 'mobile', '@sm': 'small' }
export const IconButton = styled('button', {
2021-07-10 12:01:59 +00:00
position: 'relative',
height: '32px',
width: '32px',
backgroundColor: '$panel',
borderRadius: '4px',
padding: '0',
margin: '0',
display: 'grid',
alignItems: 'center',
justifyContent: 'center',
outline: 'none',
border: 'none',
pointerEvents: 'all',
2021-07-10 12:01:59 +00:00
fontSize: '$0',
2021-07-10 20:39:29 +00:00
color: '$text',
cursor: 'pointer',
2021-05-17 10:01:11 +00:00
'& > *': {
gridRow: 1,
gridColumn: 1,
},
'&:disabled': {
opacity: '0.5',
2021-05-17 10:01:11 +00:00
},
'& > span': {
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
},
variants: {
bp: {
2021-07-10 20:39:29 +00:00
mobile: {
backgroundColor: 'transparent',
},
small: {
'&:hover:not(:disabled)': {
backgroundColor: '$hover',
},
},
},
size: {
small: {
2021-07-10 12:01:59 +00:00
height: 32,
width: 32,
'& svg:nth-of-type(1)': {
height: '16px',
width: '16px',
},
},
medium: {
height: 44,
width: 44,
2021-07-10 12:01:59 +00:00
'& svg:nth-of-type(1)': {
height: '18px',
width: '18px',
},
},
large: {
height: 44,
width: 44,
2021-07-10 12:01:59 +00:00
'& svg:nth-of-type(1)': {
height: '20px',
width: '20px',
},
},
},
isActive: {
true: {
color: '$selected',
},
},
2021-05-17 10:01:11 +00:00
},
})
2021-06-03 12:06:39 +00:00
export const RowButton = styled('button', {
position: 'relative',
display: 'flex',
width: '100%',
background: 'none',
height: '32px',
border: 'none',
cursor: 'pointer',
2021-07-10 20:39:29 +00:00
color: '$text',
2021-06-03 12:06:39 +00:00
outline: 'none',
alignItems: 'center',
fontFamily: '$ui',
2021-06-27 21:30:37 +00:00
fontWeight: 400,
fontSize: '$1',
2021-06-03 12:06:39 +00:00
justifyContent: 'space-between',
padding: '4px 8px 4px 12px',
2021-06-03 13:10:54 +00:00
borderRadius: 4,
2021-07-14 11:39:34 +00:00
userSelect: 'none',
2021-06-03 12:06:39 +00:00
'& label': {
fontWeight: '$1',
margin: 0,
padding: 0,
},
'& svg': {
position: 'relative',
2021-07-10 20:52:52 +00:00
stroke: '$overlay',
2021-06-03 12:06:39 +00:00
strokeWidth: 1,
zIndex: 1,
},
2021-07-14 13:45:24 +00:00
'&[data-disabled]': {
opacity: 0.3,
},
2021-07-10 20:39:29 +00:00
'&:disabled': {
opacity: 0.3,
2021-06-10 09:49:16 +00:00
},
2021-06-03 12:06:39 +00:00
variants: {
bp: {
mobile: {},
small: {
2021-07-10 20:39:29 +00:00
'& *[data-shy="true"]': {
opacity: 0,
},
'&:hover:not(:disabled)': {
backgroundColor: '$hover',
2021-07-10 20:39:29 +00:00
'& *[data-shy="true"]': {
opacity: 1,
},
},
},
},
2021-06-03 12:06:39 +00:00
size: {
icon: {
padding: '4px ',
width: 'auto',
},
},
variant: {
2021-07-14 11:46:19 +00:00
noIcon: {
padding: '4px 12px',
},
pageButton: {
2021-07-10 20:39:29 +00:00
display: 'grid',
gridTemplateColumns: '24px auto',
width: '100%',
paddingLeft: '$1',
gap: '$3',
justifyContent: 'flex-start',
[`& > *[data-state="checked"]`]: {
gridRow: 1,
gridColumn: 1,
},
'& > span': {
gridRow: 1,
gridColumn: 2,
width: '100%',
},
},
},
warn: {
2021-07-10 20:39:29 +00:00
true: {
color: '$warn',
2021-07-10 20:39:29 +00:00
},
},
isActive: {
true: {
backgroundColor: '$hover',
},
},
2021-06-03 12:06:39 +00:00
},
})
export const StylePanelRoot = styled(Panel.Root, {
minWidth: 1,
width: 184,
maxWidth: 184,
overflow: 'hidden',
position: 'relative',
border: '1px solid $panel',
2021-07-10 16:14:49 +00:00
boxShadow: '$4',
2021-06-03 12:06:39 +00:00
variants: {
isOpen: {
true: {},
false: {
2021-07-10 16:14:49 +00:00
padding: '$0',
2021-06-03 12:06:39 +00:00
height: 38,
width: 38,
},
},
},
})
export const Group = styled(RadioGroup.Root, {
display: 'flex',
})
2021-07-13 21:31:58 +00:00
export const ShortcutKey = styled('span', {
fontSize: '$0',
width: '16px',
height: '16px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
boxShadow: '1px 1px 0px rgba(0,0,0,.5)',
})
2021-06-03 12:06:39 +00:00
export const IconWrapper = styled('div', {
height: '100%',
borderRadius: '4px',
marginRight: '1px',
display: 'grid',
alignItems: 'center',
justifyContent: 'center',
outline: 'none',
border: 'none',
pointerEvents: 'all',
cursor: 'pointer',
2021-07-10 20:39:29 +00:00
color: '$text',
2021-06-03 12:06:39 +00:00
'& svg': {
height: 22,
width: 22,
strokeWidth: 1,
},
'& > *': {
gridRow: 1,
gridColumn: 1,
},
variants: {
size: {
small: {
'& svg': {
height: '16px',
width: '16px',
},
},
medium: {
'& svg': {
height: '22px',
width: '22px',
},
},
},
},
})
2021-07-01 22:11:09 +00:00
export const ButtonsRow = styled('div', {
position: 'relative',
display: 'flex',
width: '100%',
background: 'none',
border: 'none',
cursor: 'pointer',
outline: 'none',
alignItems: 'center',
justifyContent: 'flex-start',
padding: 0,
})
2021-07-10 12:01:59 +00:00
export const VerticalDivider = styled('hr', {
width: '1px',
margin: '-2px 3px',
border: 'none',
backgroundColor: '$brushFill',
})
2021-07-10 16:14:49 +00:00
export const FloatingContainer = styled('div', {
backgroundColor: '$panel',
border: '1px solid $panel',
borderRadius: '4px',
boxShadow: '$4',
display: 'flex',
height: 'fit-content',
padding: '$0',
pointerEvents: 'all',
position: 'relative',
userSelect: 'none',
zIndex: 200,
variants: {
direction: {
row: {
flexDirection: 'row',
},
column: {
flexDirection: 'column',
},
},
elevation: {
0: {
boxShadow: 'none',
},
2: {
boxShadow: '$2',
},
3: {
boxShadow: '$3',
},
4: {
boxShadow: '$4',
},
},
},
2021-07-10 16:14:49 +00:00
})
export const StyledKbd = styled('kbd', {
marginLeft: '32px',
fontSize: '$1',
fontFamily: '$ui',
fontWeight: 400,
'& > span': {
display: 'inline-block',
width: '12px',
},
})
export function Kbd({ children }: { children: React.ReactNode }): JSX.Element {
if (isMobile()) return null
return <StyledKbd>{children}</StyledKbd>
}
/* -------------------------------------------------- */
/* Dialog */
/* -------------------------------------------------- */
export const DialogContent = styled('div', {
position: 'fixed',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
minWidth: 240,
maxWidth: 'fit-content',
maxHeight: '85vh',
marginTop: '-5vh',
pointerEvents: 'all',
backgroundColor: '$panel',
border: '1px solid $panel',
padding: '$0',
boxShadow: '$4',
borderRadius: '4px',
font: '$ui',
'&:focus': {
outline: 'none',
},
})
export const DialogOverlay = styled('div', {
backgroundColor: 'rgba(0, 0, 0, .15)',
position: 'fixed',
top: 0,
right: 0,
bottom: 0,
left: 0,
})
export const DialogInputWrapper = styled('div', {
padding: '$4 $2',
})
export const DialogTitleRow = styled('div', {
display: 'flex',
padding: '0 0 0 $4',
alignItems: 'center',
justifyContent: 'space-between',
h3: {
fontSize: '$1',
},
})
2021-07-10 16:14:49 +00:00
/* -------------------------------------------------- */
/* Menus */
/* -------------------------------------------------- */
export const MenuContent = styled('div', {
position: 'relative',
overflow: 'hidden',
userSelect: 'none',
zIndex: 180,
minWidth: 180,
pointerEvents: 'all',
backgroundColor: '$panel',
2021-07-10 16:14:49 +00:00
border: '1px solid $panel',
padding: '$0',
boxShadow: '$4',
borderRadius: '4px',
2021-07-10 16:14:49 +00:00
font: '$ui',
})
2021-07-10 16:14:49 +00:00
export const Divider = styled('div', {
backgroundColor: '$hover',
height: 1,
marginTop: '$2',
marginRight: '-$2',
marginBottom: '$2',
marginLeft: '-$2',
2021-07-10 16:14:49 +00:00
})
export function MenuButton({
warn,
onSelect,
children,
disabled = false,
}: {
warn?: boolean
onSelect?: () => void
disabled?: boolean
children: React.ReactNode
}): JSX.Element {
return (
<RowButton
bp={breakpoints}
disabled={disabled}
warn={warn}
onSelect={onSelect}
>
{children}
</RowButton>
)
}
export const MenuTextInput = styled('input', {
backgroundColor: '$panel',
border: 'none',
padding: '$4 $3',
width: '100%',
outline: 'none',
background: '$input',
borderRadius: '4px',
font: '$ui',
fontSize: '$1',
})
2021-07-10 16:14:49 +00:00
/* -------------------------------------------------- */
/* Dropdown Menu */
/* -------------------------------------------------- */
export function DropdownMenuRoot({
isOpen,
onOpenChange,
children,
}: {
isOpen?: boolean
onOpenChange?: (isOpen: boolean) => void
children: React.ReactNode
}): JSX.Element {
return (
<DropdownMenu.Root dir="ltr" open={isOpen} onOpenChange={onOpenChange}>
{children}
</DropdownMenu.Root>
)
}
export function DropdownMenuSubMenu({
children,
2021-07-10 20:39:29 +00:00
disabled = false,
2021-07-10 16:14:49 +00:00
label,
}: {
label: string
2021-07-10 20:39:29 +00:00
disabled?: boolean
2021-07-10 16:14:49 +00:00
children: React.ReactNode
}): JSX.Element {
return (
<DropdownMenu.Root dir="ltr">
2021-07-14 13:45:24 +00:00
<DropdownMenu.TriggerItem
as={RowButton}
2021-07-10 20:39:29 +00:00
bp={breakpoints}
disabled={disabled}
>
2021-07-10 16:14:49 +00:00
<span>{label}</span>
<IconWrapper size="small">
<ChevronRightIcon />
</IconWrapper>
2021-07-14 13:45:24 +00:00
</DropdownMenu.TriggerItem>
<DropdownMenu.Content as={MenuContent} sideOffset={2} alignOffset={-2}>
2021-07-10 16:14:49 +00:00
{children}
<DropdownMenuArrow offset={13} />
2021-07-14 13:45:24 +00:00
</DropdownMenu.Content>
2021-07-10 16:14:49 +00:00
</DropdownMenu.Root>
)
}
export const DropdownMenuDivider = styled(DropdownMenu.Separator, {
backgroundColor: '$hover',
height: 1,
2021-07-10 20:39:29 +00:00
marginTop: '$2',
marginRight: '-$2',
marginBottom: '$2',
marginLeft: '-$2',
2021-07-10 16:14:49 +00:00
})
export const DropdownMenuArrow = styled(DropdownMenu.Arrow, {
fill: '$panel',
})
export function DropdownMenuButton({
onSelect,
children,
disabled = false,
}: {
onSelect?: () => void
disabled?: boolean
children: React.ReactNode
}): JSX.Element {
return (
2021-07-14 13:45:24 +00:00
<DropdownMenu.Item
as={RowButton}
2021-07-10 16:14:49 +00:00
bp={breakpoints}
disabled={disabled}
onSelect={onSelect}
>
{children}
2021-07-14 13:45:24 +00:00
</DropdownMenu.Item>
2021-07-10 16:14:49 +00:00
)
}
2021-07-10 20:39:29 +00:00
interface DropdownMenuIconButtonProps {
onSelect: () => void
disabled?: boolean
children: React.ReactNode
}
2021-07-10 16:14:49 +00:00
export function DropdownMenuIconButton({
onSelect,
children,
disabled = false,
2021-07-10 20:39:29 +00:00
}: DropdownMenuIconButtonProps): JSX.Element {
return (
2021-07-14 13:45:24 +00:00
<DropdownMenu.Item
as={IconButton}
2021-07-10 20:39:29 +00:00
bp={breakpoints}
disabled={disabled}
onSelect={onSelect}
>
{children}
2021-07-14 13:45:24 +00:00
</DropdownMenu.Item>
)
}
interface DropdownMenuIconTriggerButtonProps {
label: string
kbd?: string
disabled?: boolean
children: React.ReactNode
}
export function DropdownMenuIconTriggerButton({
label,
kbd,
children,
disabled = false,
}: DropdownMenuIconTriggerButtonProps): JSX.Element {
return (
<DropdownMenu.Trigger as={IconButton} bp={breakpoints} disabled={disabled}>
<Tooltip label={label} kbd={kbd}>
{children}
</Tooltip>
</DropdownMenu.Trigger>
2021-07-10 20:39:29 +00:00
)
}
interface MenuCheckboxItemProps {
checked: boolean
2021-07-10 16:14:49 +00:00
disabled?: boolean
2021-07-10 20:39:29 +00:00
onCheckedChange: (isChecked: boolean) => void
2021-07-10 16:14:49 +00:00
children: React.ReactNode
2021-07-10 20:39:29 +00:00
}
export function DropdownMenuCheckboxItem({
checked,
disabled = false,
onCheckedChange,
children,
}: MenuCheckboxItemProps): JSX.Element {
2021-07-10 16:14:49 +00:00
return (
2021-07-14 13:45:24 +00:00
<DropdownMenu.CheckboxItem
as={RowButton}
2021-07-10 16:14:49 +00:00
bp={breakpoints}
2021-07-10 20:39:29 +00:00
onCheckedChange={onCheckedChange}
checked={checked}
2021-07-10 16:14:49 +00:00
disabled={disabled}
>
{children}
2021-07-10 20:39:29 +00:00
<DropdownMenu.ItemIndicator>
<IconWrapper size="small">
<CheckIcon />
</IconWrapper>
</DropdownMenu.ItemIndicator>
2021-07-14 13:45:24 +00:00
</DropdownMenu.CheckboxItem>
2021-07-10 16:14:49 +00:00
)
}
/* -------------------------------------------------- */
/* Context Menu */
/* -------------------------------------------------- */
export function ContextMenuRoot({
onOpenChange,
children,
}: {
onOpenChange?: (isOpen: boolean) => void
children: React.ReactNode
}): JSX.Element {
return (
<ContextMenu.Root dir="ltr" onOpenChange={onOpenChange}>
{children}
</ContextMenu.Root>
)
}
export function ContextMenuSubMenu({
children,
label,
}: {
label: string
children: React.ReactNode
}): JSX.Element {
return (
<ContextMenu.Root dir="ltr">
<ContextMenu.TriggerItem as={RowButton} bp={breakpoints}>
<span>{label}</span>
<IconWrapper size="small">
<ChevronRightIcon />
</IconWrapper>
</ContextMenu.TriggerItem>
<ContextMenu.Content as={MenuContent} sideOffset={2} alignOffset={-2}>
2021-07-10 16:14:49 +00:00
{children}
<ContextMenuArrow offset={13} />
</ContextMenu.Content>
</ContextMenu.Root>
)
}
export const ContextMenuDivider = styled(ContextMenu.Separator, {
backgroundColor: '$hover',
height: 1,
margin: '$2 -$2',
})
export const ContextMenuArrow = styled(ContextMenu.Arrow, {
fill: '$panel',
})
export function ContextMenuButton({
onSelect,
children,
disabled = false,
}: {
onSelect?: () => void
disabled?: boolean
children: React.ReactNode
}): JSX.Element {
return (
2021-07-10 20:39:29 +00:00
<RowButton
as={ContextMenu.Item}
2021-07-10 16:14:49 +00:00
bp={breakpoints}
disabled={disabled}
onSelect={onSelect}
>
{children}
2021-07-10 20:39:29 +00:00
</RowButton>
2021-07-10 16:14:49 +00:00
)
}
export function ContextMenuIconButton({
onSelect,
children,
disabled = false,
}: {
onSelect: () => void
disabled?: boolean
children: React.ReactNode
}): JSX.Element {
return (
<ContextMenu.Item
as={IconButton}
bp={breakpoints}
disabled={disabled}
onSelect={onSelect}
>
{children}
</ContextMenu.Item>
)
}
2021-07-10 20:39:29 +00:00
export function ContextMenuCheckboxItem({
checked,
disabled = false,
onCheckedChange,
children,
}: MenuCheckboxItemProps): JSX.Element {
return (
2021-07-14 13:45:24 +00:00
<ContextMenu.CheckboxItem
as={RowButton}
2021-07-10 20:39:29 +00:00
bp={breakpoints}
onCheckedChange={onCheckedChange}
checked={checked}
disabled={disabled}
>
{children}
<ContextMenu.ItemIndicator>
<IconWrapper size="small">
<CheckIcon />
</IconWrapper>
</ContextMenu.ItemIndicator>
2021-07-14 13:45:24 +00:00
</ContextMenu.CheckboxItem>
2021-07-10 20:39:29 +00:00
)
}