OpenUI.Library (OpenUI v0.1.1)

Copy Markdown View Source

A component library: the components, custom functions, custom actions and groups a program may use (prompt.md, section 2).

A library comes from a LibrarySpec document with from_spec/2, or from a module defined with use OpenUI.Library. Either way to_spec/1 gives the LibrarySpec back, ready to share with other clients.

Summary

Functions

Defines a library in a module

Finds a custom action by name.

Finds a component by name.

Reads a LibrarySpec, given as JSON text or as a decoded OpenUI.JSON.Object.

Finds a custom function by name.

Returns the library itself, or the library a module defines with use OpenUI.Library.

Returns the LibrarySpec of a library or of a module that uses OpenUI.Library.

Types

t()

@type t() :: %OpenUI.Library{
  actions: [OpenUI.Library.Callable.t()],
  components: [OpenUI.Library.Component.t()],
  functions: [OpenUI.Library.Callable.t()],
  groups: [OpenUI.Library.Group.t()],
  id: String.t() | nil,
  root: String.t() | nil,
  spec: OpenUI.JSON.Object.t() | nil,
  version: String.t() | nil
}

Functions

__using__(opts)

(macro)

Defines a library in a module:

defmodule MyApp.Library do
  use OpenUI.Library, id: "myapp", version: "1.0.0", root: "Stack"

  component "Stack",
    description: "Lays out children in a column",
    props: [children: [type: {:array, :component}, required: true]]

  function "Percent",
    description: "Part over total as a percentage",
    params: [part: [type: :number, required: true], total: [type: :number, required: true]],
    returns: :string,
    impl: &MyApp.Formulas.percent/1

  action "CopyToClipboard", description: "Copies text", params: [text: [type: :string, required: true]]

  group "Layout", ["Stack"], notes: ["Wrap every page in a Stack."]
end

See OpenUI.Library.DSL for the macros and the prop types.

action(library, name)

@spec action(t(), String.t()) :: OpenUI.Library.Callable.t() | nil

Finds a custom action by name.

component(library, name)

@spec component(t(), String.t()) :: OpenUI.Library.Component.t() | nil

Finds a component by name.

from_spec(spec, opts \\ [])

@spec from_spec(binary() | OpenUI.JSON.Object.t(), keyword()) ::
  {:ok, t()} | {:error, [String.t()]}

Reads a LibrarySpec, given as JSON text or as a decoded OpenUI.JSON.Object.

Custom functions have no implementation in the spec; pass them as functions: %{"Name" => fun}, each fun taking one map keyed by the param names.

function(library, name)

@spec function(t(), String.t()) :: OpenUI.Library.Callable.t() | nil

Finds a custom function by name.

resolve(library)

@spec resolve(t() | module()) :: t()

Returns the library itself, or the library a module defines with use OpenUI.Library.

to_spec(library)

@spec to_spec(t() | module()) :: OpenUI.JSON.Object.t()

Returns the LibrarySpec of a library or of a module that uses OpenUI.Library.