Kino.Control (Kino v0.4.0) View Source

Various widgets for user interactions.

Each widget is a UI control element that the user interacts with, consequenty producing an event stream.

Those widgets are often useful paired with Kino.Frame for presenting content that changes upon user interactions.

Examples

First, create a control and make sure it is rendered, either by placing it at the end of a code cell or by explicitly rendering it with Kino.render/1.

button = Kino.Control.button("Hello")

Next, to receive events from the control, a process needs to subscribe to it and specify pick a name to distinguish the events.

Kino.Control.subscribe(button, :hello)

As the user interacts with the button, the subscribed process receives corresponding events.

IEx.Helpers.flush()
#=> {:hello, %{origin: #PID<10895.9854.0>}}
#=> {:hello, %{origin: #PID<10895.9854.0>}}

Link to this section Summary

Functions

Creates a new button.

Creates a new keyboard control.

Subscribes the calling process to control events.

Unsubscribes the calling process from control events.

Link to this section Types

Specs

t() :: %Kino.Control{attrs: Kino.Output.control_attrs()}

Link to this section Functions

Specs

button(String.t()) :: t()

Creates a new button.

Specs

keyboard([:keyup | :keydown | :status]) :: t()

Creates a new keyboard control.

This widget is represented as button that toggles interception mode, in which the given keyboard events are captured.

Event info

In addition to standard properties, all events include additional properties.

Key events

  • :type - either :keyup or :keydown

  • :key - the value matching the browser KeyboardEvent.key

Status event

  • :type - either :status

  • :enabled - whether the keyboard is activated

Examples

Create the widget:

keyboard = Kino.Control.keyboard([:keyup, :keydown])

Subscribe to events:

Kino.Control.subscribe(keyboard, :keyboard)

As the user types events are streamed:

IEx.Helpers.flush()
#=> {:keyboard, %{key: "o", origin: #PID<10895.9854.0>, type: :keydown}}
#=> {:keyboard, %{key: "k", origin: #PID<10895.9854.0>, type: :keydown}}
#=> {:keyboard, %{key: "o", origin: #PID<10895.9854.0>, type: :keyup}}
#=> {:keyboard, %{key: "k", origin: #PID<10895.9854.0>, type: :keyup}}

Specs

subscribe(t(), term()) :: :ok

Subscribes the calling process to control events.

The events are sent as {tag, info}, where info is a map with event details. In particular, it always includes :origin, which is an opaque identifier of the client that triggered the event.

Specs

unsubscribe(t()) :: :ok

Unsubscribes the calling process from control events.