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

Feature/dark hotfix #2722

Merged
merged 11 commits into from
Apr 24, 2024
4 changes: 3 additions & 1 deletion example/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"pages": ["indexes", "base/index", "custom/index"]
}
],
"themeLocation": "theme.json",
"usingComponents": {
"t-demo": "./components/demo-block/index",
"t-button": "tdesign-miniprogram/button/button",
Expand All @@ -107,7 +108,8 @@
},
"window": {
"navigationBarTitleText": "TDesign",
"navigationStyle": "custom"
"navigationStyle": "custom",
"navigationBarTextStyle": "@navTxtStyle"
},
"sitemapLocation": "sitemap.json"
}
3 changes: 2 additions & 1 deletion example/app.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@import '../src/common/style/_variables.less';
@import '../src/common/style/theme/_dark.less';
@import '../src/common/style/theme/_index.less';

page {
background-color: #f6f6f6;
--bg-color-demo-desc: #0009;
Expand Down
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
8 changes: 8 additions & 0 deletions example/theme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"light": {
"navTxtStyle": "black"
},
"dark": {
"navTxtStyle": "white"
}
}
258 changes: 243 additions & 15 deletions site/theme/dark.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const DARK_MODE_CLASS = 'dark-mode';
const DARK_MODE_NAME = 'dark';

const darkModeCss = `
body {
background: #181818;
--td-brand-color-1: #1b2f51;
--td-brand-color-2: #173463;
--td-brand-color-3: #143975;
--td-brand-color-4: #103d88;
--td-brand-color-5: #0d429a;
--td-brand-color-6: #054bbe;
--td-brand-color-7: #2667d4;
--td-brand-color-8: #4582e6;
--td-brand-color-9: #699ef5;
--td-brand-color-10: #96bbf8;
--td-primary-color-1: #1b2f51;
--td-primary-color-2: #173463;
--td-primary-color-3: #143975;
--td-primary-color-4: #103d88;
--td-primary-color-5: #0d429a;
--td-primary-color-6: #054bbe;
--td-primary-color-7: #2667d4;
--td-primary-color-8: #4582e6;
--td-primary-color-9: #699ef5;
--td-primary-color-10: #96bbf8;
--td-warning-color-1: #4f2a1d;
--td-warning-color-2: #582f21;
--td-warning-color-3: #733c23;
Expand Down Expand Up @@ -157,6 +156,200 @@ const darkModeCss = `
}
`;

const lightModeCss = `
body {
--td-primary-color-1: #f2f3ff;
--td-primary-color-2: #d9e1ff;
--td-primary-color-3: #b5c7ff;
--td-primary-color-4: #8eabff;
--td-primary-color-5: #618dff;
--td-primary-color-6: #366ef4;
--td-primary-color-7: #0052d9;
--td-primary-color-8: #003cab;
--td-primary-color-9: #002a7c;
--td-primary-color-10: #001a57;

--td-warning-color-1: #fff1e9;
--td-warning-color-2: #ffd9c2;
--td-warning-color-3: #ffb98c;
--td-warning-color-4: #fa9550;
--td-warning-color-5: #e37318;
--td-warning-color-6: #be5a00;
--td-warning-color-7: #954500;
--td-warning-color-8: #713300;
--td-warning-color-9: #532300;
--td-warning-color-10: #3b1700;

--td-error-color-1: #fff0ed;
--td-error-color-2: #ffd8d2;
--td-error-color-3: #ffb9b0;
--td-error-color-4: #ff9285;
--td-error-color-5: #f6685d;
--td-error-color-6: #d54941;
--td-error-color-7: #ad352f;
--td-error-color-8: #881f1c;
--td-error-color-9: #68070a;
--td-error-color-10: #490002;

--td-success-color-1: #e3f9e9;
--td-success-color-2: #c6f3d7;
--td-success-color-3: #92dab2;
--td-success-color-4: #56c08d;
--td-success-color-5: #2ba471;
--td-success-color-6: #008858;
--td-success-color-7: #006c45;
--td-success-color-8: #005334;
--td-success-color-9: #003b23;
--td-success-color-10: #002515;

--td-gray-color-1: #f3f3f3;
--td-gray-color-2: #eeeeee;
--td-gray-color-3: #e7e7e7;
--td-gray-color-4: #dcdcdc;
--td-gray-color-5: #c5c5c5;
--td-gray-color-6: #a6a6a6;
--td-gray-color-7: #8b8b8b;
--td-gray-color-8: #777777;
--td-gray-color-9: #5e5e5e;
--td-gray-color-10: #4b4b4b;
--td-gray-color-11: #383838;
--td-gray-color-12: #2c2c2c;
--td-gray-color-13: #242424;
--td-gray-color-14: #181818;

--td-font-white-1: rgba(255, 255, 255, 1);
--td-font-white-2: rgba(255, 255, 255, 0.55);
--td-font-white-3: rgba(255, 255, 255, 0.35);
--td-font-white-4: rgba(255, 255, 255, 0.22);
--td-font-gray-1: rgba(0, 0, 0, 0.9);
--td-font-gray-2: rgba(0, 0, 0, 0.6);
--td-font-gray-3: rgba(0, 0, 0, 0.4);
--td-font-gray-4: rgba(0, 0, 0, 0.26);

--td-brand-color: var(--td-primary-color-7);
--td-warning-color: var(--td-warning-color-5);
--td-error-color: var(--td-error-color-6);
--td-success-color: var(--td-success-color-5);

--td-brand-color-focus: var(--td-primary-color-1);
--td-brand-color-active: var(--td-primary-color-8);
--td-brand-color-disabled: var(--td-primary-color-3);
--td-brand-color-light: var(--td-primary-color-1);
--td-brand-color-light-active: var(--td-primary-color-2);

--td-warning-color-active: var(--td-warning-color-6);
--td-warning-color-disabled: var(--td-warning-color-3);
--td-warning-color-focus: var(--td-warning-color-2);
--td-warning-color-light: var(--td-warning-color-1);
--td-warning-color-light-active: var(--td-warning-color-2);

--td-error-color-focus: var(--td-error-color-2);
--td-error-color-active: var(--td-error-color-7);
--td-error-color-disabled: var(--td-error-color-3);
--td-error-color-light: var(--td-error-color-1);
--td-error-color-light-active: var(--td-error-color-2);

--td-success-color-focus: var(--td-success-color-2);
--td-success-color-active: var(--td-success-color-6);
--td-success-color-disabled: var(--td-success-color-3);
--td-success-color-light: var(--td-success-color-1);
--td-success-color-light-active: var(--td-success-color-2);

--td-mask-active: rgba(0, 0, 0, 60%);
--td-mask-disabled: rgba(255, 255, 255, 60%);

--td-bg-color-page: var(--td-gray-color-1);
--td-bg-color-container: var(--td-font-white-1);
--td-bg-color-container-active: var(--td-gray-color-3);
--td-bg-color-secondarycontainer: var(--td-gray-color-1);
--td-bg-color-secondarycontainer-active: var(--td-gray-color-4);
--td-bg-color-component: var(--td-gray-color-3);
--td-bg-color-component-active: var(--td-gray-color-6);
--td-bg-color-component-disabled: var(--td-gray-color-2);
--td-bg-color-secondarycomponent: var(--td-gray-color-4);
--td-bg-color-secondarycomponent-active: var(--td-gray-color-6);

--td-bg-color-specialcomponent: #fff;

--td-text-color-primary: var(--td-font-gray-1);
--td-text-color-secondary: var(--td-font-gray-2);
--td-text-color-placeholder: var(--td-font-gray-3);
--td-text-color-disabled: var(--td-font-gray-4);
--td-text-color-anti: var(--td-font-white-1);
--td-text-color-brand: var(--td-brand-color);
--td-text-color-link: var(--td-brand-color);

--td-border-level-1-color: var(--td-gray-color-3);
--td-component-stroke: var(--td-gray-color-3);
--td-border-level-2-color: var(--td-gray-color-4);
--td-component-border: var(--td-gray-color-4);

--td-shadow-1: 0 1px 10px rgba(0, 0, 0, 5%), 0 4px 5px rgba(0, 0, 0, 8%), 0 2px 4px -1px rgba(0, 0, 0, 12%);
--td-shadow-2: 0 3px 14px 2px rgba(0, 0, 0, 5%), 0 8px 10px 1px rgba(0, 0, 0, 6%), 0 5px 5px -3px rgba(0, 0, 0, 10%);
--td-shadow-3: 0 6px 30px 5px rgba(0, 0, 0, 5%), 0 16px 24px 2px rgba(0, 0, 0, 4%),
0 8px 10px -5px rgba(0, 0, 0, 8%);
--td-shadow-4: 0 2px 8px 0 rgba(0, 0, 0, 0.06);

--td-shadow-inset-top: inset 0 0.5px 0 #dcdcdc;
--td-shadow-inset-right: inset 0.5px 0 0 #dcdcdc;
--td-shadow-inset-bottom: inset 0 -0.5px 0 #dcdcdc;
--td-shadow-inset-left: inset -0.5px 0 0 #dcdcdc;

--td-table-shadow-color: rgba(0, 0, 0, 8%);

--td-scrollbar-color: rgba(0, 0, 0, 10%);
--td-scrollbar-hover-color: rgba(0, 0, 0, 30%);
--td-scroll-track-color: #fff;
--bg-color-demo-desc: #0009;
--bg-color-demo-title: #000000e6;
--bg-color-demo: #fff;
--bg-color-demo-border: #e7e7e7;
--bg-color-demo-secondary: #fff;
--td-navbar-bg-color: #f6f6f6;
--td-navbar-color: black;
--td-navbar-title-font-size: 26rpx;
--td-navbar-title-font-weight: 400;
--td-color-demo-notice-icon: rgba(0, 0, 0, 0.9);
--td-avatar-border-color: var(--bg-color-demo);
--td-progress-circle-inner-bg-color: var(--bg-color-demo);
--td-navbar-bg-color-example: var(--td-bg-color-container);
--td-navbar-color-example: var(--td-text-color-primary);
--td-grid-bg-color-example: var(--td-bg-color-container);
--td-grid-item-bg-color-example: var(--td-bg-color-container);
--td-input-border-color-example: rgba(220, 220, 220, 1);
}
`;

const LIGHT_WHITE_BACKGROUND_COMPONENTS = [
'button',
'divider',
'fab',
'icon',
'back-top',
'drawer',
'indexes',
'side-bar',
'avatar',
'count-down',
'empty',
'footer',
'image',
'image-viewer',
'progress',
'result',
'skeleton',
'swiper',
'tag',
'action-sheet',
'dialog',
'loading',
'message',
'notice-bar',
'overlay',
'popup',
'pull-down-refresh',
];

export const changeThemeMode = () => {
const parentIframe = document.querySelector('iframe');
let previewIframeList: any = null;
Expand Down Expand Up @@ -204,17 +397,52 @@ function addDarkStyle(previewIframe: any) {
const mode = targetNode.getAttribute('theme-mode');
const iframeDom = previewIframe.contentWindow.document.documentElement;
const darkStyleElement = previewIframe.contentWindow.document.getElementById('dark');
const lightStyleElement = previewIframe.contentWindow.document.getElementById('light');
// 需要重新加载不同背景的组件,所以每次都要删除重新添加
if (lightStyleElement) {
iframeDom.removeChild(lightStyleElement);
}
if (mode === DARK_MODE_NAME) {
const styleElement = document.createElement('style');
styleElement.type = 'text/css';
styleElement.textContent = darkModeCss;
styleElement.id = 'dark';
if (!darkStyleElement) {
const styleElement = getDarkStyle();
iframeDom.appendChild(styleElement);
}
} else {
if (darkStyleElement) {
iframeDom.removeChild(darkStyleElement);
}
// 需要重新加载不同背景的组件 #fff #f6f6f6
const styleElement = getLightStyle();
iframeDom.appendChild(styleElement);
}
}

function getDarkStyle() {
const styleElement = document.createElement('style');
styleElement.type = 'text/css';
styleElement.textContent = darkModeCss;
styleElement.id = 'dark';
return styleElement;
}

function getLightStyle() {
const curComponentBackground = getCurComponentBackground();
const styleElement = document.createElement('style');
styleElement.type = 'text/css';
styleElement.textContent = lightModeCss + curComponentBackground;
styleElement.id = 'light';
return styleElement;
}

function getCurComponentBackground() {
const url = window.location.href;
const lastUnderscoreIndex = url.lastIndexOf('/');
const lastSegment = url.substring(lastUnderscoreIndex + 1);
let backgroundCss = '';
if (LIGHT_WHITE_BACKGROUND_COMPONENTS.includes(lastSegment)) {
backgroundCss = 'body{ background: #fff !important; }';
} else {
backgroundCss = 'body{ background: #f6f6f6 !important; }';
}
return backgroundCss;
}
2 changes: 1 addition & 1 deletion src/avatar/_example/badge-avatar/index.wxss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

.external-class-content {
color: #fff;
background-color: #0052d9;
background-color: var(--td-brand-color, #0052d9);
font-weight: 400;
}
2 changes: 1 addition & 1 deletion src/avatar/_example/size/index.wxss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

.external-class-content {
color: #fff;
background-color: #0052d9;
background-color: var(--td-brand-color, #0052d9);
font-weight: 400;
}

Expand Down
4 changes: 2 additions & 2 deletions src/back-top/back-top.less
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
@safe-area-inset-bottom: constant(safe-area-inset-bottom);
@safe-area-inset-bottom: env(safe-area-inset-bottom);
@back-top-fixed-bottom: calc(@spacer-2 + @safe-area-inset-bottom);
@back-top-round-color: var(--td-back-top-round-color, @font-gray-1);
@back-top-round-bg-color: var(--td-back-top-round-bg-color, @font-white-1);
@back-top-round-color: var(--td-back-top-round-color, @text-color-primary);
@back-top-round-bg-color: var(--td-back-top-round-bg-color, @bg-color-container);
@back-top-round-border-color: var(--td-back-top-round-border-color, @component-border);
@back-top-round-border-radius: var(--td-back-top-round-border-radius, @radius-circle); // 'round'主题圆角大小
@back-top-round-dark-color: var(--td-back-top-round-dark-color, @font-white-1); // 'round-dark'主题内容颜色
Expand Down
2 changes: 2 additions & 0 deletions src/common/style/base.less
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
@import './theme/_light.less'; // 保证用户在不单独引入 src/common/style/theme/_index.less,浅色模式正常。2.x可移除,官网文档“快速开始”补充引入less说明

// 变量
@import './_variables.less';
3 changes: 0 additions & 3 deletions src/common/style/index.less
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// base(含变量等)
@import './base.less';

// theme(用于换肤)
@import './theme/_index.less';

// mixins
@import './mixins/_index.less';

Expand Down
Loading
Loading