@@ -7,7 +7,7 @@ import classnames from 'classnames';
7
7
* WordPress dependencies
8
8
*/
9
9
import { __ } from '@wordpress/i18n' ;
10
- import { useCallback , useState , useRef } from '@wordpress/element' ;
10
+ import { useCallback , useEffect , useState , useRef } from '@wordpress/element' ;
11
11
import {
12
12
Button ,
13
13
ButtonGroup ,
@@ -73,27 +73,42 @@ function URLPicker( {
73
73
opensInNewTab,
74
74
onToggleOpenInNewTab,
75
75
anchorRef,
76
+ richTextRef,
76
77
} ) {
77
- const [ isURLPickerOpen , setIsURLPickerOpen ] = useState ( false ) ;
78
- const urlIsSet = ! ! url ;
79
- const urlIsSetandSelected = urlIsSet && isSelected ;
80
- const openLinkControl = ( ) => {
81
- setIsURLPickerOpen ( true ) ;
82
- return false ; // prevents default behaviour for event
78
+ const [ isEditingURL , setIsEditingURL ] = useState ( false ) ;
79
+ const isURLSet = ! ! url ;
80
+
81
+ const startEditing = ( event ) => {
82
+ event . preventDefault ( ) ;
83
+ setIsEditingURL ( true ) ;
83
84
} ;
84
- const unlinkButton = ( ) => {
85
+
86
+ const unlink = ( ) => {
85
87
setAttributes ( {
86
88
url : undefined ,
87
89
linkTarget : undefined ,
88
90
rel : undefined ,
89
91
} ) ;
90
- setIsURLPickerOpen ( false ) ;
92
+ setIsEditingURL ( false ) ;
91
93
} ;
92
- const linkControl = ( isURLPickerOpen || urlIsSetandSelected ) && (
94
+
95
+ useEffect ( ( ) => {
96
+ if ( ! isSelected ) {
97
+ setIsEditingURL ( false ) ;
98
+ }
99
+ } , [ isSelected ] ) ;
100
+
101
+ const isLinkControlVisible = isSelected && ( isEditingURL || isURLSet ) ;
102
+
103
+ const linkControl = isLinkControlVisible && (
93
104
< Popover
94
105
position = "bottom center"
95
- onClose = { ( ) => setIsURLPickerOpen ( false ) }
106
+ onClose = { ( ) => {
107
+ setIsEditingURL ( false ) ;
108
+ richTextRef . current ?. focus ( ) ;
109
+ } }
96
110
anchorRef = { anchorRef ?. current }
111
+ focusOnMount = { isEditingURL ? 'firstElement' : false }
97
112
>
98
113
< LinkControl
99
114
className = "wp-block-navigation-link__inline-link-input"
@@ -108,28 +123,34 @@ function URLPicker( {
108
123
onToggleOpenInNewTab ( newOpensInNewTab ) ;
109
124
}
110
125
} }
126
+ onRemove = { ( ) => {
127
+ unlink ( ) ;
128
+ richTextRef . current ?. focus ( ) ;
129
+ } }
130
+ forceIsEditingLink = { isEditingURL }
111
131
/>
112
132
</ Popover >
113
133
) ;
134
+
114
135
return (
115
136
< >
116
137
< BlockControls group = "block" >
117
- { ! urlIsSet && (
138
+ { ! isURLSet && (
118
139
< ToolbarButton
119
140
name = "link"
120
141
icon = { link }
121
142
title = { __ ( 'Link' ) }
122
143
shortcut = { displayShortcut . primary ( 'k' ) }
123
- onClick = { openLinkControl }
144
+ onClick = { startEditing }
124
145
/>
125
146
) }
126
- { urlIsSetandSelected && (
147
+ { isURLSet && (
127
148
< ToolbarButton
128
149
name = "link"
129
150
icon = { linkOff }
130
151
title = { __ ( 'Unlink' ) }
131
152
shortcut = { displayShortcut . primaryShift ( 'k' ) }
132
- onClick = { unlinkButton }
153
+ onClick = { unlink }
133
154
isActive = { true }
134
155
/>
135
156
) }
@@ -138,8 +159,11 @@ function URLPicker( {
138
159
< KeyboardShortcuts
139
160
bindGlobal
140
161
shortcuts = { {
141
- [ rawShortcut . primary ( 'k' ) ] : openLinkControl ,
142
- [ rawShortcut . primaryShift ( 'k' ) ] : unlinkButton ,
162
+ [ rawShortcut . primary ( 'k' ) ] : startEditing ,
163
+ [ rawShortcut . primaryShift ( 'k' ) ] : ( ) => {
164
+ unlink ( ) ;
165
+ richTextRef . current ?. focus ( ) ;
166
+ } ,
143
167
} }
144
168
/>
145
169
) }
@@ -201,6 +225,7 @@ function ButtonEdit( props ) {
201
225
const colorProps = useColorProps ( attributes ) ;
202
226
const spacingProps = useSpacingProps ( attributes ) ;
203
227
const ref = useRef ( ) ;
228
+ const richTextRef = useRef ( ) ;
204
229
const blockProps = useBlockProps ( { ref } ) ;
205
230
206
231
return (
@@ -213,6 +238,7 @@ function ButtonEdit( props ) {
213
238
} ) }
214
239
>
215
240
< RichText
241
+ ref = { richTextRef }
216
242
aria-label = { __ ( 'Button text' ) }
217
243
placeholder = { placeholder || __ ( 'Add text…' ) }
218
244
value = { text }
@@ -252,6 +278,7 @@ function ButtonEdit( props ) {
252
278
opensInNewTab = { linkTarget === '_blank' }
253
279
onToggleOpenInNewTab = { onToggleOpenInNewTab }
254
280
anchorRef = { ref }
281
+ richTextRef = { richTextRef }
255
282
/>
256
283
< InspectorControls >
257
284
< WidthPanel
0 commit comments