Title: WithFocusOutside
Published: March 9, 2021
Last modified: October 14, 2026

---

# WithFocusOutside

`withFocusOutside` is a React [higher-order component](https://facebook.github.io/react/docs/higher-order-components.html)
to enable behavior to occur when focus leaves an element. Since a `blur` event will
fire in React even when focus transitions to another element in the same context,
this higher-order component encapsulates the logic necessary to determine if focus
has truly left the element.

## Usage

Wrap your original component with `withFocusOutside`, defining a `handleFocusOutside`
instance method on the component class.

**Note:** `withFocusOutside` must only be used to wrap the `Component` class.

    ```language-jsx
    import { withFocusOutside, TextControl } from '@wordpress/components';

    const MyComponentWithFocusOutside = withFocusOutside(
        class extends React.Component {
            handleFocusOutside() {
                console.log( 'Focus outside' );
            }

            render() {
                return (
                    <div>
                        <TextControl onChange={ () => {} } />
                        <TextControl onChange={ () => {} } />
                    </div>
                );
            }
        }
    );
    ```

In the above example, the `handleFocusOutside` function is only called if focus 
leaves the element, and not if transitioning focus between the two inputs.

First published

March 9, 2021

Last updated

October 14, 2026

Edit article

[ Improve it on GitHub: WithFocusOutside ](https://github.com/WordPress/gutenberg/edit/trunk/packages/components/src/higher-order/with-focus-outside/README.md)

[  Previous: WithFilters](https://developer.wordpress.org/block-editor/reference-guides/components/with-filters/)

[  Next: WithFocusReturn](https://developer.wordpress.org/block-editor/reference-guides/components/with-focus-return/)