@@ -20,25 +20,105 @@ createApp(App).use(router).mount('#app');
2020:::
2121
2222<script setup >
23- import { ThemeButton , ThemeSelect } from ' ../../src'
23+ import {
24+ ThemeButton ,
25+ ThemeSelect ,
26+ ThemeSwitch ,
27+ ThemeRadio ,
28+ SunIcon ,
29+ MoonIcon ,
30+ ThemeDefaultIcon ,
31+ Radio ,
32+ RadioGroup
33+ } from ' ../../src' ;
34+ import { ref , watch } from ' vue' ;
35+ import { getTheme , applyTheme } from ' ph-utils/theme'
36+
37+ const theme = ref (getTheme ());
38+
39+ watch (theme, (val ) => {
40+ applyTheme (val).then ();
41+ });
2442</script >
2543
2644### 基本使用
2745
2846提供多种风格的主题切换按钮: 按钮、下拉选择、单选按钮组、开关
2947
48+ <ClientOnly >
49+ <CodePreview >
50+ <textarea lang =" vue-html " v-pre >
51+ <nt-theme-button></nt-theme-button>
52+ <nt-theme-select class="ml-10"></nt-theme-select>
53+ <nt-theme-switch class="ml-10"></nt-theme-switch>
54+ <nt-theme-radio class="mt-15"></nt-theme-radio>
55+ </textarea >
56+ <template #preview>
57+ <ThemeButton ></ThemeButton >
58+ <ThemeSelect class =" ml-10 " ></ThemeSelect >
59+ <ThemeSwitch class =" ml-10 " ></ThemeSwitch >
60+ <ThemeRadio class =" mt-15 " ></ThemeRadio >
61+ </template >
62+ </CodePreview >
63+ </ClientOnly >
64+
65+ > 切换主题后,如果要让应用启动的时候也应用上一次切换的主题, 需要在应用开始的地方调用 ` initTheme() ` 函数
66+
67+ ``` js
68+ // main.js
69+ import { initTheme } from ' ph-utils/theme' ;
70+
71+ // await initTheme();
72+ initTheme ().then ();
73+ ```
74+
75+ ### 自定义风格
76+
77+ 框架内置的主题切换其实就是一个排版的样式,核心的主题切换的逻辑是调用的 ` ph-utils ` 库的 ` theme ` 模块相关函数来实现的。
78+
79+ 通常调用的模块有: ` getTheme() ` 、` applyTheme() `
80+
81+ 所以完全可以自定义按钮来实现主题切换, 下面就使用 ` RadioGroup + Icon ` 来实现。
82+
3083<ClientOnly >
3184 <CodePreview >
3285 <textarea lang =" vue " v-pre >
3386 <script setup lang="ts">
87+ import { ref, watch } from 'vue';
88+ import { getTheme, applyTheme } from 'ph-utils/theme';
89+ // 获取当前主题
90+ const theme = ref(getTheme());
91+ // 主题改变时, 应用主题
92+ watch(theme, (val) => {
93+ applyTheme(val).then();
94+ });
3495 </script>
3596 <template>
36- <hr />
97+ <nt-radio-group v-model="theme">
98+ <nt-radio value="auto" type="button">
99+ <nt-theme-default-icon></nt-theme-default-icon>
100+ </nt-radio>
101+ <nt-radio value="light" type="button">
102+ <nt-sun-icon></nt-sun-icon>
103+ </nt-radio>
104+ <nt-radio value="dark" type="button">
105+ <nt-moon-icon></nt-moon-icon>
106+ </nt-radio>
107+ </nt-radio-group>
37108 </template>
38109 </textarea >
39110 <template #preview>
40- <ThemeButton ></ThemeButton >
41- <ThemeSelect class =" ml-10 " ></ThemeSelect >
111+ <RadioGroup v-model =" theme " >
112+ <Radio value="auto" type="button">
113+ <ThemeDefaultIcon></ThemeDefaultIcon>
114+ </Radio>
115+ <Radio value="light" type="button">
116+ <SunIcon></SunIcon>
117+ </Radio>
118+ <Radio value="dark" type="button">
119+ <MoonIcon></MoonIcon>
120+ </Radio>
121+ </RadioGroup >
42122 </template >
43123 </CodePreview >
44124</ClientOnly >
0 commit comments