@@ -2,7 +2,6 @@ import type { TemplateResult } from 'lit';
22
33import { LitElement , html } from 'lit' ;
44import { property } from 'lit/decorators/property.js' ;
5- import { unsafeStatic , html as staticH } from 'lit/static-html.js' ;
65
76import { BaseAccordion } from './BaseAccordion.js' ;
87import { ComposedEvent } from '@patternfly/pfe-core' ;
@@ -32,14 +31,16 @@ export abstract class BaseAccordionHeader extends LitElement {
3231
3332 @property ( { type : Boolean , reflect : true } ) expanded = false ;
3433
35- @property ( { reflect : true , attribute : 'heading-text' } ) headingText = '' ;
34+ @property ( { reflect : true , attribute : 'heading-text' } ) headingText ?: string ;
3635
37- @property ( { reflect : true , attribute : 'heading-tag' } ) headingTag = 'h3' ;
36+ @property ( { reflect : true , attribute : 'heading-tag' } ) headingTag ?: string ;
3837
3938 #generatedHtag?: HTMLHeadingElement ;
4039
4140 #logger = new Logger ( this ) ;
4241
42+ #header?: HTMLElement ;
43+
4344 override connectedCallback ( ) {
4445 super . connectedCallback ( ) ;
4546 this . addEventListener ( 'click' , this . #onClick) ;
@@ -49,16 +50,16 @@ export abstract class BaseAccordionHeader extends LitElement {
4950 }
5051
5152 async #initHeader( ) {
52- const header = this . #getOrCreateHeader( ) ;
53+ if ( this . headingText && ! this . headingTag ) {
54+ this . headingTag = 'h3' ;
55+ }
56+ this . #header = this . #getOrCreateHeader( ) ;
5357
5458 // prevent double-logging
55- if ( header !== this . #generatedHtag) {
59+ if ( this . # header !== this . #generatedHtag) {
5660 this . #generatedHtag = undefined ;
5761 }
5862
59- this . headingTag = header ?. tagName . toLowerCase ( ) ?? 'h3' ;
60- this . headingText = header ?. textContent ?. trim ( ) ?? '' ;
61-
6263 do {
6364 await this . updateComplete ;
6465 } while ( ! await this . updateComplete ) ;
@@ -71,19 +72,28 @@ export abstract class BaseAccordionHeader extends LitElement {
7172 renderAfterButton ?( ) : TemplateResult ;
7273
7374 override render ( ) : TemplateResult {
74- const tag = unsafeStatic ( this . headingTag ) ;
75- const ariaExpandedState = String ( ! ! this . expanded ) as 'true' | 'false' ;
76- return staticH `
77- <${ tag } id="heading">
78- <button id="button"
79- class="toggle"
80- aria-expanded="${ ariaExpandedState } ">
81- <span part="text">${ this . headingText || html `
82- <slot></slot>` }
83- </span>
84- ${ this . renderAfterButton ?.( ) }
85- </button>
86- </${ tag } >
75+ switch ( this . headingTag ) {
76+ case 'h1' : return html `< h1 id ="heading "> ${ this . #renderHeaderContent( ) } </ h1 > ` ;
77+ case 'h2' : return html `< h2 id ="heading "> ${ this . #renderHeaderContent( ) } </ h2 > ` ;
78+ case 'h3' : return html `< h3 id ="heading "> ${ this . #renderHeaderContent( ) } </ h3 > ` ;
79+ case 'h4' : return html `< h4 id ="heading "> ${ this . #renderHeaderContent( ) } </ h4 > ` ;
80+ case 'h5' : return html `< h5 id ="heading "> ${ this . #renderHeaderContent( ) } </ h5 > ` ;
81+ case 'h6' : return html `< h6 id ="heading "> ${ this . #renderHeaderContent( ) } </ h6 > ` ;
82+ default : return this . #renderHeaderContent( ) ;
83+ }
84+ }
85+
86+ #renderHeaderContent( ) {
87+ const headingText = this . headingText ?. trim ( ) ?? this . #header?. textContent ?. trim ( ) ;
88+ return html `
89+ < button id ="button "
90+ class ="toggle "
91+ aria-expanded ="${ String ( ! ! this . expanded ) as 'true' | 'false' } ">
92+ < span part ="text "> ${ headingText ?? html `
93+ < slot > </ slot > ` }
94+ </ span >
95+ ${ this . renderAfterButton ?.( ) }
96+ </ button >
8797 ` ;
8898 }
8999
0 commit comments