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

107 lines
2.9 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,
IconWrapper,
} from 'components/shared'
import PageOptions from './page-options'
import { 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-07-22 10:10:02 +00:00
function handleCreatePage() {
state.send('CREATED_PAGE')
}
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>
2021-07-14 11:46:19 +00:00
<RowButton as={DropdownMenu.Trigger} bp={breakpoints} variant="noIcon">
<span>{documentPages[currentPageId].name}</span>
2021-07-10 20:39:29 +00:00
</RowButton>
</FloatingContainer>
<MenuContent as={DropdownMenu.Content} sideOffset={8} align="start">
2021-07-10 20:39:29 +00:00
<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((page) => (
<ButtonWithOptions key={page.id}>
2021-07-10 20:39:29 +00:00
<DropdownMenu.RadioItem
as={RowButton}
bp={breakpoints}
value={page.id}
2021-07-10 20:39:29 +00:00
variant="pageButton"
>
<span>{page.name}</span>
2021-07-10 20:39:29 +00:00
<DropdownMenu.ItemIndicator>
<IconWrapper size="small">
2021-07-10 20:39:29 +00:00
<CheckIcon />
</IconWrapper>
</DropdownMenu.ItemIndicator>
</DropdownMenu.RadioItem>
<PageOptions page={page} />
2021-07-10 20:39:29 +00:00
</ButtonWithOptions>
))}
</DropdownMenu.RadioGroup>
<DropdownMenuDivider />
2021-07-22 10:10:02 +00:00
<DropdownMenuButton onSelect={handleCreatePage}>
2021-07-10 20:39:29 +00:00
<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
},
})