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

Themes in NextJS doesn't work #84

Closed
ThomasGysemans opened this issue Dec 10, 2021 · 5 comments
Closed

Themes in NextJS doesn't work #84

ThomasGysemans opened this issue Dec 10, 2021 · 5 comments

Comments

@ThomasGysemans
Copy link

My project and steps to reproduce the problem

I want to use SweetAlert with the theme entitled "borderless". I was coding my project with it but I realised it wasn't working, so I built a new nextjs project with the following code:

// _app.js
import "../styles/globals.css";
import "@sweetalert2/theme-borderless/borderless.css";

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />;
}

export default MyApp;
// index.js
import styles from "../styles/Home.module.css";
import Swal from "sweetalert2";
import withReactContent from "sweetalert2-react-content";

export default function Home() {
  const MySwal = withReactContent(Swal);

  const open = () => {
    MySwal.fire({
      title: <p>Test with BORDERLESS Theme</p>,
    });
  };

  return (
    <div className={styles.container}>
      <main className={styles.main}>
        <button onClick={open} className={styles.OPENSWAL}>
          OPEN
        </button>
      </main>
    </div>
  );
}

NOTE: I did not change anything in the css files when I created the new project.

I have executed the following commands, in that order:

npx create-next-app testsweetalert
cd .\testsweetalert\
code .
npm install --save sweetalert2 sweetalert2-react-content
npm install --save sweetalert2 @sweetalert2/theme-borderless
npm run dev

Here is my package.json:

{
  "name": "testsweetalert",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@sweetalert2/theme-borderless": "^5.0.8",
    "next": "12.0.7",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "sweetalert2": "^11.3.0",
    "sweetalert2-react-content": "^4.2.0"
  },
  "devDependencies": {
    "eslint": "7.32.0",
    "eslint-config-next": "12.0.7"
  }
}

NOTE: I don't want to use SCSS

What's not working?

Swal is working, but the theme is the one by default. I don't know why the borderless theme is not used by Swal whereas it is installed, and the css file is imported in _app.js.

NOTE: importing the css file in /styles/globals.css doesn't change anything.

Expected behavior

I want to have the same behavior as on the documentation when I enable the borderless theme.

Live demo

You can reproduce easily a demo on your own computer, I have written the whole source code above.

@limonte
Copy link
Member

limonte commented Dec 10, 2021

I believe you missed this detail from the docs:

// index.js
- import Swal from 'sweetalert2'
+ import Swal from 'sweetalert2/src/sweetalert2.js'

Let me know if this helped

@ThomasGysemans
Copy link
Author

import Swal from "sweetalert2/src/sweetalert2.js"

is throwing an error:

SyntaxError: Cannot use import statement outside a module

Same if I remove the ".js" extension

@limonte
Copy link
Member

limonte commented Dec 10, 2021

@limonte limonte closed this as completed Dec 10, 2021
@ThomasGysemans
Copy link
Author

ThomasGysemans commented Dec 11, 2021

All right I had to use this instead:

import Swal from "sweetalert2/dist/sweetalert2.js";

The problem is that this isn't specified in the README of sweetalert2-react-content.
It is also false in the repo sweetalert2-themes.

As you are a member, could you change that or should I publish an issue there?
Or at least specify it in the READMEs?

@limonte
Copy link
Member

limonte commented Dec 12, 2021

That's the Next.js issue: vercel/next.js#9890

But since Next.js is quite popular, I think it does make sense to update the docs. Thanks for the suggestion! Done 👍

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