Skip to content

Commit 6ac1cd3

Browse files
authored
Refactor Popover & Menu Items (#18)
* Refactor Popover & Menu Items * Remove reduntant console
1 parent 5cf95da commit 6ac1cd3

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

src/components/AppSidebar.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AppStorage } from '../storage'
99
import { Icons } from './Icons'
1010
import SidebarButton from './SidebarButton.vue'
1111
import Popover from './Popover.vue'
12-
import MenuItemsVue, { menuItem } from './MenuItems.vue'
12+
import MenuItems, { menuItem } from './MenuItems.vue'
1313
1414
const store = useStore()
1515
@@ -20,7 +20,7 @@ function openCurrentReleaseChangelog() {
2020
const moreItems = computed(() => [
2121
menuItem({
2222
key: 'changelog',
23-
meta: { text: 'Changelog', icon: Icons.BellSlash },
23+
meta: { text: 'Changelog', icon: Icons.Info16 },
2424
onSelect: openCurrentReleaseChangelog,
2525
}),
2626
menuItem({
@@ -82,7 +82,7 @@ const moreItems = computed(() => [
8282
</SidebarButton> -->
8383

8484
<Popover
85-
:wowerlay-options="{
85+
:wowerlayOptions="{
8686
position: 'right-end',
8787
noBackground: true,
8888
style: 'display: flex; flex-direction: column: flex-wrap: nowrap: gap: 5px; padding: 5px;',
@@ -95,7 +95,7 @@ const moreItems = computed(() => [
9595
</template>
9696

9797
<template #content>
98-
<MenuItemsVue :items="moreItems" />
98+
<MenuItems :items="moreItems" />
9999
</template>
100100
</Popover>
101101
</div>

src/components/Icons.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import IssueOpenedIcon from 'virtual:icons/octicon/issue-opened-24'
2121
import TagIcon from 'virtual:icons/octicon/tag-24'
2222
import AlertIcon from 'virtual:icons/octicon/alert-24'
2323
import QuestionIcon from 'virtual:icons/octicon/question-24'
24+
import InfoIcon16 from 'virtual:icons/octicon/info-16'
2425
import MailIcon from 'virtual:icons/octicon/mail-24'
2526
import SignOutIcon16 from 'virtual:icons/octicon/sign-out-16'
2627
import ChevronLeftIcon from 'virtual:icons/octicon/chevron-left'
@@ -59,4 +60,5 @@ export const Icons = {
5960
Download16: markRaw(DownloadIcon16),
6061
Command: markRaw(CommandIcon),
6162
BellSlash: markRaw(BellSlashIcon),
63+
Info16: markRaw(InfoIcon16),
6264
}

src/components/MenuItems.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ defineOptions({
3030
inheritAttrs: false,
3131
})
3232
33-
const popoverContext = usePopoverContext()
34-
3533
const setupHandle = (ctx: Context) => {
3634
useKey('up,shift+tab', () => ctx.focusPrevious(), { input: true, repeat: true, prevent: true })
3735
useKey('down,tab', () => ctx.focusNext(), { input: true, repeat: true, prevent: true })
@@ -46,6 +44,8 @@ const setupHandle = (ctx: Context) => {
4644
el?.focus()
4745
})
4846
47+
const popoverContext = usePopoverContext()
48+
4949
ctx.onSelect((meta, item) => {
5050
popoverContext.visible.value = false
5151
})
@@ -54,10 +54,10 @@ const setupHandle = (ctx: Context) => {
5454

5555
<template>
5656
<SelectableItems
57-
:item-defaults="itemDefaults"
57+
:itemDefaults="itemDefaults"
5858
:setup="setupHandle"
5959
:items="items"
60-
no-wrapper-element
60+
noWrapperElement
6161
>
6262
<template #render="{ meta }: Item<ItemMeta>">
6363
<div class="item-icon">
@@ -81,7 +81,7 @@ const setupHandle = (ctx: Context) => {
8181
padding: 5px 10px;
8282
outline: none;
8383
height: 32px;
84-
border-radius: 8px;
84+
border-radius: 4px; // 8px parent border radius - 4px parent padding
8585
color: var(--white-faded);
8686
8787
&,

src/components/Popover.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ interface Props {
3333
wowerlayOptions?: Partial<Omit<InstanceType<typeof Wowerlay>['$props'], 'visible' | 'target'>>
3434
}
3535
36-
const { renderSlot, element: target } = useSlotWithRef()
3736
const visible = ref(false)
3837
3938
provide(popoverContextKey, { visible })
@@ -80,6 +79,10 @@ watch(visible, (value) => {
8079
setTimeout(() => lastFocusedElement instanceof HTMLElement && lastFocusedElement.focus())
8180
}
8281
})
82+
83+
const { renderSlot, element: target } = useSlotWithRef('default', {
84+
slotPropsGetter: () => ({ visible: visible.value }),
85+
})
8386
</script>
8487

8588
<template>

src/composables/useSlotWithRef.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import { ref, useSlots, withDirectives } from 'vue'
22
import { type Directive, type Ref } from 'vue'
33

4+
export interface SlotWithRefOptions {
5+
slotPropsGetter: () => Record<string, any>
6+
}
7+
48
const vRef: Directive<HTMLElement, (el: HTMLElement) => void> = (el, { value }) => value(el)
59

610
/**
711
* @param slotName Target slot default value: "default"
812
*/
9-
export function useSlotWithRef<T = HTMLElement>(slotName = 'default') {
13+
export function useSlotWithRef<T = HTMLElement>(slotName = 'default', options?: SlotWithRefOptions) {
1014
const slots = useSlots()
1115
const element: Ref<T | null> = ref(null)
1216

1317
function handleRef(el: T) {
1418
element.value = el
1519
}
1620

17-
function renderSlot(props: any) {
21+
function renderSlot() {
1822
if (slotName in slots) {
19-
const [slot] = slots[slotName]!(props)
23+
const [slot] = slots[slotName]!(options?.slotPropsGetter?.())
2024
return withDirectives(slot, [[vRef, handleRef]])
2125
}
2226

0 commit comments

Comments
 (0)