Skip to content

Commit

Permalink
Block Editor: Change default URLInput autoFocus to false
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Apr 3, 2020
1 parent b41e7c6 commit 0936dcf
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/block-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Master

### Breaking Changes

- The default `URLInput` `autoFocus` prop value has changed from `true` to `false`. If you relied on the auto-focus behavior of the input, you must explicitly assign an `autoFocus={ true }` prop. Note that (in accordance with the purpose of this change) it is usually inadvisable to auto-focus an input. Refer to the [component README](https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/components/url-input/README.md) for more information.

## 3.7.0 (2020-02-10)

### New Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,11 @@ describe( 'Selecting links', () => {
const searchInput = getURLInput();
const form = container.querySelector( 'form' );

// Focus the input via DOM. The simulated events below don't operate
// on the DOM, but for LinkControl's focus preservation to work (and
// to verify it below), focus must be set in the real DOM.
searchInput.focus();

// Simulate searching for a term
act( () => {
Simulate.change( searchInput, {
Expand Down
6 changes: 4 additions & 2 deletions packages/block-editor/src/components/url-input/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ Renders the URL input field used by the `URLInputButton` component. It can be us

### `autoFocus: Boolean`

*Optional.* By default, the input will gain focus when it is rendered, as typically it is displayed conditionally. For example when clicking on `URLInputButton` or editing a block.
*Optional.* Whether the input should receive focus as soon as it is rendered. Defaults to `false`.

If you are not conditionally rendering this component set this property to `false`.
Note that it is usually inadvisable to use `autoFocus`, since it can be a disorienting experience for keyboard usage and for users of assistive technology. It should typically only be used when the input is displayed temporarily and it is the intended user experience that the input should be focused immediately upon being shown (e.g. showing the input only after activating a toggle button).

If you are using `URLInput` in the context of a dialog, you should consider instead to use the `focusOnMount` prop of the [`Popover`](https://github.com/WordPress/gutenberg/blob/master/packages/components/src/popover/README.md#focusonmount) or [`Modal`](https://github.com/WordPress/gutenberg/blob/master/packages/components/src/modal/README.md#focusonmount) components instead, as they achieve the same effective behavior.

### `className: String`

Expand Down
7 changes: 7 additions & 0 deletions packages/block-editor/src/components/url-input/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class URLInputButton extends Component {
const { expanded } = this.state;
const buttonLabel = url ? __( 'Edit link' ) : __( 'Insert link' );

// Disable reason: The rendered URLInput is toggled in response to a
// click on the button, and it is expected that toggling it to be
// visible should shift focus from the button into the input.

/* eslint-disable jsx-a11y/no-autofocus */
return (
<div className="block-editor-url-input__button">
<Button
Expand All @@ -57,6 +62,7 @@ class URLInputButton extends Component {
onClick={ this.toggle }
/>
<URLInput
autoFocus
value={ url || '' }
onChange={ onChange }
/>
Expand All @@ -70,6 +76,7 @@ class URLInputButton extends Component {
) }
</div>
);
/* eslint-enable jsx-a11y/no-autofocus */
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/url-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class URLInput extends Component {
__experimentalRenderSuggestions: renderSuggestions,
placeholder = __( 'Paste URL or type to search' ),
value = '',
autoFocus = true,
autoFocus = false,
__experimentalShowInitialSuggestions = false,
} = this.props;

Expand Down

0 comments on commit 0936dcf

Please sign in to comment.