@@ -73,24 +73,28 @@ class Treeview {
7373 } )
7474 }
7575
76+ this . _element . classList . add ( CLASS_NAME_MENU_OPEN )
77+
7678 const childElement = this . _element ?. querySelector ( SELECTOR_TREEVIEW_MENU ) as HTMLElement | undefined
7779
7880 if ( childElement ) {
79- this . _element . classList . add ( CLASS_NAME_MENU_OPEN )
8081 slideDown ( childElement , this . _config . animationSpeed )
81- this . _element . dispatchEvent ( event )
8282 }
83+
84+ this . _element . dispatchEvent ( event )
8385 }
8486
8587 close ( ) : void {
8688 const event = new Event ( EVENT_COLLAPSED )
8789 const childElement = this . _element ?. querySelector ( SELECTOR_TREEVIEW_MENU ) as HTMLElement | undefined
8890
91+ this . _element . classList . remove ( CLASS_NAME_MENU_OPEN )
92+
8993 if ( childElement ) {
90- this . _element . classList . remove ( CLASS_NAME_MENU_OPEN )
9194 slideUp ( childElement , this . _config . animationSpeed )
92- this . _element . dispatchEvent ( event )
9395 }
96+
97+ this . _element . dispatchEvent ( event )
9498 }
9599
96100 toggle ( ) : void {
@@ -116,8 +120,14 @@ onDOMContentLoaded(() => {
116120 const target = event . target as HTMLElement
117121 const targetItem = target . closest ( SELECTOR_NAV_ITEM ) as HTMLElement | undefined
118122 const targetLink = target . closest ( SELECTOR_NAV_LINK ) as HTMLAnchorElement | undefined
123+ const targetTreeviewMenu = targetItem ?. querySelector ( SELECTOR_TREEVIEW_MENU ) as HTMLElement | undefined
119124 const lteToggleElement = event . currentTarget as HTMLElement
120125
126+ // Avoid creating Treeview instances on non menu elements
127+ if ( ! targetTreeviewMenu ) {
128+ return
129+ }
130+
121131 if ( target ?. getAttribute ( 'href' ) === '#' || targetLink ?. getAttribute ( 'href' ) === '#' ) {
122132 event . preventDefault ( )
123133 }
0 commit comments