diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.test.tsx b/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.test.tsx
index e3e9872f892a4..e4ecc39cb2d01 100644
--- a/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.test.tsx
+++ b/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.test.tsx
@@ -79,6 +79,30 @@ describe('SideNavLink', () => {
expect(wrapper.find('.enterpriseSearchNavLinks__item--isActive')).toHaveLength(1);
});
+ it("won't set an active class when route is a subroute of 'to'", () => {
+ (useLocation as jest.Mock).mockImplementationOnce(() => ({ pathname: '/documents/1234' }));
+
+ const wrapper = shallow(
+
+ Link
+
+ );
+
+ expect(wrapper.find('.enterpriseSearchNavLinks__item--isActive')).toHaveLength(0);
+ });
+
+ it('sets an active class if the current path is a subRoute of "to", and shouldShowActiveForSubroutes is true', () => {
+ (useLocation as jest.Mock).mockImplementationOnce(() => ({ pathname: '/documents/1234' }));
+
+ const wrapper = shallow(
+
+ Link
+
+ );
+
+ expect(wrapper.find('.enterpriseSearchNavLinks__item--isActive')).toHaveLength(1);
+ });
+
it('passes down custom classes and props', () => {
const wrapper = shallow(
diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.tsx b/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.tsx
index 6c4e1d084c16d..c75a48d5af41d 100644
--- a/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.tsx
+++ b/x-pack/plugins/enterprise_search/public/applications/shared/layout/side_nav.tsx
@@ -63,6 +63,7 @@ export const SideNav: React.FC = ({ product, children }) => {
interface SideNavLinkProps {
to: string;
+ shouldShowActiveForSubroutes?: boolean;
isExternal?: boolean;
className?: string;
isRoot?: boolean;
@@ -70,8 +71,9 @@ interface SideNavLinkProps {
}
export const SideNavLink: React.FC = ({
- isExternal,
to,
+ shouldShowActiveForSubroutes = false,
+ isExternal,
children,
className,
isRoot,
@@ -82,7 +84,10 @@ export const SideNavLink: React.FC = ({
const { pathname } = useLocation();
const currentPath = stripTrailingSlash(pathname);
- const isActive = currentPath === to || (isRoot && currentPath === '');
+ const isActive =
+ currentPath === to ||
+ (shouldShowActiveForSubroutes && currentPath.startsWith(to)) ||
+ (isRoot && currentPath === '');
const classes = classNames('enterpriseSearchNavLinks__item', className, {
'enterpriseSearchNavLinks__item--isActive': !isExternal && isActive, // eslint-disable-line @typescript-eslint/naming-convention