I have a sidebar component in my admin panel which has multiple links. Some of these links are in the dropdown menu. Now I want to add an active class based on an active link or if the active link is in the dropdown menu then to the dropdown toggle. Below is my code for a sidebar. Btw I am using antd framework for ui.
<Sider
width={260}
className={styles.sider}
collapsible
trigger={null}
collapsed={drawer}
breakpoint={"md"}
collapsedWidth={0}
>
<Menu mode="inline">
<Menu.Item
icon={<IoGrid />}
key="1"
onClick={(e) => {
toggleKey(e);
gotoPage("/");
}}
className={selectedKey == 1 ? styles.active_item : ""}
>
Dashboard
</Menu.Item>
<SubMenu
title="Settings"
key="2"
icon={<IoSettingsOutline />}
className={selectedKey == 2 ? styles.active_item : ""}
>
<Menu.Item
icon={<IoPersonAddOutline />}
key="2.1"
onClick={(e) => {
toggleKey(e);
gotoPage("/settings/profile");
}}
>
Profile
</Menu.Item>
</SubMenu>
</Menu>
</Sider>
Btw the selectedKey and toggleKey function comes from top level component and gotoPage is just router.push functionality.
Thanks.
As per this answer you should use useRouter hook to get current path.
For your use case instead of equality check you should probably use startsWith or maybe some other check. So it would look like this
Let's say you have
Settings -> /settings
Profile -> /settings/profile
Notifications /settings/notifications
You check if current path starts /settings and you assume that Settings group is active and you simply expand Settings submenu and add active class to Settings group.
If you are currently on /settings/notification this would mean that you can identify following:
active group
active dropdown
active sublink
I think it is better to have custom LinkGroup component that would check for its 'activeness' and would add specific styling and show/hide its submenu.
Related
I'm using ant design accordian for colapse and expand panel , In that instead of up and down arrow I want + and - icon to be display. How can I achieve that
I'm using this one
https://ant.design/components/collapse/
Please help me on this
Thanks
You can use expandIcon prop
https://codepen.io/pen/?&editors=001&prefill_data_id=ab218015-02c4-4c67-9230-2aa590cf5ff3
<Collapse
{...others}
expandIcon={({ isActive }) => isActive ? <IconYouWant /> : <IconYouWant />}
>
{children}
</Collapse>
I have drawer buttons here and I want to change Icon colors when a user clicks to each button and stays there. Like, if users click 'Dashboard' and are staying in that components, the dashboard button color will be white so that the buttons are indicating where users are in the website
const drawer = (
<div>
<Button
variant='contained'
color='secondary'
onClick={handleToDashboard}
className={classes.customButton}
startIcon={<DashboardIcon style={{ color: 'black' }} />}
>
Dashboard
</Button>
<Button
variant='contained'
color='secondary'
onClick={handleToTest}
className={classes.customButton}
startIcon={<MapIcon />}
>
Map
</Button>
<Divider />
</div>
);
If this component is self-contained – that is, it doesn't load a new route whenever a user clicks a button, and instead remains on the existing page – you should maintain state (using setState if this is a class component or the useState hook for a functional component) that keeps track of whichever button was last pressed. You can update the state in your onClick callbacks and then make your color props conditional – for example:
<Button
variant='contained'
color={this.state.opened === 'map' ? 'white' : 'secondary'}
onClick={handleToTest}
className={classes.customButton}
startIcon={<MapIcon />}
>
If this is part of a navigation bar that handles routing between pages, then you should pass as a prop from whatever component is being displayed the value in the drawer that should be highlighted. So for instance, the Dashboard component could, if it has this drawer as a child, pass a prop telling the drawer that it should render the dashboard button color white.
I'm trying to create a left sidebar only for desktop (min width 1200px), in other case (tablet, mobile), needs to hide the sidebar. I'm using Sidebar Pushable from Semantic UI React.
I tried this demo to this but doesn't works:
<Sidebar.Pushable as={Segment}>
<Sidebar
as={Menu}
animation='push'
icon='labeled'
inverted
vertical
visible={window.innerWidth >= 1200 ? true : false}
width='thin'
>
<Menu.Item as='a'>
<Icon name='home' />
Home
</Menu.Item>
<Menu.Item as='a'>
<Sidebar.Pusher>
<Segment basic>
//CONTENT
</Segment>
</Sidebar.Pusher>
</Sidebar.Pushable>
Someone else had the same problem? Many Thanks!
Armando
the issue is not with render load times. The issue is that react does not re-render a component unless the state of that component changes. Since you had tied the visible prop with window.innerWidth directly, for that component the state was not changing and hence was not re-rendering. Checkthis link with working code sandbox
https://codesandbox.io/s/semantic-ui-example-xqhjl?file=/example.js
I'm using the latest versions of react and semantic-ui-react
I have this Iconelement set up from semantic implementing navigation to an external link (github)
<Menu.Item
position="right"
to="https://github.com"
as={Link}
>
<Icon name="github" size="big" />
</Menu.Item>
When I click the icon in my UI, it's trying to append the github url to the url of my UI. So I'm at localhost:blah/someUrl it's requesting localhost:blah/someUrl/https://github.com
Is this an issue with routing or misuse of the <Icon> props?
It's rather a misuse of router link: If you want to link to external pages, don't use routers <Link> component.
It will work the following way:
<Menu.Item
href="https://github.com"
position="right"
target="_blank"
>
<Icon name="github" size="big" />
</Menu.Item>
I've stacked with some strange issue. When I create a menu block with a button inside of it, I'm expecting to get a "Regular" button, but getting, kind of, Menu Item View.
Instead of
<Menu inverted>
<Menu.Menu>
<Menu.Item header>
<Image className="logo" src={logoImg} avatar />
</Menu.Item>
</Menu.Menu>
<Menu.Menu position={'right'}>
<Menu.Item>
<Button positive>Sign up</Button>
</Menu.Item>
</Menu.Menu>
</Menu>
What could be wrong?
Your code working fine. Here is the working codepen. Issue may be some other custom styles will override your component Button styles.