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!
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
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:
Next, we’re going to let webpack know we’d like to build Less:
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:
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!
The post Setting up and Customizing the Ant Design System in a Nuxt App appeared first on CSS-Tricks.
You can support CSS-Tricks by being an MVP Supporter.