-
Notifications
You must be signed in to change notification settings - Fork 953
1195 content planner improve inline banner accessibility #23231
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
Open
vraja-pro
wants to merge
15
commits into
release/27.6
Choose a base branch
from
1195-content-planner-improve-inline-banner-accessibility
base: release/27.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
08ae0de
fix: add ARIA group role to inline banner for screen reader accessibi…
vraja-pro f89eae2
fix: make inline banner keyboard-navigable via Tab in Gutenberg editor
vraja-pro d08a853
test: add unit tests for handleBannerTabNavigation helpe
vraja-pro 6559264
add shift tab navigation
vraja-pro 9773d95
fix complexity
vraja-pro b33bde7
fix deprecated keycode
vraja-pro 863c05f
fix js doc lint
vraja-pro b388bc4
fix: reduce complexity
vraja-pro c8b3c16
fix: remove max width class
vraja-pro e21b4d4
feature(content-planner): adds learn more link to inline banner
vraja-pro d902681
fix navigation to learn more link
vraja-pro ddc63fd
fix: reduce complexity for handleBannerTabNavigation
vraja-pro aaf5cbf
fix spaces
vraja-pro 9438322
fix: approve modal learn more link
vraja-pro 254d515
remove unused class
vraja-pro 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
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
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
34 changes: 34 additions & 0 deletions
34
packages/js/src/ai-content-planner/helpers/handle-banner-tab-navigation.js
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { focus } from "@wordpress/dom"; | ||
|
|
||
| /** | ||
| * Keydown handler that keeps the inline banner reachable via Tab inside the Gutenberg | ||
| * writing flow. Gutenberg's useTabNav hook intercepts Tab in the bubble phase and | ||
| * redirects focus to sentinel divs when the next tabbable element is not inside the | ||
| * same [data-block] wrapper. Because the banner sits outside any real block, it is | ||
| * normally skipped. This handler is meant to be attached in the capture phase so it | ||
| * runs before Gutenberg; once it calls preventDefault(), Gutenberg's early-return guard | ||
| * fires and leaves focus alone. | ||
| * | ||
| * @param {HTMLElement} bannerEl The banner wrapper element. | ||
| * @param {KeyboardEvent} event The keydown event. | ||
| * @returns {void} | ||
| */ | ||
| // eslint-disable-next-line complexity | ||
|
vraja-pro marked this conversation as resolved.
Outdated
|
||
| export function handleBannerTabNavigation( bannerEl, event ) { | ||
| if ( event.defaultPrevented || event.keyCode !== 9 || ! bannerEl ) { | ||
|
vraja-pro marked this conversation as resolved.
Outdated
|
||
| return; | ||
| } | ||
|
|
||
| const findSibling = event.shiftKey ? focus.tabbable.findPrevious : focus.tabbable.findNext; | ||
| const next = findSibling( event.target ); | ||
| if ( ! next ) { | ||
|
vraja-pro marked this conversation as resolved.
Outdated
|
||
| return; | ||
| } | ||
|
|
||
| // Intercept only when Tab or Shift+Tab crosses the banner boundary (entering or leaving). | ||
| // Intra-banner navigation is already handled by Gutenberg via the data-block attribute. | ||
| if ( bannerEl.contains( event.target ) !== bannerEl.contains( next ) ) { | ||
|
vraja-pro marked this conversation as resolved.
Outdated
|
||
| event.preventDefault(); | ||
| next.focus(); | ||
| } | ||
| } | ||
191 changes: 191 additions & 0 deletions
191
packages/js/tests/ai-content-planner/helpers/handle-banner-tab-navigation.test.js
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 |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| import { handleBannerTabNavigation } from "../../../src/ai-content-planner/helpers/handle-banner-tab-navigation"; | ||
|
|
||
| const mockFindNext = jest.fn(); | ||
| const mockFindPrevious = jest.fn(); | ||
|
|
||
| jest.mock( "@wordpress/dom", () => ( { | ||
| focus: { | ||
| tabbable: { | ||
| findNext: ( ...args ) => mockFindNext( ...args ), | ||
| findPrevious: ( ...args ) => mockFindPrevious( ...args ), | ||
| }, | ||
| }, | ||
| } ) ); | ||
|
|
||
| /** | ||
| * Creates a minimal keydown event mock. | ||
| * @param {object} overrides Properties to override on the default Tab event. | ||
| * @returns {object} The event mock. | ||
| */ | ||
| const makeEvent = ( overrides = {} ) => ( { | ||
| defaultPrevented: false, | ||
| keyCode: 9, | ||
| shiftKey: false, | ||
| target: document.createElement( "button" ), | ||
| preventDefault: jest.fn(), | ||
| ...overrides, | ||
| } ); | ||
|
vraja-pro marked this conversation as resolved.
|
||
|
|
||
| describe( "handleBannerTabNavigation", () => { | ||
| let bannerEl; | ||
| let insideButton; | ||
| let outsideButton; | ||
|
|
||
| beforeEach( () => { | ||
| bannerEl = document.createElement( "div" ); | ||
| insideButton = document.createElement( "button" ); | ||
| outsideButton = document.createElement( "button" ); | ||
|
|
||
| bannerEl.appendChild( insideButton ); | ||
| document.body.appendChild( bannerEl ); | ||
| document.body.appendChild( outsideButton ); | ||
|
|
||
| mockFindNext.mockReset(); | ||
| mockFindPrevious.mockReset(); | ||
| } ); | ||
|
|
||
| afterEach( () => { | ||
| document.body.innerHTML = ""; | ||
| } ); | ||
|
|
||
| describe( "early returns", () => { | ||
| it( "does nothing when event.defaultPrevented is true", () => { | ||
| const event = makeEvent( { defaultPrevented: true } ); | ||
| handleBannerTabNavigation( bannerEl, event ); | ||
| expect( mockFindNext ).not.toHaveBeenCalled(); | ||
| expect( event.preventDefault ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( "does nothing for non-Tab keys", () => { | ||
| const event = makeEvent( { keyCode: 13 } ); | ||
| handleBannerTabNavigation( bannerEl, event ); | ||
| expect( mockFindNext ).not.toHaveBeenCalled(); | ||
| expect( event.preventDefault ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( "does nothing when bannerEl is null", () => { | ||
| const event = makeEvent(); | ||
| handleBannerTabNavigation( null, event ); | ||
| expect( mockFindNext ).not.toHaveBeenCalled(); | ||
| expect( event.preventDefault ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( "does nothing when there is no next tabbable element", () => { | ||
| mockFindNext.mockReturnValue( null ); | ||
| const event = makeEvent( { target: outsideButton } ); | ||
| handleBannerTabNavigation( bannerEl, event ); | ||
| expect( event.preventDefault ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( "does nothing when there is no previous tabbable element (Shift+Tab)", () => { | ||
| mockFindPrevious.mockReturnValue( null ); | ||
| const event = makeEvent( { shiftKey: true, target: outsideButton } ); | ||
| handleBannerTabNavigation( bannerEl, event ); | ||
| expect( event.preventDefault ).not.toHaveBeenCalled(); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( "Tab into banner", () => { | ||
| it( "focuses the first banner button and prevents default when tabbing forward from outside", () => { | ||
| mockFindNext.mockReturnValue( insideButton ); | ||
| const focusSpy = jest.spyOn( insideButton, "focus" ); | ||
| const event = makeEvent( { target: outsideButton } ); | ||
|
|
||
| handleBannerTabNavigation( bannerEl, event ); | ||
|
|
||
| expect( event.preventDefault ).toHaveBeenCalledTimes( 1 ); | ||
| expect( focusSpy ).toHaveBeenCalledTimes( 1 ); | ||
| } ); | ||
|
|
||
| it( "focuses the last banner button and prevents default when shift-tabbing backward from outside", () => { | ||
| mockFindPrevious.mockReturnValue( insideButton ); | ||
| const focusSpy = jest.spyOn( insideButton, "focus" ); | ||
| const event = makeEvent( { shiftKey: true, target: outsideButton } ); | ||
|
|
||
| handleBannerTabNavigation( bannerEl, event ); | ||
|
|
||
| expect( event.preventDefault ).toHaveBeenCalledTimes( 1 ); | ||
| expect( focusSpy ).toHaveBeenCalledTimes( 1 ); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( "Tab out of banner", () => { | ||
| it( "focuses the next element outside and prevents default when tabbing forward from the last button", () => { | ||
| mockFindNext.mockReturnValue( outsideButton ); | ||
| const focusSpy = jest.spyOn( outsideButton, "focus" ); | ||
| const event = makeEvent( { target: insideButton } ); | ||
|
|
||
| handleBannerTabNavigation( bannerEl, event ); | ||
|
|
||
| expect( event.preventDefault ).toHaveBeenCalledTimes( 1 ); | ||
| expect( focusSpy ).toHaveBeenCalledTimes( 1 ); | ||
| } ); | ||
|
|
||
| it( "focuses the previous element outside and prevents default when shift-tabbing from the first button", () => { | ||
| mockFindPrevious.mockReturnValue( outsideButton ); | ||
| const focusSpy = jest.spyOn( outsideButton, "focus" ); | ||
| const event = makeEvent( { shiftKey: true, target: insideButton } ); | ||
|
|
||
| handleBannerTabNavigation( bannerEl, event ); | ||
|
|
||
| expect( event.preventDefault ).toHaveBeenCalledTimes( 1 ); | ||
| expect( focusSpy ).toHaveBeenCalledTimes( 1 ); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( "Tab within banner", () => { | ||
| it( "does not intercept when both current and next tabbable are inside the banner", () => { | ||
| const secondInsideButton = document.createElement( "button" ); | ||
| bannerEl.appendChild( secondInsideButton ); | ||
| mockFindNext.mockReturnValue( secondInsideButton ); | ||
| const focusSpy = jest.spyOn( secondInsideButton, "focus" ); | ||
| const event = makeEvent( { target: insideButton } ); | ||
|
|
||
| handleBannerTabNavigation( bannerEl, event ); | ||
|
|
||
| expect( event.preventDefault ).not.toHaveBeenCalled(); | ||
| expect( focusSpy ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( "does not intercept when both current and previous tabbable are inside the banner (Shift+Tab)", () => { | ||
| const secondInsideButton = document.createElement( "button" ); | ||
| bannerEl.appendChild( secondInsideButton ); | ||
| mockFindPrevious.mockReturnValue( insideButton ); | ||
| const focusSpy = jest.spyOn( insideButton, "focus" ); | ||
| const event = makeEvent( { shiftKey: true, target: secondInsideButton } ); | ||
|
|
||
| handleBannerTabNavigation( bannerEl, event ); | ||
|
|
||
| expect( event.preventDefault ).not.toHaveBeenCalled(); | ||
| expect( focusSpy ).not.toHaveBeenCalled(); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( "Tab outside banner entirely", () => { | ||
| it( "does not intercept when both current and next tabbable are outside the banner", () => { | ||
| const anotherOutsideButton = document.createElement( "button" ); | ||
| document.body.appendChild( anotherOutsideButton ); | ||
| mockFindNext.mockReturnValue( anotherOutsideButton ); | ||
| const focusSpy = jest.spyOn( anotherOutsideButton, "focus" ); | ||
| const event = makeEvent( { target: outsideButton } ); | ||
|
|
||
| handleBannerTabNavigation( bannerEl, event ); | ||
|
|
||
| expect( event.preventDefault ).not.toHaveBeenCalled(); | ||
| expect( focusSpy ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( "does not intercept when both current and previous tabbable are outside the banner (Shift+Tab)", () => { | ||
| const anotherOutsideButton = document.createElement( "button" ); | ||
| document.body.appendChild( anotherOutsideButton ); | ||
| mockFindPrevious.mockReturnValue( anotherOutsideButton ); | ||
| const focusSpy = jest.spyOn( anotherOutsideButton, "focus" ); | ||
| const event = makeEvent( { shiftKey: true, target: outsideButton } ); | ||
|
|
||
| handleBannerTabNavigation( bannerEl, event ); | ||
|
|
||
| expect( event.preventDefault ).not.toHaveBeenCalled(); | ||
| expect( focusSpy ).not.toHaveBeenCalled(); | ||
| } ); | ||
| } ); | ||
| } ); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.