After this lesson you will be able to:
- Explain what a component library is and how it is different from a style library
- Add and configure a component library
- Use and set up a component from the Antd library
In web applications we have worked with styles in two different ways. The first is writing our styles by ourselves from scratch. The second option is opting for a library or framework of styles such as Bootstrap or Bulma, among others. This gives us the ability to focus on the functionality and business logic of our applications. But as our frontend development skills advance, style libraries are being left behind and giving way to UI component libraries.
A component library is a package that contains a series of previously generated React components. Unlike style libraries, which add styles through HTML classes and functionality through jQuery, component libraries are elements with everything they need to work by themselves. UI components contain all the markup (HTML/JSX), styles (CSS) and logic (JS) that they need to look good and work well.
// Styles library/framework code
<button class='btn btn-primary'>Send</button>
// Component library code
<Button primary>Send</Button>
When style libraries needed additional visual properties, such as button types, column sizes, or other properties, we added extra classes. Now React introduces a better way to handle styles based on the props of the components. By doing so, the code is greatly reduced and the markup looks much cleaner.
Antd is a React UI component library based on an elegant and reliable design system, with a great amount of styled and functional components.
In your React project:
$ npm install antd
Import the antd.css
in the index.js
file:
import 'antd/dist/antd.css'
Now you can start using UI components from the Antd library.
import { Button, Card } from "antd";
const { Meta } = Card;
const Main = () => (
<main>
<h1>React UI Components</h1>
<Button type='primary'>Components List</Button>
<Card
hoverable
style={{ width: 240 }}
cover={<img alt="example" src="https://os.alipayobjects.com/rmsportal/QBnOOoLaAfKPirc.png" />}
>
<Meta
title="Europe Street beat"
description="www.instagram.com"
/>
</Card>
</main>
);
The result should look like this:
The Antd grid system defines the frame outside the information area based on row and column to ensure that every area can have a stable arrangement.
It uses a 24-grid layout to define the width of each "box", but does not rigidly adhere to the grid layout.
import {Button, Row, Col} from 'antd'
const Main = () => (
<main>
<Row>
<Col span={12} style={{background: 'red'}}>Col</Col>
<Col span={12} style={{background: 'blue'}}>Col</Col>
</Row>
</main>
)
You can use the gutter property of Row as grid spacing.
import {Button, Row, Col} from 'antd'
const Main = () => (
<main>
<Row gutter={16}>
<Col className="gutter-row" span={12}>
<div
className="gutter-box"
style={{background: 'red'}}
>
col-6
</div>
</Col>
<Col className="gutter-row" span={12}>
<div
className="gutter-box"
style={{background: 'blue'}}
>
col-6
</div>
</Col>
</Row>
</main>
)
Handling the overall layout of a page.
import { Layout, Menu } from "antd";
const {
Header, Footer, Sider, Content,
} = Layout;
const Main = () => (
<main>
<Layout>
<Header>
<Menu
theme="dark"
mode="horizontal"
defaultSelectedKeys={['1']}
style={{ lineHeight: '64px' }}
>
<Menu.Item key="1">nav 1</Menu.Item>
<Menu.Item key="2">nav 2</Menu.Item>
<Menu.Item key="3">nav 3</Menu.Item>
</Menu>
</Header>
<Content>
App Content
</Content>
<Footer>Footer</Footer>
</Layout>
</main>
);
There are a lot of Layouts already created at the Antd layout page
In this lesson, you learned how to distinguish between a styles library and a component library, and how a library of UI components works. You also learned about Antd and how to install it. It is a library with many incredible component: we saw just a few of them, but the principle for using the others is the same. Try to integrate more Antd components into your next exercises.