Packages

🍩 Lustre Bindings to Oat UI Components

Current section

Files

Jump to
glaze_oat src glaze@oat@toast.erl
Raw

src/glaze@oat@toast.erl

-module(glaze@oat@toast).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glaze/oat/toast.gleam").
-export([variant_to_json/1, placement_to_json/1, options_to_json/1, default_options/1, with_placement/2, with_duration/2, dispatch/3]).
-export_type([variant/0, placement/0, options/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Oat documentation: <https://oat.ink/components/toast/>\n"
"\n"
" Programmatic toast notifications.\n"
"\n"
" ## Anatomy\n"
"\n"
" Choose a [`Variant`](#variant), configure [`Options`](#options), and call\n"
" [`toast`](#toast) to trigger a notification from application code.\n"
"\n"
" ## Recipes\n"
"\n"
" ```gleam\n"
" import glaze/oat/toast\n"
"\n"
" let options =\n"
" toast.default_options(toast.Success)\n"
" |> toast.with_placement(toast.BottomRight)\n"
" |> toast.with_duration_ms(3000)\n"
"\n"
" toast.dispatch(\"Saved\", \"Your changes were stored.\", options)\n"
" ```\n"
"\n"
).
-type variant() :: info | success | danger | warning.
-type placement() :: top_right |
top_left |
top_center |
bottom_left |
bottom_right |
bottom_center.
-type options() :: {options, variant(), placement(), integer()}.
-file("src/glaze/oat/toast.gleam", 35).
-spec variant_to_json(variant()) -> gleam@json:json().
variant_to_json(Variant) ->
case Variant of
info ->
gleam@json:string(<<"info"/utf8>>);
success ->
gleam@json:string(<<"success"/utf8>>);
danger ->
gleam@json:string(<<"danger"/utf8>>);
warning ->
gleam@json:string(<<"warning"/utf8>>)
end.
-file("src/glaze/oat/toast.gleam", 55).
-spec placement_to_json(placement()) -> gleam@json:json().
placement_to_json(Placement) ->
case Placement of
top_right ->
gleam@json:string(<<"top-right"/utf8>>);
top_left ->
gleam@json:string(<<"top-left"/utf8>>);
top_center ->
gleam@json:string(<<"top-center"/utf8>>);
bottom_left ->
gleam@json:string(<<"bottom-left"/utf8>>);
bottom_right ->
gleam@json:string(<<"bottom-right"/utf8>>);
bottom_center ->
gleam@json:string(<<"bottom-center"/utf8>>)
end.
-file("src/glaze/oat/toast.gleam", 72).
-spec options_to_json(options()) -> gleam@json:json().
options_to_json(Options) ->
{options, Variant, Placement, Duration_ms} = Options,
gleam@json:object(
[{<<"variant"/utf8>>, variant_to_json(Variant)},
{<<"placement"/utf8>>, placement_to_json(Placement)},
{<<"duration"/utf8>>, gleam@json:int(Duration_ms)}]
).
-file("src/glaze/oat/toast.gleam", 85).
?DOC(
" Create default options for a given variant.\n"
"\n"
" Defaults are top-right placement and `4000` milliseconds duration.\n"
).
-spec default_options(variant()) -> options().
default_options(Variant) ->
{options, Variant, top_right, 4000}.
-file("src/glaze/oat/toast.gleam", 89).
-spec with_placement(options(), placement()) -> options().
with_placement(Options, Placement) ->
{options, erlang:element(2, Options), Placement, erlang:element(4, Options)}.
-file("src/glaze/oat/toast.gleam", 93).
-spec with_duration(options(), integer()) -> options().
with_duration(Options, Duration_ms) ->
{options,
erlang:element(2, Options),
erlang:element(3, Options),
Duration_ms}.
-file("src/glaze/oat/toast.gleam", 102).
?DOC(
" Trigger a toast notification.\n"
"\n"
" Available in the Browser.\n"
).
-spec dispatch(binary(), binary(), options()) -> nil.
dispatch(_, _, _) ->
nil.