Repository navigation
Closes #1062 prevent Chrome aria-hidden focus warning - #1077
faisalahammad wants to merge 2 commits into
Conversation
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 5 |
| Duplication | 0 |
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.
- restore missing use import for Container in inc/main.php - fix filter_input parameter order in classes/Bulk/Bulk.php Refs wp-media#1077
…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
…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
left a comment
There was a problem hiding this comment.
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 whendocument.activeElementis 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.
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
d77c0b7 to
7bd2338
Compare
faisalahammad
left a comment
There was a problem hiding this comment.
@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.bodyanymore. modal-is-openis 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/Unitand$wp_versionchanges. - Added
Tests/e2e/specs/modal-focus.spec.ts. It needsIMAGIFY_TESTS_API_KEYand 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
left a comment
There was a problem hiding this comment.
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.
- 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
left a comment
There was a problem hiding this comment.
@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.
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:The branch was rebased on
developand reduced to this fix only.Type of change
Detailed scenario
What was tested
Tests/e2e/specs/modal-focus.spec.ts(payment modal: choose a plan, close with the button and withEsc, check focus and that noBlocked aria-hiddenmessage is logged). It needsIMAGIFY_TESTS_API_KEYand a free plan account with quota, likepricing-modal-layout.spec.ts, so it skips otherwise. It was type-checked locally but not run.npm run build(includes ESLint) andnpm run test:unitpass.How to test
Tab: focus must stay inside the modal.Esc, then repeat and click outside the box.Blocked aria-hiddenmessage in the console.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.jsimagify.openModal()stores the opener element in the modal data (imagifyOpener).modal-is-openfirst, then moves focus out of the modal (opener, orblur()as a fallback), and only then fades the modal out and setsaria-hidden="true".blurhandler 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.jsswitchToView()checks whetherdocument.activeElementis inside a view that will be hidden. If so, focus first goes to the modal container (still visible, soaria-hiddenis not set on an element holding focus), and then into the new view after it is displayed..min.jsfiles are rebuilt withnpm 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
Code style
Unticked items justification
None. Note that the Playwright spec depends on a real account state, see "What was tested".
Additional Checks