Use it where the next step is obvious, such as the first field of a dialog or a code field that has just appeared. The directive has no styles.

import { KuiAutoFocus } from '@kikita-labs/ui';
  • The attribute without a value means true. Focus moves once after the first render when the value is true, and again every time it changes from false to true; a value that stays true never pulls focus back.
  • On a wrapper element the directive focuses the first focusable descendant. kui-otp-input exposes the same behaviour as its own autoFocus input and focuses its first cell.

The directive is conservative about when it moves focus.

  • Browser only. Nothing runs during server rendering and the directive never writes the native autofocus attribute, so the server-rendered page does not move focus before hydration.
  • No tabindex. A target that cannot take focus is skipped. A disabled, hidden or inert host receives no focus, and neither does a host inside an inert subtree.
  • Do not steal. On first render the request yields when the user has already focused something outside the dialog or popover of the target. A later false to true change is an explicit request and always focuses.
  • Dialogs and popovers. While the dialog or popover that holds the target is animating in, focus waits for the animation for at most one second, so the page does not scroll or jump. While enabled, the host also carries cdkFocusInitial, so the focus trap of kuiDialog and kuiDrawer lands on the same element.
  • Cancel on destroy. A request that has not run yet is dropped when the host is destroyed. Use one kuiAutoFocus element per dialog.

There are no outputs, providers or CSS custom properties.

NameTypeDefaultDescription
kuiAutoFocusboolean — Enables focus (default false). The attribute without a value is true, and false to true focuses again.
kuiAutoFocusPreventScrollboolean — Focuses without scrolling the element into view (default false).

Automatic focus can disorient screen-reader users, so keep it off by default.

  • A screen-reader user hears only the label of the focused control and misses the content before it. On touch devices it can open the on-screen keyboard and scroll the page. Use it only where focus on arrival is the expected behaviour, and never on the load of a content page.
  • Moving focus into a modal dialog is already handled by its focus trap; kuiAutoFocus only chooses which element inside it. Use it instead of the native autofocus attribute, which runs once per document, cannot repeat and fires before hydration in a server-rendered page.
  • iOS Safari may not open the on-screen keyboard for focus that does not come from a user gesture, and the directive does not work around that.