feature/tauri-v2
svelte 106 lines 2.75 KB
Raw
1 <script lang="ts">
2 import { afterUpdate } from 'svelte';
3 export let menu;
4
5 export let right = false;
6
7 let button = null;
8 let dropdownList = null;
9 let open = false;
10
11 afterUpdate(() => {
12 if (open && dropdownList) {
13 const { height , left, width, top } = button.getBoundingClientRect();
14 dropdownList.style.top = `${height}px`;
15 if (right) {
16 dropdownList.style.right = `0px`;
17 dropdownList.style.left = `auto`;
18 }
19 else {
20 dropdownList.style.left = `${0}px`;
21 }
22 }
23 })
24 </script>
25
26 <svelte:window on:click={(e) => {
27 if (open && !button.contains(e.target)) {
28 open = false;
29 }
30 }}></svelte:window>
31
32 <!-- svelte-ignore a11y-click-events-have-key-events -->
33 <div bind:this={button} class="menu-button" class:open on:click={() => {open = !open}}>
34 {#if menu.icon}
35 <svelte:component this={menu.icon}></svelte:component>
36 {:else}
37 {menu.menuname}
38 {/if}
39 {#if open}
40 <div bind:this={dropdownList} class="menu-list">
41 {#each menu.children as child}
42 <!-- svelte-ignore a11y-click-events-have-key-events -->
43 <div class="menu-item" class:disabled={child.disabled} on:click={() => {
44 if (!child.disabled) {
45 child.action();
46 }
47 }}>
48 <span class="item-name">
49 {child.name}
50 </span>
51 {#if child.shortcut}
52 <span class="shortcut">{child.shortcut}</span>
53 {/if}
54 </div>
55 {/each}
56 </div>
57 {/if}
58 </div>
59
60 <style lang="scss">
61 .menu-button {
62 height: 100%;
63 display: flex;
64 align-items: center;
65 justify-content: center;
66 font-size: 0.875rem;
67 min-width: 2.2rem;
68 padding: 0 5px;
69 position: relative;
70 cursor: pointer;
71 :global(svg) {
72 width: 18px;
73 height: 18px;
74 }
75 }
76 .menu-list {
77 position: absolute;
78 display: flex;
79 flex-direction: column;
80 justify-content: center;
81 min-width: 11.5rem;
82 width: max-content;
83 align-items: center;
84 border-radius: 3px;
85 z-index: 9999;
86 }
87 .menu-item {
88 display: flex;
89 align-items: center;
90 justify-content: space-between;
91 height: 17px;
92 padding: 7px 0;
93 width: 100%;
94 font-size: 0.875rem;
95 cursor: pointer;
96 }
97 .item-name, .shortcut {
98 padding: 0 10px;
99 }
100 .shortcut {
101 margin-left: 20px;
102 }
103 .disabled {
104 cursor: not-allowed;
105 }
106 </style>