react-router-dom#useResolvedPath TypeScript Examples
The following examples show how to use
react-router-dom#useResolvedPath.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Items.tsx From backstage with Apache License 2.0 | 5 votes |
WorkaroundNavLink = React.forwardRef<
HTMLAnchorElement,
NavLinkProps
>(function WorkaroundNavLinkWithRef(
{
to,
end,
style,
className,
activeStyle,
caseSensitive,
activeClassName = 'active',
'aria-current': ariaCurrentProp = 'page',
...rest
},
ref,
) {
let { pathname: locationPathname } = useLocation();
let { pathname: toPathname } = useResolvedPath(to);
if (!caseSensitive) {
locationPathname = locationPathname.toLocaleLowerCase('en-US');
toPathname = toPathname.toLocaleLowerCase('en-US');
}
let isActive = locationPathname === toPathname;
if (!isActive && !end) {
// This is the behavior that is different from the original NavLink
isActive = locationPathname.startsWith(`${toPathname}/`);
}
const ariaCurrent = isActive ? ariaCurrentProp : undefined;
return (
<Link
{...rest}
to={to}
ref={ref}
aria-current={ariaCurrent}
style={{ ...style, ...(isActive ? activeStyle : undefined) }}
className={classnames([
className,
isActive ? activeClassName : undefined,
])}
/>
);
})
Example #2
Source File: SidebarSubmenuItem.tsx From backstage with Apache License 2.0 | 4 votes |
SidebarSubmenuItem = (props: SidebarSubmenuItemProps) => {
const { title, to, icon: Icon, dropdownItems } = props;
const classes = useStyles();
const { setIsHoveredOn } = useContext(SidebarItemWithSubmenuContext);
const closeSubmenu = () => {
setIsHoveredOn(false);
};
const toLocation = useResolvedPath(to ?? '');
const currentLocation = useLocation();
let isActive = isLocationMatch(currentLocation, toLocation);
const [showDropDown, setShowDropDown] = useState(false);
const handleClickDropdown = () => {
setShowDropDown(!showDropDown);
};
if (dropdownItems !== undefined) {
dropdownItems.some(item => {
const resolvedPath = resolvePath(item.to);
isActive = isLocationMatch(currentLocation, resolvedPath);
return isActive;
});
return (
<div className={classes.itemContainer}>
<Tooltip title={title} enterDelay={500} enterNextDelay={500}>
<button
onClick={handleClickDropdown}
onTouchStart={e => e.stopPropagation()}
className={classnames(
classes.item,
isActive ? classes.selected : undefined,
)}
>
{Icon && <Icon fontSize="small" />}
<Typography variant="subtitle1" className={classes.label}>
{title}
</Typography>
{showDropDown ? (
<ArrowDropUpIcon className={classes.dropdownArrow} />
) : (
<ArrowDropDownIcon className={classes.dropdownArrow} />
)}
</button>
</Tooltip>
{dropdownItems && showDropDown && (
<div className={classes.dropdown}>
{dropdownItems.map((object, key) => (
<Tooltip
key={key}
title={object.title}
enterDelay={500}
enterNextDelay={500}
>
<Link
to={object.to}
underline="none"
className={classes.dropdownItem}
onClick={closeSubmenu}
onTouchStart={e => e.stopPropagation()}
>
<Typography className={classes.textContent}>
{object.title}
</Typography>
</Link>
</Tooltip>
))}
</div>
)}
</div>
);
}
return (
<div className={classes.itemContainer}>
<Tooltip title={title} enterDelay={500} enterNextDelay={500}>
<Link
to={to!}
underline="none"
className={classnames(
classes.item,
isActive ? classes.selected : undefined,
)}
onClick={closeSubmenu}
onTouchStart={e => e.stopPropagation()}
>
{Icon && <Icon fontSize="small" />}
<Typography variant="subtitle1" className={classes.label}>
{title}
</Typography>
</Link>
</Tooltip>
</div>
);
}