next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Work with antd?

Open thebetterjort opened this issue 9 years ago • 44 comments

I'm trying to get nextjs to work with antd but get

"You may need an appropriate loader to handle this file type."

What do I need to do in order to get these things working together? Thanks.

thebetterjort avatar Dec 22 '16 19:12 thebetterjort

May be that's related to importing .css files. Currently, we don't support importing custom modules.

Even if you've added a correct webpack-loader, it won't work since SSR env cannot import that.

arunoda avatar Dec 22 '16 22:12 arunoda

https://github.com/ant-design/babel-plugin-import#via-babelrc

This is no help then. Is custom modules on the map, or not so much because of SSR?

thebetterjort avatar Dec 22 '16 23:12 thebetterjort

We wanted to support it. But I think we need to work a bit harder for that. So, that's after a priority after 2.0

arunoda avatar Dec 22 '16 23:12 arunoda

Thanks Arunoda! I seem to have followed you from meteor. Nice to see you around.

thebetterjort avatar Dec 22 '16 23:12 thebetterjort

Is is still planned for after 2.0? Should this be reopened then?

sedubois avatar Jan 12 '17 10:01 sedubois

+1 for antd

soulmachine avatar Jan 17 '17 22:01 soulmachine

feature supported now? I am tring to use antd with nextjs

liadbiz avatar Feb 10 '17 09:02 liadbiz

@liadbiz have a look here https://github.com/zeit/next.js/issues/544

timneutkens avatar Feb 10 '17 12:02 timneutkens

@timneutkens thanks. I will give feedback after tring.

liadbiz avatar Feb 11 '17 02:02 liadbiz

Just thought I'd chime in since this seems to be a bit of a tricky issue in next.js. Anyhow here's how I'm able to use antd's css imports in my project: https://github.com/zeit/next.js/issues/544#issuecomment-280190145

jozanza avatar Feb 16 '17 00:02 jozanza

@jozanza do you have a working example of this? Where you render an antd component? And how you then import the css? Because I can't make it work :'(

AugustinLF avatar Mar 15 '17 15:03 AugustinLF

@AugustinLF I've figured out an example if you're interested. I haven't made a really in depth application so I don't know if any other problems exist yet, but I've been able to import a Button component and have it display.

If you clone this example: https://github.com/zeit/next.js/tree/master/examples/with-global-stylesheet

Then import that antd styling like so in an app level component.

components/App.js

import styles from 'antd/dist/antd.css';

export default ({ children }) => (
  <main>
    <style dangerouslySetInnerHTML={{ __html: styles}} />
    {children}
  </main>
)

Then in any of your pages, I'm using index.js as an example:

import App from '../components/App'
import Header from '../components/Header'
import withData from '../lib/withData'

import { Button } from 'antd';

export default withData((props) => (
  <App >
    <Header pathname={props.url.pathname} />
    <Button>Test</Button>
  </App>
))

And voila you have an antd styled button. Again, I'm still learning to see if this works without issue, so I'm going to continue playing with this solution. Unfortunately I haven't found a solution for importing a single component's set of styles at a time so this will be kind of expensive over the network.

blakedietz avatar Mar 18 '17 17:03 blakedietz

Yes, thank you, it worked out for me!

AugustinLF avatar Mar 18 '17 20:03 AugustinLF

Yeah, that works pretty well. You can also enable global imports like this import { Button, Input, Form } from 'antd' by using the babel-plugin-import. Loading all the CSS is not such a big issue, since it is just 43 KB gzipped.

MrLoh avatar Apr 09 '17 13:04 MrLoh

#484

PhanXuanThuan avatar Apr 09 '17 13:04 PhanXuanThuan

Do you recommend the approach from @blakedietz ?

rdewolff avatar Apr 11 '17 12:04 rdewolff

I compiled the less files in the static folder, since I needed my own theming and then just added a style tag, but the end result is about the same I think

MrLoh avatar Apr 11 '17 12:04 MrLoh

OK, I solved it, but you must to import the whole css, such as me, I import the css file in Head. Just see the code:

layout/index.js

import Head from 'next/head'
export default ({children}) => (
  <div>
    <Head>
      <link rel='stylesheet' href='http://cdn.bootcss.com/antd/2.9.3/antd.css' />
    </Head>
    {children}
  </div>
)

pages/index.js

import React from 'react'
import Layout from '../layout'
import { Button } from 'antd'
export default () => (
  <Layout>
    <Button type="primary">Primary</Button>
  </Layout>
)

And to import on demand, we should add babel plugins, please run npm install -D babel-plugin-import at first.

.babelrc

{
  "presets": ["next/babel"],
  "plugins": [
  	["import", {
           "libraryName": "antd",
           "style": false
        }]
  ]
}

That's all, good luck to you!

ouxu avatar May 06 '17 13:05 ouxu

This issue still exists today. it seems SSR just won't support global css import.

viztor avatar Jul 02 '17 04:07 viztor

I'm also having this issue. I'm trying to import semantic.min.css but couldn't. Throws me an error.

Ikhan avatar Jul 02 '17 16:07 Ikhan