Merged
Conversation
28d92c1 to
0a44ea7
Compare
louismaximepiton
approved these changes
Oct 4, 2023
Member
louismaximepiton
left a comment
There was a problem hiding this comment.
Thanks for the PR 🎉
I'm not in the JS team but it adds some consistency for color-mode. This looks safe to me to implement:
- This doesn't interfer with any other mode than
autothat we've been adding on Bootstrap. - It adds consistency over the
data-bs-themeadded on the HTML. - It defaults to the two commons color modes proposed by every browsers/OS.
This comment was marked as off-topic.
This comment was marked as off-topic.
julien-deramond
approved these changes
Oct 26, 2023
Member
There was a problem hiding this comment.
Thanks for this PR @MewenLeHo
I can confirm the weird behavior in auto mode that switched between data-bs-theme="dark" (dark OS) and data-bs-theme="auto" (light OS). You fix allows to set the second use case to data-bs-theme="light" so that the behavior is consistent and allows to avoid handling "auto" in CSS.
@twbs/js-review, do you want to look at it too for a final review?
Note: when merged, we might want to:
- Inform users in the release note if they copied & pasted this file to update it
- Update our other repositories where a similar
color-modes.jsis used
mdo
approved these changes
Oct 31, 2023
This was referenced Nov 2, 2023
This was referenced May 19, 2024
This was referenced May 24, 2024
This was referenced Oct 2, 2024
This was referenced Oct 5, 2024
This was referenced Oct 12, 2024
This was referenced Oct 15, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Our team found a potential edge case while working on our Bootstrap fork.
When using the current version of
color-mode.js:The results will be:
color-modeon devicecolor-modeon Bootstrap*darkdata-bs-theme="dark"*lightdata-bs-theme="light"darkautodata-bs-theme="dark"lightautodata-bs-theme="auto"At this point you may have an edge case when your device is set to 'Light mode' and you use the 'Auto color mode' on the page because you will have to add more CSS to make something like:
Proposed patch:
const setTheme = theme => { - if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) { - document.documentElement.setAttribute('data-bs-theme', 'dark') + if (theme === 'auto') { + document.documentElement.setAttribute('data-bs-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')) } else {Then you will have:
color-modeon devicecolor-modeon Bootstrap*darkdata-bs-theme="dark"*lightdata-bs-theme="light"darkautodata-bs-theme="dark"lightautodata-bs-theme="light"And you don't need to add extra CSS everywhere.
Motivation & Context
Fix edge case when your device is set to 'Light mode' and you use the 'Auto color mode' on the page.
Type of changes
Checklist
npm run lint)Live previews