Primitives / foldkit-primitives/interaction

FocusRecovery

namespace · packages/primitives/src/interaction/focus-recovery.ts:7

export * as FocusRecovery from './focus-recovery.js'

FocusRecovery as a Behavior: marks an owner slot and the part slots whose focus it recovers, and attaches the foldkit-primitives/dom Mount to the owner. No Bundle, because where focus is is a DOM fact; the part's own OnFocus keeps the Model's current in step.

In the guides

Members

FocusRecovery.behavior

function · packages/primitives/src/interaction/focus-recovery.ts:46

function behavior<Slots, Input, ParentMessage>(
  bound: SlotView.Bound<Slots, Input, ParentMessage>,
): <Part extends string = string>(
  options: BehaviorOptions<Slots, Input, Part>,
) => Behavior.NamedBehavior<Slots, Input, ParentMessage>
function behavior<Slots>(
  slots: SlotView.Unbound<Slots>,
): <Input, ParentMessage, Part extends string = string>(
  options: BehaviorOptions<Slots, Input, Part>,
) => Behavior.NamedBehavior<Slots, Input, ParentMessage>

Focus that was on a part goes to the part's first fallback step that can take it when that part is removed, hidden, made inert or disabled; focus that was moved elsewhere is never taken. The marks are drawn by the view (data-fk-focus-owner, data-fk-focus-part, and for a slot drawn per item data-fk-focus-key from item.id), so a server render has them.

FocusRecovery.BehaviorOptions

interface · packages/primitives/src/interaction/focus-recovery.ts:21

interface BehaviorOptions<Slots, Input, Part extends string = string> {
  readonly owner: keyof Slots & string
  readonly parts: {
    readonly [P in Part]?: Steps<NoInfer<Part>>
  }
  readonly parent?: (input: Input, item: SlotItem) => Option.Option<string>
}

FocusRecovery.FOCUS_MARK

const · packages/primitives/src/dom/focusable.ts:34

const FOCUS_MARK = { owner, part, key, parent }

The data- names (without the prefix) that mark focus owners and their parts.

FocusRecovery.mount

const · packages/primitives/src/dom/focus-recovery.ts:179

const mount: Mount.MountDefinitionWithArgs<
  'FocusRecovery',
  {
    fallback: Schema.$Record<
      Schema.String,
      Schema.$Array<
        Schema.Union<
          readonly [
            Schema.Literals<
              readonly ['same', 'next', 'previous', 'parent', 'stop', 'owner']
            >,
            Schema.Struct<{ readonly part: Schema.String }>,
          ]
        >
      >
    >
  },
  never
>

Recovers focus for a container of parts, as a Mount. While focus is on a marked part (FocusRecovery.behavior draws the marks), the container remembers it; when that element is removed, hidden, made inert or disabled and focus is lost with it, focus goes to the first element the part's fallback steps find, in the same task as the change and before the browser's own fix-up. Nothing is done when focus has moved elsewhere, and nested containers decide once: the innermost one, then outward through foldkit-plus:focus-unrecovered. It sends no Message: the part's OnFocus tells the Model where focus went.

FocusRecovery.Steps

type · packages/primitives/src/interaction/focus-recovery.ts:17

type Steps<Part extends string> = ReadonlyArray<
  | Exclude<
      Step,
      {
        readonly part: string
      }
    >
  | {
      readonly part: Part
    }
>

A step in part terms: { part } names a key of parts.

FocusRecovery.UNRECOVERED

const · packages/primitives/src/dom/focus-recovery.ts:14

const UNRECOVERED: 'foldkit-plus:focus-unrecovered'

Dispatched, bubbling, from an owner that found no step to take: the next owner out decides.