-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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(v2): Add <Root> theme element #3932
Conversation
✔️ Deploy preview for docusaurus-2 ready! 🔨 Explore the source changes: 2f0914b 🔍 Inspect the deploy logs: https://app.netlify.com/sites/docusaurus-2/deploys/5fdba1ed4c5d6200087a5200 😎 Browse the preview: https://deploy-preview-3932--docusaurus-2.netlify.app |
⚡️ Lighthouse report for the changes in this PR:
Lighthouse ran on https://deploy-preview-3932--docusaurus-2.netlify.app/classic/ |
Size Change: +20 B (0%) Total Size: 152 kB ℹ️ View Unchanged
|
Wow that was fast!!! |
@slorber Hey, thank you for your hard work! It seems that the Looking forward to your reply, thank you! |
@guanghechen , Root is a "core" component, not a theme component. It is one layer above the theme, and thus can't access anything to the theme. The concept of dark mode does not exist in the context of the Docusaurus core, it's a theme-specific feature. Theoretically, some theme might decide to not implement dark mode, or you can even use multiple themes at the same time. You can still access the theme by reading Can you share your use-case for this? maybe there's a workaround I can suggest. |
@slorber thank you for your quick reply. I use the material-ui component library, so I hope to use the
This is a great idea, if possible, I hope it could be implemented! I think this is very useful for the case of using use of mature third-party component libraries.
At this stage, I think this method is enough, thank you for your suggestion, it is very useful! |
@slorber I have encountered a new problem, how can I trigger a component update when switching the theme, if I just simply get the theme through the following code? function Root({ children }) {
const [isDarkTheme, setIsDarkTheme] = useState(false)
useEffect(() => {
const theme = document.documentElement.getAttribute('data-theme')
setIsDarkTheme(theme === 'dark')
}, [])
return (
<ThemeProvider theme={isDarkTheme ? muiDarkTheme : muiLightTheme}>
{children}
</ThemeProvider>
)
} |
Okay, this works! function Root({ children }) {
const [isDarkTheme, setIsDarkTheme] = useState(false)
useEffect(() => {
const update = () => {
console.log('clicked')
const theme = document.documentElement.getAttribute('data-theme')
setIsDarkTheme(theme === 'dark')
}
update()
const observer = new MutationObserver(function (mutations) {
for (const mutation of mutations) {
if (mutation.type == 'attributes') update()
}
})
observer.observe(document.documentElement, { attributes: true })
return () => observer.disconnect()
}, [])
return (
<ThemeProvider theme={isDarkTheme ? muiDarkTheme : muiLightTheme}>
{children}
</ThemeProvider>
)
} |
We don't have proper CSS-in-JS support on the server, so it's possible you end-up with FOUC after building your site (and there's no good workaround for that). We'll likely add a way for CSS-in-JS plugins to add such a provider without using swizzling. |
Excuse me, since I am not a native English speaker, I would like to ask what FOUC means. My dictionary tells me it is |
Yes, it means that some css will only inserted/applied after React hydration, so on the first milliseconds of the static page load, your UI would look broken (because of the missing CSS), and only later it would be fixed (because that CSS would be inserted by your CSS-in-JS lib). To prevent these issues, CSS-in-JS libs provide server-side system so we can inline critical CSS directly in the static HTML files, ensuring there's no missing CSS rule on the first render. |
Oh, I see! Thank you for your patient explanation, you are the best! In fact, I did encounter this problem. So, there will be FOUC problems in all the libraries that use CSS-in-JS in docusaurus at this stage? |
Yes, unfortunately, until we implement #3236 (we really want this to happen, just a matter of time) |
Okay, I get it. (A year flies so fast) |
Motivation
Fixes #3919
We need a way to add state components (often providers) at the very top of the tree, and avoid unmount/remount when navigating.
This
<Root>
theme component is at the very top of the app, even above the<Layout>
component, and unlike the layout (that is applied on a per-page basic), the Root component is rendered consistently, never unmounts, and can hold stateful logic.Example: put this in
website/src/theme/Root.js
Note: we might still provide a
wrapRootElement
API, like Gatsby does here, but this Root theme element can be useful in the meantime.Have you read the Contributing Guidelines on pull requests?
yes
Test Plan
Not sure how, but theme aliases are already tested