Skip to content

Commit cb60073

Browse files
authored
fix(overlay): hide from screen readers while animating (#29951)
Issue number: resolves #29857 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> Screen readers like Android Talkback would not have the focus ring on the correct element. For example, Talkback would announce the buttons correctly within action sheet but the focus ring was no where to be seen. After digging around, the focus rings are located out of screen because the action sheet is mounted to the DOM out of the screen first then transitions into the screen. There are some screen readers that do not behave as expected when an element uses `transform` styles like action sheet. https://github.com/user-attachments/assets/5a700bcc-3149-47a9-96ff-0aef99dd2bb3 ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - When an overlay is being animated (presenting or dismissing), the overlay will hide from screen readers. This allows the element to navigate to its correct destination for screen readers to interact with. Plus, we shouldn't allow screen readers to interact with content in the middle of an animation. It could lead to some confusion. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Dev build: 8.3.3-dev.11729276019.194c165c **A physical Android device will be needed, the issue does not appear in simulators** Components that need to be tested because they use overlays: - Action sheet - Alert - Loading - Modal - Popover - Select w/ action sheet interface - Select w/ alert interface - Select w/ popover interface - Toast How to test: 1. Create a starter app (any framework will do) 2. Add an action sheet 3. Build app for mobile devices ``` ionic build ionic cap add ios ionic cap add android ionic cap copy && ionic cap sync ``` 4. Open the app in Android Studio: `ionic cap open android` 5. Connect the Android device to Android Studio 6. Open app in Android device 7. Launch Talkback 8. Navigate back to the app 9. Open action sheet 10. Swipe over to the action sheet's buttons 11. Notice that the buttons don't have a focus ring 12. Go back to the starter 4. Install the dev build 5. Add the components to the app 6. Sync app: `ionic cap copy && ionic cap sync` 13. Relaunch the app on the Android device 14. Verify that the focus ring appears on the action sheet's buttons 15. Verify that the other overlays are working as intended
1 parent e32fbe0 commit cb60073

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

core/src/utils/overlays.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ export const present = async <OverlayPresentOptions>(
514514

515515
document.body.classList.add(BACKDROP_NO_SCROLL);
516516

517-
hideOverlaysFromScreenReaders(overlay.el);
517+
hideUnderlyingOverlaysFromScreenReaders(overlay.el);
518+
hideAnimatingOverlayFromScreenReaders(overlay.el);
518519

519520
overlay.presented = true;
520521
overlay.willPresent.emit();
@@ -560,6 +561,11 @@ export const present = async <OverlayPresentOptions>(
560561
* it would still have aria-hidden on being presented again.
561562
* Removing it here ensures the overlay is visible to screen
562563
* readers.
564+
*
565+
* If this overlay was being presented, then it was hidden
566+
* from screen readers during the animation. Now that the
567+
* animation is complete, we can reveal the overlay to
568+
* screen readers.
563569
*/
564570
overlay.el.removeAttribute('aria-hidden');
565571
};
@@ -644,6 +650,13 @@ export const dismiss = async <OverlayDismissOptions>(
644650
overlay.presented = false;
645651

646652
try {
653+
/**
654+
* There is no need to show the overlay to screen readers during
655+
* the dismiss animation. This is because the overlay will be removed
656+
* from the DOM after the animation is complete.
657+
*/
658+
hideAnimatingOverlayFromScreenReaders(overlay.el);
659+
647660
// Overlay contents should not be clickable during dismiss
648661
overlay.el.style.setProperty('pointer-events', 'none');
649662
overlay.willDismiss.emit({ data, role });
@@ -929,6 +942,29 @@ export const createTriggerController = () => {
929942
};
930943
};
931944

945+
/**
946+
* The overlay that is being animated also needs to hide from screen
947+
* readers during its animation. This ensures that assistive technologies
948+
* like TalkBack do not announce or interact with the content until the
949+
* animation is complete, avoiding confusion for users.
950+
*
951+
* If the overlay is being presented, it prevents focus rings from appearing
952+
* in incorrect positions due to the transition (specifically `transform`
953+
* styles), ensuring that when aria-hidden is removed, the focus rings are
954+
* correctly displayed in the final location of the elements.
955+
*
956+
* @param overlay - The overlay that is being animated.
957+
*/
958+
const hideAnimatingOverlayFromScreenReaders = (overlay: HTMLIonOverlayElement) => {
959+
if (doc === undefined) return;
960+
961+
/**
962+
* Once the animation is complete, this attribute will be removed.
963+
* This is done at the end of the `present` method.
964+
*/
965+
overlay.setAttribute('aria-hidden', 'true');
966+
};
967+
932968
/**
933969
* Ensure that underlying overlays have aria-hidden if necessary so that screen readers
934970
* cannot move focus to these elements. Note that we cannot rely on focus/focusin/focusout
@@ -939,7 +975,7 @@ export const createTriggerController = () => {
939975
* @param newTopMostOverlay - The overlay that is being presented. Since the overlay has not been
940976
* fully presented yet at the time this function is called it will not be included in the getPresentedOverlays result.
941977
*/
942-
const hideOverlaysFromScreenReaders = (newTopMostOverlay: HTMLIonOverlayElement) => {
978+
const hideUnderlyingOverlaysFromScreenReaders = (newTopMostOverlay: HTMLIonOverlayElement) => {
943979
if (doc === undefined) return;
944980

945981
const overlays = getPresentedOverlays(doc);

0 commit comments

Comments
 (0)