Open In App

Chakra UI Pseudo

Chakra UI, provides a way to style components dynamically using pseudo-classes. pseudo-classes are special styles that you can apply to elements based on their current state or where they are in the component structure. For example, you might want a button to change its color when a user hovers over it. Chakra UI makes this super easy with pseudo-classes. This way, you can add a bit of interactivity and visual appeal to your website without writing a bunch of complex code.

Prerequisites

Approach:

Steps To Create React Application and Installing Module:

Step 1: Create a React application using the following command:



npx create-react-app newapp

Step 2: After creating your project folder(i.e. newapp), move to it by using the following command:

cd newapp

Step 3: After creating the React application, Install the Chakra UI package using the following command:



npm i @chakra-ui/react @emotion/react@^11 @emotion/styled@^11 framer-motion@^6

Project Structure:

The updated dependencies in the package.json file.

"dependencies": {
"@chakra-ui/react": "^2.8.2",
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"framer-motion": "^6.5.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Example: In this example, Chakra UI Components, including ChakraProvider, were used to wrap components for styling.




import {
    ChakraProvider, Box,
    Heading, Text, Button
}
    from '@chakra-ui/react';
import React from 'react';
import './App.css';
 
const App = () => {
 
    return (
        <ChakraProvider>
            <Box
                textAlign="center"
                p={4}
                // Apply styles on hover
                _hover={{ backgroundColor: 'gray.200' }}
                // Apply styles on click
                _active={{ backgroundColor: 'teal.200' }}
            >
                <Heading>Chakra UI Pseudo Example</Heading>
                <Text mt={4} _before={{ content: '"????"' }}>
                    Welcome to Chakra UI Pseudo Code!
                </Text>
                <Button
                    colorScheme="teal"
                    mt={4}
                    // Apply styles when focused
                    _focus={{ boxShadow: 'outline' }}
                >
                    Click me
                </Button>
            </Box>
        </ChakraProvider>
    );
};
 
export default App;

Step to Run the application:

npm start

Output:


Article Tags :