-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fix(segment-button): protect connectedCallback for when segment-content has not yet been created #30133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix(segment-button): protect connectedCallback for when segment-content has not yet been created #30133
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,7 +67,51 @@ export class SegmentButton implements ComponentInterface, ButtonInterface { | |
| this.updateState(); | ||
| } | ||
|
|
||
| connectedCallback() { | ||
| private getNextSiblingOfType<T extends Element>(element: Element): T | null { | ||
| let sibling = element.nextSibling; | ||
| while (sibling) { | ||
| if (sibling.nodeType === Node.ELEMENT_NODE && (sibling as T) !== null) { | ||
| return sibling as T; | ||
| } | ||
| sibling = sibling.nextSibling; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private waitForSegmentContent(ionSegment: HTMLIonSegmentElement | null, contentId: string): Promise<HTMLElement> { | ||
| return new Promise((resolve, reject) => { | ||
| if (!ionSegment) { | ||
| reject(new Error(`Segment not found when looking for Segment Content`)); | ||
| } | ||
|
|
||
| let timeoutId: any = null; | ||
| let animationFrameId: number; | ||
|
|
||
| const check = () => { | ||
| const segmentView = this.getNextSiblingOfType<HTMLIonSegmentViewElement>(ionSegment!); // Skip the text nodes | ||
|
||
| const segmentContent = segmentView?.querySelector( | ||
OS-pedrolourenco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `ion-segment-content[id="${contentId}"]` | ||
| ) as HTMLIonSegmentContentElement | null; | ||
| if (segmentContent) { | ||
| clearTimeout(timeoutId); // Clear the timeout if the segmentContent is found | ||
| cancelAnimationFrame(animationFrameId); | ||
| resolve(segmentContent); | ||
| } else { | ||
| animationFrameId = requestAnimationFrame(check); // Keep checking on the next animation frame | ||
| } | ||
| }; | ||
|
|
||
| check(); | ||
|
|
||
| // Set a timeout to reject the promise | ||
| timeoutId = setTimeout(() => { | ||
| cancelAnimationFrame(animationFrameId); | ||
| reject(new Error(`Unable to find Segment Content with id="${contentId} within 1000 ms`)); | ||
| }, 1000); | ||
| }); | ||
| } | ||
|
|
||
| async connectedCallback() { | ||
| const segmentEl = (this.segmentEl = this.el.closest('ion-segment')); | ||
| if (segmentEl) { | ||
| this.updateState(); | ||
|
|
@@ -78,12 +122,13 @@ export class SegmentButton implements ComponentInterface, ButtonInterface { | |
| // Return if there is no contentId defined | ||
| if (!this.contentId) return; | ||
|
|
||
| // Attempt to find the Segment Content by its contentId | ||
| const segmentContent = document.getElementById(this.contentId) as HTMLIonSegmentContentElement | null; | ||
|
|
||
| // If no associated Segment Content exists, log an error and return | ||
| if (!segmentContent) { | ||
| console.error(`Segment Button: Unable to find Segment Content with id="${this.contentId}".`); | ||
| let segmentContent; | ||
| try { | ||
| // Attempt to find the Segment Content by its contentId | ||
| segmentContent = await this.waitForSegmentContent(segmentEl, this.contentId); | ||
| } catch (error) { | ||
| // If no associated Segment Content exists, log an error and return | ||
| console.error('Segment Button: ', (error as Error).message); | ||
| return; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's properly type this