Helix
Library for building reactive web interfaces.
open Helix
open Stdweb
let counter () =
let count = Signal.make 0 in
let open Html in
div
[ style_list [ ("border", "1px solid #eee"); ("padding", "1em") ] ]
[
h2 [] [ text "Counter" ];
div [] [ text "Compute a count." ];
div []
[
button
[ on_click (fun _ -> Signal.update (fun n -> n + 1) count) ]
[ text "+" ];
button
[ on_click (fun _ -> Signal.update (fun n -> n - 1) count) ]
[ text "-" ];
div
[
style_list [ ("font-size", "32px") ];
bind
(fun n ->
if n < 0 then style_list [ ("color", "red") ]
else style_list [ ("color", "blue") ]
)
count;
]
[ show (fun n -> text (string_of_int n)) count ];
];
]
let () =
match Dom.Document.get_element_by_id "root" with
| Some root -> Html.mount root (counter ())
| None -> failwith "No #root element found"
val signal : ?equal:('a -> 'a -> bool) -> ?label:string -> 'a -> 'a signal
An alias for Signal.make
.
show to_html signal
is a dynamic HTML node created from signal
values using to_html
.
show_some
is similar to show
, but operates on reactive option values. When the signal's value is None
, Html.empty
is rendered.
show_ok
is similar to show
, but operates on reactive result values. When the signal's value is Error _
, Html.empty
is rendered.
each to_html signal
reactively renders items from signal
with to_html
.
let items = Signal.make [ 1; 2; 3 ] in
ul [] [ each (fun item -> li [] [ int item ]) items ]
bind to_attr signal
is a dynamic HTML attribute created from signal
values using to_attr
.
let style = Signal.make [ ("color", "red") ] in
div [ Html.bind Html.style style ] [ text "Hello!" ]
bind_some
is similar to bind
, but operates on reactive option values. When the signal's value is None
, an empty attribute is produced.
bind_ok
is similar to bind
, but operates on reactive result values. When the signal's value is Error _
, an empty attribute is produced.
toggle ~on:pred attr s
is attr
if pred x
is true
and Html.empty
otherwise, where x
is the value of s
.
val conditional : on:bool Signal.t -> Html.attr
conditional on:signal
an attribute that shows the element if signal
is true
.
val visible : on:bool Signal.t -> Html.attr
visible ~on:signal
is a reactive attribute that controls the display
style of HTML elements. When signal
is false
this attribute is display: none
.
module Mouse : sig ... end
Mouse signals.
module Time : sig ... end
Time signals.
module Http : sig ... end
HTTP requests.
module History : sig ... end
Browser location and navigation helpers.
module Router : sig ... end
Reactive routing.
let
operators are provided to simplify rendering signals to HTML: (let$)
and (and$)
.
Example:
let view user_id todo_title =
let$ user_id = Signal.map int_of_string site_id in
and$ todo_title in
let open Html in
div [] [ h2 [] [ text "User id: "; text user_id ]; h3 [] [ text "Todo: "; todo_title ] ]
Which is the same as:
let view user_id todo_title =
let user_id = Signal.map int_of_string site_id in
show
(fun (user_id, todo_title) ->
let open Html in
div []
[
h2 [] [ text "User id: "; text user_id ];
h3 [] [ text "Todo: "; todo_title ];
]
)
(Signal.pair user_id todo_title)
Additionally, reactive attributes can be bound with let@
and and@
.