Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kevinschaich
Copy link
Member

I tried it like this first but it was complaining that an import has to be a top-level declaration:

if (process.env.NODE_ENV === 'development') {
    import('jotai-devtools/styles.css')
}

I also tried it with next/dynamic but no luck there.

Copy link

codesandbox-ci bot commented May 6, 2024

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')
Copy link
Member

@arjunvegda arjunvegda May 8, 2024

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;

Copy link
Member

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?

Group 1854

Copy link

@npearson72 npearson72 May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

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.

Copy link

@npearson72 npearson72 May 10, 2024

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.

@npearson72
Copy link

npearson72 commented May 9, 2024

@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?

@kevinschaich
Copy link
Member Author

@arjunvegda is there a reason the devtools themselves can't import styles.css? I think it would simplify imports for all frameworks

@kevinschaich
Copy link
Member Author

if that's not an option this is probably the preferred import method for Next

providers.tsx

'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>
    )
}

jotai-devtools.tsx

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 }}
        />
    )
}

@arjunvegda
Copy link
Member

@arjunvegda is there a reason the devtools themselves can't import styles.css? I think it would simplify imports for all frameworks

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 🤔 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants