Add unfocus button

This commit is contained in:
Steve Ruiz 2021-09-22 12:40:53 +01:00
parent 4a99c13006
commit 7bc01c70f5

View file

@ -17,6 +17,8 @@ import { StylePanel } from '~components/style-panel'
import { ToolsPanel } from '~components/tools-panel' import { ToolsPanel } from '~components/tools-panel'
import { PagePanel } from '~components/page-panel' import { PagePanel } from '~components/page-panel'
import { Menu } from '~components/menu' import { Menu } from '~components/menu'
import { breakpoints, iconButton } from '~components'
import { DotFilledIcon } from '@radix-ui/react-icons'
// Selectors // Selectors
const isInSelectSelector = (s: Data) => s.appState.activeTool === 'select' const isInSelectSelector = (s: Data) => s.appState.activeTool === 'select'
@ -115,11 +117,9 @@ function InnerTldraw({
}) { }) {
const { tlstate, useSelector } = useTLDrawContext() const { tlstate, useSelector } = useTLDrawContext()
const rTheme = React.useRef<HTMLDivElement>(null)
const rWrapper = React.useRef<HTMLDivElement>(null) const rWrapper = React.useRef<HTMLDivElement>(null)
useThemeEffect(rTheme) useThemeEffect(rWrapper)
const page = useSelector(pageSelector) const page = useSelector(pageSelector)
@ -180,9 +180,9 @@ function InnerTldraw({
}, [currentPageId, tlstate]) }, [currentPageId, tlstate])
return ( return (
<div ref={rTheme}> <div ref={rWrapper} tabIndex={0}>
<div ref={rWrapper} className={layout()} tabIndex={0}> <div className={layout()}>
<OneOff rWrapper={rWrapper} autofocus={autofocus} /> <OneOff focusableRef={rWrapper} autofocus={autofocus} />
<ContextMenu> <ContextMenu>
<Renderer <Renderer
id={id} id={id}
@ -242,7 +242,13 @@ function InnerTldraw({
onBoundsChange={tlstate.updateBounds} onBoundsChange={tlstate.updateBounds}
/> />
</ContextMenu> </ContextMenu>
{!isFocusMode && ( {isFocusMode ? (
<div className={unfocusButton()}>
<button className={iconButton({ bp: breakpoints })} onClick={tlstate.toggleFocusMode}>
<DotFilledIcon />
</button>
</div>
) : (
<> <>
<div className={menuButtons()}> <div className={menuButtons()}>
<Menu /> <Menu />
@ -259,13 +265,19 @@ function InnerTldraw({
} }
const OneOff = React.memo( const OneOff = React.memo(
({ rWrapper, autofocus }: { autofocus?: boolean; rWrapper: React.RefObject<HTMLDivElement> }) => { ({
useKeyboardShortcuts(rWrapper) focusableRef,
autofocus,
}: {
autofocus?: boolean
focusableRef: React.RefObject<HTMLDivElement>
}) => {
useKeyboardShortcuts(focusableRef)
useCustomFonts() useCustomFonts()
React.useEffect(() => { React.useEffect(() => {
if (autofocus) { if (autofocus) {
rWrapper.current?.focus() focusableRef.current?.focus()
} }
}, [autofocus]) }, [autofocus])
@ -315,3 +327,17 @@ const menuButtons = css({
display: 'flex', display: 'flex',
gap: 8, gap: 8,
}) })
const unfocusButton = css({
opacity: 1,
zIndex: 100,
backgroundColor: 'transparent',
'& svg': {
color: '$muted',
},
'&:hover svg': {
color: '$text',
},
})