Current section
Files
Jump to
Current section
Files
src/m3e/snackbar.gleam
//// Snackbar is presents short updates about application processes at the bottom of the screen.
////
//// This file was generated by m3e/generator
////
//// DO NOT EDIT
////
import gleam/float
import gleam/list
import lustre/attribute.{type Attribute}
import lustre/element.{type Element}
import m3e/attr
// --- Types ---
/// Snackbar is a View Model for this component
///
/// ## Fields:
///
/// - action: The label of the snackbar's action.
/// - close_label: The accessible label given to the button used to dismiss the snackbar.
/// - dismissible: Whether a button is presented that can be used to close the snackbar.
/// - duration: The length of time, in milliseconds, to wait before automatically dismissing the snackbar.
///
pub opaque type Snackbar {
Snackbar(
action: String,
close_label: String,
dismissible: Dismissible,
duration: Float,
)
}
/// Dismissible is whether a button is presented that can be used to close the snackbar.
///
pub type Dismissible {
IsDismissible
IsNotDismissible
}
// --- Defaults ---
pub const default_action: String = ""
pub const default_close_label: String = "Close"
pub const default_dismissible: Dismissible = IsNotDismissible
pub const default_duration: Float = 3000.0
/// Slots are used in child elements to insert content into this component
///
pub type Slot {
CloseIcon
// Renders the icon of the button used to close the snackbar.
}
// --- Configuration ---
/// Config is a public record for configuring this component.
///
pub type Config {
Config(
action: String,
close_label: String,
dismissible: Dismissible,
duration: Float,
)
}
/// default_config is the default configuration for this component.
///
pub fn default_config() -> Config {
Config(
action: "",
close_label: "Close",
dismissible: IsNotDismissible,
duration: 3000.0,
)
}
// --- Constructors ---
/// from_config creates a new Snackbar from the given configuration.
///
pub fn from_config(config: Config) -> Snackbar {
Snackbar(
action: config.action,
close_label: config.close_label,
dismissible: config.dismissible,
duration: config.duration,
)
}
/// new creates a new Snackbar with the default configuration.
///
pub fn new() -> Snackbar {
from_config(default_config())
}
// --- Setters ---
/// action sets the value of action for this Snackbar.
///
pub fn action(record: Snackbar, action: String) -> Snackbar {
Snackbar(..record, action: action)
}
/// close_label sets the value of close_label for this Snackbar.
///
pub fn close_label(record: Snackbar, close_label: String) -> Snackbar {
Snackbar(..record, close_label: close_label)
}
/// dismissible sets the value of dismissible for this Snackbar.
///
pub fn dismissible(record: Snackbar, dismissible: Dismissible) -> Snackbar {
Snackbar(..record, dismissible: dismissible)
}
/// duration sets the value of duration for this Snackbar.
///
pub fn duration(record: Snackbar, duration: Float) -> Snackbar {
Snackbar(..record, duration: duration)
}
// --- Renderers ---
/// render creates a Lustre Element for a Snackbar
///
pub fn render(
model: Snackbar,
attributes: List(Attribute(msg)),
children: List(Element(msg)),
) -> Element(msg) {
element.element(
"m3e-snackbar",
list.flatten([
[
attr.with_default("action", model.action, default_action),
attr.with_default("close-label", model.close_label, default_close_label),
attr.boolean("dismissible", model.dismissible == IsDismissible),
attr.with_default(
"duration",
float.to_string(model.duration),
float.to_string(default_duration),
),
],
attributes,
])
|> list.filter(fn(a) { a != attribute.none() }),
children,
)
}
/// render_config creates a Lustre Element from a Snackbar Config
///
pub fn render_config(
c: Config,
attributes: List(Attribute(msg)),
children: List(Element(msg)),
) -> Element(msg) {
render(from_config(c), attributes, children)
}
/// slot returns a Lustre Attribute(msg) for the given slot name
///
pub fn slot(s: Slot) -> Attribute(msg) {
case s {
CloseIcon -> attribute.attribute("slot", "close-icon")
}
}