skip to Main Content

I’m trying to implement a page with fixed navbar and collapsible fixed sidebar, using React and chakra-ui component. Below are the example code.

import { Box } from "@chakra-ui/react";

export const App = () => {
  return (
    <Box display="flex">
      <Box position="fixed" width="200px" height="100%" bg="red" /> {/* Sidebar */}
      <Box flex="1" position="fixed">
        <Box position="fixed" top="0" width="100%" height="50px" bg="blue" /> {/* Navbar */}
        <Box mt="50px" p="4">
          {/* Rest content */}
        </Box>
      </Box>
    </Box>
  );
};

But now the width of the top navbar never meets my expectations. This is its current output.

current output

I want to achieve the following output.

expected

The red box is the sidebar and the blue box is the top navbar. Both of them are expected to be fixed on their current position. Please offer some advice on how to achieve my expectation. I will appreciate it a lot!

2

Answers


  1. you can try:

    <Box position="fixed" top="0" right="0" width="calc(100vh - 200px)" height="50px" bg="blue" /> {/* Navbar */}
    

    notice that 200px is the sidebar width

    Login or Signup to reply.
  2. You can use this layout component. it have header and collapsible sider.

    also you can checkout more with chakra-ui at chakra-sidebar-layout-components

    const Layout = () => {
        const sidebar = useDisclosure();
        const integrations = useDisclosure();
        const color = useColorModeValue("gray.600", "gray.300");
      
        const NavItem = (props) => {
          const { icon, children, ...rest } = props;
          return (
            <Flex
              align="center"
              px="4"
              pl="4"
              py="3"
              cursor="pointer"
              color="inherit"
              _dark={{
                color: "gray.400",
              }}
              _hover={{
                bg: "gray.100",
                _dark: {
                  bg: "gray.900",
                },
                color: "gray.900",
              }}
              role="group"
              fontWeight="semibold"
              transition=".15s ease"
              {...rest}
            >
              {icon && (
                <Icon
                  mx="2"
                  boxSize="4"
                  _groupHover={{
                    color: color,
                  }}
                  as={icon}
                />
              )}
              {children}
            </Flex>
          );
        };
      
        const SidebarContent = (props) => (
          <Box
            as="nav"
            pos="fixed"
            top="0"
            left="0"
            zIndex="sticky"
            h="full"
            pb="10"
            overflowX="hidden"
            overflowY="auto"
            bg="white"
            _dark={{
              bg: "gray.800",
            }}
            border
            color="inherit"
            borderRightWidth="1px"
            w="60"
            {...props}
          >
            <Flex px="4" py="5" align="center">
              <Logo />
              <Text
                fontSize="2xl"
                ml="2"
                color="brand.500"
                _dark={{
                  color: "white",
                }}
                fontWeight="semibold"
              >
                Choc UI
              </Text>
            </Flex>
            <Flex
              direction="column"
              as="nav"
              fontSize="sm"
              color="gray.600"
              aria-label="Main Navigation"
            >
              <NavItem icon={MdHome}>Home</NavItem>
              <NavItem icon={FaRss}>Articles</NavItem>
              <NavItem icon={HiCollection}>Collections</NavItem>
              <NavItem icon={FaClipboardCheck}>Checklists</NavItem>
              <NavItem icon={HiCode} onClick={integrations.onToggle}>
                Integrations
                <Icon
                  as={MdKeyboardArrowRight}
                  ml="auto"
                  transform={integrations.isOpen && "rotate(90deg)"}
                />
              </NavItem>
              <Collapse in={integrations.isOpen}>
                <NavItem pl="12" py="2">
                  Shopify
                </NavItem>
                <NavItem pl="12" py="2">
                  Slack
                </NavItem>
                <NavItem pl="12" py="2">
                  Zapier
                </NavItem>
              </Collapse>
              <NavItem icon={AiFillGift}>Changelog</NavItem>
              <NavItem icon={BsGearFill}>Settings</NavItem>
            </Flex>
          </Box>
        );
      
        return (
          <Box
            as="section"
            bg="gray.50"
            _dark={{
              bg: "gray.700",
            }}
            minH="100vh"
          >
            <SidebarContent
              display={{
                base: "none",
                md: "unset",
              }}
            />
            <Drawer
              isOpen={sidebar.isOpen}
              onClose={sidebar.onClose}
              placement="left"
            >
              <DrawerOverlay />
              <DrawerContent>
                <SidebarContent w="full" borderRight="none" />
              </DrawerContent>
            </Drawer>
            <Box
              ml={{
                base: 0,
                md: 60,
              }}
              transition=".3s ease"
            >
              <Flex
                as="header"
                align="center"
                justify="space-between"
                w="full"
                px="4"
                bg="white"
                _dark={{
                  bg: "gray.800",
                }}
                borderBottomWidth="1px"
                color="inherit"
                h="14"
              >
                <IconButton
                  aria-label="Menu"
                  display={{
                    base: "inline-flex",
                    md: "none",
                  }}
                  onClick={sidebar.onOpen}
                  icon={<FiMenu />}
                  size="sm"
                />
                <InputGroup
                  w="96"
                  display={{
                    base: "none",
                    md: "flex",
                  }}
                >
                  <InputLeftElement color="gray.500">
                    <FiSearch />
                  </InputLeftElement>
                  <Input placeholder="Search for articles..." />
                </InputGroup>
      
                <Flex align="center">
                  <Icon color="gray.500" as={FaBell} cursor="pointer" />
                  <Avatar
                    ml="4"
                    size="sm"
                    name="anubra266"
                    src="https://avatars.githubusercontent.com/u/30869823?v=4"
                    cursor="pointer"
                  />
                </Flex>
              </Flex>
      
              <Box as="main" p="4">
                {/* Add content here, remove div below  */}
                <Box borderWidth="4px" borderStyle="dashed" rounded="md" h="96" />
              </Box>
            </Box>
          </Box>
        );
      };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search