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: 新增侧边栏颜色切换 #681

Merged
merged 5 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions src/config/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default {
mode: 'light',
layout: 'side',
splitMenu: false,
sideMode: 'light',
isFooterAside: false,
isSidebarFixed: true,
isHeaderFixed: true,
Expand Down
1 change: 0 additions & 1 deletion src/layouts/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ const navToHelper = () => {
display: flex;
align-items: normal;
line-height: 0;
padding-left: var(--td-comp-margin-xl);
}

.header-logo-container {
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/components/LayoutSideNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:layout="settingStore.layout"
:is-fixed="settingStore.isSidebarFixed"
:menu="sideMenu"
:theme="settingStore.displayMode"
:theme="settingStore.displaySideMode"
:is-compact="settingStore.isSidebarCompact"
/>
</template>
Expand Down
25 changes: 22 additions & 3 deletions src/layouts/components/SideNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<t-menu :class="menuCls" :theme="theme" :value="active" :collapsed="collapsed" :default-expanded="defaultExpanded">
<template #logo>
<span v-if="showLogo" :class="`${prefix}-side-nav-logo-wrapper`" @click="goHome">
<component :is="getLogo()" :class="`${prefix}-side-nav-logo-${collapsed ? 't' : 'tdesign'}-logo`" />
<component :is="getLogo()" :class="logoCls" />
</span>
</template>
<menu-content :nav-data="menu" />
<template #operations>
<span class="version-container"> {{ !collapsed ? 'TDesign Starter' : '' }} {{ pgk.version }} </span>
<span :class="versionCls"> {{ !collapsed ? 'TDesign Starter' : '' }} {{ pgk.version }} </span>
</template>
</t-menu>
<div :class="`${prefix}-side-nav-placeholder${collapsed ? '-hidden' : ''}`"></div>
Expand Down Expand Up @@ -75,6 +75,10 @@ const defaultExpanded = computed(() => {
return union(expanded, parentPath === '' ? [] : [parentPath]);
});

const sideMode = computed(() => {
const { theme } = props;
return theme === 'dark';
});
const sideNavCls = computed(() => {
const { isCompact } = props;
return [
Expand All @@ -84,7 +88,22 @@ const sideNavCls = computed(() => {
},
];
});

const logoCls = computed(() => {
return [
`${prefix}-side-nav-logo-${collapsed.value ? 't' : 'tdesign'}-logo`,
{
[`${prefix}-side-nav-dark`]: sideMode.value,
},
];
});
const versionCls = computed(() => {
return [
`version-container`,
{
[`${prefix}-side-nav-dark`]: sideMode.value,
},
];
});
const menuCls = computed(() => {
const { showLogo, isFixed, layout } = props;
return [
Expand Down
21 changes: 17 additions & 4 deletions src/layouts/setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
</div>
</div>
</t-radio-group>
<div class="setting-group-title">{{ $t('layout.setting.sideMode') }}</div>
<t-radio-group v-model="formData.sideMode">
<div v-for="(item, index) in MODE_OPTIONS" :key="index" class="setting-layout-drawer">
<div>
<t-radio-button :key="index" :value="item.type"
><component :is="getModeIcon(item.type)"
/></t-radio-button>
<p :style="{ textAlign: 'center', marginTop: '8px' }">{{ item.text }}</p>
</div>
</div>
</t-radio-group>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我感觉这个其实做成个radiogroup就可以 一个暗黑一个明亮 这个侧边栏也再多个跟随系统 跟随系统感觉是全局的 单独一个侧边栏有这种配置 不太合适

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commit cd6e478, 经过测试,只能禁用掉跟随系统按钮。否则就只能大改样式文件了,如果要改就肯定要写死margin left了,这是配置面板建议可以不用太要命(或者有什么更好的展示方式?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commit 5606355, 现在好多了

<div class="setting-group-title">{{ $t('layout.setting.theme.color') }}</div>
<t-radio-group v-model="formData.brandTheme">
<div v-for="(item, index) in DEFAULT_COLOR_OPTIONS" :key="index" class="setting-layout-drawer">
Expand Down Expand Up @@ -52,7 +63,6 @@
</t-popup>
</div>
</t-radio-group>

<div class="setting-group-title">{{ $t('layout.setting.navigationLayout') }}</div>
<t-radio-group v-model="formData.layout">
<div v-for="(item, index) in LAYOUT_OPTION" :key="index" class="setting-layout-drawer">
Expand All @@ -62,11 +72,14 @@
</div>
</t-radio-group>

<t-form-item v-show="formData.layout === 'mix'" label="分割菜单(混合模式下有效)" name="splitMenu">
<t-form-item v-show="formData.layout === 'mix'" :label="$t('layout.setting.splitMenu')" name="splitMenu">
<t-switch v-model="formData.splitMenu" />
</t-form-item>

<t-form-item v-show="formData.layout === 'mix'" label="固定 Sidebar" name="isSidebarFixed">
<t-form-item
v-show="formData.layout === 'mix'"
:label="$t('layout.setting.fixedSidebar')"
name="isSidebarFixed"
>
<t-switch v-model="formData.isSidebarFixed" />
</t-form-item>

Expand Down
1 change: 1 addition & 0 deletions src/locales/lang/en_US/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default {
},
},
navigationLayout: 'Navigation Layout',
sideMode: 'Side Menu Mode',
splitMenu: 'Split Menu(Only Mix mode)',
fixedSidebar: 'Fixed Sidebar',
element: {
Expand Down
1 change: 1 addition & 0 deletions src/locales/lang/zh_CN/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default {
},
},
navigationLayout: '导航布局',
sideMode: '侧边栏模式',
splitMenu: '分割菜单(混合模式下有效)',
fixedSidebar: '固定侧边栏',
element: {
Expand Down
38 changes: 32 additions & 6 deletions src/store/modules/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,48 @@ export const useSettingStore = defineStore('setting', {
}
return state.mode as 'dark' | 'light';
},
displaySideMode: (state): 'dark' | 'light' => {
if (state.sideMode === 'auto') {
const media = window.matchMedia('(prefers-color-scheme:dark)');
if (media.matches) {
return 'dark';
}
return 'light';
}
return state.sideMode as 'dark' | 'light';
},
},
actions: {
async changeMode(mode: 'dark' | 'light' | 'auto') {
let theme = mode;

if (mode === 'auto') {
const media = window.matchMedia('(prefers-color-scheme:dark)');
if (media.matches) {
theme = 'dark';
} else {
theme = 'light';
}
theme = this.getMediaColor();
}
const isDarkMode = theme === 'dark';

document.documentElement.setAttribute('theme-mode', isDarkMode ? 'dark' : '');

this.chartColors = isDarkMode ? DARK_CHART_COLORS : LIGHT_CHART_COLORS;
},
async changeSideMode(mode: 'dark' | 'light' | 'auto') {
let sideMode = mode;

if (mode === 'auto') {
sideMode = this.getMediaColor();
}
const isDarkMode = sideMode === 'dark';

document.documentElement.setAttribute('side-mode', isDarkMode ? 'dark' : '');
},
getMediaColor() {
const media = window.matchMedia('(prefers-color-scheme:dark)');

if (media.matches) {
return 'dark';
}
return 'light';
},
changeBrandTheme(brandTheme: string) {
const mode = this.displayMode;
// 以主题色加显示模式作为键
Expand Down Expand Up @@ -79,6 +102,9 @@ export const useSettingStore = defineStore('setting', {
if (key === 'mode') {
this.changeMode(payload[key]);
}
if (key === 'sideMode') {
this.changeSideMode(payload[key]);
}
if (key === 'brandTheme') {
this.changeBrandTheme(payload[key]);
}
Expand Down
6 changes: 5 additions & 1 deletion src/style/layout.less
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
}

&-logo-tdesign-logo {
padding: 0 var(--td-comp-paddingLR-xl);
margin-right: var(--td-comp-margin-xxxl);
height: var(--td-comp-size-s);
width: 100%;
color: var(--td-text-color-primary);
Expand All @@ -176,6 +176,10 @@
}
}

&-side-nav-dark {
color: var(--td-font-white-1) !important;
uyarn marked this conversation as resolved.
Show resolved Hide resolved
}

&-side-nav-placeholder {
flex: 1 1 232px;
min-width: 232px;
Expand Down
Loading