11import { Observer } from '@playcanvas/observer' ;
22import { BindingTwoWay , BooleanInput , Container , Label , LabelGroup , Panel , TextInput } from '@playcanvas/pcui/react' ;
33import { Component } from 'react' ;
4- import { Link } from 'react-router-dom' ;
4+ import { Link , useLocation } from 'react-router-dom' ;
55
66import { exampleMetaData } from '../../../cache/metadata.mjs' ;
77import { MIN_DESKTOP_WIDTH , VERSION } from '../constants.mjs' ;
@@ -10,16 +10,15 @@ import { jsx } from '../jsx.mjs';
1010import { thumbnailPath } from '../paths.mjs' ;
1111import { getOrientation } from '../utils.mjs' ;
1212
13- // eslint-disable-next-line jsdoc/require-property
1413/**
1514 * @typedef {object } Props
15+ * @property {{ pathname: string, hash: string } } location - The router location.
1616 */
1717
1818/**
1919 * @typedef {object } State
2020 * @property {Record<string, Record<string, object>> } defaultCategories - The default categories.
2121 * @property {Record<string, Record<string, object>>|null } filteredCategories - The filtered categories.
22- * @property {string } hash - The hash.
2322 * @property {Observer } observer - The observer.
2423 * @property {boolean } collapsed - Collapsed or not.
2524 * @property {string } orientation - Current orientation.
@@ -52,7 +51,6 @@ class SideBar extends TypedComponent {
5251 state = {
5352 defaultCategories : getDefaultExampleFiles ( ) ,
5453 filteredCategories : null ,
55- hash : location . hash ,
5654 observer : new Observer ( { largeThumbnails : false } ) ,
5755 // @ts -ignore
5856 collapsed : localStorage . getItem ( 'sideBarCollapsed' ) === 'true' || window . top . innerWidth < MIN_DESKTOP_WIDTH ,
@@ -99,9 +97,6 @@ class SideBar extends TypedComponent {
9997 if ( ! sideBar ) {
10098 return ;
10199 }
102- window . addEventListener ( 'hashchange' , ( ) => {
103- this . mergeState ( { hash : location . hash } ) ;
104- } ) ;
105100 this . state . observer . on ( 'largeThumbnails:set' , ( ) => {
106101 let minTopNavItemDistance = Number . MAX_VALUE ;
107102
@@ -207,7 +202,7 @@ class SideBar extends TypedComponent {
207202 if ( Object . keys ( categories ) . length === 0 ) {
208203 return jsx ( Label , { text : 'No results' } ) ;
209204 }
210- const { hash } = this . state ;
205+ const { pathname } = this . props . location ;
211206 return Object . keys ( categories )
212207 . sort ( ( a , b ) => ( a > b ? 1 : - 1 ) )
213208 . map ( ( category ) => {
@@ -229,7 +224,7 @@ class SideBar extends TypedComponent {
229224 . sort ( ( a , b ) => ( a > b ? 1 : - 1 ) )
230225 . map ( ( example ) => {
231226 const path = `/${ category } /${ example } ` ;
232- const isSelected = new RegExp ( ` ${ path } $` ) . test ( hash ) ;
227+ const isSelected = pathname === path ;
233228 const className = `nav-item ${ isSelected ? 'selected' : null } ` ;
234229 return jsx (
235230 Link ,
@@ -303,4 +298,13 @@ class SideBar extends TypedComponent {
303298 }
304299}
305300
306- export { SideBar } ;
301+ /**
302+ * Provide the current router location to the SideBar class component.
303+ * @returns {JSX.Element } The SideBar element with the router location passed as the `location` prop.
304+ */
305+ function SideBarWithRouter ( ) {
306+ const location = useLocation ( ) ;
307+ return jsx ( SideBar , { location } ) ;
308+ }
309+
310+ export { SideBarWithRouter as SideBar } ;
0 commit comments