I don’t typically work with UI libraries because they can be cumbersome and hard to override, which can contribute to a bloated. However, Ant Design has recently gained some some of my affection because it’s easy to use, has extensible defaults, and features a delicate design.
Nuxt and Ant Design work well together, in part because of Nuxt’s code-splitting and tree-shaking abilities, not to mention Nuxt’s new static target deployment option. I can serve an app using Ant Design with great performance scores.
Combining the two was a little tricky and there isn’t a lot in the way of documentation for how to do it, so what follows are the steps you need to set it up. Let’s get started!
Install Ant.design
The first step is installing the ant-design-vue package, along with Less.js and less-loader, which we will need to create our Less variables:
yarn add ant-design-vue less less-loader
# or
npm i ant-design-vue less less-loader
Now lets tell Nuxt to use it globally via a plugin. We’ll create a file called antd-ui.js
:
import Vue from 'vue'
import Antd from 'ant-design-vue/lib'
Vue.use(Antd)
You may notice that unlike the process outlined in the Ant Design getting started guide, we are not importing the global CSS file they mention. That’s because we’re going to manually import the base variable Less file instead so that we can override it.
We have a few things to do in our nuxt.config.js
file. First, let’s register the plugin we just made:
plugins: ["@/plugins/antd-ui"],
Next, we’re going to let webpack know we’d like to build Less:
build: {
loaders: {
less: {
lessOptions: {
javascriptEnabled: true,
},
},
},
}
Finally, we need to create a global stylesheet for our variables that imports Ant Design’s defaults as well as our overrides:
css: [
"~/assets/variables.less"
],
We can see that this file exists in a /assets
folder, so let’s make it. We’ll create a file in there called variables.less
, and import Ant Design’s Less variables:
@import '~ant-design-vue/dist/antd.less';
Below this line, there are myriad variables you can override. This is just a sampling. The rest of the variables are here, and you’ll need to include them by their @
and can change it to whatever you wish:
@primary-color: #1890ff; // primary color for all components
@link-color: #1890ff; // link color
@success-color: #52c41a; // success state color
@warning-color: #faad14; // warning state color
@error-color: #f5222d; // error state color
@font-size-base: 14px; // major text font size
@heading-color: rgba(0, 0, 0, 0.85); // heading text color
@text-color: rgba(0, 0, 0, 0.65); // major text color
@text-color-secondary: rgba(0, 0, 0, 0.45); // secondary text color
@disabled-color: rgba(0, 0, 0, 0.25); // disable state color
@border-radius-base: 4px; // major border radius
@border-color-base: #d9d9d9; // major border color
@box-shadow-base: 0 2px 8px rgba(0, 0, 0, 0.15); // major shadow for layers
We’re good to go! There’s no need to import what we need into every component because Nuxt will now take care of that. If you’d like to override very specific styles not included in the variables, you can find the associative classes and override them in your layouts/default.vue
file as well.
Ant.design and Nuxt allow you a great framework for building apps very quickly and with ease. Enjoy!
Ant design is using momentjs very cumbersome and probably not going to be maintained further…
Great tutorial!! Helps a lot!