-
Notifications
You must be signed in to change notification settings - Fork 920
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
feat(theme-default): dynamically change theme-color with dark mode (close #215) #218
Conversation
packages/@vuepress/theme-default/src/client/components/ToggleDarkButton.vue
Outdated
Show resolved
Hide resolved
packages/@vuepress/theme-default/src/client/components/ToggleDarkButton.vue
Outdated
Show resolved
Hide resolved
We are using following snippet to dedupe But I think that supporting multiple |
It seems little rude to allow duplicated meta name 'theme-color' in this way, but it simple. Or still avoid duplicated 'theme-color', but keep if they has a different hash. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's another problem.
Meta tags set in user config will be injected to client with useSiteData
. So we also have to update the sitedata ref, or the tag will be reset once navigating to another page.
packages/@vuepress/theme-default/src/client/components/ToggleDarkButton.vue
Outdated
Show resolved
Hide resolved
packages/@vuepress/theme-default/src/client/components/ToggleDarkButton.vue
Outdated
Show resolved
Hide resolved
packages/@vuepress/theme-default/src/client/components/ToggleDarkButton.vue
Outdated
Show resolved
Hide resolved
packages/@vuepress/theme-default/src/client/components/ToggleDarkButton.vue
Outdated
Show resolved
Hide resolved
The problem about site data could be handled in this PR. If you are not familiar with that, I'll update it later. |
I'm not familiar with that and now I may not have enough time to learn and handle that correctly during my term examination (maybe later I cloud have a try). You can also update it, thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks much better.
packages/@vuepress/theme-default/src/client/components/ToggleDarkButton.vue
Outdated
Show resolved
Hide resolved
packages/@vuepress/theme-default/src/client/components/ToggleDarkButton.vue
Outdated
Show resolved
Hide resolved
Another point: Currently in this PR, we are using |
Do you think this workaround would be better? The let themeColorLight: string
let themeColorDark: string
const updateThemeColor = (): void => {
const themeColorEl = window?.document.querySelectorAll('meta[name="theme-color"]')
if (!themeColorEl) return
// get current system appearance mode by `prefers-color-scheme`
const systemAppearanceMode = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
// iterate `theme-color` meta tags
themeColorEl.forEach((meta) => {
// get `media` attribute from this meta tag
const mediaAttr = meta.getAttribute('media')
// initial themeColorLight or themeColorDark if uninitialized by appearance mode
if (!themeColorLight && mediaAttr?.includes('light')) {
themeColorLight = meta.getAttribute('content')!
}
if (!themeColorDark && mediaAttr?.includes('dark')) {
themeColorDark = meta.getAttribute('content')!
}
// if themeColorLight and themeColorDark are initiated, change `content` attribute if the meta `media` attribute matches current system appearance mode
if (themeColorLight && themeColorLight && mediaAttr?.includes(systemAppearanceMode)) {
meta.setAttribute('content', isDark.value ? themeColorDark : themeColorLight)
}
})
} But according to #221 (comment), multiple themeConfig: {
...
themeColorLight: '#xxxxxx',
themeColorDark: '#xxxxxx',
....
} Or we'd better wait new standard came out, thanks your patient advice : )! |
Currently, we could use user config as the light mode theme-color, and use Also, the I've updated this PR with a force push to resolve some conflicts. You could help to check if it works as expected when you have time. |
Thanks, it works as expected after fix a logic typo in commit 5bd5498. But has another problem you mentioned before:
And for better dark mode Screen.Recording.2021-06-17.at.22.13.27.movI changed this PR into draft again, may try to finish this later. |
Considering that simply changing the meta tag of I prefer to do this with a new option in Tested on my laptop, Safari 15.0: Untitled.movStill have a lot to learn before I can write quality code. |
@Unbinilium is attempting to deploy a commit to the VuePress Team on Vercel. A member of the Team first needs to authorize it. |
As a reference, this https://github.com/tolking/vuepress-theme-default-prefers-color-scheme |
Closing as it's stale for a long time. Thanks for all the efforts anyway |
Closing as it's stale for a long time. Thanks for all the efforts anyway |
Description
Linked with this feature request issue vuepress/ecosystem#7
Note
For better user experience, the browser should get color hex before the page loaded, but it seems vuepress-next is not currently supported duplicated meta tags like this:
It means the
theme-color
was changed after the script executes if system side is dark mode by default, because I could not have different color scheme in duplicated meta tags currently, and due to some reason that#3eaf7c
seems too bright that Safari will not use it for address bar color if system side is dark mode by default (darker would works).Also it may not be the best or proper workaround, some further commit may should be added.