# useFocus

Opens the floating element while the reference element has focus,
like CSS `:focus`.

```js
import {useFocus} from '@floating-ui/react';
```

## Usage

This hook is an interaction hook that returns event handler
props.

To use it, pass it the `context{:.const}` object returned from
`useFloating(){:js}`, and then feed its result into the
`useInteractions(){:js}` array. The returned prop getters are
then spread onto the elements for rendering.

```js {9-13} /context/
function App() {
  const [isOpen, setIsOpen] = useState(false);

  const {refs, floatingStyles, context} = useFloating({
    open: isOpen,
    onOpenChange: setIsOpen,
  });

  const focus = useFocus(context);

  const {getReferenceProps, getFloatingProps} = useInteractions([
    focus,
  ]);

  return (
    <>
      <div ref={refs.setReference} {...getReferenceProps()}>
        Reference element
      </div>
      {isOpen && (
        <div
          ref={refs.setFloating}
          style={floatingStyles}
          {...getFloatingProps()}
        >
          Floating element
        </div>
      )}
    </>
  );
}
```

## Props

```ts
interface Props {
  enabled?: boolean;
  keyboardOnly?: boolean;
}
```

### enabled

default: `true{:js}`

Conditionally enable/disable the hook.

```js
useFocus(context, {
  enabled: false,
});
```

### keyboardOnly

default: `true{:js}`

Whether the state only changes if the `focus{:.string}` event was
triggered via keyboard rather than any pointer inputs
(mouse/touch/pen). Programmatic `.focus(){:js}` calls are
considered as keyboard.

```js
useFocus(context, {
  keyboardOnly: false,
});
```
