Skip to content

Commit

Permalink
feat: preference button supports automatic positioning (#4243)
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb authored Aug 26, 2024
1 parent dc5cfab commit d2f3a9d
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/src/en/guide/essentials/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ const defaultPreferences: Preferences = {
locale: 'zh-CN',
loginExpiredMode: 'modal',
name: 'Vben Admin',
preferencesButtonPosition: 'fixed',
preferencesButtonPosition: 'auto',
watermark: false,
},
breadcrumb: {
Expand Down
2 changes: 1 addition & 1 deletion docs/src/guide/essentials/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ const defaultPreferences: Preferences = {
locale: 'zh-CN',
loginExpiredMode: 'modal',
name: 'Vben Admin',
preferencesButtonPosition: 'fixed',
preferencesButtonPosition: 'auto',
watermark: false,
},
breadcrumb: {
Expand Down
3 changes: 2 additions & 1 deletion packages/@core/base/typings/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ type ThemeModeType = 'auto' | 'dark' | 'light';
* 偏好设置按钮位置
* fixed 固定在右侧
* header 顶栏
* auto 自动
*/
type PreferencesButtonPositionType = 'fixed' | 'header';
type PreferencesButtonPositionType = 'auto' | 'fixed' | 'header';

type BuiltinThemeType =
| 'custom'
Expand Down
2 changes: 1 addition & 1 deletion packages/@core/preferences/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const defaultPreferences: Preferences = {
locale: 'zh-CN',
loginExpiredMode: 'page',
name: 'Vben Admin',
preferencesButtonPosition: 'fixed',
preferencesButtonPosition: 'auto',
watermark: false,
},
breadcrumb: {
Expand Down
40 changes: 40 additions & 0 deletions packages/@core/preferences/src/use-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,45 @@ function usePreferences() {
return enable && globalLockScreen;
});

/**
* @zh_CN 偏好设置按钮位置
*/
const preferencesButtonPosition = computed(() => {
const { enablePreferences, preferencesButtonPosition } = preferences.app;

// 如果没有启用偏好设置按钮
if (!enablePreferences) {
return {
fixed: false,
header: false,
};
}

const { header, sidebar } = preferences;
const headerHidden = header.hidden;
const sidebarHidden = sidebar.hidden;

const contentIsMaximize = headerHidden && sidebarHidden;

const isHeaderPosition = preferencesButtonPosition === 'header';

// 如果设置了固定位置
if (preferencesButtonPosition !== 'auto') {
return {
fixed: preferencesButtonPosition === 'fixed',
header: isHeaderPosition,
};
}

// 如果是全屏模式或者没有固定在顶部,
const fixed = contentIsMaximize || isFullContent.value || isMobile.value;

return {
fixed,
header: !fixed,
};
});

return {
authPanelCenter,
authPanelLeft,
Expand All @@ -168,6 +207,7 @@ function usePreferences() {
isSideNav,
keepAlive,
layout,
preferencesButtonPosition,
sidebarCollapsed,
theme,
};
Expand Down
9 changes: 3 additions & 6 deletions packages/effects/layouts/src/basic/header/header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ withDefaults(defineProps<Props>(), {
});
const accessStore = useAccessStore();
const { globalSearchShortcutKey } = usePreferences();
const { globalSearchShortcutKey, preferencesButtonPosition } = usePreferences();
const slots = useSlots();
const rightSlots = computed(() => {
const list = [{ index: 100, name: 'user-dropdown' }];
Expand All @@ -39,10 +39,7 @@ const rightSlots = computed(() => {
});
}
if (
preferences.app.enablePreferences &&
preferences.app.preferencesButtonPosition === 'header'
) {
if (preferencesButtonPosition.value.header) {
list.push({
index: 10,
name: 'preferences',
Expand Down Expand Up @@ -121,7 +118,7 @@ const leftSlots = computed(() => {
<GlobalSearch
:enable-shortcut-key="globalSearchShortcutKey"
:menus="accessStore.accessMenus"
class="mr-4"
class="mr-1 sm:mr-4"
/>
</template>

Expand Down
8 changes: 2 additions & 6 deletions packages/effects/layouts/src/basic/layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const {
isMobile,
isSideMixedNav,
layout,
preferencesButtonPosition,
sidebarCollapsed,
theme,
} = usePreferences();
Expand Down Expand Up @@ -326,12 +327,7 @@ const headerSlots = computed(() => {
<slot v-if="lockStore.isLockScreen" name="lock-screen"></slot>
</Transition>

<template
v-if="
preferences.app.enablePreferences &&
preferences.app.preferencesButtonPosition === 'fixed'
"
>
<template v-if="preferencesButtonPosition.fixed">
<Preferences
class="z-100 fixed bottom-20 right-0"
@clear-preferences-and-logout="clearPreferencesAndLogout"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ onMounted(() => {
@click="toggleOpen()"
>
<Search
class="text-muted-foreground group-hover:text-foreground size-3 group-hover:opacity-100"
class="text-muted-foreground group-hover:text-foreground size-4 group-hover:opacity-100"
/>
<span
class="text-muted-foreground group-hover:text-foreground hidden text-xs duration-300 md:block"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const appPreferencesButtonPosition = defineModel<string>(
);
const positionItems = computed((): SelectOption[] => [
{
label: $t('preferences.position.auto'),
value: 'auto',
},
{
label: $t('preferences.position.header'),
value: 'header',
Expand Down
1 change: 1 addition & 0 deletions packages/locales/src/langs/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
"position": {
"title": "Preferences Postion",
"header": "Header",
"auto": "Auto",
"fixed": "Fixed"
},
"sidebar": {
Expand Down
1 change: 1 addition & 0 deletions packages/locales/src/langs/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
"position": {
"title": "偏好设置位置",
"header": "顶栏",
"auto": "自动",
"fixed": "固定"
},
"sidebar": {
Expand Down

0 comments on commit d2f3a9d

Please sign in to comment.