Repository navigation
Closed
Labels
package: core@ionic/core package@ionic/core packagetype: buga confirmed bug reporta confirmed bug report
Description
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already report this problem, without success.
Ionic Framework Version
v8.x
Current Behavior
After upgrading from version 7 to 8, the ion-alerts created via the AlertController no longer have the custom ID attribute I set via the htmlAttributes property like so
alertController.create({
htmlAttributes: {id: 'custom-id'}
...
});Instead, it has an id of ion-overlay-1.
Expected Behavior
The ion-alert should have the custom ID
Steps to Reproduce
- Create a new alert via the AlertController, giving it a custom ID
- Observe that the underlying ion-alert does not have the custom ID
Code Reproduction URL
https://github.com/mikelhamer/ion8-alert-bug
Ionic Info
[WARN] Error loading @capacitor/ios package.json: Error: Cannot find module '@capacitor/ios/package.json'
Require stack:
- /opt/homebrew/lib/node_modules/@ionic/cli/lib/project/index.js
- /opt/homebrew/lib/node_modules/@ionic/cli/lib/index.js
- /opt/homebrew/lib/node_modules/@ionic/cli/index.js
- /opt/homebrew/lib/node_modules/@ionic/cli/bin/ionic
[WARN] Error loading @capacitor/android package.json: Error: Cannot find module '@capacitor/android/package.json'
Require stack:
- /opt/homebrew/lib/node_modules/@ionic/cli/lib/project/index.js
- /opt/homebrew/lib/node_modules/@ionic/cli/lib/index.js
- /opt/homebrew/lib/node_modules/@ionic/cli/index.js
- /opt/homebrew/lib/node_modules/@ionic/cli/bin/ionic
Ionic:
Ionic CLI : 7.2.0 (/opt/homebrew/lib/node_modules/@ionic/cli)
Ionic Framework : @ionic/angular 8.2.5
@angular-devkit/build-angular : 18.1.0
@angular-devkit/schematics : 18.1.0
@angular/cli : 18.1.0
@ionic/angular-toolkit : 11.0.1
Capacitor:
Capacitor CLI : 6.1.0
@capacitor/android : not installed
@capacitor/core : 6.1.0
@capacitor/ios : not installed
Utility:
cordova-res (update available: 0.15.4) : 0.15.3
native-run : 2.0.1
System:
NodeJS : v20.15.1 (/usr/local/bin/node)
npm : 9.7.1
OS : macOS Monterey
Additional Information
No response
Metadata
Metadata
Assignees
Labels
package: core@ionic/core package@ionic/core packagetype: buga confirmed bug reporta confirmed bug report
Type
Fields
No fields configured for issues without a type.
Activity
sean-perkins commentedon Jul 11, 2024
@mikelhamer can you make the repository public so the Ionic team can review it? Thanks!
mikelhamer commentedon Jul 11, 2024
Oops! Done
sean-perkins commentedon Jul 11, 2024
@mikelhamer awesome, thanks!
This behavior changed in this PR from me 😅
This is a bug. Ionic Framework's overlays need to be updated to additionally account for the
idbeing passed throughhtmlAttributes, since the element will not have reflected theidat the time we are checking to set the incremental overlay id.As a workaround you can manually assigning the attribute after the overlay is presented:
[-]bug: AlertController not setting id from htmlAttributes[/-][+]bug: Overlays override id assigned through htmlAttributes[/+]mikelhamer commentedon Jul 11, 2024
@sean-perkins woohoo! Thanks for confirming, and for the workaround! I wouldn't mind trying to take this on as a first contribution, but could use some insights into the impact of that PR and what to consider when applying a fix.
sean-perkins commentedon Jul 11, 2024
@mikelhamer awesome!
I believe the fix is straightforward, we need to opt-out of calling
setOverlayIdin the overlay components when thehtmlAttributeshas anid.For example, something similar to:
This should prevent Ionic from overriding the
idthat a developer passes in.Ionic's tests can feel intimidating, but I promise they aren't too bad to get started with 👍 The documentation for how testing works in Ionic Framework is available here: https://github.com/ionic-team/ionic-framework/tree/main/docs/core/testing. You may be able to write a simple Stencil spec test instead of relying on something heavier like the Playwright e2e tests.
An example of a Stencil spec test is here: https://github.com/ionic-team/ionic-framework/blob/main/core/src/components/alert/test/alert.spec.ts
The test case should be:
Let me know if you run into any challenges or have questions!
mikelhamer commentedon Jul 12, 2024
@sean-perkins many thanks! I will dive in
mikelhamer commentedon Jul 12, 2024
@sean-perkins hmmmm, I am a bit lost. I've created this test which I thought should fail, but it's passing before making any changes to
alert.tsx. I'm struggling to get this test to fail.sean-perkins commentedon Jul 12, 2024
@mikelhamer I would recommend renaming the
alert.spec.tstoalert.spec.tsxso you can use JSX to assign the test template easier.For example:
With it, I was able to confirm the test fails on
mainand passes with:componentWillLoad() { + if (!this.htmlAttributes?.id) { setOverlayId(this.el); + } this.inputsChanged(); this.buttonsChanged(); }fix(alert): dont overwrite id set in htmlAttributes
mikelhamer commentedon Jul 12, 2024
@sean-perkins thanks for the help! PR opened.
fix(alert): do not overwrite id set in htmlAttributes (#29708)
[-]bug: Overlays override id assigned through htmlAttributes[/-][+]bug: Alerts override id assigned through htmlAttributes[/+]ionitron-bot commentedon Aug 14, 2024
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.