Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant design responsive NavBar

Tags:

I'm using AntD, thought it would be an easy and quick set up for a common and standard Responsive NavBar, but it turned out it's not responsive by default:

squashed

As you can see, it's squashed.

Here is the code:

<Menu                         theme="dark"                         mode="horizontal"                         defaultSelectedKeys={["1"]}                         style={{ lineHeight: '64px' }}                         breakpoint="lg"                         collapsedWidth="0"                     >                                 <Menu.Item key="1">nav 1</Menu.Item>                                 <Menu.Item key="2">nav 2</Menu.Item>                                 <Menu.Item key="3">nav 3</Menu.Item>                                 <Menu.Item key="4"><Icon spin={true} type="plus-circle" className="publish-btn" /></Menu.Item>                                 <Menu.Item key="5"><Icon spin={true} type="login" className="loggin-btn" /></Menu.Item>                     </Menu> 

So I read the document again and thought I would have to use Grid inside the Menu to make it responsive. However, it threw me errors: errors

Here is the code:

<Menu                         theme="dark"                         mode="horizontal"                         defaultSelectedKeys={["1"]}                         style={{ lineHeight: '64px' }}                         breakpoint="lg"                         collapsedWidth="0"                     >                          <Row key="1" gutter={16}>                             <Col span={3} key="1">                                 <Menu.Item key="1">nav 1</Menu.Item>                             </Col>                             <Col span={3} key="2">                                 <Menu.Item key="2">nav 2</Menu.Item>                             </Col>                             <Col span={3} key="3">                                 <Menu.Item key="3">nav 3</Menu.Item>                             </Col>                             <Col span={3} offset={9} key="4">                                 <Menu.Item key="4"><Icon spin={true} type="plus-circle" className="publish-btn" /></Menu.Item>                             </Col>                             <Col span={3} key="5">                                 <Menu.Item key="5"><Icon type="login" className="loggin-btn" /></Menu.Item>                             </Col>                         </Row>                     </Menu> 

Update

Let me clarify myself. I do not want a Sidebar(as in my case it's a small website, not many pages). All I want is to have a top NavBar when re-sizing the Menu.Items can collapse and become to a hamburger icon. Also, I need 2 menu.items on the right side.

like image 684
Franva Avatar asked Feb 22 '18 15:02

Franva


People also ask

Is the navbar from antdesign event responsive?

This was the feeling about Ant Design when I figured out the NavBar from AntDesign isn't event responsive. You are right, Franva and although this particular feature is still missing from Ant Design, there are way too many great features which are still missing in others UI frameworks.

How to create a sidebar with Ant design?

My solution involves splitting this into two components: the SideBar and the NavBar. With Ant Design, a SideBar can be created with a Layout.Sider component that takes in a Menu component. With that, here’s the code for the SideBar component, in SideBar.js : The Sidebar takes in a menu prop, which contains the menu to display the contents with.

How to display the antd icon when the menu collapses?

For the responsive behavior use the "overflowedIndicator" property from antd's Menu component. You can choose any antd icon to be displayed when menu collapses. Control when the menu collapses by using the antd's Grid capabilities (Col xl,lg,md,sm,xs) properties. Please, check the example bellow for a simple responsive NavBar:

What is The navbar based on?

This navbar is based on ant-design-responsive-navbar which is made using Ant Design v3.x Fork the repository. Clone the repository to your machine. Install node_modules. Start the React development server.


2 Answers

I was looking at this question in a decision to pick Ant design for a project. Responsive navbar is common scenario, but I was wondering why there is no such thing in Ant Design? Then I searched for issues in the repo and found the Ant Design Mobile as a comment to an issue.

They have a separate package for mobile devices. Inside Ant Design Mobile there is separate section for web components. In that section you can find a Menu component which is suitable for mobile devices hamburger icon.

Hope this will be helpful for future readers.

like image 155
user158 Avatar answered Oct 03 '22 04:10

user158


I was looking for such functionality not long ago as well and in order to make Ant Menu responsive, I have written a simple React Component.

This Component accepts Ant Menu markup as a prop and conditionally renders the Menu based on the viewport width, either as is (for desktop), or in a Popover component which will wrap passed menu markup (for mobile).

I'm including screenshots of how it may look once the viewport is narrow enough to render just the hamburger icon.

Default Ant Menu Responsive Ant Menu

You can find a Github repo with working example here.

Hope it helps.

like image 28
Tom Avatar answered Oct 03 '22 06:10

Tom