tldraw/components/menu/menu.tsx

122 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-07-10 16:14:49 +00:00
import * as React from 'react'
import { ExitIcon, HamburgerMenuIcon } from '@radix-ui/react-icons'
2021-07-10 16:14:49 +00:00
import { Trigger, Content } from '@radix-ui/react-dropdown-menu'
import { memo } from 'react'
import {
FloatingContainer,
DropdownMenuRoot,
MenuContent,
IconButton,
breakpoints,
DropdownMenuButton,
DropdownMenuSubMenu,
DropdownMenuDivider,
2021-07-10 20:39:29 +00:00
DropdownMenuCheckboxItem,
IconWrapper,
Kbd,
2021-07-10 16:14:49 +00:00
} from '../shared'
2021-07-10 20:39:29 +00:00
import state, { useSelector } from 'state'
2021-07-10 16:14:49 +00:00
import { commandKey } from 'utils'
import { signOut } from 'next-auth/client'
2021-07-10 16:14:49 +00:00
const handleNew = () => state.send('CREATED_NEW_PROJECT')
const handleSave = () => state.send('SAVED')
const handleLoad = () => state.send('LOADED_FROM_FILE_STSTEM')
const toggleDarkMode = () => state.send('TOGGLED_DARK_MODE')
const toggleDebugMode = () => state.send('TOGGLED_DEBUG_MODE')
2021-07-10 16:14:49 +00:00
function Menu() {
return (
<FloatingContainer>
<DropdownMenuRoot>
<IconButton as={Trigger} bp={breakpoints}>
<HamburgerMenuIcon />
</IconButton>
<Content as={MenuContent} sideOffset={8}>
2021-07-10 20:39:29 +00:00
<DropdownMenuButton onSelect={handleNew} disabled>
2021-07-10 16:14:49 +00:00
<span>New Project</span>
<Kbd>
2021-07-10 16:14:49 +00:00
<span>{commandKey()}</span>
<span>N</span>
</Kbd>
2021-07-10 16:14:49 +00:00
</DropdownMenuButton>
<DropdownMenuDivider />
<DropdownMenuButton onSelect={handleLoad}>
<span>Open...</span>
<Kbd>
2021-07-10 16:14:49 +00:00
<span>{commandKey()}</span>
<span>L</span>
</Kbd>
2021-07-10 16:14:49 +00:00
</DropdownMenuButton>
<RecentFiles />
<DropdownMenuDivider />
<DropdownMenuButton onSelect={handleSave}>
<span>Save</span>
<Kbd>
2021-07-10 16:14:49 +00:00
<span>{commandKey()}</span>
<span>S</span>
</Kbd>
2021-07-10 16:14:49 +00:00
</DropdownMenuButton>
<DropdownMenuButton onSelect={handleSave}>
<span>Save As...</span>
<Kbd>
2021-07-10 16:14:49 +00:00
<span></span>
<span>{commandKey()}</span>
<span>S</span>
</Kbd>
2021-07-10 16:14:49 +00:00
</DropdownMenuButton>
<DropdownMenuDivider />
<Preferences />
<DropdownMenuDivider />
<DropdownMenuButton onSelect={signOut}>
<span>Sign Out</span>
<IconWrapper size="small">
<ExitIcon />
</IconWrapper>
</DropdownMenuButton>
2021-07-10 16:14:49 +00:00
</Content>
</DropdownMenuRoot>
</FloatingContainer>
)
}
export default memo(Menu)
function RecentFiles() {
return (
2021-07-10 20:39:29 +00:00
<DropdownMenuSubMenu label="Open Recent..." disabled>
2021-07-10 16:14:49 +00:00
<DropdownMenuButton>
<span>Project A</span>
</DropdownMenuButton>
<DropdownMenuButton>
<span>Project B</span>
</DropdownMenuButton>
<DropdownMenuButton>
<span>Project C</span>
</DropdownMenuButton>
</DropdownMenuSubMenu>
)
}
function Preferences() {
const isDebugMode = useSelector((s) => s.data.settings.isDebugMode)
2021-07-10 20:39:29 +00:00
const isDarkMode = useSelector((s) => s.data.settings.isDarkMode)
2021-07-10 16:14:49 +00:00
return (
<DropdownMenuSubMenu label="Preferences">
2021-07-10 20:39:29 +00:00
<DropdownMenuCheckboxItem
checked={isDarkMode}
onCheckedChange={toggleDarkMode}
>
<span>Dark Mode</span>
</DropdownMenuCheckboxItem>
<DropdownMenuCheckboxItem
checked={isDebugMode}
onCheckedChange={toggleDebugMode}
>
<span>Debug Mode</span>
</DropdownMenuCheckboxItem>
2021-07-10 16:14:49 +00:00
</DropdownMenuSubMenu>
)
}