Ant Design Sidebar Menu Items Issue

161 views Asked by At

app screenshot

in this image you can see the logout items showing in screen bottom. how can i do this here using ant design.

my sidebar layout code:

 <Layout>
        <Sider
          width="220px"
          trigger={null}
          collapsible
          collapsed={collapsed}
          style={{
            backgroundColor: "#2492EB",
            height: "100vh",

            zIndex: 2,
            overflow: "auto",
            position: "fixed",
          }}
        >
          <div className="demo-logo-vertical" />
          <div
            style={{
              backgroundColor: "white",
              padding: "14px 0",
            }}
          >
            <img src={logo} alt="" />
          </div>
          <Menu
            style={{
              backgroundColor: "#2492EB",
              color: "white",
              marginTop: "10px",
            }}
            selectedKeys={[selectedKey ? selectedKey : "/dashboard"]}
            // defaultSelectedKeys={[sidebarItems[0].key]}
            items={sidebarItems}
            onClick={handleMenuSelect}
          ></Menu>
        </Sider>

        <Layout style={{ marginLeft: collapsed ? 80 : 180 }}>
          <Header
            style={{
              padding: 0,
              height: "100px",
              background: colorBgContainer,
              display: "flex", // Added display: flex
              alignItems: "center", // Added align-items: center
            }}
          >
            <Button
              type="text"
              icon={collapsed ? <MenuOutlined /> : <MenuOutlined />}
              onClick={() => setCollapsed(!collapsed)}
              style={{
                marginLeft: collapsed ? "20px" : "60px",
                fontSize: "16px",
                width: 45,
                height: 45,
                marginRight: "10px",
              }}
            />
            <div className="flex items-center justify-between  container mx-auto">
              <div className="flex ">
                <ConfigProvider
                  theme={{
                    components: {
                      Input: {
                        colorBgContainer: "rgb(244, 244, 244)",
                      },
                    },
                  }}
                >
                  <Input
                    allowClear={true}
                    prefix={<SearchOutlined className="text-[#A7A7A7] " />}
                    placeholder="search here"
                    className="h-[50px] w-[461px] border-0"
                  />
                </ConfigProvider>
              </div>
              <div className="flex items-center gap-x-6">
                <Select
                  options={options}
                  defaultValue={options[0]}
                  value={selectedLanguage}
                  style={{ width: 150 }}
                  onChange={handleSelectLanguage}
                ></Select>

                <Link to="/notification " className="flex items-center">
                  <Badge count={5} className="cursor-pointer">
                    <IoIosNotificationsOutline
                      style={{ width: "30px", height: "30px" }}
                    />
                  </Badge>
                </Link>
                <div className="flex items-center gap-x-2">
                  <img
                    src="https://t.ly/18Nvk"
                    className="w-[40px] h-[40px] object-cover rounded-full"
                    alt=""
                  />

                  <div className="my-[2px]">
                    <h1 className="font-semibold">Mr. Admin John Doe</h1>
                  </div>
                </div>
              </div>
            </div>
          </Header>
          <Content
            style={{
              margin: "0 16px",
              padding: 24,
              backgroundColor: "#F6F8FA",
              borderRadius: borderRadiusLG,
            }}
          >
            <Outlet />
          </Content>
        </Layout>
      </Layout>

my sidebar items array of object:

export const sidebarItems = [
  {
    key: "/dashboard",
    label: "Dashboard",
    icon: <MdOutlineDashboard />,
  },
  {
    key: "/students",
    label: "Students",
    icon: <GoPeople />,
  },
  {
    key: "/student/dashboard",
    label: "StudentDashboard",
    icon: <GoPeople />,
  },
  {
    key: `/profile/${id}`,
    label: "Profile",
    icon: <GoPeople />,
  },
  {
    key: "/courses",
    label: "Courses",
    icon: <BsBook />,
  },
  {
    key: "/mentors",
    label: "Mentors",
    icon: <IoPersonCircleOutline />,
  },
  {
    key: "/MentorsDashboard",
    label: "MentorsDashboard",
    icon: <IoPersonCircleOutline />,
  },
  {
    key: "/departments",
    label: "Departments",
    icon: <LuFolders />,
  },
  {
    key: "/attendence",
    label: "Attendence",
    icon: <PiIdentificationCard />,
  },
  {
    key: "/events",
    label: "Events",
    icon: <BsCalendar2Event />,
  },
  {
    key: "/class-schedule",
    label: "Class Schedule",
    icon: <LuClipboardList />,
  },
  {
    key: "/wallet",
    label: "Wallet",
    icon: <HiOutlineCreditCard />,
  },
  {
    key: "/setting",
    label: "setting",
    icon: <IoSettingsOutline />,
  },

  {
    key: "/logout",
    label: "Log Out",
    icon: <LoginOutlined />,
  },
];

i need to show the logout menu in screen bottom.i already show it on photo. my code working like this: enter image description here i need like this: enter image description here

1

There are 1 answers

1
Muhammad Nouman Rafique On

They may be multiple solutions to achieve the desired current. I use flex, an extra menu item (divider item with set visibility to hidden). If there's enough space between top items and logout item, divider takes the remaining space. Here's the complete code.

import { Badge, Button, ConfigProvider, Input, Layout, Menu, MenuProps, Select } from 'antd';
import {
    BookFilled,
    CalendarFilled,
    CreditCardOutlined,
    DashOutlined,
    FolderAddFilled,
    LoginOutlined,
    NotificationFilled,
    PaperClipOutlined,
    SearchOutlined,
    SettingFilled,
    UserOutlined,
    UsergroupAddOutlined,
    WalletFilled
} from '@ant-design/icons';

const { Header, Content, Sider } = Layout;

const sidebarItems: MenuProps['items'] = [
    { key: '/dashboard', label: 'Dashboard', icon: <DashOutlined /> },
    { key: '/students', label: 'Students', icon: <UserOutlined /> },
    { key: '/student/dashboard', label: 'StudentDashboard', icon: <UsergroupAddOutlined /> },
    { key: `/profile/1`, label: 'Profile', icon: <UserOutlined /> },
    { key: '/courses', label: 'Courses', icon: <BookFilled /> },
    { key: '/mentors', label: 'Mentors', icon: <UserOutlined /> },
    { key: '/MentorsDashboard', label: 'MentorsDashboard', icon: <UsergroupAddOutlined /> },
    { key: '/departments', label: 'Departments', icon: <FolderAddFilled /> },
    { key: '/attendence', label: 'Attendence', icon: <CreditCardOutlined /> },
    { key: '/events', label: 'Events', icon: <CalendarFilled /> },
    { key: '/class-schedule', label: 'Class Schedule', icon: <PaperClipOutlined /> },
    { key: '/wallet', label: 'Wallet', icon: <WalletFilled /> },
    { key: '/setting', label: 'setting', icon: <SettingFilled /> },
    { type: 'divider', style: { margin: 0, border: 0, flexGrow: 1, visibility: 'hidden' } },
    { key: '/logout', label: 'Log Out', icon: <LoginOutlined /> }
];

const App = () => {
    return (
        <Layout>
            <Sider
                width='220px'
                trigger={null}
                collapsible
                style={{ backgroundColor: '#2492EB', height: '100vh', zIndex: 2, overflow: 'auto', position: 'fixed' }}
            >
                <div style={{ display: 'flex', flexDirection: 'column', height: 'inherit' }}>
                    <div className='demo-logo-vertical' />
                    <div style={{ backgroundColor: 'white', padding: '14px 0' }}>
                        <img src='https://t.ly/18Nvk' alt='' style={{ width: '2rem', height: '2rem', borderRadius: '50%', marginLeft: '80px' }} />
                    </div>
                    <Menu
                        items={sidebarItems}
                        style={{
                            backgroundColor: '#2492EB',
                            color: 'white',
                            marginTop: '10px',
                            flexGrow: 1,
                            display: 'flex',
                            flexDirection: 'column',
                            paddingBlockEnd: '1rem'
                        }}
                    />
                </div>
            </Sider>

            <Layout>
                <Header style={{ padding: 0, height: '100px', display: 'flex', alignItems: 'center' }}>
                    <Button type='text' style={{ fontSize: '16px', width: 45, height: 45, marginRight: '10px' }} />
                    <div className='flex items-center justify-between  container mx-auto'>
                        <div className='flex '>
                            <ConfigProvider theme={{ components: { Input: { colorBgContainer: 'rgb(244, 244, 244)' } } }}>
                                <Input
                                    allowClear={true}
                                    prefix={<SearchOutlined className='text-[#A7A7A7] ' />}
                                    placeholder='search here'
                                    className='h-[50px] w-[461px] border-0'
                                />
                            </ConfigProvider>
                        </div>
                        <div className='flex items-center gap-x-6'>
                            <Select />
                            <a className='flex items-center'>
                                <Badge count={5} className='cursor-pointer'>
                                    <NotificationFilled style={{ width: '30px', height: '30px' }} />
                                </Badge>
                            </a>
                            <div className='flex items-center gap-x-2'>
                                <img src='https://t.ly/18Nvk' className='w-[40px] h-[40px] object-cover rounded-full' alt='' />

                                <div className='my-[2px]'>
                                    <h1 className='font-semibold'>Mr. Admin John Doe</h1>
                                </div>
                            </div>
                        </div>
                    </div>
                </Header>
                <Content style={{ margin: '0 16px', padding: 24, backgroundColor: '#F6F8FA' }}></Content>
            </Layout>
        </Layout>
    );
};

export default App;