Skip to content

Commit c9e700b

Browse files
committed
chore: add themeChangeBehavior
1 parent 02b9c48 commit c9e700b

File tree

7 files changed

+51
-34
lines changed

7 files changed

+51
-34
lines changed

example/pages/home/home.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import themeChangeBehavior from 'tdesign-miniprogram/mixins/theme-change';
12
import list from './data/index';
23

34
Page({
5+
behaviors: [themeChangeBehavior],
46
data: {
57
list,
68
currentYear: new Date().getFullYear(),
7-
isDarkMode: false,
89
},
910
onLoad(options) {
1011
const { path, q } = options;
@@ -21,7 +22,6 @@ Page({
2122
}
2223

2324
this.trdPrivacy = this.selectComponent('#trdPrivacy');
24-
this.setData({ isDarkMode: wx.getSystemInfoSync().theme === 'dark' });
2525
},
2626

2727
showPrivacyWin() {

example/pages/home/home.wxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<image
55
class="title-icon"
66
mode="aspectFit"
7-
src="/assets/{{isDarkMode ? 'TDesign-logo_dark' : 'TDesign-logo_light'}}.png"
7+
src="/assets/{{theme === 'dark' ? 'TDesign-logo_dark' : 'TDesign-logo_light'}}.png"
88
aria-label="TDesign Logo"
99
/>
1010
</view>

src/footer/_example/logo/index.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1+
import themeChangeBehavior from 'tdesign-miniprogram/mixins/theme-change';
2+
13
Component({
4+
behaviors: [themeChangeBehavior],
25
data: {
3-
logoList: [
4-
{
5-
icon: 'https://tdesign.gtimg.com/mobile/demos/logo2.png',
6-
title: '品牌名称',
7-
},
8-
{
9-
url: 'https://tdesign.gtimg.com/mobile/demos/logo1.png',
10-
},
11-
],
12-
},
13-
14-
lifetimes: {
15-
attached() {
16-
if (wx.getSystemInfoSync().theme === 'dark') {
17-
const updateKeyName = 'logoList[1].url';
18-
this.setData({ [updateKeyName]: 'https://tdesign.gtimg.com/mobile/demos/footer-logo-dark.png' });
19-
}
6+
logo: {
7+
icon: 'https://tdesign.gtimg.com/mobile/demos/logo2.png',
8+
title: '品牌名称',
209
},
2110
},
2211
});

src/footer/_example/logo/index.wxml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<!-- theme 为 logo -->
22
<view class="footer-example">
3-
<t-footer logo="{{logoList[0]}}" />
3+
<t-footer logo="{{logo}}" />
44
</view>
55

66
<view class="footer-example">
7-
<t-footer logo="{{logoList[1]}}" />
7+
<t-footer
8+
logo="{{{url: theme === 'dark'?'https://tdesign.gtimg.com/mobile/demos/footer-logo-dark.png':'https://tdesign.gtimg.com/mobile/demos/logo1.png'} }}"
9+
/>
810
</view>

src/mixins/theme-change.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const themeChangeBehavior = Behavior({
2+
data: {
3+
theme: 'light',
4+
},
5+
6+
attached() {
7+
this._initTheme();
8+
},
9+
10+
methods: {
11+
_initTheme() {
12+
const that = this;
13+
wx.getSystemInfo({
14+
success(res) {
15+
that.setData({
16+
theme: res.theme,
17+
});
18+
},
19+
});
20+
21+
wx.onThemeChange((res) => {
22+
that.setData({
23+
theme: res.theme,
24+
});
25+
});
26+
},
27+
},
28+
});
29+
30+
export default themeChangeBehavior;

src/navbar/_example/img/index.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
Component({
2-
data: {
3-
image: 'https://tdesign.gtimg.com/mobile/demos/logo-light.png',
4-
},
1+
import themeChangeBehavior from 'tdesign-miniprogram/mixins/theme-change';
52

6-
lifetimes: {
7-
attached() {
8-
if (wx.getSystemInfoSync().theme === 'dark') {
9-
this.setData({ image: 'https://tdesign.gtimg.com/mobile/demos/image-dark.png' });
10-
}
11-
},
12-
},
3+
Component({
4+
behaviors: [themeChangeBehavior],
135
});

src/navbar/_example/img/index.wxml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<t-navbar>
22
<view slot="left">
3-
<t-image t-class="custom-image" src="{{image}}" aria-label="导航栏图片" />
3+
<t-image
4+
t-class="custom-image"
5+
src="{{theme === 'dark' ? 'https://tdesign.gtimg.com/mobile/demos/image-dark.png' : 'https://tdesign.gtimg.com/mobile/demos/logo-light.png'}}"
6+
aria-label="导航栏图片"
7+
/>
48
</view>
59
</t-navbar>

0 commit comments

Comments
 (0)