1- import React , { forwardRef , Ref , useState } from "react" ;
1+ import React , { forwardRef , Ref , useMemo , useState } from "react" ;
22import styled , { ThemeProvider } from "styled-components" ;
33import { responsiveSizes } from "../common/variables.js" ;
44import { useResponsiveSidenavVisibility } from "../layout/SidenavContext" ;
@@ -72,10 +72,18 @@ const Section = ({ children }: SidenavSectionPropsType): JSX.Element => (
7272
7373const Group = ( { children, title, collapsable = false , icon } : SidenavGroupPropsType ) : JSX . Element => {
7474 const [ collapsed , setCollapsed ] = useState ( false ) ;
75+ const selectedGroup = useMemo ( ( ) => {
76+ return collapsed ? React . Children . toArray ( children ) . some ( ( child ) => child [ "props" ] ?. selected ) : false ;
77+ } , [ collapsed , children ] ) ;
7578 return (
7679 < SidenavGroup >
7780 { collapsable && title ? (
78- < SidenavGroupTitleButton role = "button" aria-expanded = { ! collapsed } onClick = { ( ) => setCollapsed ( ! collapsed ) } >
81+ < SidenavGroupTitleButton
82+ role = "button"
83+ aria-expanded = { ! collapsed }
84+ onClick = { ( ) => setCollapsed ( ! collapsed ) }
85+ selectedGroup = { selectedGroup }
86+ >
7987 < SidenavContent >
8088 { typeof icon === "string" ? < SidenavIcon src = { icon } /> : icon }
8189 { title }
@@ -213,7 +221,7 @@ const SidenavGroupTitle = styled.span`
213221 padding: 0.5rem 1.2rem;
214222` ;
215223
216- const SidenavGroupTitleButton = styled . button `
224+ const SidenavGroupTitleButton = styled . button < { selectedGroup : boolean } > `
217225 all: unset;
218226 cursor: pointer;
219227 justify-content: space-between;
@@ -230,17 +238,24 @@ const SidenavGroupTitleButton = styled.button`
230238 align-items: center;
231239 margin: 0px;
232240 padding: 0.5rem 1.2rem;
233- &:focus {
241+ &:focus-visible {
234242 outline: 2px solid ${ ( props ) => props . theme . linkFocusColor } ;
235243 outline-offset: -2px;
236244 }
237245 &:hover {
238- background-color: ${ ( props ) => props . theme . groupTitleHoverBackgroundColor } ;
246+ ${ ( props ) =>
247+ props . selectedGroup
248+ ? `color: ${ props . theme . groupTitleSelectedHoverFontColor } ; background: ${ props . theme . groupTitleSelectedHoverBackgroundColor } ;`
249+ : `color: ${ props . theme . groupTitleFontColor } ; background: ${ props . theme . groupTitleHoverBackgroundColor } ;` }
239250 }
240251 &:active {
241252 background-color: ${ ( props ) => props . theme . groupTitleActiveBackgroundColor } ;
253+ color: ${ ( props ) => props . theme . groupTitleFontColor } ;
242254 }
243-
255+ ${ ( props ) =>
256+ props . selectedGroup
257+ ? `color: ${ props . theme . groupTitleSelectedFontColor } ; background: ${ props . theme . groupTitleSelectedBackgroundColor } ;`
258+ : `color: ${ props . theme . groupTitleFontColor } ; background: transparent;` }
244259 svg {
245260 width: 18px;
246261 height: auto;
0 commit comments