Skip to content

Commit 65fb7dc

Browse files
📝 Add docstrings to main
Docstrings generation was requested by @JaclynCodes. * #8 (comment) The following files were modified: * `examples/src/app/components/Sidebar.mjs`
1 parent 7fe4a20 commit 65fb7dc

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

examples/src/app/components/Sidebar.mjs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Observer } from '@playcanvas/observer';
22
import { BindingTwoWay, BooleanInput, Container, Label, LabelGroup, Panel, TextInput } from '@playcanvas/pcui/react';
33
import { Component } from 'react';
4-
import { Link } from 'react-router-dom';
4+
import { Link, useLocation } from 'react-router-dom';
55

66
import { exampleMetaData } from '../../../cache/metadata.mjs';
77
import { MIN_DESKTOP_WIDTH, VERSION } from '../constants.mjs';
@@ -10,16 +10,15 @@ import { jsx } from '../jsx.mjs';
1010
import { thumbnailPath } from '../paths.mjs';
1111
import { 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

Comments
 (0)