Skip to content

Commit

Permalink
fix(react): uishell sidenavmenu active descendants (#15027)
Browse files Browse the repository at this point in the history
  • Loading branch information
cordesmj authored Nov 2, 2023
1 parent 541bae9 commit 72c2a05
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/react/src/components/UIShell/SideNavMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const SideNavMenu = React.forwardRef<HTMLElement, SideNavMenuProps>(
const className = cx({
[`${prefix}--side-nav__item`]: true,
[`${prefix}--side-nav__item--active`]:
isActive || (hasActiveChild(children) && !isExpanded),
isActive || (hasActiveDescendant(children) && !isExpanded),
[`${prefix}--side-nav__item--icon`]: IconElement,
[`${prefix}--side-nav__item--large`]: large,
[customClassName as string]: !!customClassName,
Expand Down Expand Up @@ -200,7 +200,7 @@ SideNavMenu.propTypes = {
Defining the children parameter with the type ReactNode | ReactNode[]. This allows for various possibilities:
a single element, an array of elements, or null or undefined.
**/
function hasActiveChild(children: ReactNode | ReactNode[]): boolean {
function hasActiveDescendant(children: ReactNode | ReactNode[]): boolean {
if (Array.isArray(children)) {
return children.some((child) => {
if (!React.isValidElement(child)) {
Expand All @@ -213,9 +213,14 @@ function hasActiveChild(children: ReactNode | ReactNode[]): boolean {
const props = child.props as {
isActive?: boolean;
'aria-current'?: string;
children: ReactNode | ReactNode[];
};

if (props.isActive === true || props['aria-current']) {
if (
props.isActive === true ||
props['aria-current'] ||
(props.children instanceof Array && hasActiveDescendant(props.children))
) {
return true;
}

Expand Down

0 comments on commit 72c2a05

Please sign in to comment.