tldraw/components/menu/menu.tsx

124 lines
3.5 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-14 13:45:24 +00:00
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'
2021-07-10 16:14:49 +00:00
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'
import { useTheme } from 'next-themes'
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 toggleDebugMode = () => state.send('TOGGLED_DEBUG_MODE')
2021-07-10 16:14:49 +00:00
function Menu() {
return (
<FloatingContainer>
<DropdownMenuRoot>
2021-07-14 13:45:24 +00:00
<DropdownMenu.Trigger as={IconButton} bp={breakpoints}>
2021-07-10 16:14:49 +00:00
<HamburgerMenuIcon />
2021-07-14 13:45:24 +00:00
</DropdownMenu.Trigger>
<DropdownMenu.Content as={MenuContent} sideOffset={8} align="end">
<DropdownMenuButton onSelect={handleNew} disabled={true}>
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-14 13:45:24 +00:00
</DropdownMenu.Content>
2021-07-10 16:14:49 +00:00
</DropdownMenuRoot>
</FloatingContainer>
)
}
export default memo(Menu)
function RecentFiles() {
return (
2021-07-14 13:45:24 +00:00
<DropdownMenuSubMenu label="Open Recent..." disabled={true}>
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 { theme, setTheme } = useTheme()
const isDebugMode = useSelector((s) => s.data.settings.isDebugMode)
const isDarkMode = theme === 'dark'
2021-07-10 20:39:29 +00:00
2021-07-10 16:14:49 +00:00
return (
<DropdownMenuSubMenu label="Preferences">
2021-07-10 20:39:29 +00:00
<DropdownMenuCheckboxItem
checked={isDarkMode}
onCheckedChange={() => setTheme(isDarkMode ? 'light' : 'dark')}
2021-07-10 20:39:29 +00:00
>
<span>Dark Mode</span>
</DropdownMenuCheckboxItem>
<DropdownMenuCheckboxItem
checked={isDebugMode}
onCheckedChange={toggleDebugMode}
>
<span>Debug Mode</span>
</DropdownMenuCheckboxItem>
2021-07-10 16:14:49 +00:00
</DropdownMenuSubMenu>
)
}