Skip to content

Commit 28f0606

Browse files
committed
refactor: extract shared BackButton component from LinkUI
- Create shared BackButton component to eliminate duplication - Replace duplicated back button implementations in LinkUIBlockInserter and LinkUIPageCreator - Standardize back button behavior and styling across components - Remove unused chevron icon imports from page-creator.js - Add proper JSDoc documentation for BackButton component This is the first phase of LinkUI duplication cleanup as discussed in PR #71163 follow-ups.
1 parent c3f4b72 commit 28f0606

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

packages/block-library/src/navigation-link/link-ui.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ import { useInstanceId, useFocusOnMount } from '@wordpress/compose';
3333
import { unlock } from '../lock-unlock';
3434
import { LinkUIPageCreator } from './page-creator';
3535

36+
/**
37+
* Shared BackButton component for consistent navigation across LinkUI sub-components.
38+
*
39+
* @param {Object} props Component props.
40+
* @param {string} props.className CSS class name for the button.
41+
* @param {Function} props.onBack Callback when user wants to go back.
42+
*/
43+
function BackButton( { className, onBack } ) {
44+
return (
45+
<Button
46+
className={ className }
47+
icon={ isRTL() ? chevronRightSmall : chevronLeftSmall }
48+
onClick={ ( e ) => {
49+
e.preventDefault();
50+
onBack();
51+
} }
52+
size="small"
53+
>
54+
{ __( 'Back' ) }
55+
</Button>
56+
);
57+
}
58+
3659
const { PrivateQuickInserter: QuickInserter } = unlock(
3760
blockEditorPrivateApis
3861
);
@@ -118,17 +141,10 @@ function LinkUIBlockInserter( { clientId, onBack, onBlockInsert } ) {
118141
</p>
119142
</VisuallyHidden>
120143

121-
<Button
144+
<BackButton
122145
className="link-ui-block-inserter__back"
123-
icon={ isRTL() ? chevronRightSmall : chevronLeftSmall }
124-
onClick={ ( e ) => {
125-
e.preventDefault();
126-
onBack();
127-
} }
128-
size="small"
129-
>
130-
{ __( 'Back' ) }
131-
</Button>
146+
onBack={ onBack }
147+
/>
132148

133149
<QuickInserter
134150
rootClientId={ rootBlockClientId }
@@ -272,6 +288,8 @@ function UnforwardedLinkUI( props, ref ) {
272288

273289
export const LinkUI = forwardRef( UnforwardedLinkUI );
274290

291+
export { BackButton };
292+
275293
const LinkUITools = ( {
276294
setAddingBlock,
277295
setAddingPage,

packages/block-library/src/navigation-link/page-creator.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ import {
99
__experimentalVStack as VStack,
1010
__experimentalHStack as HStack,
1111
} from '@wordpress/components';
12-
import { __, isRTL } from '@wordpress/i18n';
12+
import { __ } from '@wordpress/i18n';
1313
import { useSelect, useDispatch } from '@wordpress/data';
1414
import { store as coreStore } from '@wordpress/core-data';
1515
import { decodeEntities } from '@wordpress/html-entities';
1616
import { useState } from '@wordpress/element';
17-
import { chevronLeftSmall, chevronRightSmall } from '@wordpress/icons';
1817
import { useFocusOnMount } from '@wordpress/compose';
1918

19+
/**
20+
* Internal dependencies
21+
*/
22+
import { BackButton } from './link-ui';
23+
2024
/**
2125
* Component for creating new pages within the Navigation Link UI.
2226
*
@@ -96,17 +100,10 @@ export function LinkUIPageCreator( {
96100

97101
return (
98102
<div className="link-ui-page-creator" ref={ focusOnMountRef }>
99-
<Button
103+
<BackButton
100104
className="link-ui-page-creator__back"
101-
icon={ isRTL() ? chevronRightSmall : chevronLeftSmall }
102-
onClick={ ( e ) => {
103-
e.preventDefault();
104-
onBack();
105-
} }
106-
size="small"
107-
>
108-
{ __( 'Back' ) }
109-
</Button>
105+
onBack={ onBack }
106+
/>
110107

111108
<VStack className="link-ui-page-creator__inner" spacing={ 4 }>
112109
<form onSubmit={ createPage }>

0 commit comments

Comments
 (0)