[fix] mobile style panel switching open / closed (#2101)

This PR fixes a bug where the mobile style panel would be immediately
re-opened when clicking on the style panel icon on mobile.

### Change Type

- [x] `patch` — Bug fix

### Test Plan

1. On mobile, tap the style panel to open it
2. Tap again to close it.

### Release Notes

- Fix bug with style panel
This commit is contained in:
Steve Ruiz 2023-10-17 17:02:34 +01:00 committed by GitHub
parent ed95b6252d
commit 23ab6ff870
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 27 deletions

View file

@ -6,7 +6,6 @@ import {
useValue,
} from '@tldraw/editor'
import { useCallback } from 'react'
import { useOpenMenuCloser } from '../hooks/useOpenMenuCloser'
import { useRelevantStyles } from '../hooks/useRevelantStyles'
import { useTranslation } from '../hooks/useTranslation/useTranslation'
import { StylePanel } from './StylePanel/StylePanel'
@ -40,8 +39,6 @@ export function MobileStylePanel() {
[editor]
)
const extraEventsToToggleMenu = useOpenMenuCloser()
return (
<Popover id="style menu" onOpenChange={handleStylesOpenChange}>
<PopoverTrigger disabled={disableStylePanel}>
@ -52,7 +49,6 @@ export function MobileStylePanel() {
color: disableStylePanel ? 'var(--color-muted-1)' : currentColor,
}}
title={msg('style-panel.title')}
{...extraEventsToToggleMenu}
>
<Icon icon={disableStylePanel ? 'blob' : color?.type === 'mixed' ? 'mixed' : 'blob'} />
</Button>

View file

@ -8,9 +8,17 @@ export function useMenuIsOpen(id: string, cb?: (isOpen: boolean) => void) {
const rIsOpen = useRef(false)
const trackEvent = useUiEvents()
const rLastChange = useRef(0)
const onOpenChange = useCallback(
(isOpen: boolean) => {
// prevent multiple calls in quick succession
const now = Date.now()
if (now - rLastChange.current < 50) return
rLastChange.current = now
rIsOpen.current = isOpen
editor.batch(() => {
if (isOpen) {
editor.complete()

View file

@ -1,23 +0,0 @@
import { preventDefault, useEditor } from '@tldraw/editor'
import { useCallback, useRef } from 'react'
export function useOpenMenuCloser() {
const editor = useEditor()
const rIsPointingToClose = useRef(false)
const handlePointerDown = useCallback(() => {
if (editor.openMenus.length > 0) {
editor.updateInstanceState({ openMenus: [] })
rIsPointingToClose.current = true
}
}, [editor])
const handleClick = useCallback((e: React.MouseEvent) => {
if (rIsPointingToClose.current) {
preventDefault(e)
}
rIsPointingToClose.current = false
}, [])
return { handlePointerDown, handleClick }
}