tldraw/components/page-panel/page-panel.tsx

105 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-06-03 12:06:39 +00:00
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'
2021-07-10 20:39:29 +00:00
import styled from 'styles'
import {
breakpoints,
DropdownMenuButton,
DropdownMenuDivider,
RowButton,
MenuContent,
FloatingContainer,
IconButton,
IconWrapper,
} from 'components/shared'
import { MixerVerticalIcon, PlusIcon, CheckIcon } from '@radix-ui/react-icons'
2021-06-03 12:06:39 +00:00
import state, { useSelector } from 'state'
2021-06-03 13:10:54 +00:00
import { useEffect, useRef, useState } from 'react'
2021-06-03 12:06:39 +00:00
2021-06-21 21:35:28 +00:00
export default function PagePanel(): JSX.Element {
2021-06-03 13:10:54 +00:00
const rIsOpen = useRef(false)
const [isOpen, setIsOpen] = useState(false)
useEffect(() => {
if (rIsOpen.current !== isOpen) {
rIsOpen.current = isOpen
}
}, [isOpen])
2021-06-03 12:06:39 +00:00
const documentPages = useSelector((s) => s.data.document.pages)
2021-06-03 13:10:54 +00:00
const currentPageId = useSelector((s) => s.data.currentPageId)
if (!documentPages[currentPageId]) return null
2021-06-03 12:06:39 +00:00
const sorted = Object.values(documentPages).sort(
(a, b) => a.childIndex - b.childIndex
)
return (
<DropdownMenu.Root
dir="ltr"
open={isOpen}
onOpenChange={(isOpen) => {
if (rIsOpen.current !== isOpen) {
setIsOpen(isOpen)
}
}}
>
2021-07-10 20:39:29 +00:00
<FloatingContainer>
<RowButton as={DropdownMenu.Trigger} bp={breakpoints}>
<span>{documentPages[currentPageId].name}</span>
2021-07-10 20:39:29 +00:00
</RowButton>
</FloatingContainer>
<MenuContent as={DropdownMenu.Content} sideOffset={8}>
<DropdownMenu.RadioGroup
value={currentPageId}
2021-07-10 22:07:53 +00:00
onValueChange={(id) => {
2021-07-10 20:39:29 +00:00
setIsOpen(false)
state.send('CHANGED_PAGE', { id })
}}
>
{sorted.map(({ id, name }) => (
<ButtonWithOptions key={id}>
<DropdownMenu.RadioItem
as={RowButton}
bp={breakpoints}
value={id}
variant="pageButton"
>
<span>{name}</span>
<DropdownMenu.ItemIndicator>
<IconWrapper>
<CheckIcon />
</IconWrapper>
</DropdownMenu.ItemIndicator>
</DropdownMenu.RadioItem>
<IconButton bp={breakpoints} size="small" data-shy="true">
<MixerVerticalIcon />
</IconButton>
</ButtonWithOptions>
))}
</DropdownMenu.RadioGroup>
<DropdownMenuDivider />
<DropdownMenuButton onSelect={() => state.send('CREATED_PAGE')}>
<span>Create Page</span>
<IconWrapper size="small">
<PlusIcon />
</IconWrapper>
</DropdownMenuButton>
</MenuContent>
</DropdownMenu.Root>
2021-06-03 12:06:39 +00:00
)
}
2021-07-10 20:39:29 +00:00
const ButtonWithOptions = styled('div', {
display: 'grid',
gridTemplateColumns: '1fr auto',
gridAutoFlow: 'column',
2021-06-03 12:06:39 +00:00
2021-07-10 20:39:29 +00:00
'& > *[data-shy="true"]': {
opacity: 0,
2021-06-03 13:10:54 +00:00
},
2021-07-10 20:39:29 +00:00
'&:hover > *[data-shy="true"]': {
opacity: 1,
2021-06-03 13:10:54 +00:00
},
})