admin管理员组文章数量:1425999
I have a NavLink wrapper ponent. I want to add an .active
style to this ponent when it bees active, but I don't know how it can be done with styled()
. How can this be achieved?
Here is my code:
import { forwardRef } from "react";
import { NavLink as NavLinkBase } from "react-router-dom";
import { styled } from "@mui/system";
const NavLink = forwardRef((props, ref) => {
return (
<NavLinkBase
ref={ref}
{...props}
className={({ isActive }) => [
props.className,
isActive ? 'active' : null,
]
.filter(Boolean)
.join(" ")
}
end={props.to === '/' ? true : false}
/>
)
});
export default NavLink
I have a NavLink wrapper ponent. I want to add an .active
style to this ponent when it bees active, but I don't know how it can be done with styled()
. How can this be achieved?
Here is my code:
import { forwardRef } from "react";
import { NavLink as NavLinkBase } from "react-router-dom";
import { styled } from "@mui/system";
const NavLink = forwardRef((props, ref) => {
return (
<NavLinkBase
ref={ref}
{...props}
className={({ isActive }) => [
props.className,
isActive ? 'active' : null,
]
.filter(Boolean)
.join(" ")
}
end={props.to === '/' ? true : false}
/>
)
});
export default NavLink
Share
Improve this question
edited Oct 21, 2022 at 15:45
Drew Reese
204k18 gold badges246 silver badges274 bronze badges
asked Oct 21, 2022 at 12:06
HaslinHaslin
431 silver badge5 bronze badges
2 Answers
Reset to default 5The NavLink
ponent has an "active"
classname by default when it is active. In the simplest terms you could do the following to define the CSS/styling.
const NavLink = styled(NavLinkBase)(({ theme }) => ({
... default styling ...
"&.active": {
... active styling ...
}
}));
If you are also wanting to define some default props, like the end
prop logic, then create an anonymous function ponent.
const NavLink = styled(props => (
<NavLinkBase {...props} /* set default props here */ />
))(({ theme }) => ({
... default styling ...
"&.active": {
... active styling ...
}
}));
Example:
const NavLink = styled((props) => (
<NavLinkBase {...props} end={props.to === "/"} />
))(({ theme }) => ({
textDecoration: "none",
"&.active": {
color: "green",
fontSize: theme.spacing(3)
}
}));
...
<ul>
<li>
<NavLink to="/">Home</NavLink>
</li>
<li>
<NavLink to="/foo">Foo</NavLink>
</li>
<li>
<NavLink to="/bar">Bar</NavLink>
</li>
</ul>
You can also use AppBar
and Toolbar
to achieve that.
This is how I did that:
import { NavLink } from 'react-router-dom';
import styled from '@emotion/styled';
const ActiveLink = styled(NavLink)({
'&.active': {
color: '#edff00'
}
});
<AppBar position="static">
<Toolbar>
<Button
ponent={ActiveLink}
to="/"
color="inherit"
startIcon={<LoginIcon />}
sx={{ display: { xs: 'none', sm: 'flex' } }}
>
Login
</Button>
<Button
ponent={ActiveLink}
to="/register"
color="inherit"
startIcon={<AppRegistrationIcon />}
sx={{ display: { xs: 'none', sm: 'flex' } }}
>
Register
</Button>
</Toolbar>
</AppBar>
So, I'm using styled
from MUI to style NavLink
, which I then use as a ponent for Button
, thus enabling me to directly define the route (to="") I'll navigate to when clicking on the button.
版权声明:本文标题:javascript - How to apply a style to the active link of a NavLink element using mui styled()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745466132a2659541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论