Skip to content

Commit

Permalink
enhance: MenuButtonのactiveにcomputedを受け付けられるように
Browse files Browse the repository at this point in the history
  • Loading branch information
hideki0403 committed Jan 21, 2024
1 parent 5515562 commit 7fa6d62
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions packages/frontend/src/components/MkMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<span :class="$style.caret" style="pointer-events: none;"><i class="ti ti-chevron-right ti-fw"></i></span>
</div>
</button>
<button v-else :tabindex="i" class="_button" role="menuitem" :class="[$style.item, { [$style.danger]: item.danger, [$style.active]: item.active }]" :disabled="item.active" @click="clicked(item.action, $event)" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
<button v-else :key="i" :tabindex="i" class="_button" role="menuitem" :class="[$style.item, { [$style.danger]: item.danger, [$style.active]: getValue(item.active) }]" :disabled="getValue(item.active)" @click="clicked(item.action, $event)" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
<i v-if="item.icon" class="ti-fw" :class="[$style.icon, item.icon]"></i>
<MkAvatar v-if="item.avatar" :user="item.avatar" :class="$style.avatar"/>
<div :class="$style.item_content">
Expand All @@ -74,7 +74,7 @@ SPDX-License-Identifier: AGPL-3.0-only
</template>

<script lang="ts">
import { computed, defineAsyncComponent, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue';
import { ComputedRef, computed, defineAsyncComponent, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue';
import { focusPrev, focusNext } from '@/scripts/focus.js';
import MkSwitchButton from '@/components/MkSwitch.button.vue';
import { MenuItem, InnerMenuItem, MenuPending, MenuAction, MenuSwitch, MenuParent } from '@/types/menu.js';
Expand Down Expand Up @@ -218,6 +218,14 @@ function switchItem(item: MenuSwitch & { ref: any }) {
item.ref = !item.ref;
}

function getValue<T>(item?: ComputedRef<T> | T) {
if (typeof item === 'object' && item !== null && 'value' in item) {
return item.value;
} else {
return item;
}
}

onMounted(() => {
if (props.viaKeyboard) {
nextTick(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/components/MkSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function show() {
const pushOption = (option: VNode) => {
menu.push({
text: option.children as string,
active: v.value === option.props?.value,
active: computed(() => v.value === option.props?.value),
action: () => {
v.value = option.props?.value;
emit('changeByUser', v.value);
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/src/types/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import * as Misskey from 'misskey-js';
import { Ref } from 'vue';
import { ComputedRef, Ref } from 'vue';

export type MenuAction = (ev: MouseEvent) => void;

Expand All @@ -15,7 +15,7 @@ export type MenuLink = { type: 'link', to: string, text: string, icon?: string,
export type MenuA = { type: 'a', href: string, target?: string, download?: string, text: string, icon?: string, indicate?: boolean };
export type MenuUser = { type: 'user', user: Misskey.entities.User, active?: boolean, indicate?: boolean, action: MenuAction };
export type MenuSwitch = { type: 'switch', ref: Ref<boolean>, text: string, disabled?: boolean | Ref<boolean> };
export type MenuButton = { type?: 'button', text: string, icon?: string, indicate?: boolean, danger?: boolean, active?: boolean, avatar?: Misskey.entities.User; action: MenuAction };
export type MenuButton = { type?: 'button', text: string, icon?: string, indicate?: boolean, danger?: boolean, active?: boolean | ComputedRef<boolean>, avatar?: Misskey.entities.User; action: MenuAction };
export type MenuParent = { type: 'parent', text: string, icon?: string, children: MenuItem[] | (() => Promise<MenuItem[]> | MenuItem[]) };

export type MenuPending = { type: 'pending' };
Expand Down

0 comments on commit 7fa6d62

Please sign in to comment.