Below is an example of how you can build a toggled sidebar using React and Chakra UI. In this example, the sidebar width toggles between 5% (collapsed) and 15% (expanded), and the text labels next to icons are only visible when expanded.
Step-by-Step Implementation
-
Install Dependencies
Make sure you have Chakra UI and React Icons installed in your project:
npm install @chakra-ui/react @emotion/react @emotion/styled framer-motion react-icons
-
Create the Sidebar Component
Create a file (e.g., Sidebar.jsx) and add the following code:
import React, { useState } from 'react';
import {
Box,
Flex,
IconButton,
Text,
} from '@chakra-ui/react';
import { ChevronLeftIcon } from '@chakra-ui/icons';
import { FaHome, FaUser, FaCog } from 'react-icons/fa';
const Sidebar = () => {
// State to toggle sidebar collapsed/expanded
const [collapsed, setCollapsed] = useState(true);
// Toggle handler
const toggleSidebar = () => setCollapsed(!collapsed);
// Dynamically set the sidebar width based on collapsed state
const sidebarWidth = collapsed ? '5%' : '15%';
// When collapsed, we center the icons. When expanded, align left.
const alignItems = collapsed ? 'center' : 'flex-start';
return (
<Box
bg="gray.700"
color="white"
w={sidebarWidth}
h="100vh"
transition="width 0.3s ease"
>
<Flex direction="column" align={alignItems} p={4}>
{/* Sidebar toggle button */}
<IconButton
aria-label="Toggle sidebar"
icon={
<ChevronLeftIcon
transform={collapsed ? 'rotate(180deg)' : 'rotate(0deg)'}
transition="transform 0.3s ease"
/>
}
onClick={toggleSidebar}
mb={4}
variant="ghost"
_hover={{ bg: 'gray.600' }}
/>
{/* Sidebar items */}
<Flex
align="center"
mb={4}
p={2}
w="full"
cursor="pointer"
_hover={{ bg: 'gray.600' }}
>
<FaHome />
{/* Only display text if sidebar is expanded */}
{!collapsed && <Text ml={3}>Home</Text>}
</Flex>
<Flex
align="center"
mb={4}
p={2}
w="full"
cursor="pointer"
_hover={{ bg: 'gray.600' }}
>
<FaUser />
{!collapsed && <Text ml={3}>Profile</Text>}
</Flex>
<Flex
align="center"
mb={4}
p={2}
w="full"
cursor="pointer"
_hover={{ bg: 'gray.600' }}
>
<FaCog />
{!collapsed && <Text ml={3}>Settings</Text>}
</Flex>
</Flex>
</Box>
);
};
export default Sidebar;
Explanation:
-
State Management:
The collapsed boolean state manages whether the sidebar is collapsed (true) or expanded (false).
-
Dynamic Width:
The width of the sidebar is set dynamically via Chakra UI’s w prop using the variable sidebarWidth.
-
Conditional Rendering:
The text next to each icon is conditionally rendered using {!collapsed && <Text ml={3}>Item</Text>}. This ensures that when the sidebar is collapsed, only the icons are visible.
-
Transitions:
The transition CSS property provides a smooth animation when toggling the sidebar's width.
-
Using the Sidebar in Your App
Finally, import and use the Sidebar component in your main application file (e.g., App.jsx):
import React from 'react';
import { ChakraProvider } from '@chakra-ui/react';
import Sidebar from './Sidebar';
function App() {
return (
<ChakraProvider>
<Sidebar />
{/* Your other components */}
</ChakraProvider>
);
}
export default App;
Result
When you run your project, you will have a sidebar that toggles its width between 5% (showing only icons) and 15% (showing both icons and text) each time you click the toggle button.
This approach is easily customizable, and you can extend it by adding more sidebar items, additional styling, or even more interactive behaviors. Enjoy building your React application with Chakra UI!