Skip to content

Implement TODO: enhance dark mode detection#373

Open
tageniu wants to merge 3 commits intoalibaba:mainfrom
tageniu:feat/dark-mode-detect
Open

Implement TODO: enhance dark mode detection#373
tageniu wants to merge 3 commits intoalibaba:mainfrom
tageniu:feat/dark-mode-detect

Conversation

@tageniu
Copy link
Copy Markdown

@tageniu tageniu commented Mar 30, 2026

What

Resolve the @todo in checkDarkMode.ts by adding 3 new dark mode detection strategies and expanding data-attribute coverage:

  • Data attributes: also check data-color-mode (GitHub), data-bs-theme (Bootstrap 5), data-color-scheme on both <html> and <body>
  • CSS color-scheme: read <meta name="color-scheme"> and the computed color-scheme property on :root
  • Layout containers: inspect background color of <main>, #app, #root, #__next, [role="main"] for SPAs where <body> stays transparent
  • Text color: treat high-luminance body text (> 180) as a dark-theme signal

Type

  • Bug fix
  • Feature / Improvement
  • Refactor
  • Documentation
  • Website
  • Demo / Testing
  • Breaking change

Testing

  • Tested in modern browsers
  • No console errors
  • Types/doc added

Requirements / 要求

  • I have read and follow the Code of Conduct and Contributing Guide . / 我已阅读并遵守行为准则。
  • This PR is NOT generated by a bot or AI agent acting autonomously. I have authored or meaningfully reviewed every change. / 此 PR 不是由 bot 或 AI 自主生成的,我已亲自编写或充分审查了每一处变更。

Resolve the @todo in checkDarkMode.ts by adding 3 new detection
strategies and expanding data-attribute coverage:

- Check data-color-mode, data-bs-theme, data-color-scheme attributes
- Read CSS color-scheme property and <meta name="color-scheme"> tag
- Inspect background of SPA containers (#app, #root, #__next, etc.)
- Detect light text color as a dark-theme signal
Copilot AI review requested due to automatic review settings March 30, 2026 22:05
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 30, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
2 out of 3 committers have signed the CLA.

✅ gaomeng1900
✅ tageniu
❌ admin


admin seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Enhances dark mode detection in isPageDark() by adding additional heuristics beyond class and background checks to better handle modern frameworks and theming approaches.

Changes:

  • Expanded dark-mode detection via additional HTML/body data attributes.
  • Added detection via <meta name="color-scheme"> and computed color-scheme on :root.
  • Added layout-container background inspection and a light-text heuristic as additional signals.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +100 to +105
if (meta) {
const content = meta.content.toLowerCase()
// "dark" or "only dark" → dark; "light dark" is ambiguous so skip
if (content === 'dark' || content === 'only dark') return true
if (content === 'light' || content === 'only light') return false
}
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meta.content is lowercased but not trimmed before exact comparisons. Valid/meta-authored values like " dark " or "only dark " will currently be treated as inconclusive. Consider applying .trim() (and ideally normalizing internal whitespace) before comparing tokens.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment on lines +142 to +146
if (!el) continue

const style = window.getComputedStyle(el)
if (isColorDark(style.backgroundColor)) {
return true
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isMainContentDark() relies on isColorDark(style.backgroundColor), but isColorDark/parseRgbColor ignore the alpha channel. Semi-transparent backgrounds like rgba(0, 0, 0, 0.4) will be classified as "dark" even though the effective blended background might be light, and formatting variations like rgba(0,0,0,0) can slip past the current transparency guard. Consider parsing alpha and treating alpha=0 as transparent (skip) and alpha<1 as inconclusive or walking up to a non-transparent ancestor/background.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

tageniu and others added 2 commits March 30, 2026 18:16
@gaomeng1900
Copy link
Copy Markdown
Collaborator

gaomeng1900 commented Apr 2, 2026

Thanks for your contribution.

Before I merge this PR. Please rebase and fix the following problems:

  • fix the commit message to fit conventional style Update packages/page-controller/src/mask/checkDarkMode.ts
  • replace the author admin with real username

@tageniu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants