|
1 |
| -const DARK_MODE_CLASS = 'dark-mode'; |
2 | 1 | const DARK_MODE_NAME = 'dark';
|
3 | 2 |
|
4 | 3 | const darkModeCss = `
|
@@ -157,6 +156,200 @@ const darkModeCss = `
|
157 | 156 | }
|
158 | 157 | `;
|
159 | 158 |
|
| 159 | +const lightModeCss = ` |
| 160 | + body { |
| 161 | + --td-primary-color-1: #f2f3ff; |
| 162 | + --td-primary-color-2: #d9e1ff; |
| 163 | + --td-primary-color-3: #b5c7ff; |
| 164 | + --td-primary-color-4: #8eabff; |
| 165 | + --td-primary-color-5: #618dff; |
| 166 | + --td-primary-color-6: #366ef4; |
| 167 | + --td-primary-color-7: #0052d9; |
| 168 | + --td-primary-color-8: #003cab; |
| 169 | + --td-primary-color-9: #002a7c; |
| 170 | + --td-primary-color-10: #001a57; |
| 171 | +
|
| 172 | + --td-warning-color-1: #fff1e9; |
| 173 | + --td-warning-color-2: #ffd9c2; |
| 174 | + --td-warning-color-3: #ffb98c; |
| 175 | + --td-warning-color-4: #fa9550; |
| 176 | + --td-warning-color-5: #e37318; |
| 177 | + --td-warning-color-6: #be5a00; |
| 178 | + --td-warning-color-7: #954500; |
| 179 | + --td-warning-color-8: #713300; |
| 180 | + --td-warning-color-9: #532300; |
| 181 | + --td-warning-color-10: #3b1700; |
| 182 | +
|
| 183 | + --td-error-color-1: #fff0ed; |
| 184 | + --td-error-color-2: #ffd8d2; |
| 185 | + --td-error-color-3: #ffb9b0; |
| 186 | + --td-error-color-4: #ff9285; |
| 187 | + --td-error-color-5: #f6685d; |
| 188 | + --td-error-color-6: #d54941; |
| 189 | + --td-error-color-7: #ad352f; |
| 190 | + --td-error-color-8: #881f1c; |
| 191 | + --td-error-color-9: #68070a; |
| 192 | + --td-error-color-10: #490002; |
| 193 | +
|
| 194 | + --td-success-color-1: #e3f9e9; |
| 195 | + --td-success-color-2: #c6f3d7; |
| 196 | + --td-success-color-3: #92dab2; |
| 197 | + --td-success-color-4: #56c08d; |
| 198 | + --td-success-color-5: #2ba471; |
| 199 | + --td-success-color-6: #008858; |
| 200 | + --td-success-color-7: #006c45; |
| 201 | + --td-success-color-8: #005334; |
| 202 | + --td-success-color-9: #003b23; |
| 203 | + --td-success-color-10: #002515; |
| 204 | +
|
| 205 | + --td-gray-color-1: #f3f3f3; |
| 206 | + --td-gray-color-2: #eeeeee; |
| 207 | + --td-gray-color-3: #e7e7e7; |
| 208 | + --td-gray-color-4: #dcdcdc; |
| 209 | + --td-gray-color-5: #c5c5c5; |
| 210 | + --td-gray-color-6: #a6a6a6; |
| 211 | + --td-gray-color-7: #8b8b8b; |
| 212 | + --td-gray-color-8: #777777; |
| 213 | + --td-gray-color-9: #5e5e5e; |
| 214 | + --td-gray-color-10: #4b4b4b; |
| 215 | + --td-gray-color-11: #383838; |
| 216 | + --td-gray-color-12: #2c2c2c; |
| 217 | + --td-gray-color-13: #242424; |
| 218 | + --td-gray-color-14: #181818; |
| 219 | +
|
| 220 | + --td-font-white-1: rgba(255, 255, 255, 1); |
| 221 | + --td-font-white-2: rgba(255, 255, 255, 0.55); |
| 222 | + --td-font-white-3: rgba(255, 255, 255, 0.35); |
| 223 | + --td-font-white-4: rgba(255, 255, 255, 0.22); |
| 224 | + --td-font-gray-1: rgba(0, 0, 0, 0.9); |
| 225 | + --td-font-gray-2: rgba(0, 0, 0, 0.6); |
| 226 | + --td-font-gray-3: rgba(0, 0, 0, 0.4); |
| 227 | + --td-font-gray-4: rgba(0, 0, 0, 0.26); |
| 228 | +
|
| 229 | + --td-brand-color: var(--td-primary-color-7); |
| 230 | + --td-warning-color: var(--td-warning-color-5); |
| 231 | + --td-error-color: var(--td-error-color-6); |
| 232 | + --td-success-color: var(--td-success-color-5); |
| 233 | +
|
| 234 | + --td-brand-color-focus: var(--td-primary-color-1); |
| 235 | + --td-brand-color-active: var(--td-primary-color-8); |
| 236 | + --td-brand-color-disabled: var(--td-primary-color-3); |
| 237 | + --td-brand-color-light: var(--td-primary-color-1); |
| 238 | + --td-brand-color-light-active: var(--td-primary-color-2); |
| 239 | +
|
| 240 | + --td-warning-color-active: var(--td-warning-color-6); |
| 241 | + --td-warning-color-disabled: var(--td-warning-color-3); |
| 242 | + --td-warning-color-focus: var(--td-warning-color-2); |
| 243 | + --td-warning-color-light: var(--td-warning-color-1); |
| 244 | + --td-warning-color-light-active: var(--td-warning-color-2); |
| 245 | +
|
| 246 | + --td-error-color-focus: var(--td-error-color-2); |
| 247 | + --td-error-color-active: var(--td-error-color-7); |
| 248 | + --td-error-color-disabled: var(--td-error-color-3); |
| 249 | + --td-error-color-light: var(--td-error-color-1); |
| 250 | + --td-error-color-light-active: var(--td-error-color-2); |
| 251 | +
|
| 252 | + --td-success-color-focus: var(--td-success-color-2); |
| 253 | + --td-success-color-active: var(--td-success-color-6); |
| 254 | + --td-success-color-disabled: var(--td-success-color-3); |
| 255 | + --td-success-color-light: var(--td-success-color-1); |
| 256 | + --td-success-color-light-active: var(--td-success-color-2); |
| 257 | +
|
| 258 | + --td-mask-active: rgba(0, 0, 0, 60%); |
| 259 | + --td-mask-disabled: rgba(255, 255, 255, 60%); |
| 260 | +
|
| 261 | + --td-bg-color-page: var(--td-gray-color-1); |
| 262 | + --td-bg-color-container: var(--td-font-white-1); |
| 263 | + --td-bg-color-container-active: var(--td-gray-color-3); |
| 264 | + --td-bg-color-secondarycontainer: var(--td-gray-color-1); |
| 265 | + --td-bg-color-secondarycontainer-active: var(--td-gray-color-4); |
| 266 | + --td-bg-color-component: var(--td-gray-color-3); |
| 267 | + --td-bg-color-component-active: var(--td-gray-color-6); |
| 268 | + --td-bg-color-component-disabled: var(--td-gray-color-2); |
| 269 | + --td-bg-color-secondarycomponent: var(--td-gray-color-4); |
| 270 | + --td-bg-color-secondarycomponent-active: var(--td-gray-color-6); |
| 271 | +
|
| 272 | + --td-bg-color-specialcomponent: #fff; |
| 273 | +
|
| 274 | + --td-text-color-primary: var(--td-font-gray-1); |
| 275 | + --td-text-color-secondary: var(--td-font-gray-2); |
| 276 | + --td-text-color-placeholder: var(--td-font-gray-3); |
| 277 | + --td-text-color-disabled: var(--td-font-gray-4); |
| 278 | + --td-text-color-anti: var(--td-font-white-1); |
| 279 | + --td-text-color-brand: var(--td-brand-color); |
| 280 | + --td-text-color-link: var(--td-brand-color); |
| 281 | +
|
| 282 | + --td-border-level-1-color: var(--td-gray-color-3); |
| 283 | + --td-component-stroke: var(--td-gray-color-3); |
| 284 | + --td-border-level-2-color: var(--td-gray-color-4); |
| 285 | + --td-component-border: var(--td-gray-color-4); |
| 286 | +
|
| 287 | + --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%); |
| 288 | + --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%); |
| 289 | + --td-shadow-3: 0 6px 30px 5px rgba(0, 0, 0, 5%), 0 16px 24px 2px rgba(0, 0, 0, 4%), |
| 290 | + 0 8px 10px -5px rgba(0, 0, 0, 8%); |
| 291 | + --td-shadow-4: 0 2px 8px 0 rgba(0, 0, 0, 0.06); |
| 292 | +
|
| 293 | + --td-shadow-inset-top: inset 0 0.5px 0 #dcdcdc; |
| 294 | + --td-shadow-inset-right: inset 0.5px 0 0 #dcdcdc; |
| 295 | + --td-shadow-inset-bottom: inset 0 -0.5px 0 #dcdcdc; |
| 296 | + --td-shadow-inset-left: inset -0.5px 0 0 #dcdcdc; |
| 297 | +
|
| 298 | + --td-table-shadow-color: rgba(0, 0, 0, 8%); |
| 299 | +
|
| 300 | + --td-scrollbar-color: rgba(0, 0, 0, 10%); |
| 301 | + --td-scrollbar-hover-color: rgba(0, 0, 0, 30%); |
| 302 | + --td-scroll-track-color: #fff; |
| 303 | + --bg-color-demo-desc: #0009; |
| 304 | + --bg-color-demo-title: #000000e6; |
| 305 | + --bg-color-demo: #fff; |
| 306 | + --bg-color-demo-border: #e7e7e7; |
| 307 | + --bg-color-demo-secondary: #fff; |
| 308 | + --td-navbar-bg-color: #f6f6f6; |
| 309 | + --td-navbar-color: black; |
| 310 | + --td-navbar-title-font-size: 26rpx; |
| 311 | + --td-navbar-title-font-weight: 400; |
| 312 | + --td-color-demo-notice-icon: rgba(0, 0, 0, 0.9); |
| 313 | + --td-avatar-border-color: var(--bg-color-demo); |
| 314 | + --td-progress-circle-inner-bg-color: var(--bg-color-demo); |
| 315 | + --td-navbar-bg-color-example: var(--td-bg-color-container); |
| 316 | + --td-navbar-color-example: var(--td-text-color-primary); |
| 317 | + --td-grid-bg-color-example: var(--td-bg-color-container); |
| 318 | + --td-grid-item-bg-color-example: var(--td-bg-color-container); |
| 319 | + --td-input-border-color-example: rgba(220, 220, 220, 1); |
| 320 | + } |
| 321 | +`; |
| 322 | + |
| 323 | +const LIGHT_WHITE_BACKGROUND_COMPONENTS = [ |
| 324 | + 'button', |
| 325 | + 'divider', |
| 326 | + 'fab', |
| 327 | + 'icon', |
| 328 | + 'back-top', |
| 329 | + 'drawer', |
| 330 | + 'indexes', |
| 331 | + 'side-bar', |
| 332 | + 'avatar', |
| 333 | + 'count-down', |
| 334 | + 'empty', |
| 335 | + 'footer', |
| 336 | + 'image', |
| 337 | + 'image-viewer', |
| 338 | + 'progress', |
| 339 | + 'result', |
| 340 | + 'skeleton', |
| 341 | + 'swiper', |
| 342 | + 'tag', |
| 343 | + 'action-sheet', |
| 344 | + 'dialog', |
| 345 | + 'loading', |
| 346 | + 'message', |
| 347 | + 'notice-bar', |
| 348 | + 'overlay', |
| 349 | + 'popup', |
| 350 | + 'pull-down-refresh', |
| 351 | +]; |
| 352 | + |
160 | 353 | export const changeThemeMode = () => {
|
161 | 354 | const parentIframe = document.querySelector('iframe');
|
162 | 355 | let previewIframeList: any = null;
|
@@ -204,17 +397,52 @@ function addDarkStyle(previewIframe: any) {
|
204 | 397 | const mode = targetNode.getAttribute('theme-mode');
|
205 | 398 | const iframeDom = previewIframe.contentWindow.document.documentElement;
|
206 | 399 | const darkStyleElement = previewIframe.contentWindow.document.getElementById('dark');
|
| 400 | + const lightStyleElement = previewIframe.contentWindow.document.getElementById('light'); |
| 401 | + // 需要重新加载不同背景的组件,所以每次都要删除重新添加 |
| 402 | + if (lightStyleElement) { |
| 403 | + iframeDom.removeChild(lightStyleElement); |
| 404 | + } |
207 | 405 | if (mode === DARK_MODE_NAME) {
|
208 |
| - const styleElement = document.createElement('style'); |
209 |
| - styleElement.type = 'text/css'; |
210 |
| - styleElement.textContent = darkModeCss; |
211 |
| - styleElement.id = 'dark'; |
212 | 406 | if (!darkStyleElement) {
|
| 407 | + const styleElement = getDarkStyle(); |
213 | 408 | iframeDom.appendChild(styleElement);
|
214 | 409 | }
|
215 | 410 | } else {
|
216 | 411 | if (darkStyleElement) {
|
217 | 412 | iframeDom.removeChild(darkStyleElement);
|
218 | 413 | }
|
| 414 | + // 需要重新加载不同背景的组件 #fff #f6f6f6 |
| 415 | + const styleElement = getLightStyle(); |
| 416 | + iframeDom.appendChild(styleElement); |
| 417 | + } |
| 418 | +} |
| 419 | + |
| 420 | +function getDarkStyle() { |
| 421 | + const styleElement = document.createElement('style'); |
| 422 | + styleElement.type = 'text/css'; |
| 423 | + styleElement.textContent = darkModeCss; |
| 424 | + styleElement.id = 'dark'; |
| 425 | + return styleElement; |
| 426 | +} |
| 427 | + |
| 428 | +function getLightStyle() { |
| 429 | + const curComponentBackground = getCurComponentBackground(); |
| 430 | + const styleElement = document.createElement('style'); |
| 431 | + styleElement.type = 'text/css'; |
| 432 | + styleElement.textContent = lightModeCss + curComponentBackground; |
| 433 | + styleElement.id = 'light'; |
| 434 | + return styleElement; |
| 435 | +} |
| 436 | + |
| 437 | +function getCurComponentBackground() { |
| 438 | + const url = window.location.href; |
| 439 | + const lastUnderscoreIndex = url.lastIndexOf('/'); |
| 440 | + const lastSegment = url.substring(lastUnderscoreIndex + 1); |
| 441 | + let backgroundCss = ''; |
| 442 | + if (LIGHT_WHITE_BACKGROUND_COMPONENTS.includes(lastSegment)) { |
| 443 | + backgroundCss = 'body{ background: #fff !important; }'; |
| 444 | + } else { |
| 445 | + backgroundCss = 'body{ background: #f6f6f6 !important; }'; |
219 | 446 | }
|
| 447 | + return backgroundCss; |
220 | 448 | }
|
0 commit comments