Primitives / foldkit-primitives/interaction

Press

namespace · packages/primitives/src/interaction/press.ts:10

export * as Press from './press.js'

Press: pointer and keyboard activation of one element as one fact. A Mount reports what the element saw (pointer down, up, cancel, key down, up, click) with the detail Foldkit's declarative attributes do not carry; the Bundle's update decides what counts as a press; the ghost click that follows a touch is suppressed by a timed Command on Effect's clock, never by a clock read in a handler. Activation is an OutMessage, Pressed, so the placement must say what a press does.

In the guides

Members

Press.Args

const · packages/primitives/src/interaction/press.ts:64

const Args: Schema.Struct<{ readonly clickSuppressionMs: Schema.Number }>

Press.behavior

function · packages/primitives/src/interaction/press.ts:290

function behavior<Field extends string>(
  declared: Declared<typeof bundle, Field>,
): {
  <Slots, Input extends { readonly [K in Field]: Model }, ParentMessage>(
    bound: SlotView.Bound<Slots, Input, ParentMessage>,
  ): <Target extends keyof Slots & string>(
    options: BehaviorOptions<Input, Slots, Target>,
  ) => Behavior.NamedBehavior<
    Slots,
    Input,
    ParentMessage,
    A11y.ProvidedOn<Target, 'activation'>
  >
  <Slots>(
    slots: SlotView.Unbound<Slots>,
  ): <Input extends { readonly [K in Field]: Model }, ParentMessage>(
    options: BehaviorOptions<Input, Slots>,
  ) => Behavior.NamedBehavior<Slots, Input, ParentMessage>
}

Attaches the events Mount to the target slot, mapped into the placement's Messages, and writes data-pressed while down for styling. The element decides nothing; a click, a touch, Enter, Space, or an assistive technology's click all reach update and come out as one Pressed.

Press.BehaviorOptions

interface · packages/primitives/src/interaction/press.ts:274

interface BehaviorOptions<
  Input,
  Slots,
  Target extends keyof Slots & string = keyof Slots & string,
> {
  readonly target: Target
  readonly disabled?: (input: Input) => boolean
}

Press.bundle

const · packages/primitives/src/interaction/press.ts:82

const bundle: Bundle.Bundle<
  'Press',
  { readonly clickSuppressionMs: number },
  {
    readonly pressed: boolean
    readonly pointerId: number | null
    readonly key: string | null
    readonly suppressing: number | null
    readonly generation: number
  },
  | {
      readonly _tag: 'Clicked'
      readonly detail: number
      readonly modifiers: {
        readonly shiftKey: boolean
        readonly ctrlKey: boolean
        readonly metaKey: boolean
        readonly altKey: boolean
      }
    }
  | {
      readonly _tag: 'PointerDown'
      readonly pointerId: number
      readonly button: number
      readonly pointerType: string
    }
  | {
      readonly _tag: 'PointerUp'
      readonly pointerId: number
      readonly pointerType: string
      readonly modifiers: {
        readonly shiftKey: boolean
        readonly ctrlKey: boolean
        readonly metaKey: boolean
        readonly altKey: boolean
      }
    }
  | { readonly _tag: 'PointerCancelled'; readonly pointerId: number }
  | { readonly _tag: 'KeyDown'; readonly key: string; readonly repeat: boolean }
  | {
      readonly _tag: 'KeyUp'
      readonly key: string
      readonly modifiers: {
        readonly shiftKey: boolean
        readonly ctrlKey: boolean
        readonly metaKey: boolean
        readonly altKey: boolean
      }
    }
  | { readonly _tag: 'KeyCancelled'; readonly key: string }
  | { readonly _tag: 'Unsuppressed'; readonly generation: number },
  {
    readonly _tag: 'Pressed'
    readonly pointerType: 'keyboard' | 'mouse' | 'pen' | 'touch' | 'virtual'
    readonly modifiers: {
      readonly shiftKey: boolean
      readonly ctrlKey: boolean
      readonly metaKey: boolean
      readonly altKey: boolean
    }
  },
  never,
  never,
  NoView,
  {},
  {},
  undefined
>

Press.events

const · packages/primitives/src/interaction/press.ts:165

const events: Mount.MountDefinitionNoArgs<
  'PressEvents',
  | {
      readonly _tag: 'Clicked'
      readonly detail: number
      readonly modifiers: {
        readonly shiftKey: boolean
        readonly ctrlKey: boolean
        readonly metaKey: boolean
        readonly altKey: boolean
      }
    }
  | {
      readonly _tag: 'PointerDown'
      readonly pointerId: number
      readonly button: number
      readonly pointerType: string
    }
  | {
      readonly _tag: 'PointerUp'
      readonly pointerId: number
      readonly pointerType: string
      readonly modifiers: {
        readonly shiftKey: boolean
        readonly ctrlKey: boolean
        readonly metaKey: boolean
        readonly altKey: boolean
      }
    }
  | { readonly _tag: 'PointerCancelled'; readonly pointerId: number }
  | { readonly _tag: 'KeyDown'; readonly key: string; readonly repeat: boolean }
  | {
      readonly _tag: 'KeyUp'
      readonly key: string
      readonly modifiers: {
        readonly shiftKey: boolean
        readonly ctrlKey: boolean
        readonly metaKey: boolean
        readonly altKey: boolean
      }
    }
  | { readonly _tag: 'KeyCancelled'; readonly key: string }
>

The element's press-relevant events as facts. Enter and Space key downs are default-prevented so a native control does not also click, and Space does not scroll; the Bundle owns activation. An element marked aria-disabled="true" (which the Behavior writes) refuses new presses, read at event time so a toggle needs no remount. A press that began while it was enabled still ends: a release or a key up after it disabled is reported as a cancel, so a stepper that disables itself at its bound mid-hold is not left held. Nothing else is decided here.

Press.Fact

type · packages/primitives/src/interaction/press.ts:153

type Fact = Exclude<
  Message,
  {
    readonly _tag: 'Unsuppressed'
  }
>

What the element reports; Unsuppressed comes from the timer, not the DOM.

Press.Message

const · packages/primitives/src/interaction/press.ts:43

const Message: MessageUnion<{
  readonly PointerDown: {
    readonly pointerId: Schema.Number
    readonly button: Schema.Number
    readonly pointerType: Schema.String
  }
  readonly PointerUp: {
    readonly pointerId: Schema.Number
    readonly pointerType: Schema.String
    readonly modifiers: Schema.Struct<{
      readonly shiftKey: Schema.Boolean
      readonly ctrlKey: Schema.Boolean
      readonly metaKey: Schema.Boolean
      readonly altKey: Schema.Boolean
    }>
  }
  readonly PointerCancelled: { readonly pointerId: Schema.Number }
  readonly KeyDown: {
    readonly key: Schema.String
    readonly repeat: Schema.Boolean
  }
  readonly KeyUp: {
    readonly key: Schema.String
    readonly modifiers: Schema.Struct<{
      readonly shiftKey: Schema.Boolean
      readonly ctrlKey: Schema.Boolean
      readonly metaKey: Schema.Boolean
      readonly altKey: Schema.Boolean
    }>
  }
  readonly KeyCancelled: { readonly key: Schema.String }
  readonly Clicked: {
    readonly detail: Schema.Number
    readonly modifiers: Schema.Struct<{
      readonly shiftKey: Schema.Boolean
      readonly ctrlKey: Schema.Boolean
      readonly metaKey: Schema.Boolean
      readonly altKey: Schema.Boolean
    }>
  }
  readonly Unsuppressed: { readonly generation: Schema.Number }
}>

Press.Model

const · packages/primitives/src/interaction/press.ts:30

const Model: Schema.Struct<{
  readonly pressed: Schema.Boolean
  readonly pointerId: Schema.NullOr<Schema.Number>
  readonly key: Schema.NullOr<Schema.String>
  readonly suppressing: Schema.NullOr<Schema.Number>
  readonly generation: Schema.Number
}>

Press.Modifiers

const · packages/primitives/src/interaction/press.ts:22

const Modifiers: Schema.Struct<{
  readonly shiftKey: Schema.Boolean
  readonly ctrlKey: Schema.Boolean
  readonly metaKey: Schema.Boolean
  readonly altKey: Schema.Boolean
}>

The keys held when a press completed, as the pointer, key or click event reported them.

Press.PointerType

const · packages/primitives/src/interaction/press.ts:18

const PointerType: Schema.Literals<
  readonly ['mouse', 'touch', 'pen', 'keyboard', 'virtual']
>

Press.Pressed

const · packages/primitives/src/interaction/press.ts:57

const Pressed: Schema.TaggedStruct<
  'Pressed',
  {
    readonly pointerType: Schema.Literals<
      readonly ['mouse', 'touch', 'pen', 'keyboard', 'virtual']
    >
    readonly modifiers: Schema.Struct<{
      readonly shiftKey: Schema.Boolean
      readonly ctrlKey: Schema.Boolean
      readonly metaKey: Schema.Boolean
      readonly altKey: Schema.Boolean
    }>
  }
>