Styleframe Logo
States

Focus State

Apply consistent focus-visible outline styles across all elements with configurable color, width, style, and offset using the useFocusState composable.
Part of the Global Preset:useFocusState is included in the Global Preset (useGlobalPreset) and you can configure it through the preset's focus option. For most projects, applying it via the preset is the recommended approach.

Overview

The useFocusState composable applies a consistent :focus-visible outline to all focusable elements. It creates CSS custom properties for the outline color, width, style, and offset, making focus indicators easy to customize per-theme.

useFocusState

function useFocusState(
    s: Styleframe,
    options?: WithThemes<FocusStateConfig>
): FocusStateResult
styleframe.config.ts
import { styleframe } from 'styleframe';
import { useFocusState } from '@styleframe/theme';

const s = styleframe();

const {
    focusOutlineColor,
    focusOutlineWidth,
    focusOutlineStyle,
    focusOutlineOffset,
} = useFocusState(s);

export default s;

Default Values

OptionDefaultVariableExport
outlineColor"@color.primary"--focus--outline-colorfocusOutlineColor
outlineWidth"2px"--focus--outline-widthfocusOutlineWidth
outlineStyle"solid"--focus--outline-stylefocusOutlineStyle
outlineOffset"2px"--focus--outline-offsetfocusOutlineOffset

Custom Values

styleframe.config.ts
import { styleframe } from 'styleframe';
import { useFocusState } from '@styleframe/theme';

const s = styleframe();

const { focusOutlineColor, focusOutlineWidth } = useFocusState(s, {
    outlineColor: '@color.info',
    outlineWidth: '3px',
    outlineOffset: '3px',
});

export default s;

Best Practices

  • Use :focus-visible over :focus: The composable targets :focus-visible to show outlines only for keyboard navigation, not mouse clicks.
  • Keep outlines visible: Use your primary or info color for outlines to meet WCAG 2.1 focus indicator requirements.
  • Add offset for spacing: The default 2px offset prevents the outline from overlapping element borders.
  • Customize per-theme: Use theme overrides to adjust outline color for dark mode, ensuring focus indicators remain visible against darker backgrounds.