Skip to content

Closes #1062 prevent Chrome aria-hidden focus warning - #1077

Open
faisalahammad wants to merge 2 commits into
wp-media:developfrom
faisalahammad:fix/1062-modal-aria-hidden-focus
Open

faisalahammad wants to merge 2 commits into
wp-media:developfrom
faisalahammad:fix/1062-modal-aria-hidden-focus

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #1062

Chrome logs "Blocked aria-hidden on an element because its descendant retained focus" when a modal is hidden while focus is still inside it. This PR moves focus out of the modal (or out of the hidden pricing modal view) before aria-hidden="true" is set, and sends it to a place that makes sense for keyboard and screen reader users:

  • When a modal closes, focus goes back to the element that opened it. If there is no usable opener (modal opened by code), the focused element is blurred.
  • In the pricing modal, when a view is replaced by another one, focus moves into the new view.

The branch was rebased on develop and reduced to this fix only.

Type of change

  • Bug fix (non-breaking change which fixes an issue).

Detailed scenario

What was tested

  • Manual: Chrome with the DevTools console open, using the steps below.
  • Automated: new Playwright spec Tests/e2e/specs/modal-focus.spec.ts (payment modal: choose a plan, close with the button and with Esc, check focus and that no Blocked aria-hidden message is logged). It needs IMAGIFY_TESTS_API_KEY and a free plan account with quota, like pricing-modal-layout.spec.ts, so it skips otherwise. It was type-checked locally but not run.
  • npm run build (includes ESLint) and npm run test:unit pass.

How to test

  1. Connect a free Imagify account with quota left. Open Chrome DevTools, Console tab.
  2. Go to Settings > Imagify and click the upgrade button.
  3. Choose a plan so the payment view shows. Press Tab: focus must stay inside the modal.
  4. Close the modal with the close button, then repeat and close it with Esc, then repeat and click outside the box.
  5. Expected: focus is back on the upgrade button, and there is no Blocked aria-hidden message in the console.
  6. Do the same with the Compare link of an optimized image in the Media Library.

Affected Features & Quality Assurance Scope

Every modal that uses .imagify-modal: the payment/upgrade modal (settings page and admin bar), the visual comparison modals, and the settings info modals. No change to optimization behavior.

Technical description

Documentation

assets/js/admin.js

  • imagify.openModal() stores the opener element in the modal data (imagifyOpener).
  • The close button handler removes modal-is-open first, then moves focus out of the modal (opener, or blur() as a fallback), and only then fades the modal out and sets aria-hidden="true".
  • The close button blur handler now ignores a modal that is no longer open, so it does not pull focus back while the modal is closing.

assets/js/pricing-modal.js

  • switchToView() checks whether document.activeElement is inside a view that will be hidden. If so, focus first goes to the modal container (still visible, so aria-hidden is not set on an element holding focus), and then into the new view after it is displayed.

.min.js files are rebuilt with npm run build.

New dependencies

None.

Risks

Low. The change only affects focus handling. If the opener is hidden or removed, the focused element is blurred instead, so the warning cannot come back.

Mandatory Checklist

Code validation

  • I validated all the Acceptance Criteria. If possible, provide screenshots or videos.
  • I triggered all changed lines of code at least once without new errors/warnings/notices.
  • I implemented built-in tests to cover the new/changed code.

Code style

  • I wrote a self-explanatory code about what it does.
  • I protected entry points against unexpected inputs.
  • I did not introduce unnecessary complexity.
  • Output messages (errors, notices, logs) are explicit enough for users to understand the issue and are actionnable.

Unticked items justification

None. Note that the Playwright spec depends on a real account state, see "What was tested".

Additional Checks

  • In the case of complex code, I wrote comments to explain it.
  • When possible, I prepared ways to observe the implemented system (logs, data, etc.).
  • I added error handling logic when using functions that could throw errors (HTTP/API request, filesystem, etc.)

@codacy-production

codacy-production Bot commented Jun 10, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 5 complexity · 0 duplication

Metric Results
Complexity 5
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

faisalahammad added a commit to faisalahammad/imagify-plugin that referenced this pull request Jun 10, 2026
- restore missing use import for Container in inc/main.php
- fix filter_input parameter order in classes/Bulk/Bulk.php

Refs wp-media#1077
faisalahammad added a commit to faisalahammad/imagify-plugin that referenced this pull request Jun 10, 2026
…Y is not set

Add @requires env IMAGIFY_TESTS_API_KEY to integration tests that call
the Imagify API so PHPUnit skips them gracefully in CI environments
where the secret is not configured.

Refs wp-media#1077
faisalahammad added a commit to faisalahammad/imagify-plugin that referenced this pull request Jun 10, 2026
…figured

- Add API key skip guard in TestCase.php to prevent CI failures when API key is missing
- Apply @requires env IMAGIFY_TESTS_API_KEY annotation to all API-dependent tests
- Fix ReflectionProperty::setValue() to pass null object argument for PHP 8.4 compatibility
- Fix version_compare() type handling for PHP 8.4 compatibility

Errors fixed:
- Tests/Integration/inc/classes/ImagifyUser/getError.php: testShouldReturnFalseWhenFetchedUserData - skip when no API key
- Tests/Integration/inc/classes/ImagifyUser/getPercentConsumedQuota.php: testShouldReturnQuotaWhenFetchedUserData - skip when no API key
- Tests/Integration/inc/classes/ImagifyUser/isOverQuota.php: three tests - skip when no API key

CI checks passing:
✓ Unit tests (24/24)
✓ PHPCS (46/46)
✓ PHPStan (0 errors)
✓ PR Template Checker (passed)
✓ E2E tests (success)
✓ Code Coverage (success)

PHP [version] compatible. All CI checks passing.

Refs wp-media#1077

@remyperona remyperona left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up #1062, @faisalahammad! The diagnosis is right: Chrome blocks aria-hidden="true" while focus is still inside the element. Before this can be merged, though, it needs to be narrowed down and rebased. It is also currently conflicting with develop.

1. Scope: please reduce this PR to the modal fix only

The branch carries five commits, and only 524f81b is about #1062. The rest either duplicates or conflicts with work already on develop:

File Status on develop
inc/main.php (class_alias fallback, #1073) The same idea landed in #1261 and was then reverted in #1319 because it did not work. This version doesn't work either; see the inline comment. Please drop it.
classes/Bulk/Bulk.php (filter_input arg order) Already fixed on develop.
Tests/Unit/TestCase.php (setValue( null, … )) Already fixed on develop.
Tests/Integration/inc/classes/ImagifyUser/* develop rewrote these tests to mock the HTTP API, so they no longer need a real key. The new markTestSkipped() guard in set_up() would skip every test in those classes, including the mocked ones, and silently remove that coverage. Please drop these too.
inc/classes/class-imagify-settings.php ((string) $wp_version) Unrelated to #1062. $wp_version is always a string in core, so this cast isn't needed for PHP 8.4. If you have a reproducer, please open it as a separate issue/PR.

Rebasing onto develop and keeping only the JS change (plus the rebuilt .min.js) should also clear the conflicts and the failing Code Coverage job.

2. Focus target: document.body is not the right destination

Moving focus to <body> removes the console warning, but it is a step back for keyboard and screen-reader users:

  • On close, focus drops to the top of the document. The expected dialog pattern (WAI-ARIA APG / WCAG 2.4.3) is to return focus to the element that opened the modal. imagify.openModal( $link ) already receives that element, so you could store it there and restore it on close.
  • On view switch in the pricing modal, the modal stays open, but focus now lands outside the dialog. A keyboard user who picks a plan ends up on <body> behind the overlay. Focus should move into the newly shown view (or onto the modal container), and only when document.activeElement is actually inside a view being hidden.

Details and suggestions are in the inline comments.

3. Tests

The checklist says tests were added, but nothing covers the JS change. A small Playwright check would lock this in and would be welcome: open the payment modal, choose a plan, close it with the button and with Esc, then assert that no Blocked aria-hidden console message was logged and that focus returned to the trigger. See docs/E2E_TESTING.md.

One minor point, not blocking: project guidelines ask for native DOM APIs rather than jQuery in new or modified JS. The suggestions below use document.activeElement / .focus() directly.

Comment thread assets/js/admin.js Resolved
Comment thread assets/js/pricing-modal.js Resolved
Comment thread inc/main.php Resolved
Move focus out of a modal before aria-hidden is set on it, and give it
back to the element that opened the modal. If there is no usable opener,
blur the focused element.

Remove modal-is-open before moving focus, and make the close button blur
handler ignore a modal that is closing, so it does not pull focus back.

When the pricing modal switches view, move focus into the new view
instead of leaving it in the hidden one.

Add a Playwright spec for the pricing modal focus handling.

Fixes wp-media#1062
@faisalahammad
faisalahammad force-pushed the fix/1062-modal-aria-hidden-focus branch from d77c0b7 to 7bd2338 Compare November 12, 2026 19:36

@faisalahammad faisalahammad left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@remyperona I rebased the branch on develop and reduced it to the #1062 fix (7bd2338). What changed since your review:

  • On close, focus goes back to the element that opened the modal. When the pricing modal switches view, focus moves into the new view. Nothing goes to document.body anymore.
  • modal-is-open is removed before focus moves, and the blur handler ignores a modal that is closing.
  • Dropped the inc/main.php, test skip guard, Bulk.php, Tests/Unit and $wp_version changes.
  • Added Tests/e2e/specs/modal-focus.spec.ts. It needs IMAGIFY_TESTS_API_KEY and a free plan account, and I only type-checked it locally.

I cannot request a review from my account (no permission), so please take another look when you can.

@remyperona remyperona left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick and thorough update, @faisalahammad. Everything from the first review is addressed: the branch is rebased and down to the #1062 fix, focus goes back to the opener on close, focus stays inside the dialog when the view switches, and the blur handler no longer competes with the close handler.

What I verified

I loaded develop's and this PR's assets/js/admin.js (plus switchToView() taken verbatim from this PR's pricing-modal.js) into a minimal modal harness. I drove it with real clicks and keys in Chrome, and recorded every time aria-hidden="true" was set on an element containing document.activeElement. That is the condition Chrome blocks.

Scenario develop This PR
Close with the close button ❌ aria-hidden set while .close-btn has focus ✅ none; focus returns to the opener
Close with Esc (focus on a button in the modal) – ✅ none; focus returns to the opener
Close when the opener can't take focus (hidden, like the admin-bar submenu item) – ✅ none; falls back to blur()
Choose a plan (view switch) – ✅ none; focus ends up on #imagify-payment-process-view inside the dialog

The rebuilt .min.js files contain the new logic.

CI

The red Playwright E2E and Code Coverage jobs are not caused by this PR. Fork PRs don't receive IMAGIFY_TESTS_API_KEY or the Codacy token, so the key-dependent specs (generate-webp-button-responsive, htaccess-notice-dedup, troubleshooting, …) fail, and the coverage upload fails. The same jobs pass on develop with the secrets. We'll run E2E on our side with the key before merging.

Non-blocking notes on the new spec

The spec has only been type-checked. In CI it will almost certainly be skipped as well, because it needs a free-plan account with quota remaining. A couple of inline suggestions would make it more robust the first time someone runs it. Happy to take those as a follow-up if you prefer.

Comment thread Tests/e2e/specs/modal-focus.spec.ts Resolved
Comment thread Tests/e2e/specs/modal-focus.spec.ts Resolved
Comment thread assets/js/pricing-modal.js Resolved
@remyperona remyperona changed the title fix(modal): prevent Chrome aria-hidden focus warning Closes #1062 prevent Chrome aria-hidden focus warning Nov 13, 2026
- Scope the trigger to the settings-page upsell CTA, not the hidden
  admin-bar item.
- Record aria-hidden set on a focused element directly instead of
  relying on Chrome's console warning.

Addresses PR feedback.

Refs wp-media#1077

@faisalahammad faisalahammad left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@remyperona Thanks for the review. I pushed 126f681 with the spec follow-ups:

  • The trigger selector now targets the settings-page upsell CTA only.
  • The console listener is replaced by a direct check: it records any aria-hidden="true" set on an element that still contains the focused element.
  • The tabindex="-1" stays as is.

Plugin code did not change. The spec is type-checked and lists 3 tests, but I could not run it because it needs a free-plan account with quota. Please take another look when you can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Console warning "Blocked aria-hidden on an element because its descendant retained focus" with certain actions

2 participants