-
Notifications
You must be signed in to change notification settings - Fork 67
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: appearance support light/dark mode theme in CodeHike #190
base: master
Are you sure you want to change the base?
feat: appearance support light/dark mode theme in CodeHike #190
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
✅ Deploy Preview for jade-conkies-8119e7 ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
I think it should be added in the custom theme instead of framework logic. |
I agree with you. I checked the documentation again and implemented the desired effect using // `.island/theme/index.tsx`
import React, { useEffect } from 'react';
import {
Layout as DefaultLayout,
NotFoundLayout,
HomeLayout,
setup
} from 'islandjs/theme';
// Add some custom styles
import "./github-from-css.css";
const docEl = document.documentElement
const getIsDark = (target: HTMLElement) => target.classList.contains('dark')
const setCHTheme = (isDark: boolean) => {
const chThemeEls = document.querySelectorAll("[data-ch-theme='github-from-css'], [data-ch-theme='material-from-css']")
const mode = isDark ? '' : 'light'
chThemeEls.forEach(el => el.setAttribute('data-theme', mode))
}
const classListChanged = (mutationList) => {
for (const mutation of mutationList) {
const { type, target } = mutation
if (type === "attributes") {
setCHTheme(getIsDark(target))
}
}
}
const observer = new MutationObserver(classListChanged)
const Layout = () => {
useEffect(() => {
// Execute immediately once during initialization
setCHTheme(getIsDark(docEl))
observer.observe(docEl, { attributes: true })
return () => {
observer.disconnect()
}
})
return <DefaultLayout />
}
// Export the three components and the setup function
export { Layout, HomeLayout, NotFoundLayout, setup }; and the effect is like this: Welcome any suggestions to improve it or better approaches. |
Nice! |
But there are some lint problem.Please check it locally. |
As you said, it should be added in the custom theme instead of framework logic. So this pr can probably be closed right? |
No description provided.