Skip to content

Commit

Permalink
chore: add themeChangeBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
anlyyao committed Apr 24, 2024
1 parent 02b9c48 commit c9e700b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 34 deletions.
4 changes: 2 additions & 2 deletions example/pages/home/home.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import themeChangeBehavior from 'tdesign-miniprogram/mixins/theme-change';
import list from './data/index';

Page({
behaviors: [themeChangeBehavior],
data: {
list,
currentYear: new Date().getFullYear(),
isDarkMode: false,
},
onLoad(options) {
const { path, q } = options;
Expand All @@ -21,7 +22,6 @@ Page({
}

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

showPrivacyWin() {
Expand Down
2 changes: 1 addition & 1 deletion example/pages/home/home.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<image
class="title-icon"
mode="aspectFit"
src="/assets/{{isDarkMode ? 'TDesign-logo_dark' : 'TDesign-logo_light'}}.png"
src="/assets/{{theme === 'dark' ? 'TDesign-logo_dark' : 'TDesign-logo_light'}}.png"
aria-label="TDesign Logo"
/>
</view>
Expand Down
23 changes: 6 additions & 17 deletions src/footer/_example/logo/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import themeChangeBehavior from 'tdesign-miniprogram/mixins/theme-change';

Component({
behaviors: [themeChangeBehavior],
data: {
logoList: [
{
icon: 'https://tdesign.gtimg.com/mobile/demos/logo2.png',
title: '品牌名称',
},
{
url: 'https://tdesign.gtimg.com/mobile/demos/logo1.png',
},
],
},

lifetimes: {
attached() {
if (wx.getSystemInfoSync().theme === 'dark') {
const updateKeyName = 'logoList[1].url';
this.setData({ [updateKeyName]: 'https://tdesign.gtimg.com/mobile/demos/footer-logo-dark.png' });
}
logo: {
icon: 'https://tdesign.gtimg.com/mobile/demos/logo2.png',
title: '品牌名称',
},
},
});
6 changes: 4 additions & 2 deletions src/footer/_example/logo/index.wxml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<!-- theme 为 logo -->
<view class="footer-example">
<t-footer logo="{{logoList[0]}}" />
<t-footer logo="{{logo}}" />
</view>

<view class="footer-example">
<t-footer logo="{{logoList[1]}}" />
<t-footer
logo="{{{url: theme === 'dark'?'https://tdesign.gtimg.com/mobile/demos/footer-logo-dark.png':'https://tdesign.gtimg.com/mobile/demos/logo1.png'} }}"
/>
</view>
30 changes: 30 additions & 0 deletions src/mixins/theme-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const themeChangeBehavior = Behavior({
data: {
theme: 'light',
},

attached() {
this._initTheme();
},

methods: {
_initTheme() {
const that = this;
wx.getSystemInfo({
success(res) {
that.setData({
theme: res.theme,
});
},
});

wx.onThemeChange((res) => {
that.setData({
theme: res.theme,
});
});
},
},
});

export default themeChangeBehavior;
14 changes: 3 additions & 11 deletions src/navbar/_example/img/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
Component({
data: {
image: 'https://tdesign.gtimg.com/mobile/demos/logo-light.png',
},
import themeChangeBehavior from 'tdesign-miniprogram/mixins/theme-change';

lifetimes: {
attached() {
if (wx.getSystemInfoSync().theme === 'dark') {
this.setData({ image: 'https://tdesign.gtimg.com/mobile/demos/image-dark.png' });
}
},
},
Component({
behaviors: [themeChangeBehavior],
});
6 changes: 5 additions & 1 deletion src/navbar/_example/img/index.wxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<t-navbar>
<view slot="left">
<t-image t-class="custom-image" src="{{image}}" aria-label="导航栏图片" />
<t-image
t-class="custom-image"
src="{{theme === 'dark' ? 'https://tdesign.gtimg.com/mobile/demos/image-dark.png' : 'https://tdesign.gtimg.com/mobile/demos/logo-light.png'}}"
aria-label="导航栏图片"
/>
</view>
</t-navbar>

0 comments on commit c9e700b

Please sign in to comment.