···18181919## Styling
20202121-You may want to look at [styles.css](https://forge.strawmelonjuice.com/strawmelonjuice/chilp/src/branch/main/examples/lustre_chilp_app_autoloading/assets/styles.css) to see how to style your own comment sections!
2121+<!--You may want to look at [styles.css](https://forge.strawmelonjuice.com/strawmelonjuice/chilp/src/branch/main/examples/lustre_chilp_app_autoloading/assets/styles.css) to see how to style your own comment sections!-->
22222323## Development
2424
···44import lustre
55import lustre/effect.{type Effect}
66import lustre/element.{type Element}
77+import lustre/element/html
7889// MAIN ------------------------------------------------------------------------
9101011pub fn main() {
1111- // In this example, we're not making much sense. Just Chilp.
1212+ // In this example, we're not making much sense. Just showing off Chilp!
1313+1414+ let assert Ok(_) = widget.register()
1515+1216 let app = lustre.application(init, update, view)
1317 let assert Ok(_) = lustre.start(app, "#app", Nil)
1418···1822// MODEL -----------------------------------------------------------------------
19232024type Model {
2121- Model(
2222- string: String,
2323- // .. and other things your application would need to know, for chilp we have:
2424- chilp_model: widget.ChilpDataInYourModel(Msg),
2525- )
2525+ Model(string: String, num: Int)
2626}
27272828fn init(_) -> #(Model, Effect(Msg)) {
2929- let model = Model(string: "Hi", chilp_model: widget.init(ChilpMessage))
2929+ let model = Model(string: "Hi", num: 4)
3030 // No effects, though you could force Chilp to pre-fetch a post with widget.force()!
3131 let effect = effect.none()
32323333 #(model, effect)
3434}
35353636-// HELPERS----------------------------------------------------------------------
3737-@external(javascript, "./ffi_lustre_chilp_app.mjs", "lassign")
3838-fn js_browse(to: String) -> Nil {
3939- Nil
4040-}
4141-4242-fn browse(to: String) {
4343- js_browse(to)
4444- effect.none()
4545-}
4646-4736// UPDATE ----------------------------------------------------------------------
48374938type Msg {
5050- ChilpMessage(widget.ChilpMsg)
3939+ SetString(String)
5140}
52415342// You can't usually make `ChilpMsg`s, with a few exceptions.
···5544// One of them being `widget.trigger()`, which does what `widget.force()` does but instead of an effect it returns a `ChilpMsg`!
5645fn update(model: Model, msg: Msg) -> #(Model, Effect(Msg)) {
5746 case msg {
5858- // Normally, your own variants would be here too.
5959- ChilpMessage(message) -> {
6060- let #(chilp_model, effect) =
6161- widget.update(message, model.chilp_model, browse)
6262- #(Model(..model, chilp_model:), effect)
6363- }
4747+ SetString(string) -> #(Model(string: string, num: model.num), effect.none())
6448 }
6549}
66506751// VIEW ------------------------------------------------------------------------
68526953fn view(model: Model) -> Element(Msg) {
7070- // Let's render comments under https://pony.social/@strawmelonjuice/115911235653686237 and nothing else
7171- widget.new("pony.social", "115911235653686237", model.chilp_model)
7272- |> widget.show(model.chilp_model)
5454+ html.div([], [
5555+ element.text(model.string),
5656+ // Let's render comments under https://pony.social/@strawmelonjuice/115911235653686237 and nothing else
5757+ widget.element(instance: "pony.social", post_id: "115911235653686237"),
5858+ ])
7359}
···11-// Same example as
22-33-// IMPORTS ---------------------------------------------------------------------
44-55-import chilp/widget
66-import lustre
77-import lustre/effect.{type Effect}
88-import lustre/element.{type Element}
99-1010-// MAIN ------------------------------------------------------------------------
1111-1212-pub fn main() {
1313- // In this example, we're not making much sense. Just Chilp.
1414- let app = lustre.application(init, update, view)
1515- let assert Ok(_) = lustre.start(app, "#app", Nil)
1616-1717- Nil
1818-}
1919-2020-// MODEL -----------------------------------------------------------------------
2121-2222-type Model {
2323- Model(
2424- string: String,
2525- // .. and other things your application would need to know, for chilp we have:
2626- chilp_model: widget.ChilpDataInYourModel(Msg),
2727- // A widget we pre-create in the init function, this could also be done inside of
2828- // your update function, and you don't NEED to store the widget data itself in your
2929- // model, you are allowed to call `widget.new()` twice and it'll create the same widget.
3030- my_widget: widget.CommentWidget(Msg),
3131- )
3232-}
3333-3434-fn init(_) -> #(Model, Effect(Msg)) {
3535- // Let's create a widget!
3636- // In this case we create the widget and let it travel with the model, but just creating it twice works too!
3737- let chilp_model = widget.init(ChilpMessage)
3838- let my_widget =
3939- widget.new(
4040- instance: "mastodon.social",
4141- post_id: "115978549407058619",
4242- chilp_model:,
4343- )
4444- let model = Model(string: "Hi", chilp_model:, my_widget:)
4545- let effect = widget.force(chilp_model:, on: my_widget)
4646-4747- #(model, effect)
4848-}
4949-5050-// HELPERS----------------------------------------------------------------------
5151-@external(javascript, "./ffi_lustre_chilp_app_autoload.mjs", "lassign")
5252-fn js_browse(to: String) -> Nil {
5353- Nil
5454-}
5555-5656-fn browse(to: String) {
5757- let _ = js_browse(to) == Nil
5858- effect.none()
5959-}
6060-6161-// UPDATE ----------------------------------------------------------------------
6262-6363-type Msg {
6464- ChilpMessage(widget.ChilpMsg)
6565-}
6666-6767-// You can't usually make `ChilpMsg`s, with a few exceptions.
6868-//
6969-// One of them being `widget.trigger()`, which does what `widget.force()` does but instead of an effect it returns a `ChilpMsg`!
7070-fn update(model: Model, msg: Msg) -> #(Model, Effect(Msg)) {
7171- case msg {
7272- // Normally, your own variants would be here too.
7373- ChilpMessage(message) -> {
7474- let #(chilp_model, effect) =
7575- widget.update(message, model.chilp_model, browse)
7676- #(Model(..model, chilp_model:), effect)
7777- }
7878- }
7979-}
8080-8181-// VIEW ------------------------------------------------------------------------
8282-8383-fn view(model: Model) -> Element(Msg) {
8484- // Render the widget we made in init().
8585- widget.show(model.my_widget, model.chilp_model)
8686-}
···11+// IMPORTS ---------------------------------------------------------------------
22+33+import chilp/widget/base
44+import lustre
55+import lustre/effect.{type Effect}
66+import lustre/element.{type Element}
77+88+// MAIN ------------------------------------------------------------------------
99+1010+pub fn main() {
1111+ // In this example, we're not making much sense. Just Chilp.
1212+ let app = lustre.application(init, update, view)
1313+ let assert Ok(_) = lustre.start(app, "#app", Nil)
1414+1515+ Nil
1616+}
1717+1818+// MODEL -----------------------------------------------------------------------
1919+2020+type Model {
2121+ Model(
2222+ string: String,
2323+ // .. and other things your application would need to know, for chilp we have:
2424+ chilp_model: base.ChilpDataInYourModel(Msg),
2525+ )
2626+}
2727+2828+fn init(_) -> #(Model, Effect(Msg)) {
2929+ let model = Model(string: "Hi", chilp_model: base.init(ChilpMessage))
3030+ // No effects, though you could force Chilp to pre-fetch a post with base.force()!
3131+ let effect = effect.none()
3232+3333+ #(model, effect)
3434+}
3535+3636+// HELPERS----------------------------------------------------------------------
3737+@external(javascript, "./ffi_lustre_chilp_app.mjs", "lassign")
3838+fn js_browse(_: String) -> Nil {
3939+ Nil
4040+}
4141+4242+fn browse(to: String) {
4343+ let Nil = js_browse(to)
4444+ effect.none()
4545+}
4646+4747+// UPDATE ----------------------------------------------------------------------
4848+4949+type Msg {
5050+ ChilpMessage(base.ChilpMsg)
5151+}
5252+5353+// You can't usually make `ChilpMsg`s, with a few exceptions.
5454+//
5555+// One of them being `base.trigger()`, which does what `base.force()` does but instead of an effect it returns a `ChilpMsg`!
5656+fn update(model: Model, msg: Msg) -> #(Model, Effect(Msg)) {
5757+ case msg {
5858+ // Normally, your own variants would be here too.
5959+ ChilpMessage(message) -> {
6060+ let #(chilp_model, effect) =
6161+ base.update(message, model.chilp_model, browse)
6262+ #(Model(..model, chilp_model:), effect)
6363+ }
6464+ }
6565+}
6666+6767+// VIEW ------------------------------------------------------------------------
6868+6969+fn view(model: Model) -> Element(Msg) {
7070+ // Let's render comments under https://pony.social/@strawmelonjuice/115911235653686237 and nothing else
7171+ base.new("pony.social", "115911235653686237", model.chilp_model)
7272+ |> base.show(model.chilp_model)
7373+}
-1
src/chilp.gleam
···11-//// More API focussed stuff should go here
21