Improve clickoutside to handle right clicks properly

This commit is contained in:
Andrew Kingston 2024-04-25 08:46:51 +01:00
parent 54621b1539
commit 80cbd70687
4 changed files with 21 additions and 21 deletions

View File

@ -15,6 +15,9 @@ let clickHandlers = []
* Handle a body click event * Handle a body click event
*/ */
const handleClick = event => { const handleClick = event => {
// Treat right clicks (context menu events) as normal clicks
const eventType = event.type === "contextmenu" ? "click" : event.type
// Ignore click if this is an ignored class // Ignore click if this is an ignored class
if (event.target.closest('[data-ignore-click-outside="true"]')) { if (event.target.closest('[data-ignore-click-outside="true"]')) {
return return
@ -28,7 +31,7 @@ const handleClick = event => {
// Process handlers // Process handlers
clickHandlers.forEach(handler => { clickHandlers.forEach(handler => {
// Check that we're the right kind of click event // Check that we're the right kind of click event
if (handler.allowedType && event.type !== handler.allowedType) { if (handler.allowedType && eventType !== handler.allowedType) {
return return
} }
@ -51,6 +54,7 @@ const handleClick = event => {
} }
document.documentElement.addEventListener("click", handleClick, true) document.documentElement.addEventListener("click", handleClick, true)
document.documentElement.addEventListener("mousedown", handleClick, true) document.documentElement.addEventListener("mousedown", handleClick, true)
document.documentElement.addEventListener("contextmenu", handleClick, true)
/** /**
* Adds or updates a click handler * Adds or updates a click handler

View File

@ -101,11 +101,10 @@
<style> <style>
.wrapper { .wrapper {
flex: 0 0 400px; flex: 0 0 100px;
margin: -28px -40px -40px -40px; margin: -28px -40px -40px -40px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
background: var(--background); background: var(--background);
overflow: hidden;
} }
</style> </style>

View File

@ -1,6 +1,7 @@
<script> <script>
import { getContext, onMount } from "svelte" import { getContext, onMount } from "svelte"
import { Icon, Popover, clickOutside } from "@budibase/bbui" import { Icon } from "@budibase/bbui"
import GridPopover from "../overlays/GridPopover.svelte"
const { visibleColumns, scroll, width, subscribe } = getContext("grid") const { visibleColumns, scroll, width, subscribe } = getContext("grid")
@ -32,23 +33,18 @@
> >
<Icon name="Add" /> <Icon name="Add" />
</div> </div>
<Popover {#if open}
bind:open <GridPopover
{anchor} {anchor}
align={$visibleColumns.length ? "right" : "left"} align={$visibleColumns.length ? "right" : "left"}
offset={0} on:close={close}
popoverTarget={document.getElementById(`add-column-button`)} maxHeight={null}
customZindex={50}
>
<div
use:clickOutside={() => {
open = false
}}
class="content"
> >
<slot /> <div class="content">
</div> <slot />
</Popover> </div>
</GridPopover>
{/if}
<style> <style>
.add { .add {

View File

@ -13,6 +13,7 @@
export let maxWidth = PopoverMaxWidth export let maxWidth = PopoverMaxWidth
export let maxHeight = PopoverMaxHeight export let maxHeight = PopoverMaxHeight
export let align = "left" export let align = "left"
export let open = true
const { gridID } = getContext("grid") const { gridID } = getContext("grid")
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
@ -35,7 +36,7 @@
</script> </script>
<Popover <Popover
open {open}
{anchor} {anchor}
{align} {align}
portalTarget="#{gridID} .grid-popover-container" portalTarget="#{gridID} .grid-popover-container"