@@ -67,7 +67,52 @@ export class SegmentButton implements ComponentInterface, ButtonInterface {
6767 this . updateState ( ) ;
6868 }
6969
70- connectedCallback ( ) {
70+ private getNextSiblingOfType < T extends Element > ( element : Element ) : T | null {
71+ let sibling = element . nextSibling ;
72+ while ( sibling ) {
73+ if ( sibling . nodeType === Node . ELEMENT_NODE && ( sibling as T ) !== null ) {
74+ return sibling as T ;
75+ }
76+ sibling = sibling . nextSibling ;
77+ }
78+ return null ;
79+ }
80+
81+ private waitForSegmentContent ( ionSegment : HTMLIonSegmentElement | null , contentId : string ) : Promise < HTMLElement > {
82+ return new Promise ( ( resolve , reject ) => {
83+ let timeoutId : NodeJS . Timeout | undefined = undefined ;
84+ let animationFrameId : number ;
85+
86+ const check = ( ) => {
87+ if ( ! ionSegment ) {
88+ reject ( new Error ( `Segment not found when looking for Segment Content` ) ) ;
89+ return ;
90+ }
91+
92+ const segmentView = this . getNextSiblingOfType < HTMLIonSegmentViewElement > ( ionSegment ) ; // Skip the text nodes
93+ const segmentContent = segmentView ?. querySelector (
94+ `ion-segment-content[id="${ contentId } "]`
95+ ) as HTMLIonSegmentContentElement | null ;
96+ if ( segmentContent ) {
97+ clearTimeout ( timeoutId ) ; // Clear the timeout if the segmentContent is found
98+ cancelAnimationFrame ( animationFrameId ) ;
99+ resolve ( segmentContent ) ;
100+ } else {
101+ animationFrameId = requestAnimationFrame ( check ) ; // Keep checking on the next animation frame
102+ }
103+ } ;
104+
105+ check ( ) ;
106+
107+ // Set a timeout to reject the promise
108+ timeoutId = setTimeout ( ( ) => {
109+ cancelAnimationFrame ( animationFrameId ) ;
110+ reject ( new Error ( `Unable to find Segment Content with id="${ contentId } within 1000 ms` ) ) ;
111+ } , 1000 ) ;
112+ } ) ;
113+ }
114+
115+ async connectedCallback ( ) {
71116 const segmentEl = ( this . segmentEl = this . el . closest ( 'ion-segment' ) ) ;
72117 if ( segmentEl ) {
73118 this . updateState ( ) ;
@@ -78,12 +123,13 @@ export class SegmentButton implements ComponentInterface, ButtonInterface {
78123 // Return if there is no contentId defined
79124 if ( ! this . contentId ) return ;
80125
81- // Attempt to find the Segment Content by its contentId
82- const segmentContent = document . getElementById ( this . contentId ) as HTMLIonSegmentContentElement | null ;
83-
84- // If no associated Segment Content exists, log an error and return
85- if ( ! segmentContent ) {
86- console . error ( `Segment Button: Unable to find Segment Content with id="${ this . contentId } ".` ) ;
126+ let segmentContent ;
127+ try {
128+ // Attempt to find the Segment Content by its contentId
129+ segmentContent = await this . waitForSegmentContent ( segmentEl , this . contentId ) ;
130+ } catch ( error ) {
131+ // If no associated Segment Content exists, log an error and return
132+ console . error ( 'Segment Button: ' , ( error as Error ) . message ) ;
87133 return ;
88134 }
89135
0 commit comments