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

Problem with importing styling from npm package #328

Open
wa1id opened this issue Mar 27, 2023 · 2 comments
Open

Problem with importing styling from npm package #328

wa1id opened this issue Mar 27, 2023 · 2 comments

Comments

@wa1id
Copy link

wa1id commented Mar 27, 2023

Hi,

I'm trying to import styling from another npm package (https://www.npmjs.com/package/react-flatpickr#installation) but it doesn't seem to work.

import "flatpickr/dist/themes/material_green.css";

Something I am missing in the vite build?

import react from '@vitejs/plugin-react';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  // includes app env settings into process.env;
  // Only settings starts with `HYVA_` works
  process.env = { ...process.env, ...loadEnv(mode, process.cwd(), ['HYVA_']) };

  return {
    envPrefix: 'HYVA_',
    build: {
      cssCodeSplit: false,
      outDir: '../view/frontend/web',
      emptyOutDir: true,
      lib: {
        formats: ['es'],
        entry: 'src/main.jsx',
        fileName: (format) => `react-checkout.${format}.min.js`,
        name: 'react-checkout.js',
      },
    },
    esbuild: {
      jsxFactory: 'h',
      jsxFragment: 'Fragment',
      jsxInject: `import { h, Fragment } from 'react'`,
    },
    server: {
      proxy: {
        '/backend': {
          target: process.env.HYVA_BASE_URL || 'https://demo.hyva.io',
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/backend/, ''),
        },
      },
    },
    plugins: [react()],
  };
});

EDIT:
I can inject the css with js, not sure if this is the right approach

import materialGreenCSS from 'flatpickr/dist/themes/material_green.css';

function App() {
  useEffect(() => {
    const style = document.createElement('style');
    style.textContent = materialGreenCSS;
    document.head.appendChild(style);

    return () => {
      document.head.removeChild(style);
    };
  }, []);

  // Rest of your app components
}
@rajeev-k-tomy
Copy link
Collaborator

Where did you try to import this?

I assume you are using 2.0-develop in your project since you mentioned vitejs. You can see that in the reactapp/src/main.jsx file (this is entry point compoent), we have a css import there. In ViteJs, an import like that should work without any problem.

You can also try to use the src/index.css file which is already imported via main.jsx file. You can use @import commands there as you see examples of tailwindcss imports.

@rajeev-k-tomy
Copy link
Collaborator

Are you still facing the problem?

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

No branches or pull requests

2 participants