Kino.Control (Kino v0.5.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 form.

Returns a new interval event source.

Creates a new keyboard control.

Returns a Stream of control events.

Subscribes the calling process to control or input events.

Same as stream/1, but attaches custom tag to every stream item.

Unsubscribes the calling process from control or input events.

Link to this section Types

Specs

interval() :: {:interval, milliseconds :: non_neg_integer()}

Specs

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

Link to this section Functions

Specs

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

Creates a new button.

Link to this function

form(fields, opts \\ [])

View Source

Specs

form([{atom(), Kino.Input.t()}], keyword()) :: t()

Creates a new form.

A form is composed of regular inputs from the Kino.Input module, however in a form input values are not synchronized between users. Consequently, the form is another control for producing user-specific events.

Either :submit or :report_changes must be specified.

Options

  • :submit - specifies the label to use for the submit button and enables submit events

  • :report_changes - whether to send new form value whenever any of the input changes. Defaults to false

  • :reset_on_submit - a list of fields to revert to their default values once the form is submitted. Use true to indicate all fields. Defaults to []

Event info

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

  • :type - either :submit or :change

  • :data - a map with field values, matching the field list

Examples

Create a form out of inputs:

form =
  Kino.Control.form(
    [
      name: Kino.Input.text("Name"),
      message: Kino.Input.textarea("Message")
    ],
    submit: "Send"
  )

Subscribe to events:

Kino.Control.subscribe(form, :chat_form)

As users submit the form the payload is sent:

IEx.Helpers.flush()
#=> {:chat_form,
#=>   %{
#=>     data: %{message: "Hola", name: "Amy"},
#=>     origin: #PID<10905.5195.0>,
#=>     type: :submit
#=>   }}
#=> {:chat_form,
#=>   %{
#=>     data: %{message: "Hey!", name: "Jake"},
#=>     origin: #PID<10905.5186.0>,
#=>     type: :submit
#=>   }}

Specs

interval(non_neg_integer()) :: interval()

Returns a new interval event source.

This can be used as event source for stream/1 and tagged_stream/1. The events are emitted periodically with an increasing value, starting from 0 and have the form:

%{type: :interval, iteration: non_neg_integer()}

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, :status])

Subscribe to events:

Kino.Control.subscribe(keyboard, :keyboard)

As the user types events are streamed:

IEx.Helpers.flush()
#=> {:keyboard, %{enabled: true, origin: #PID<10895.9854.0>, type: :status}
#=> {: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

stream(source | [source]) :: Enumerable.t()
when source: t() | Kino.Input.t() | interval()

Returns a Stream of control events.

This is an alternative API to subscribe/2, such that event messages are consume via stream instead of process messages.

It accepts a single source or a list of sources, where each source is either of:

Example

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

for event <- Kino.Control.stream(button) do
  IO.inspect(event)
end
#=> %{origin: #PID<10895.9854.0>, type: :click}
#=> %{origin: #PID<10895.9854.0>, type: :click}

Or with multiple sources:

button = Kino.Control.button("Hello")
input = Kino.Input.checkbox("Check")
interval = Kino.Control.interval(1000)

for event <- Kino.Control.stream([button, input, interval]) do
  IO.inspect(event)
end
#=> %{type: :interval, iteration: 0}
#=> %{origin: #PID<10895.9854.0>, type: :click}
#=> %{origin: #PID<10895.9854.0>, type: :change, value: true}

Specs

subscribe(t() | Kino.Input.t(), term()) :: :ok

Subscribes the calling process to control or input 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.

Same as stream/1, but attaches custom tag to every stream item.

Example

button = Kino.Control.button("Hello")
input = Kino.Input.checkbox("Check")

for event <- Kino.Control.tagged_stream([hello: button, check: input]) do
  IO.inspect(event)
end
#=> {:hello, %{origin: #PID<10895.9854.0>, type: :click}}
#=> {:check, %{origin: #PID<10895.9854.0>, type: :change, value: true}}

Specs

unsubscribe(t() | Kino.Input.t()) :: :ok

Unsubscribes the calling process from control or input events.