This is the last part of a guide on building dynamic analytics dashboards and applications with React, GraphQL, and Cube.js. It covers an introduction to the customization of the UI. At the end of the blog post, I’ll add links to the customization of individual components of the dashboard, such as query builders and the charts themselves. The online demo is available here. Parts one and two are available at their respective links. 

The dashboard template was generated using the Ant Design UI React library. It is one of the most popular React UI kits, alongside Material UI. It uses Less as a stylesheet language and allows us to customize the design by overriding default Less variables.

As I mentioned in the first chapter, our Dashboard App is based on Create React App (CRA). Currently, it doesn’t support Less out of the box, and to make it work, we need to use an eject command.

Create React App provides a fully configured environment and a default configuration. And all this configuration is hidden from you. But, when you eject, all that configuration will be exposed to you. This means that you will get full control and be able to add things like Less support. But at the same time, you will be responsible for maintaining all of that configuration.

You may also like: Data Visualization With the Highcharts React Wrapper Dashboard.

Eject is irreversible. You need to commit your changes before and then run eject in the dashboard-app folder.

$ git commit -a $ yarn eject

Once you’ve run it, you can find a new folder called config. Inside the config folder, you can find all the project configuration files, but today, we only need the webpack.config.js file.

Now let’s install Less.

$ yarn add less less-loader

Next, we need to modify the webpack configuration file. Open config/webpack.config.js and
find the cssRegex constant and change it:

-const cssRegex = /.css$/; +const cssRegex = /.(?:le|c)ss$/;

Then, find the getStyleLoaders function. On the use array, after the css-loader object, add:

{ loader: require.resolve('less-loader'), options: { importLoaders: 1,
javascriptEnabled: true, } }

That’s it! With this in place, we are ready to override some of the antd’s default variables and styles. We are going to customize some variables according to the antd’s Customize Theme guide.

Create a dashboard-app/src/variables.less file with the following content.

Next, let’s create an index.less file, which will be imported in index.js. Here, we do several things: import and styles, import the Dm Sans font from Google Fonts, import the just-created file with modified variables, and finally, add some minor customization.

The last thing is to import the index.less in our index.js. Replace the old import of index.css and feel free to delete this old file as well.

-import "./index.css"; +import "./index.less";

The styles above customize our design globally, changing the look of some components. But to customize some specific components, like the top menu, we are going to use Styled Components.

Styled Components allow you to write CSS right inside your components. It is a variant of “CSS-in-JS” — which solves many of the problems with traditional CSS like selector name collisions.

Let’s first add styled-components to our project.

$ yarn add styled-components

The first component to style with Styled Components is going to be the <Header />. Let’s first download a logo in SVG. We are using the Cube.js logo here as an example, but you can place your product’s logo the same way.

$ cd dashboard-app/src/components && curl http://cube.dev/downloads/logo.svg >
logo.svg

Next, replace the content of the src/components/Header.js with the following.

Yay! We’ve finished another chapter. We have customized global antd variables and updated the design of our navigation bar. Restart the Dashboard App server and test the changes at http://localhost:3000.

I hope it gives you a general idea of how to customize your dashboard. Below you can find links to customization guides of the specific components of the dashboard:

Please let me if you have any questions or feedback about this guide — I’d love to hear from you, thank you!

Further Reading



Source link

Write A Comment