-
-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1213 from centerofci/context_menu
Add ContextMenu component
- Loading branch information
Showing
5 changed files
with
219 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
mathesar_ui/src/component-library/context-menu/ContextMenu.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<script lang="ts"> | ||
import Menu from '@mathesar-component-library-dir/menu/Menu.svelte'; | ||
import popper from '@mathesar-component-library-dir/common/actions/popper'; | ||
import portal from '@mathesar-component-library-dir/common/actions/portal'; | ||
import clickOffBounds from '@mathesar-component-library-dir/common/actions/clickOffBounds'; | ||
import { onMount } from 'svelte'; | ||
/** | ||
* A reference to the DOM node where we'll attach the contextmenu event | ||
* listener. Right-clicking anywhere inside this element will open the context | ||
* menu. If you don't pass an element, we'll use the parent element wherever | ||
* this component is mounted. | ||
*/ | ||
let customTriggerElement: HTMLElement | undefined = undefined; | ||
export { customTriggerElement as triggerElement }; | ||
export let closeOnInnerClick = true; | ||
interface MousePosition { | ||
x: number; | ||
y: number; | ||
} | ||
let isVisible = false; | ||
let element: HTMLDivElement | undefined; | ||
let mousePosition: MousePosition = { x: 0, y: 0 }; | ||
function getVirtualReferenceElement(_mousePosition: MousePosition) { | ||
const { x, y } = _mousePosition; | ||
return { | ||
getBoundingClientRect: () => ({ | ||
width: 0, | ||
height: 0, | ||
x, | ||
y, | ||
top: y, | ||
right: x, | ||
bottom: y, | ||
left: x, | ||
toJSON: () => ({ x, y }), | ||
}), | ||
}; | ||
} | ||
function handleContextMenu(event: MouseEvent) { | ||
event.preventDefault(); | ||
mousePosition = { | ||
x: event.clientX, | ||
y: event.clientY, | ||
}; | ||
isVisible = !isVisible; | ||
} | ||
onMount(() => { | ||
const triggerElement = | ||
customTriggerElement ?? element?.parentElement ?? undefined; | ||
if (!triggerElement) { | ||
return () => {}; | ||
} | ||
triggerElement.addEventListener('contextmenu', handleContextMenu); | ||
return () => { | ||
triggerElement.removeEventListener('contextmenu', handleContextMenu); | ||
}; | ||
}); | ||
function close() { | ||
isVisible = false; | ||
} | ||
function checkAndCloseOnInnerClick() { | ||
if (closeOnInnerClick) { | ||
close(); | ||
} | ||
} | ||
</script> | ||
|
||
<div bind:this={element} class="context-menu-wrapper"> | ||
{#if isVisible} | ||
<div | ||
class="context-menu dropdown content" | ||
use:clickOffBounds={{ callback: close }} | ||
use:popper={{ reference: getVirtualReferenceElement(mousePosition) }} | ||
use:portal | ||
on:click={checkAndCloseOnInnerClick} | ||
> | ||
<Menu> | ||
<slot /> | ||
</Menu> | ||
</div> | ||
{/if} | ||
</div> |
75 changes: 75 additions & 0 deletions
75
mathesar_ui/src/component-library/context-menu/__meta__/ContextMenu.stories.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<script> | ||
import { | ||
faFillDrip, | ||
faPlus, | ||
faTimes, | ||
} from '@fortawesome/free-solid-svg-icons'; | ||
import { Meta, Story } from '@storybook/addon-svelte-csf'; | ||
import MenuItem from '@mathesar-component-library-dir/menu/MenuItem.svelte'; | ||
import Checkbox from '@mathesar-component-library-dir/checkbox/Checkbox.svelte'; | ||
import ContextMenu from '../ContextMenu.svelte'; | ||
const meta = { | ||
title: 'Components/ContextMenu', | ||
}; | ||
let count = 0; | ||
let hasBackground = true; | ||
function incrementCount() { | ||
count += 1; | ||
} | ||
function toggleBackground() { | ||
hasBackground = !hasBackground; | ||
} | ||
</script> | ||
|
||
<Meta {...meta} /> | ||
|
||
<Story name="Basic"> | ||
<div class="box without-context"> | ||
<p>This box does <em>not</em> have a context menu.</p> | ||
</div> | ||
|
||
<div class="box with-context" class:has-background={hasBackground}> | ||
<p>This box has a context menu.</p> | ||
<p>The count is: <strong>{count}</strong>.</p> | ||
<ContextMenu> | ||
<MenuItem icon={{ data: faFillDrip }} on:click={toggleBackground}> | ||
<Checkbox slot="control" checked={hasBackground} /> | ||
Use Background | ||
</MenuItem> | ||
<MenuItem icon={{ data: faPlus }} on:click={incrementCount}> | ||
Increment Counter | ||
</MenuItem> | ||
</ContextMenu> | ||
</div> | ||
|
||
<div class="box with-context" has-background> | ||
<p>This box <em>also</em> has a context menu.</p> | ||
<ContextMenu> | ||
<MenuItem icon={{ data: faTimes }}>I don't do anything</MenuItem> | ||
</ContextMenu> | ||
</div> | ||
|
||
<div class="box without-context"> | ||
<p>This box does <em>not</em> have a context menu.</p> | ||
</div> | ||
</Story> | ||
|
||
<style> | ||
.box { | ||
padding: 0 1em; | ||
border: solid 0.1em #ccc; | ||
border-radius: 0.5em; | ||
margin: 1em 0; | ||
} | ||
.with-context.has-background { | ||
background: rgb(220, 255, 199); | ||
} | ||
.without-context { | ||
background: rgb(255, 220, 199); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters