-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
211 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<template> | ||
<div :class="$style.menuBar"> | ||
<n-dropdown v-for="item in props.items" :key="item.key" :class="$style.dropdown" :options="item.children" | ||
placement="bottom-start" :show="showingItem == item.key" @select="handleSelect" | ||
@clickoutside="handleClickOutside"> | ||
<n-button quaternary :ref="(el) => bindMenuButton(item.key as string, el as ComponentPublicInstance | null)" | ||
@mouseenter="(e) => handleMouseEnter(e, item.key as string)" @click="(e) => handleClick(e, item.key as string)"> | ||
{{ item.label }} | ||
</n-button> | ||
</n-dropdown> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { defineProps, ref, type ComponentPublicInstance } from 'vue'; | ||
import { | ||
type DropdownOption, | ||
NButton, | ||
NDropdown, | ||
} from 'naive-ui'; | ||
const props = defineProps<{ | ||
items: DropdownOption[]; | ||
}>(); | ||
const showingItem = ref<string | null>(null); | ||
const menuButtons: Record<string, HTMLElement> = {}; | ||
function bindMenuButton(key: string, el: ComponentPublicInstance | null): void { | ||
if (el) { | ||
menuButtons[key] = el.$el; | ||
} else { | ||
delete menuButtons[key]; | ||
} | ||
} | ||
function handleClick(_e: MouseEvent, key: string): void { | ||
if (showingItem.value == null) { | ||
showingItem.value = key; | ||
} else { | ||
showingItem.value = null; | ||
} | ||
} | ||
function handleMouseEnter(_e: MouseEvent, key: string): void { | ||
if (showingItem.value != null) { | ||
menuButtons[showingItem.value]?.blur(); | ||
showingItem.value = key; | ||
menuButtons[showingItem.value]?.focus(); | ||
} | ||
} | ||
function handleSelect(): void { | ||
showingItem.value = null; | ||
} | ||
function handleClickOutside(e: MouseEvent): void { | ||
if (showingItem.value && menuButtons[showingItem.value]?.contains(e.target as Node)) { | ||
return; | ||
} | ||
showingItem.value = null; | ||
} | ||
</script> | ||
|
||
<style lang="scss" module> | ||
.menuBar { | ||
display: flex; | ||
border-bottom: 1px solid #ddd; | ||
box-sizing: border-box; | ||
} | ||
.dropdown { | ||
min-width: 200px; | ||
} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<template> | ||
<div :class="$style.setter"> | ||
<span>Icon size</span> | ||
<n-slider v-model:value="editorStore.size" :min="20" :max="60" /> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { | ||
NSlider, | ||
} from 'naive-ui'; | ||
import { useEditorStore } from '@/stores/editor'; | ||
const editorStore = useEditorStore(); | ||
</script> | ||
|
||
<style lang="scss" module> | ||
.setter { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 4px; | ||
width: 200px; | ||
padding: 8px var(--n-option-prefix-width); | ||
font-size: var(--n-font-size); | ||
} | ||
</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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { type UnwrapRef, onBeforeUnmount, onMounted, ref } from 'vue'; | ||
|
||
export default function useDomEvented<T>(target: EventTarget, type: string, getter: () => T) { | ||
const value = ref<T>(getter()); | ||
|
||
const listener = () => { | ||
value.value = getter() as UnwrapRef<T>; | ||
}; | ||
|
||
onMounted(() => { | ||
target.addEventListener(type, listener); | ||
}); | ||
|
||
onBeforeUnmount(() => { | ||
target.removeEventListener(type, listener); | ||
}); | ||
|
||
return value; | ||
} |