1
0
Fork 0
mirror of synced 2024-10-03 19:43:32 +13:00

Refactor clickoutside fix flashing side panel issues, but also fix annoying drag-outside closing actions

This commit is contained in:
Andrew Kingston 2024-05-02 16:46:58 +01:00
parent df25ff307c
commit ff23825241
3 changed files with 30 additions and 21 deletions

View file

@ -9,14 +9,13 @@ const conditionallyIgnoredClasses = [
".spectrum-Popover",
]
let clickHandlers = []
let candidateTarget
/**
* Handle a body click event
*/
const handleClick = event => {
// Treat right clicks (context menu events) as normal clicks
const eventType = event.type === "contextmenu" ? "click" : event.type
console.log("CLOSE")
// Ignore click if this is an ignored class
if (event.target.closest('[data-ignore-click-outside="true"]')) {
return
@ -29,11 +28,6 @@ const handleClick = event => {
// Process handlers
clickHandlers.forEach(handler => {
// Check that we're the right kind of click event
if (handler.allowedType && eventType !== handler.allowedType) {
return
}
// Check that the click isn't inside the target
if (handler.element.contains(event.target)) {
return
@ -51,17 +45,34 @@ const handleClick = event => {
handler.callback?.(event)
})
}
document.documentElement.addEventListener("click", handleClick, true)
document.documentElement.addEventListener("mousedown", handleClick, true)
document.documentElement.addEventListener("contextmenu", handleClick, true)
const handleMouseUp = e => {
console.log("up")
if (candidateTarget === e.target) {
handleClick(e)
}
candidateTarget = null
}
const handleMouseDown = e => {
if (e.button !== 0) {
return
}
candidateTarget = e.target
document.removeEventListener("mouseup", handleMouseUp)
document.addEventListener("mouseup", handleMouseUp, true)
}
document.addEventListener("mousedown", handleMouseDown)
document.addEventListener("contextmenu", handleClick)
/**
* Adds or updates a click handler
*/
const updateHandler = (id, element, anchor, callback, allowedType) => {
const updateHandler = (id, element, anchor, callback) => {
let existingHandler = clickHandlers.find(x => x.id === id)
if (!existingHandler) {
clickHandlers.push({ id, element, anchor, callback, allowedType })
clickHandlers.push({ id, element, anchor, callback })
} else {
existingHandler.callback = callback
}
@ -88,8 +99,7 @@ export default (element, opts) => {
const callback =
newOpts?.callback || (typeof newOpts === "function" ? newOpts : null)
const anchor = newOpts?.anchor || element
const allowedType = newOpts?.allowedType || "click"
updateHandler(id, element, anchor, callback, allowedType)
updateHandler(id, element, anchor, callback)
}
update(opts)
return {

View file

@ -324,10 +324,7 @@
<div
id="side-panel-container"
class:open={$sidePanelStore.open}
use:clickOutside={{
callback: autoCloseSidePanel ? sidePanelStore.actions.close : null,
allowedType: "mousedown",
}}
use:clickOutside={autoCloseSidePanel ? sidePanelStore.actions.close : null}
class:builder={$builderStore.inBuilder}
>
<div class="side-panel-header">

View file

@ -121,8 +121,10 @@
const onContextMenu = e => {
e.preventDefault()
ui.actions.blur()
open = !open
setTimeout(() => {
ui.actions.blur()
open = !open
}, 10)
}
const sortAscending = () => {