Documentation navigation

Published API · 0.1.0-rc.1

foldkit-ui · switch

Published foldkit-ui API declarations for switch.

./switch

@wsagency/wseffect-foldkit-ui · MIT · source · MIT license · WS-AGENCY-NOTICE · THIRD-PARTY-NOTICES

This entry point is published in @wsagency/wseffect-foldkit-ui@0.1.0-rc.1. Import it from the package subpath shown below; the signatures and documentation are read from the installed package declarations.

import * as Wseffect from '@wsagency/wseffect-foldkit-ui/switch'

Exported declarations

descriptionId

Returns the description element id, derived from the switch's base id.

descriptionId: (id: string) => string

labelId

Returns the label element id, derived from the switch's base id.

labelId: (id: string) => string

SwitchAttributes

Attribute groups the switch provides to the consumer's `toView` callback. Each group is a `ReadonlyArray<Attribute<Message>>` the consumer spreads into its own element attribute arrays. The `button` and `label` bundles carry the click and Space handlers that dispatch the configured `onToggle` Message. The `button` bundle sets `type="button"` so that rendering the switch as a `button` element inside a `form` element toggles without also submitting the form. Setting it is harmless on the other elements a switch might use, such as a `div` or a `span`, because the builder assigns a DOM property rather than an HTML attribute. Spread a later `h.Type` to override it.

export type SwitchAttributes<Message> = Readonly<{
    button: ReadonlyArray<Attribute<Message>>;
    label: ReadonlyArray<Attribute<Message>>;
    description: ReadonlyArray<Attribute<Message>>;
    hiddenInput: ReadonlyArray<Attribute<Message>>;
}>;

view

Renders an accessible switch as a stateless controlled component. The parent owns the checked state (`isChecked`) and receives the new state via `onToggle` when the user toggles it. ```ts // In view: Switch.view( { id: 'notifications', isChecked: model.notificationsEnabled, onToggle: isChecked => ToggledNotifications({ isChecked }), toView: attributes => ..., }, h, ) // In update: ToggledNotifications: ({ isChecked }) => ({ model: modifyFields(model, { notificationsEnabled: () => isChecked }), }), ```

view: <Message>(config: ViewConfig<Message>, h: HtmlBuilder<Message>) => Html

ViewConfig

export type ViewConfig<Message> = Readonly<{
    id: string;
    isChecked: boolean;
    onToggle: (isChecked: boolean) => Message;
    toView: (attributes: SwitchAttributes<Message>) => Html;
    isDisabled?: boolean;
    isReadOnly?: boolean;
    hasDescription?: boolean;
    name?: string;
    value?: string;
}>;