-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Clean up LinkUI duplication for Add block and Create page flows #71499
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
28f0606
refactor: extract shared BackButton component from LinkUI
getdave 86d39bc
refactor: extract useDialogIds hook for dialog accessibility IDs
getdave 01f963b
refactor: create DialogWrapper component with minimal API
getdave 05fa701
refactor: extract LinkUIBlockInserter to separate file and consolidat…
getdave c88c7c5
refactor: simplify DialogWrapper instance ID generation
getdave bfc81ca
Standardise ID generation in Dialog
getdave 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
65 changes: 65 additions & 0 deletions
65
packages/block-library/src/navigation-link/block-inserter.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,65 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { | ||
| store as blockEditorStore, | ||
| privateApis as blockEditorPrivateApis, | ||
| } from '@wordpress/block-editor'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import DialogWrapper from './dialog-wrapper'; | ||
| import { unlock } from '../lock-unlock'; | ||
|
|
||
| const { PrivateQuickInserter: QuickInserter } = unlock( | ||
| blockEditorPrivateApis | ||
| ); | ||
|
|
||
| /** | ||
| * Component for inserting blocks within the Navigation Link UI. | ||
| * | ||
| * @param {Object} props Component props. | ||
| * @param {string} props.clientId Client ID of the navigation link block. | ||
| * @param {Function} props.onBack Callback when user wants to go back. | ||
| * @param {Function} props.onBlockInsert Callback when a block is inserted. | ||
| */ | ||
| function LinkUIBlockInserter( { clientId, onBack, onBlockInsert } ) { | ||
| const { rootBlockClientId } = useSelect( | ||
| ( select ) => { | ||
| const { getBlockRootClientId } = select( blockEditorStore ); | ||
|
|
||
| return { | ||
| rootBlockClientId: getBlockRootClientId( clientId ), | ||
| }; | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
|
|
||
| if ( ! clientId ) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <DialogWrapper | ||
| className="link-ui-block-inserter" | ||
| title={ __( 'Add block' ) } | ||
| description={ __( 'Choose a block to add to your Navigation.' ) } | ||
| onBack={ onBack } | ||
| > | ||
| <QuickInserter | ||
| rootClientId={ rootBlockClientId } | ||
| clientId={ clientId } | ||
| isAppender={ false } | ||
| prioritizePatterns={ false } | ||
| selectBlockOnInsert={ ! onBlockInsert } | ||
| onSelect={ onBlockInsert ? onBlockInsert : undefined } | ||
| hasSearch={ false } | ||
| /> | ||
| </DialogWrapper> | ||
| ); | ||
| } | ||
|
|
||
| export default LinkUIBlockInserter; |
74 changes: 74 additions & 0 deletions
74
packages/block-library/src/navigation-link/dialog-wrapper.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,74 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Button, VisuallyHidden } from '@wordpress/components'; | ||
| import { __, isRTL } from '@wordpress/i18n'; | ||
| import { chevronLeftSmall, chevronRightSmall } from '@wordpress/icons'; | ||
| import { useInstanceId, useFocusOnMount } from '@wordpress/compose'; | ||
|
|
||
| /** | ||
| * Shared BackButton component for consistent navigation across LinkUI sub-components. | ||
| * | ||
| * @param {Object} props Component props. | ||
| * @param {string} props.className CSS class name for the button. | ||
| * @param {Function} props.onBack Callback when user wants to go back. | ||
| */ | ||
| function BackButton( { className, onBack } ) { | ||
| return ( | ||
| <Button | ||
| className={ className } | ||
| icon={ isRTL() ? chevronRightSmall : chevronLeftSmall } | ||
| onClick={ ( e ) => { | ||
| e.preventDefault(); | ||
| onBack(); | ||
| } } | ||
| size="small" | ||
| > | ||
| { __( 'Back' ) } | ||
| </Button> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Shared DialogWrapper component for consistent dialog structure across LinkUI sub-components. | ||
| * | ||
| * @param {Object} props Component props. | ||
| * @param {string} props.className CSS class name for the dialog container. | ||
| * @param {string} props.title Dialog title for accessibility. | ||
| * @param {string} props.description Dialog description for accessibility. | ||
| * @param {Function} props.onBack Callback when user wants to go back. | ||
| * @param {Object} props.children Child components to render inside the dialog. | ||
| */ | ||
| function DialogWrapper( { className, title, description, onBack, children } ) { | ||
| const dialogTitleId = useInstanceId( | ||
| DialogWrapper, | ||
| 'link-ui-dialog-title' | ||
| ); | ||
| const dialogDescriptionId = useInstanceId( | ||
| DialogWrapper, | ||
| 'link-ui-dialog-description' | ||
| ); | ||
| const focusOnMountRef = useFocusOnMount( 'firstElement' ); | ||
| const backButtonClassName = `${ className }__back`; | ||
|
|
||
| return ( | ||
| <div | ||
| className={ className } | ||
| role="dialog" | ||
| aria-labelledby={ dialogTitleId } | ||
| aria-describedby={ dialogDescriptionId } | ||
| ref={ focusOnMountRef } | ||
| > | ||
| <VisuallyHidden> | ||
| <h2 id={ dialogTitleId }>{ title }</h2> | ||
| <p id={ dialogDescriptionId }>{ description }</p> | ||
| </VisuallyHidden> | ||
|
|
||
| <BackButton className={ backButtonClassName } onBack={ onBack } /> | ||
|
|
||
| { children } | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default DialogWrapper; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between backticks and single quotes in this context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just an error when I remove some interpolation from this string. Single quotes are preferred. Suprised the linter doesn't pick up on it!