On our journey through the cosmos of frontend technologies, component frameworks are like advanced space stations - ready, comprehensive structures that we can populate with our own logic and customize to our needs. In this module, we will explore three popular component frameworks: shadcn/ui, Material-UI, and Chakra UI, which let you create consistent, beautiful, and functional user interfaces.
Imagine you need to build an advanced spaceship. You can either construct every part from scratch, or use ready-made, tested, and optimized components. Component frameworks give you that second option, offering:
shadcn/ui is a unique approach to component libraries. Unlike traditional frameworks, shadcn/ui is not a library you install as a dependency. Instead, it is a collection of components that you copy directly into your project. This gives you full control over the code and the ability to modify components without restrictions.
To start your adventure with shadcn/ui, follow these steps:
1# Initialize shadcn/ui in the project
2npx shadcn-ui@latest initDuring initialization, you will answer a few questions about configuration (styles, colors, etc.). Then you can add selected components:
1# Adding the button component
2npx shadcn-ui@latest add button
3
4# Adding other components
5npx shadcn-ui@latest add card
6npx shadcn-ui@latest add dialog1import { Button } from "@/components/ui/button";
2import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
3
4function MissionControl() {
5 return (
6 <Card>
7 <CardHeader>
8 <CardTitle>Mission Control Panel</CardTitle>
9 <CardDescription>Manage your cosmic expedition</CardDescription>
10 </CardHeader>
11 <CardContent>
12 <p>System status: All systems are operating correctly</p>
13 </CardContent>
14 <CardFooter>
15 <Button variant="outline" className="mr-2">Cancel</Button>
16 <Button>Start mission</Button>
17 </CardFooter>
18 </Card>
19 );
20}Material-UI (MUI) is a popular React component library implementing the Material Design principles created by Google. It is like building a spaceship according to proven NASA plans - you get an interface that is intuitive, consistent, and familiar to users worldwide.
1# Installing basic MUI packages
2npm install @mui/material @emotion/react @emotion/styled
3
4# Optionally, for icons
5npm install @mui/icons-material1import { Button, Card, CardActions, CardContent, Typography } from '@mui/material';
2import RocketLaunchIcon from '@mui/icons-material/RocketLaunch';
3
4function SpaceMission() {
5 return (
6 <Card sx={{ maxWidth: 345, m: 2 }}>
7 <CardContent>
8 <Typography gutterBottom variant="h5" component="div">
9 Mission to Mars
10 </Typography>
11 <Typography variant="body2" color="text.secondary">
12 Prepare for a cosmic adventure. The mission to Mars requires courage,
13 knowledge, and readiness for the unknown.
14 </Typography>
15 </CardContent>
16 <CardActions>
17 <Button size="small" color="primary">Details</Button>
18 <Button
19 size="small"
20 variant="contained"
21 color="primary"
22 startIcon={<RocketLaunchIcon />}
23 >
24 Start
25 </Button>
26 </CardActions>
27 </Card>
28 );
29}Material-UI offers an advanced theming system that lets you customize colors, typography, border radius, and many other aspects:
1import { createTheme, ThemeProvider } from '@mui/material/styles';
2
3// Creating a custom theme
4const theme = createTheme({
5 palette: {
6 primary: {
7 main: '#2196f3', // cosmic blue
8 },
9 secondary: {
10 main: '#f50057', // stellar pink
11 },
12 },
13 typography: {
14 fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
15 h1: {
16 fontSize: '2.5rem',
17 fontWeight: 500,
18 },
19 },
20 shape: {
21 borderRadius: 8,
22 },
23});
24
25// Applying the theme
26function App() {
27 return (
28 <ThemeProvider theme={theme}>
29 {/* Place your components here */}
30 </ThemeProvider>
31 );
32}Chakra UI is a modern component library that prioritizes simplicity, modularity, and accessibility. It is like building a lightweight, agile space vehicle that is simultaneously safe and easy to operate for every crew member, regardless of their abilities.
1# Installing Chakra UI
2npm i @chakra-ui/react @emotion/react @emotion/styled framer-motion1// In the main application file
2import { ChakraProvider } from '@chakra-ui/react';
3
4function App() {
5 return (
6 <ChakraProvider>
7 {/* Place your components here */}
8 </ChakraProvider>
9 );
10}1import { Box, Button, Heading, Stack, Text, useColorModeValue } from '@chakra-ui/react';
2
3function SpaceStation() {
4 // Dynamic colors depending on mode (light/dark)
5 const bgColor = useColorModeValue('blue.50', 'blue.900');
6 const textColor = useColorModeValue('gray.800', 'white');
7
8 return (
9 <Box
10 p={5}
11 shadow="md"
12 borderWidth="1px"
13 borderRadius="md"
14 bg={bgColor}
15 color={textColor}
16 >
17 <Heading size="md" mb={2}>International Space Station</Heading>
18 <Text mb={4}>
19 The orbital home of astronauts and scientists from around the world.
20 </Text>
21 <Stack direction="row" spacing={4}>
22 <Button colorScheme="blue" variant="outline">
23 Information
24 </Button>
25 <Button colorScheme="blue">
26 Visit
27 </Button>
28 </Stack>
29 </Box>
30 );
31}Chakra UI offers an intuitive way to define responsive styles:
1<Stack
2 direction={["column", "row"]} // column on small screens, row on larger
3 spacing={[2, 4, 6]} // different spacing for different breakpoints
4>
5 <Box
6 p={[2, 4, 6]} // padding growing with screen size
7 width={["100%", "50%", "33%"]} // width depending on screen size
8 bg="blue.500"
9 >
10 Panel 1
11 </Box>
12 <Box
13 p={[2, 4, 6]}
14 width={["100%", "50%", "33%"]}
15 bg="green.500"
16 >
17 Panel 2
18 </Box>
19 <Box
20 p={[2, 4, 6]}
21 width={["100%", "50%", "33%"]}
22 bg="purple.500"
23 display={["none", "block"]} // hidden on smallest screens
24 >
25 Panel 3
26 </Box>
27</Stack>Ideal for:
Potential drawbacks:
Ideal for:
Potential drawbacks:
Ideal for:
Potential drawbacks:
All discussed frameworks can be combined with other technologies:
Choose one of the discussed frameworks (shadcn/ui, Material-UI, or Chakra UI) and create a space mission control dashboard containing:
Remember about:
This exercise will help you understand how component frameworks can accelerate building complex interfaces and help you decide which one best suits your work style and project needs.