Skip to content

Commit

Permalink
feat(layout): 支持深/浅色模式切换
Browse files Browse the repository at this point in the history
  • Loading branch information
yulimchen committed Apr 25, 2023
1 parent 40aee75 commit 5178735
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 9 deletions.
4 changes: 4 additions & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ export {}

declare module '@vue/runtime-core' {
export interface GlobalComponents {
NavBar: typeof import('./src/components/NavBar/index.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
SvgIcon: typeof import('./src/components/SvgIcon/index.vue')['default']
Tabbar: typeof import('./src/components/Tabbar/index.vue')['default']
VanButton: typeof import('vant/es')['Button']
VanCell: typeof import('vant/es')['Cell']
VanConfigProvider: typeof import('vant/es')['ConfigProvider']
VanIcon: typeof import('vant/es')['Icon']
VanNavBar: typeof import('vant/es')['NavBar']
VanSpace: typeof import('vant/es')['Space']
VanTabbar: typeof import('vant/es')['Tabbar']
VanTabbarItem: typeof import('vant/es')['TabbarItem']
Expand Down
17 changes: 17 additions & 0 deletions src/components/NavBar/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { useDarkMode, useToggleDarkMode } from "@/hooks/useToggleDarkMode";
const onClickRight = () => {
useToggleDarkMode();
};
</script>

<template>
<van-nav-bar fixed placeholder @click-right="onClickRight">
<template #right>
<svg-icon class="text-[18px]" :name="useDarkMode() ? 'light' : 'dark'" />
</template>
</van-nav-bar>
</template>

<style scoped></style>
9 changes: 9 additions & 0 deletions src/hooks/useToggleDarkMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useDarkModeStoreHook } from "@/store/modules/darkMode";

export function useDarkMode() {
return useDarkModeStoreHook().darkMode;
}

export function useToggleDarkMode() {
useDarkModeStoreHook().toggleDarkMode();
}
1 change: 1 addition & 0 deletions src/icons/svg/dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 5 additions & 4 deletions src/layout/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script setup lang="ts">
import Tabbar from "@/components/Tabbar/index.vue";
import NavBar from "@/components/NavBar/index.vue";
import { useCachedViewStoreHook } from "@/store/modules/cachedView";
import { useDarkMode } from "@/hooks/useToggleDarkMode";
import { computed } from "vue";
const cachedViews = computed(() => {
Expand All @@ -10,16 +12,15 @@ const cachedViews = computed(() => {

<template>
<div class="app-wrapper">
<div class="app-wrapper__content">
<van-config-provider :theme="useDarkMode() ? 'dark' : 'light'">
<nav-bar />
<router-view v-slot="{ Component }">
<keep-alive :include="cachedViews">
<component :is="Component" />
</keep-alive>
</router-view>
</div>
<div class="app-wrapper__footer">
<tabbar />
</div>
</van-config-provider>
</div>
</template>

Expand Down
23 changes: 23 additions & 0 deletions src/store/modules/darkMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { defineStore } from "pinia";
import { store } from "@/store";

export const useDarkModeStore = defineStore({
id: "dark-mode",
state: () => ({
darkMode: false
}),
actions: {
toggleDarkMode() {
this.darkMode = !this.darkMode;
if (this.darkMode) {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
}
}
});

export function useDarkModeStoreHook() {
return useDarkModeStore(store);
}
3 changes: 2 additions & 1 deletion src/styles/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ body {
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
color: var(--color-text);
color: var(--van-text-color);
background-color: var(--color-background-2);
}

html {
Expand Down
12 changes: 10 additions & 2 deletions src/styles/variables.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
:root {
--color-text: #2c3e50;
}
--color-text: var(--van-text-color, #323233);
--color-background-2: var(--van-background-2, #fff);
--color-block-background: var(--van-border-color, #ebedf0);
}

html.dark {
--color-text: var(--van-text-color, #f5f5f5);
--color-background-2: var(--van-background-2, #1c1c1e);
--color-block-background: var(--van-border-color, #3a3a3c);
}
2 changes: 1 addition & 1 deletion src/views/demo/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const contentList = reactive([
</a>
</div>
<div
class="text-[14px] py-[2px] px-[10px] rounded-[4px] bg-[#eee] mt-[14px]"
class="text-[14px] py-[2px] px-[10px] rounded-[4px] bg-[var(--color-block-background)] mt-[14px]"
>
<p class="my-[14px] leading-[24px]">
🌱 基于 Vue3 全家桶、TypeScript、Vite 构建工具,开箱即用的 H5
Expand Down
2 changes: 1 addition & 1 deletion src/views/tools/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const handleErrorReq = () => {
<van-button type="danger" @click="handleErrorReq">失败请求</van-button>
</van-space>
<div
class="text-[14px] py-[2px] px-[10px] rounded-[4px] bg-[#eee] mt-[14px]"
class="text-[14px] py-[2px] px-[10px] rounded-[4px] bg-[var(--color-block-background)] mt-[14px]"
>
<p class="my-[14px] leading-[24px]">
{{ showList }}
Expand Down

0 comments on commit 5178735

Please sign in to comment.