vleam/vue

Types

A Vue App

pub type App

A Vue Component

pub type Component

An incomplete component representation piped during component definition. See define_component for a full example.

pub type ComponentBase(props, emits)

A vue computed

pub type Computed(value)

DebuggerEvent returned from Vue

pub type DebuggerEvent

A vue directive

pub type Directive

NullableComputed auto (un)wraps Option This is a convenience for using Computed(Option) in a template

pub type NullableComputed(value)

NullableRef auto (un)wraps Option This is a convenience for using Ref(Option) in a template

pub type NullableRef(value)

Shallow version of NullableRef

pub type NullableShallowRef(value)

Prop record to be used on component definition. See define_component for a full example.

pub type Prop(value) {
  Prop(name: String, default: Option(value))
}

Constructors

  • Prop(name: String, default: Option(value))

A vue ref

pub type Ref(value)

A vue shallowRef

pub type ShallowRef(value)
pub type VueError {
  ComponentNotFound
}

Constructors

  • ComponentNotFound

Watchable types

pub type Watchable(value) {
  Ref(watchable: Ref(value))
  ShallowRef(watchable: ShallowRef(value))
  NullableRef(watchable: NullableRef(value))
  NullableShallowRef(watchable: NullableShallowRef(value))
  Computed(watchable: Computed(value))
  NullableComputed(watchable: NullableComputed(value))
  Function(watchable: fn() -> value)
  Plain(watchable: value)
}

Constructors

  • Ref(watchable: Ref(value))
  • ShallowRef(watchable: ShallowRef(value))
  • NullableRef(watchable: NullableRef(value))
  • NullableShallowRef(watchable: NullableShallowRef(value))
  • Computed(watchable: Computed(value))
  • NullableComputed(watchable: NullableComputed(value))
  • Function(watchable: fn() -> value)
  • Plain(watchable: value)

Functions

pub fn computed(value: fn() -> a) -> Computed(a)

Define computed

pub fn computed_value(computed: Computed(a)) -> a

Get computed value

pub fn define_component(
  components components: List(#(String, fn() -> Component)),
  directives directives: List(#(String, fn() -> Directive)),
  inherit_attrs inherit_attrs: Bool,
) -> ComponentBase(Nil, Nil)

Entrypoint for component definition. optionally piped to a with_n_props and/or with_emits functions, and must be finally piped to setup.

components is a list of #(component_name, fn() -> Component) tuples. There are three possible ways to gain access to components:

  1. Using @external to refer to a component defined in Javascript
  2. Importing a component defined in Gleam using this library
  3. Using them without the need to refer them in define_component, either by gloabl definition with vue.component or by an auto-import mechanism similar to Nuxt’s

directives is a list of #(directive_name, fn() -> Directive) tuples.

inherit_attrs is a boolean, identical to Vue’s javascript configuration.

Example

import gleam/option.{None, Some}
import gleam/string
import vleam/vue.{
  type Component, Prop, define_component, setup, with_2_props,
}

import vleam_todo/models.{type Todo, type TodoError}

type InputEvent

// Import the default export from HelloWorld.vue
// This is technically an incorrect type, but it'll work. C'est la vie.
@external(javascript, "/src/components/HelloWorld.vue", "default")
fn hello_world_component() -> Component

// Also works for external packages, although @ sign has to be aliased
@external(javascript, "heroicons/vue/24/solid", "UserIcon")
fn user_icon() -> Component

// or you can predefine in TS/JS
@external(javascript, "/src/ffi.ts", "CheckIcon")
fn check_icon() -> #(String, fn() -> Component)

pub fn default_export() -> Component {
  define_component(
    [
      #("HelloWorld", hello_world_component),
      #("UserIcon", user_icon),
      check_icon(),
    ],
    [],
    False,
  )
  |> with_2_props(#(
    // No default value, therefore mandatory
    Prop("initialName", None),
    // Default value, therefore optional
    Prop("greeting", Some("Hello, ")),
  ))
  // Props are handed as Computed for reactivity. It's best practice to
  // always type them, as types are only inferred if there's a default value.
  |> setup(fn(props: #(Computed(String), Computed(String)), _) {
    let initial_name = props.0
    let greeting = props.1

    // Errors are thrown. This is just a demo, don't throw on bad input,
    // only on irrecoverable errors.
    use <- bool.guard(
      {
        greeting
        |> vue.computed_value
        |> string.length
      }
      > 0,
      Error("Empty greeting"),
    )

    let change_count = vue.shallow_ref(0)
    let increment_count = fn() {
      change_count
      |> vue.shallow_ref_set(vue.shallow_ref_value(change_count) + 1)
    }

    let name =
      initial_name
      |> vue.computed_value
      |> vue.shallow_ref

    let change_name = fn(new_name) {
      name
      |> vue.shallow_ref_set(new_name)
    }
    let full_greeting =
      vue.computed(fn() {
        name
        |> vue.shallow_ref_value
        |> string.append(
          greeting
          |> vue.computed_value
        )

        increment_count()
      })

    // To return values to the template, the FFI expects an Ok() with a
    // tuple of object entires tuples. The following is identical to Vue's:
    //
    // return {
    //   fullGreeting: full_greeting,
    //   changeName: change_name,
    //   changeCount: change_count,
    // };
    Ok(#(
      #("fullGreeting", full_greeting),
      #("changeName", change_name),
      #("changeCount", change_count),
    ))
  })
}
pub fn get_component(
  vue_app: App,
  component_name: String,
) -> Result(Component, VueError)

Returns components that were registered using app.component

pub fn inject(key: String) -> Option(a)
pub fn inject_with_default(key: String, default: a) -> a
pub fn inject_with_factory(key: String, factory: fn() -> a) -> a
pub fn next_tick() -> Promise(Nil)

vue.nextTick() https://vuejs.org/api/general.html#nexttick

pub fn next_tick_action(callback: fn() -> Nil) -> Promise(Nil)
pub fn nullable_computed(
  callback: fn() -> Option(a),
) -> NullableComputed(a)
pub fn nullable_computed_value(
  ref: NullableComputed(a),
) -> Option(a)
pub fn nullable_ref(value: Option(a)) -> NullableRef(a)
pub fn nullable_ref_set(
  ref: NullableRef(a),
  new_value: Option(a),
) -> NullableRef(a)
pub fn nullable_ref_value(ref: NullableRef(a)) -> Option(a)
pub fn nullable_shallow(
  value: Option(a),
) -> NullableShallowRef(a)
pub fn nullable_shallow_set(
  ref: NullableShallowRef(a),
  new_value: Option(a),
) -> NullableShallowRef(a)
pub fn nullable_shallow_value(
  ref: NullableShallowRef(a),
) -> Option(a)
pub fn on_activated(handler: fn() -> Nil) -> Nil
pub fn on_before_mount(handler: fn() -> Nil) -> Nil
pub fn on_before_unmount(handler: fn() -> Nil) -> Nil
pub fn on_before_update(handler: fn() -> Nil) -> Nil
pub fn on_deactivated(handler: fn() -> Nil) -> Nil
pub fn on_mounted(handler: fn() -> Nil) -> Nil
pub fn on_unmounted(handler: fn() -> Nil) -> Nil
pub fn on_updated(handler: fn() -> Nil) -> Nil
pub fn provide(key: String, value: a) -> Nil
pub fn ref(initial_value: a) -> Ref(a)

Define a ref

pub fn ref_set(ref: Ref(a), value: a) -> Ref(a)

Set ref value

pub fn ref_value(value: Ref(a)) -> a

Get ref value

pub fn register_component(
  vue_app: App,
  component_name: String,
  component: Component,
) -> App

Registers components using app.component

pub fn setup(
  base: ComponentBase(a, b),
  setup: fn(a, b) -> c,
) -> Component

Component setup function See define_component for a full example.

pub fn shallow_ref(value: a) -> ShallowRef(a)

Define a shallow ref

pub fn shallow_ref_set(ref: ShallowRef(a), value: a) -> Ref(a)

Set shallow ref value

pub fn shallow_ref_value(a: ShallowRef(a)) -> a

Get shallow ref value

pub fn trigger_nullable_shallow(
  ref: NullableShallowRef(a),
) -> Nil
pub fn trigger_ref(ref: ShallowRef(a)) -> Nil

Trigger a shallow ref

pub fn watch1(
  reflikes reflikes: #(Watchable(a)),
  callback callback: fn(a, a, Option(fn(fn() -> Nil) -> Nil)) ->
    Nil,
) -> Nil
pub fn watch1_with_options(
  reflikes reflikes: #(Watchable(a)),
  callback callback: fn(a, a, Option(fn(fn() -> Nil) -> Nil)) ->
    Nil,
  immediate immediate: Option(Bool),
  deep deep: Option(Bool),
  flush flush: Option(String),
  on_track on_track: Option(fn(DebuggerEvent) -> Nil),
  on_trigger on_trigger: Option(fn(DebuggerEvent) -> Nil),
  once once: Option(Bool),
) -> Nil
pub fn watch2(
  reflikes reflikes: #(Watchable(a), Watchable(b)),
  callback callback: fn(
    #(Watchable(a), Watchable(b)),
    #(Watchable(a), Watchable(b)),
    Option(fn(fn() -> Nil) -> Nil),
  ) -> Nil,
) -> Nil
pub fn watch2_with_options(
  reflikes reflikes: #(Watchable(a), Watchable(b)),
  callback callback: fn(
    #(Watchable(a), Watchable(b)),
    #(Watchable(a), Watchable(b)),
    Option(fn(fn() -> Nil) -> Nil),
  ) -> Nil,
  immediate immediate: Option(Bool),
  deep deep: Option(Bool),
  flush flush: Option(String),
  on_track on_track: Option(fn(DebuggerEvent) -> Nil),
  on_trigger on_trigger: Option(fn(DebuggerEvent) -> Nil),
  once once: Option(Bool),
) -> Nil
pub fn watch3(
  reflikes reflikes: #(Watchable(a), Watchable(b), Watchable(c)),
  callback callback: fn(
    #(Watchable(a), Watchable(b), Watchable(c)),
    #(Watchable(a), Watchable(b), Watchable(c)),
    Option(fn(fn() -> Nil) -> Nil),
  ) -> Nil,
) -> Nil
pub fn watch3_with_options(
  reflikes reflikes: #(Watchable(a), Watchable(b), Watchable(c)),
  callback callback: fn(
    #(Watchable(a), Watchable(b), Watchable(c)),
    #(Watchable(a), Watchable(b), Watchable(c)),
    Option(fn(fn() -> Nil) -> Nil),
  ) -> Nil,
  immediate immediate: Option(Bool),
  deep deep: Option(Bool),
  flush flush: Option(String),
  on_track on_track: Option(fn(DebuggerEvent) -> Nil),
  on_trigger on_trigger: Option(fn(DebuggerEvent) -> Nil),
  once once: Option(Bool),
) -> Nil
pub fn watch4(
  reflikes reflikes: #(
    Watchable(a),
    Watchable(b),
    Watchable(c),
    Watchable(d),
  ),
  callback callback: fn(
    #(Watchable(a), Watchable(b), Watchable(c), Watchable(d)),
    #(Watchable(a), Watchable(b), Watchable(c), Watchable(d)),
    Option(fn(fn() -> Nil) -> Nil),
  ) -> Nil,
) -> Nil
pub fn watch4_with_options(
  reflikes reflikes: #(
    Watchable(a),
    Watchable(b),
    Watchable(c),
    Watchable(d),
  ),
  callback callback: fn(
    #(Watchable(a), Watchable(b), Watchable(c), Watchable(d)),
    #(Watchable(a), Watchable(b), Watchable(c), Watchable(d)),
    Option(fn(fn() -> Nil) -> Nil),
  ) -> Nil,
  immediate immediate: Option(Bool),
  deep deep: Option(Bool),
  flush flush: Option(String),
  on_track on_track: Option(fn(DebuggerEvent) -> Nil),
  on_trigger on_trigger: Option(fn(DebuggerEvent) -> Nil),
  once once: Option(Bool),
) -> Nil
pub fn watch5(
  reflikes reflikes: #(
    Watchable(a),
    Watchable(b),
    Watchable(c),
    Watchable(d),
    Watchable(e),
  ),
  callback callback: fn(
    #(
      Watchable(a),
      Watchable(b),
      Watchable(c),
      Watchable(d),
      Watchable(e),
    ),
    #(
      Watchable(a),
      Watchable(b),
      Watchable(c),
      Watchable(d),
      Watchable(e),
    ),
    Option(fn(fn() -> Nil) -> Nil),
  ) -> Nil,
) -> Nil
pub fn watch5_with_options(
  reflikes reflikes: #(
    Watchable(a),
    Watchable(b),
    Watchable(c),
    Watchable(d),
    Watchable(e),
  ),
  callback callback: fn(
    #(
      Watchable(a),
      Watchable(b),
      Watchable(c),
      Watchable(d),
      Watchable(e),
    ),
    #(
      Watchable(a),
      Watchable(b),
      Watchable(c),
      Watchable(d),
      Watchable(e),
    ),
    Option(fn(fn() -> Nil) -> Nil),
  ) -> Nil,
  immediate immediate: Option(Bool),
  deep deep: Option(Bool),
  flush flush: Option(String),
  on_track on_track: Option(fn(DebuggerEvent) -> Nil),
  on_trigger on_trigger: Option(fn(DebuggerEvent) -> Nil),
  once once: Option(Bool),
) -> Nil
pub fn with_1_prop(
  component: ComponentBase(a, b),
  props: #(Prop(c)),
) -> ComponentBase(#(Computed(c)), b)

Define 1 prop on a component See define_component for a full example.

pub fn with_2_props(
  component: ComponentBase(a, b),
  props: #(Prop(c), Prop(d)),
) -> ComponentBase(#(Computed(c), Computed(d)), b)

Define 2 props on a component See define_component for a full example.

pub fn with_3_props(
  component: ComponentBase(a, b),
  props: #(Prop(c), Prop(d), Prop(e)),
) -> ComponentBase(#(Computed(c), Computed(d), Computed(e)), b)

Define 3 props on a component See define_component for a full example.

pub fn with_4_props(
  component: ComponentBase(a, b),
  props: #(Prop(c), Prop(d), Prop(e), Prop(f)),
) -> ComponentBase(
  #(Computed(c), Computed(d), Computed(e), Computed(f)),
  b,
)

Define 4 props on a component See define_component for a full example.

pub fn with_5_props(
  component: ComponentBase(a, b),
  props: #(Prop(c), Prop(d), Prop(e), Prop(f), Prop(g)),
) -> ComponentBase(
  #(
    Computed(c),
    Computed(d),
    Computed(e),
    Computed(f),
    Computed(g),
  ),
  b,
)

Define 5 props on a component See define_component for a full example.

pub fn with_6_props(
  component: ComponentBase(a, b),
  props: #(Prop(c), Prop(d), Prop(e), Prop(f), Prop(g), Prop(h)),
) -> ComponentBase(
  #(
    Computed(c),
    Computed(d),
    Computed(e),
    Computed(f),
    Computed(g),
    Computed(h),
  ),
  b,
)

Define 6 props on a component See define_component for a full example.

pub fn with_7_props(
  component: ComponentBase(a, b),
  props: #(
    Prop(c),
    Prop(d),
    Prop(e),
    Prop(f),
    Prop(g),
    Prop(h),
    Prop(i),
  ),
) -> ComponentBase(
  #(
    Computed(c),
    Computed(d),
    Computed(e),
    Computed(f),
    Computed(g),
    Computed(h),
    Computed(i),
  ),
  b,
)

Define 7 props on a component See define_component for a full example.

pub fn with_8_props(
  component: ComponentBase(a, b),
  props: #(
    Prop(c),
    Prop(d),
    Prop(e),
    Prop(f),
    Prop(g),
    Prop(h),
    Prop(i),
    Prop(j),
  ),
) -> ComponentBase(
  #(
    Computed(c),
    Computed(d),
    Computed(e),
    Computed(f),
    Computed(g),
    Computed(h),
    Computed(i),
    Computed(j),
  ),
  b,
)

Define 8 props on a component See define_component for a full example.

pub fn with_9_props(
  component: ComponentBase(a, b),
  props: #(
    Prop(c),
    Prop(d),
    Prop(e),
    Prop(f),
    Prop(g),
    Prop(h),
    Prop(i),
    Prop(j),
    Prop(k),
  ),
) -> ComponentBase(
  #(
    Computed(c),
    Computed(d),
    Computed(e),
    Computed(f),
    Computed(g),
    Computed(h),
    Computed(i),
    Computed(j),
    Computed(k),
  ),
  b,
)

Define 9 props on a component See define_component for a full example.

pub fn with_emits(
  base: ComponentBase(a, b),
  emits: List(String),
) -> ComponentBase(a, c)

Define emits on a component

Search Document