Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for custom slot header-index, sorted by index #4039

Merged
merged 5 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 64 additions & 11 deletions packages/effects/layouts/src/basic/header/header.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script lang="ts" setup>
import { computed, useSlots } from 'vue';

import { preferences, usePreferences } from '@vben/preferences';
import { useAccessStore } from '@vben/stores';
import { VbenFullScreen } from '@vben-core/shadcn-ui';
Expand All @@ -22,6 +24,48 @@ withDefaults(defineProps<Props>(), {

const accessStore = useAccessStore();
const { globalSearchShortcutKey } = usePreferences();
const slots = useSlots();
const headerSlots = computed(() => {
const list = [{ index: 30, name: 'user-dropdown' }];
if (preferences.widget.globalSearch) {
likui628 marked this conversation as resolved.
Show resolved Hide resolved
list.push({
index: 5,
name: 'global-search',
});
}
if (preferences.widget.themeToggle) {
list.push({
index: 10,
name: 'theme-toggle',
});
}
if (preferences.widget.languageToggle) {
list.push({
index: 15,
name: 'language-toggle',
});
}
if (preferences.widget.fullscreen) {
list.push({
index: 20,
name: 'fullscreen',
});
}
if (preferences.widget.notification) {
list.push({
index: 25,
name: 'notification',
});
}

Object.keys(slots).forEach((key) => {
const name = key.split('-');
if (key.startsWith('header-')) {
list.push({ index: Number(name[1]), name: key });
}
});
return list.sort((a, b) => a.index - b.index);
});
</script>

<template>
Expand All @@ -32,16 +76,25 @@ const { globalSearchShortcutKey } = usePreferences();
<slot name="menu"></slot>
</div>
<div class="flex h-full min-w-0 flex-shrink-0 items-center">
<GlobalSearch
v-if="preferences.widget.globalSearch"
:enable-shortcut-key="globalSearchShortcutKey"
:menus="accessStore.accessMenus"
class="mr-4"
/>
<ThemeToggle v-if="preferences.widget.themeToggle" class="mr-2" />
<LanguageToggle v-if="preferences.widget.languageToggle" class="mr-2" />
<VbenFullScreen v-if="preferences.widget.fullscreen" class="mr-2" />
<slot v-if="preferences.widget.notification" name="notification"></slot>
<slot name="user-dropdown"></slot>
<template v-for="slot in headerSlots" :key="slot.name">
<slot :name="slot.name">
<template v-if="slot.name === 'global-search'">
<GlobalSearch
:enable-shortcut-key="globalSearchShortcutKey"
:menus="accessStore.accessMenus"
class="mr-4"
/>
</template>
<template v-else-if="slot.name === 'theme-toggle'">
<ThemeToggle class="mr-2" />
</template>
<template v-else-if="slot.name === 'language-toggle'">
<LanguageToggle class="mr-2" />
</template>
<template v-else-if="slot.name === 'fullscreen'">
<VbenFullScreen class="mr-2" />
</template>
</slot>
</template>
</div>
</template>
15 changes: 10 additions & 5 deletions packages/effects/layouts/src/basic/layout.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts" setup>
import type { MenuRecordRaw } from '@vben/types';

import { computed, watch } from 'vue';
import { computed, useSlots, watch } from 'vue';

import { useWatermark } from '@vben/hooks';
import { $t } from '@vben/locales';
Expand Down Expand Up @@ -136,18 +136,20 @@ watch(
() => preferences.app.watermark,
async (val) => {
if (val) {
// await nextTick();

updateWatermark({
await updateWatermark({
content: `${preferences.app.name} 用户名: ${userStore.userInfo?.username}`,
// parent: contentRef.value,
});
}
},
{
immediate: true,
},
);

const slots = useSlots();
const headerSlots = computed(() => {
return Object.keys(slots).filter((key) => key.startsWith('header-'));
});
</script>

<template>
Expand Down Expand Up @@ -240,6 +242,9 @@ watch(
<template #notification>
<slot name="notification"></slot>
</template>
<template v-for="item in headerSlots" #[item]>
<slot :name="item"></slot>
</template>
</LayoutHeader>
</template>
<!-- 侧边菜单区域 -->
Expand Down
Loading