1+ /* eslint-disable react/jsx-props-no-spreading */
12import React , { Key , useCallback , useEffect , useState } from 'react' ;
23import {
34 ActionButton ,
@@ -12,6 +13,7 @@ import { vsMenu } from '@deephaven/icons';
1213import {
1314 MENU_CATEGORY_DATA_ATTRIBUTE ,
1415 NO_MENU_DATA_ATTRIBUTE ,
16+ SPECTRUM_COMPARISON_SAMPLES_ID ,
1517 SPECTRUM_COMPONENT_SAMPLES_ID ,
1618} from './constants' ;
1719
@@ -26,18 +28,18 @@ type LinkCategory = { category: string; items: Link[] };
2628 * menu category. These will be queried by the SamplesMenu component to build
2729 * up the menu sections.
2830 */
29- export function SampleMenuCategory ( {
30- 'data-menu-category' : dataMenuCategory ,
31- } : Record < typeof MENU_CATEGORY_DATA_ATTRIBUTE , string > ) : JSX . Element {
32- return < div data-menu-category = { dataMenuCategory } /> ;
31+ export function SampleMenuCategory (
32+ props : Record < typeof MENU_CATEGORY_DATA_ATTRIBUTE , string >
33+ ) : JSX . Element {
34+ return < div { ... props } /> ;
3335}
3436
3537/**
3638 * Creates a menu from h2, h3 elements on the page and assigns them each an id
3739 * for hash navigation purposes. If the current hash matches one of the ids, it
3840 * will scroll to that element. This handles the initial page load scenario.
39- * Menu sections are identified by divs with MENU_CATEGORY_DATA_ATTRIBUTE
40- * attributes.
41+ * Menu categories are identified by divs with MENU_CATEGORY_DATA_ATTRIBUTE
42+ * attributes originating from the <SampleMenuCategory> component .
4143 */
4244export function SamplesMenu ( ) : JSX . Element {
4345 const [ links , setLinks ] = useState < LinkCategory [ ] > ( [ ] ) ;
@@ -53,34 +55,48 @@ export function SamplesMenu(): JSX.Element {
5355 `#${ SPECTRUM_COMPONENT_SAMPLES_ID } `
5456 ) ;
5557
58+ const spectrumComparisonSamples = document . querySelector (
59+ `#${ SPECTRUM_COMPARISON_SAMPLES_ID } `
60+ ) ;
61+
5662 document
5763 . querySelectorAll ( `h2,h3,[${ MENU_CATEGORY_DATA_ATTRIBUTE } ]` )
58- . forEach ( el => {
59- if ( el . textContent == null || el . hasAttribute ( NO_MENU_DATA_ATTRIBUTE ) ) {
64+ . forEach ( headingEl => {
65+ if (
66+ headingEl . textContent == null ||
67+ headingEl . hasAttribute ( NO_MENU_DATA_ATTRIBUTE )
68+ ) {
6069 return ;
6170 }
6271
6372 // Create a new category section
64- if ( el . hasAttribute ( MENU_CATEGORY_DATA_ATTRIBUTE ) ) {
73+ if ( headingEl . hasAttribute ( MENU_CATEGORY_DATA_ATTRIBUTE ) ) {
6574 currentCategory = {
66- category : el . getAttribute ( MENU_CATEGORY_DATA_ATTRIBUTE ) ?? '' ,
75+ category :
76+ headingEl . getAttribute ( MENU_CATEGORY_DATA_ATTRIBUTE ) ?? '' ,
6777 items : [ ] ,
6878 } ;
6979 categories . push ( currentCategory ) ;
7080
7181 return ;
7282 }
7383
74- const id = `${
75- spectrumComponentsSamples ?. contains ( el ) === true ? 'spectrum-' : ''
76- } ${ el . textContent } `
84+ const idPrefix =
85+ // eslint-disable-next-line no-nested-ternary
86+ spectrumComponentsSamples ?. contains ( headingEl ) === true
87+ ? 'spectrum-'
88+ : spectrumComparisonSamples ?. contains ( headingEl ) === true
89+ ? 'spectrum-compare-'
90+ : '' ;
91+
92+ const id = `${ idPrefix } ${ headingEl . textContent } `
7793 . toLowerCase ( )
7894 . replace ( / \s / g, '-' ) ;
7995
8096 // eslint-disable-next-line no-param-reassign
81- el . id = id ;
97+ headingEl . id = id ;
8298
83- currentCategory . items . push ( { id, label : el . textContent } ) ;
99+ currentCategory . items . push ( { id, label : headingEl . textContent } ) ;
84100 } ) ;
85101
86102 setLinks ( categories ) ;
0 commit comments