package vdom

  1. Overview
  2. Docs

Module VdomSource

Virtual DOM

A "Virtual application" (or "Elm-like" application) is described by two types:

  • model: the current state of the UI,
  • msg ("messages"): possible state transitions;

and by the following values:

  • init: the initial state of the application, plus some initial commands to be spawned;
  • view: a function mapping the current state to a "virtual" DOM tree (vdom);
  • update: a function that processes messages to update the current state, and potentially spawns some commands.

Commands represents (typically) asynchronous operations, such as querying a server, or waiting for some timer.

Messages can be generated either by a VDOM tree (to encapsulate DOM events) or by commands (to notify their outcome).

Commands

Sourcemodule Cmd : sig ... end
Sourcetype js_object = ..
Sourcemodule Decoder : sig ... end

Custom elements

Sourcemodule Custom : sig ... end

Properties and event handlers

Sourcetype mouse_event = {
  1. x : float;
  2. y : float;
  3. page_x : float;
  4. page_y : float;
  5. element_x : float Lazy.t;
  6. element_y : float Lazy.t;
  7. buttons : int;
  8. alt_key : bool;
  9. ctrl_key : bool;
  10. shift_key : bool;
}
Sourcetype key_event = {
  1. which : int;
  2. alt_key : bool;
  3. ctrl_key : bool;
  4. shift_key : bool;
}
Sourcetype paste_event = {
  1. text : string;
  2. selection_start : int;
  3. selection_end : int;
}
Sourcetype 'msg msg_options = {
  1. msg : 'msg option;
  2. stop_propagation : bool;
  3. prevent_default : bool;
}
Sourcetype +'msg event_handler =
  1. | Decoder : {
    1. event_type : string;
    2. decoder : 'a msg_options Decoder.t;
    3. map : 'a option -> 'msg option;
    } -> 'msg event_handler
  2. | CustomEvent of Custom.event -> 'msg option
Sourcetype prop_val =
  1. | String of string
  2. | Int of int
  3. | Float of float
  4. | Bool of bool
Sourcetype 'msg attribute =
  1. | Property of string * prop_val
  2. | Style of string * string
  3. | Handler of 'msg event_handler
  4. | Attribute of string * string

Event handlers

Sourceval on : ?prevent_default:unit -> ?stop_propagation:unit -> string -> 'msg option Decoder.t -> 'msg attribute
Sourceval on_with_options : string -> 'msg msg_options Decoder.t -> 'msg attribute
Sourceval on_js : ?prevent_default:unit -> ?stop_propagation:unit -> string -> (js_object -> 'msg option) -> 'msg attribute
Sourceval on_js_with_options : string -> (js_object -> 'msg msg_options) -> 'msg attribute
Sourceval onmousedown : ?prevent_default:unit -> ?stop_propagation:unit -> (mouse_event -> 'msg) -> 'msg attribute
Sourceval onmousedown_cancel : ?stop_propagation:unit -> (mouse_event -> 'msg option) -> 'msg attribute
Sourceval onmouseup : ?prevent_default:unit -> ?stop_propagation:unit -> (mouse_event -> 'msg) -> 'msg attribute
Sourceval onclick : ?prevent_default:unit -> ?stop_propagation:unit -> (mouse_event -> 'msg) -> 'msg attribute
Sourceval onclick_cancel : ?stop_propagation:unit -> (mouse_event -> 'msg option) -> 'msg attribute
Sourceval ondblclick : ?prevent_default:unit -> ?stop_propagation:unit -> (mouse_event -> 'msg) -> 'msg attribute
Sourceval oncontextmenu : ?stop_propagation:unit -> (mouse_event -> 'msg) -> 'msg attribute
Sourceval onfocus : ?prevent_default:unit -> ?stop_propagation:unit -> 'msg -> 'msg attribute
Sourceval onblur : ?prevent_default:unit -> ?stop_propagation:unit -> 'msg -> 'msg attribute
Sourceval oninput : ?prevent_default:unit -> ?stop_propagation:unit -> (string -> 'msg) -> 'msg attribute

Pass the value property of the event target.

Sourceval onchange_checked : ?prevent_default:unit -> ?stop_propagation:unit -> (bool -> 'msg) -> 'msg attribute

Pass the checked property of the event targer.

Sourceval onchange : ?prevent_default:unit -> ?stop_propagation:unit -> (string -> 'msg) -> 'msg attribute

Pass the value property of the event target.

Sourceval onchange_index : ?prevent_default:unit -> ?stop_propagation:unit -> (int -> 'msg) -> 'msg attribute

Pass the selected_index property of the event target.

Sourceval onmousemove : ?prevent_default:unit -> ?stop_propagation:unit -> (mouse_event -> 'msg) -> 'msg attribute
Sourceval onmouseenter : ?prevent_default:unit -> ?stop_propagation:unit -> (mouse_event -> 'msg) -> 'msg attribute
Sourceval onmouseleave : ?prevent_default:unit -> ?stop_propagation:unit -> (mouse_event -> 'msg) -> 'msg attribute
Sourceval onmouseover : ?prevent_default:unit -> ?stop_propagation:unit -> (mouse_event -> 'msg) -> 'msg attribute
Sourceval onkeydown : ?prevent_default:unit -> ?stop_propagation:unit -> (key_event -> 'msg) -> 'msg attribute
Sourceval onkeydown_cancel : ?stop_propagation:unit -> (key_event -> 'msg option) -> 'msg attribute
Sourceval onkeyup : ?prevent_default:unit -> ?stop_propagation:unit -> (key_event -> 'msg) -> 'msg attribute
Sourceval onkeyup_cancel : ?stop_propagation:unit -> (key_event -> 'msg option) -> 'msg attribute
Sourceval onpaste : ?prevent_default:unit -> ?stop_propagation:unit -> (paste_event -> 'msg option) -> 'msg attribute
Sourceval oncustomevent : (Custom.event -> 'msg option) -> 'msg attribute

Generic DOM properties

Generic DOM properties correspond to actual properties on DOM objects. The name of these properties is usually the same as the corresponding HTML attribute, but not always (e.g. "class" attribute, but "className" property).

Sourceval str_prop : string -> string -> 'msg attribute
Sourceval int_prop : string -> int -> 'msg attribute
Sourceval bool_prop : string -> bool -> 'msg attribute
Sourceval float_prop : string -> float -> 'msg attribute
Sourceval style : string -> string -> 'msg attribute

A sub-field of the "style" DOM property.

Sourceval attr : string -> string -> 'msg attribute
Sourceval int_attr : string -> int -> 'msg attribute
Sourceval float_attr : string -> float -> 'msg attribute

Common DOM properties

Sourceval class_ : string -> 'msg attribute
Sourceval type_ : string -> 'msg attribute
Sourceval type_button : 'msg attribute
Sourceval value : string -> 'msg attribute
Sourceval disabled : bool -> 'msg attribute
Sourceval add_class : string -> 'msg attribute list -> 'msg attribute list

Pseudo-attributes

Pseudo-attributes are interpreted in a special way by the infrastructure.

Sourceval scroll_to_show : align_top:bool -> 'msg attribute

When this pseudo-attribute is first applied to an element, its parent is automatically scrolled (vertically) to show the element.

Sourceval autofocus : 'msg attribute

When this pseudo-attribute is first applied to an element, the element gets focused.

Sourceval autofocus_if_visible : 'msg attribute

When this pseudo-attribute is first applied to an element, the element gets focused if the element is visible in the viewport.

Sourceval autofocus_counter : int -> 'msg attribute

When this pseudo-attribute is first applied to an element, or applied with a different counter as the previous time, the element gets focused.

Sourceval select : 'msg attribute

When this pseudo-attribute is first applied to an input or textarea element, select the content.

Sourceval autosubmit : 'msg attribute

When this pseudo_attribute is first applied to a form element, it will be submitted automatically.

VDOM

Sourcetype +'msg vdom =
  1. | Text of {
    1. key : string;
    2. txt : string;
    }
  2. | Fragment of {
    1. key : string;
    2. children : 'msg vdom list;
    }
  3. | Element of {
    1. key : string;
    2. ns : string;
    3. tag : string;
    4. attributes : 'msg attribute list;
    5. children : 'msg vdom list;
    }
  4. | Map : {
    1. key : string;
    2. f : 'submsg -> 'msg;
    3. child : 'submsg vdom;
    } -> 'msg vdom
  5. | Memo : {
    1. key : string;
    2. f : 'a -> 'msg vdom;
    3. arg : 'a;
    } -> 'msg vdom
  6. | Custom of {
    1. key : string;
    2. elt : Custom.t;
    3. attributes : 'msg attribute list;
    4. propagate_events : bool;
    }

Generic VDOM constructors

Sourcetype ('msg, 'res) elt_gen = ?key:string -> ?a:'msg attribute list -> 'res
Sourceval elt : ?ns:string -> string -> ('msg, 'msg vdom list -> 'msg vdom) elt_gen

A generic element.

Sourceval svg_elt : string -> ('msg, 'msg vdom list -> 'msg vdom) elt_gen

A generic element in the SVG namespace.

Sourceval text : ?key:string -> string -> 'msg vdom

A text node.

Sourceval fragment : ?key:string -> 'msg vdom list -> 'msg vdom

A fragment node (not appearing in the dom).

Sourceval map_attr : ('msg attribute list -> 'msg attribute list) -> 'msg vdom -> 'msg vdom

Map attributes of a vdom element

Sourceval map : ?key:string -> ('a -> 'b) -> 'a vdom -> 'b vdom

Wrap all messages generated by a VDOM tree with the provided function.

Sourceval memo : ?key:string -> ('a -> 'msg vdom) -> 'a -> 'msg vdom

Apply the function to generate a VDOM tree only if the function or its argument have changed (physically) from the previous synchronization.

Note that physical equality is used both for the function and its argument. In particular, this is unlikely to behave as expected if the function is defined inline (as an abstraction) or obtained by a (partial) function application. Instead, the functional argument should be a simple reference to a globally defined function.

TODO: n-ary versions.

Sourceval custom : ?key:string -> ?a:'msg attribute list -> ?propagate_events:unit -> Custom.t -> 'msg vdom

A custom kind of node. Usually not used directly.

Common elements

Sourceval div : ('msg, 'msg vdom list -> 'msg vdom) elt_gen
Sourceval input : ('msg, 'msg vdom list -> 'msg vdom) elt_gen
Sourceval txt_span : ('msg, string -> 'msg vdom) elt_gen

Virtual Application

Sourceval return : ?c:'msg Cmd.t list -> 'model -> 'model * 'msg Cmd.t
Sourcetype ('model, 'msg) app = {
  1. init : 'model * 'msg Cmd.t;
  2. update : 'model -> 'msg -> 'model * 'msg Cmd.t;
  3. view : 'model -> 'msg vdom;
}
Sourceval app : init:('model * 'msg Cmd.t) -> update:('model -> 'msg -> 'model * 'msg Cmd.t) -> view:('model -> 'msg vdom) -> unit -> ('model, 'msg) app
Sourceval simple_app : init:'model -> update:('model -> 'msg -> 'model) -> view:('model -> 'msg vdom) -> unit -> ('model, 'msg) app

A simple app does not trigger commands.

Sourceval to_html : 'msg vdom -> string

Convert to HTML

OCaml

Innovation. Community. Security.