Files
react/packages/react-events/docs/Focus.md
Nicolas Gallagher 2cca18728e React Events: add onFocusVisibleChange to Focus (#15516)
Called when focus visibility changes. Focus is only considered visible if a
focus event occurs after keyboard navigation. This provides a way for people to
provide visual focus styles for keyboard accessible UIs without those styles
appearing if focus is triggered by mouse, touch, pen.
2019-04-29 13:52:15 -07:00

1.3 KiB

Focus

The Focus module responds to focus and blur events on its child. Focus events are dispatched for all input types, with the exception of onFocusVisibleChange which is only dispatched when focusing with a keyboard.

Focus events do not propagate between Focus event responders.

// Example
const Button = (props) => {
  const [ focusVisible, setFocusVisible ] = useState(false);

  return (
    <Focus
      onBlur={props.onBlur}
      onFocus={props.onFocus}
      onFocusVisibleChange={setFocusVisible}
    >
      <button
        children={props.children}
        style={{
          ...(focusVisible && focusVisibleStyles)
        }}
      >
    </Focus>
  );
};

Types

type FocusEvent = {
  target: Element,
  type: 'blur' | 'focus' | 'focuschange' | 'focusvisiblechange'
}

Props

disabled: boolean = false

Disables all Focus events.

onBlur: (e: FocusEvent) => void

Called when the element loses focus.

onFocus: (e: FocusEvent) => void

Called when the element gains focus.

onFocusChange: boolean => void

Called when the element changes focus state (i.e., after onBlur and onFocus).

onFocusVisibleChange: boolean => void

Called when the element receives or loses focus following keyboard navigation. This can be used to display focus styles only for keyboard interactions.