···11import chilp/widget
2233+/// Widget component!
44+/// Before adding this component make sure to call `widget.register()` to register it!
55+///
66+/// This component adds in inline CSS, to not do this, use `widget.element("bla", "111", False)` instead.
37pub fn widget(instance instance: String, post_id post: String) {
44- widget.element(instance:, post_id: post)
88+ widget.element(instance:, post_id: post, with_styles: True)
59}
+35-20
src/chilp/widget.gleam
···11// IMPORTS ---------------------------------------------------------------------
2233import chilp/widget/base as widget
44+import gleam/bool
45import gleam/result
56import gleam/string
67import lustre
···1314// MAIN ------------------------------------------------------------------------
14151516pub fn register() -> Result(Nil, lustre.Error) {
1717+ // I went along with the examples at https://github.com/lustre-labs/lustre/tree/main/examples/05-components!
1818+ // If you're looking for good examples, look there!
1619 let component =
1720 lustre.component(init, update, view, [
1818- // Attributes are string values that are set on the component's HTML element.
1919- // We can set up listeners for any attributes we care about and decode them
2020- // into a message for our update function. Any time the parent app changes
2121- // the attribute, this function will run and if it is `Ok` the message will
2222- // be sent to the component's update function.
2321 component.on_attribute_change("postpointer", fn(value) {
2422 value
2523 |> string.split_once(":")
···3028 lustre.register(component, "comment-widget")
3129}
32303333-pub fn element(instance instance: String, post_id post: String) -> Element(msg) {
3131+pub fn element(
3232+ instance instance: String,
3333+ post_id post: String,
3434+ with_styles styles: Bool,
3535+) -> Element(msg) {
3436 element.element(
3537 "comment-widget",
3636- [attribute.attribute("postpointer", instance <> ":" <> post)],
3838+ [
3939+ attribute.attribute("postpointer", instance <> ":" <> post),
4040+ attribute.attribute("styles-enabled", styles |> bool.to_string),
4141+ ],
3742 [],
3843 )
3944}
···4348type WrappingModel {
4449 WrappingModelSet(
4550 chilp_model: widget.ChilpDataInYourModel(Msg),
5151+ styles: Bool,
4652 widget: widget.CommentWidget(Msg),
4753 )
4848- WrappingModelUnset(chilp_model: widget.ChilpDataInYourModel(Msg))
5454+ WrappingModelUnset(
5555+ chilp_model: widget.ChilpDataInYourModel(Msg),
5656+ styles: Bool,
5757+ )
4958}
50595160fn init(_) -> #(WrappingModel, Effect(Msg)) {
5252- #(WrappingModelUnset(widget.init(ChilpMsgWrapper)), effect.none())
6161+ #(WrappingModelUnset(widget.init(ChilpMsgWrapper), True), effect.none())
5362}
54635564// UPDATE ----------------------------------------------------------------------
···6574 let chilp_model = model.chilp_model
6675 let widget = widget.new(instance, post, chilp_model)
6776 #(
6868- WrappingModelSet(widget:, chilp_model:),
7777+ WrappingModelSet(widget:, chilp_model:, styles: model.styles),
6978 widget.force(widget, chilp_model:),
7079 )
7180 }
···7382 let #(chilp_model, effects) =
7483 widget.update(message, model.chilp_model, browse)
7584 let new_model = case model {
7676- WrappingModelSet(_, widget) -> WrappingModelSet(chilp_model:, widget:)
7777- WrappingModelUnset(_) -> WrappingModelUnset(chilp_model:)
8585+ WrappingModelSet(_, styles, widget) ->
8686+ WrappingModelSet(chilp_model:, styles:, widget:)
8787+ WrappingModelUnset(_, styles:) ->
8888+ WrappingModelUnset(chilp_model:, styles:)
7889 }
7990 #(new_model, effects)
8091 }
···99110// VIEW ------------------------------------------------------------------------
100111101112fn view(model: WrappingModel) -> Element(Msg) {
102102- html.div([attribute.class("chilp-widget-component")], [
103103- html.style([], js_inline_styles()),
104104- case model {
105105- WrappingModelSet(chilp_model:, widget:) ->
106106- widget.show(from: widget, data: chilp_model)
107107- WrappingModelUnset(chilp_model: _) -> element.none()
108108- },
109109- ])
113113+ case model {
114114+ WrappingModelSet(chilp_model:, widget:, styles: True) ->
115115+ html.div([attribute.class("chilp-widget-component")], [
116116+ html.style([], js_inline_styles()),
117117+ widget.show(from: widget, data: chilp_model),
118118+ ])
119119+ WrappingModelSet(chilp_model:, widget:, styles: False) ->
120120+ html.div([attribute.class("chilp-widget-component")], [
121121+ widget.show(from: widget, data: chilp_model),
122122+ ])
123123+ WrappingModelUnset(..) -> element.none()
124124+ }
110125}