-
Notifications
You must be signed in to change notification settings - Fork 34
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
Update docs for conditional CSS import #142
base: main
Are you sure you want to change the base?
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
+ import 'jotai-devtools/styles.css'; | ||
+ | ||
+ if (process.env.NODE_ENV === 'development') { | ||
+ require('jotai-devtools/styles.css') |
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.
Thanks for the PR! I'm not sure if I'd recommend using require
due to its limited support 🤔
What about lazy loading the component like this 👇? Would that for Next.Js?
const LazyDevtools = lazy(() =>
import("./JotaiDevtools").then((module) => ({
default: module.DevTools,
}))
);
export const YourApp = () => {
return (
<Suspense fallback="">
{ process.env.NODE_ENV === "development" ? <LazyDevtools/> : null }
</Suspense>
);
}
In ./JotaiDevTools.tsx
import "jotai-devtools/styles.css";
export { DevTools } from "jotai-devtools";
**Vite users could do something like this 👇 **
import { DevTools } from "jotai-devtools";
import css from "jotai-devtools/styles.css?inline";
export const JotaiDevTools = () =>
process.env.NODE_ENV !== "production" ? (
<>
<style>{css}</style>
<DevTools />
</>
) : null;
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.
However, for some reason when I do this the build file turns out to be MUCH larger.
@npearson72 - Thanks for reporting. That seems strange! 🤔 Works fine on my end (see screenshot below). Could you create a small repro?
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 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.
@arjunvegda have you looked at jotai-devtools/dist/index.css
?
It has all the mantine styles in it. I'm assuming you wanted those to live in jotai-devtools/dist/internal__devtools.css
, but perhaps I've misunderstood.
In any case, it seems v0.8.0, which is using Mantine 6x which is not having this issue. But all later versions do.
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.
Ah! you're missing process.env.NODE_ENV !== "production"
check in DevTools.tsx#L4.
See the full code in the above comment.
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.
Oh, I see. I didn't realize that process.env
was available in the Vite UI, but that make sense. Thank you.
@arjunvegda this is helpful, thanks for the tip. I'm using the vite version you posted. However, for some reason when I do this the build file turns out to be MUCH larger. It appears as if it's pulling in CSS from other libraries (i.e. Mantine) in my node_modules. Having trouble understand what's actually happening other than my vendor file is twice as large now. Any ideas? |
@arjunvegda is there a reason the devtools themselves can't import |
if that's not an option this is probably the preferred import method for Next
'use client'
import { Provider as JotaiProvider, createStore } from 'jotai'
import dynamic from 'next/dynamic'
import { ReactNode } from 'react'
const JotaiDevtools = dynamic(() => import('@/components/providers/jotai-devtools'), {ssr: false})
const store = createStore()
export const Providers = ({children}: {children: ReactNode}) => {
return (
<JotaiProvider store={store}>
{process.env.NODE_ENV === 'development' && <JotaiDevtools store={store} />}
{children}
</JotaiProvider>
)
}
import { createStore } from 'jotai'
import { DevTools } from 'jotai-devtools'
import 'jotai-devtools/styles.css'
export default function JotaiDevtools({ store }: { store: ReturnType<typeof createStore> }) {
return (
<DevTools
store={store}
theme='dark'
position='bottom-right'
options={{ shouldExpandJsonTreeViewInitially: true }}
/>
)
} |
I tried that only to find that most bundlers don't support that and they error out. 😟 If our current approach becomes too difficult for users, we could look into converting these styles from css files to inline styles 🤔 . |
We took a similar approach for Next.js, but separated out all the conditional logic into its own component: JotaiProvider.tsx
DevOnlyJotaiDebugging.tsx
JotaiDebugging.tsx
|
I tried it like this first but it was complaining that an import has to be a top-level declaration:
I also tried it with
next/dynamic
but no luck there.