Skip to content

Commit 539f311

Browse files
committed
Release construct
1 parent 3efcb6c commit 539f311

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

sketch_lustre/CHANGELOG.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1+
## v3.1.0 - 2025-06-04
2+
3+
Sketch Lustre adds a new function, `construct`, to let you inject initial
4+
styling in the stylesheet. Most of the time, so initial style should be
5+
injected, whether to style `<body>`, add some keyframe, some font-faces, or
6+
anything else. Before any use of the stylsheet, `construct` allows you to set
7+
all those things you need!
8+
9+
You can use it to build something like `normalize.css`, but also to build your
10+
very own stylesheet, tailored to your needs!
11+
12+
An example :
13+
14+
```gleam
15+
import lustre
16+
import lustre/element/html
17+
import sketch
18+
import sketch/css
19+
import sketch/css/length.{percent, px}
20+
import sketch/lustre as sl
21+
22+
pub fn main() {
23+
let assert Ok(stylesheet) = setup_sketch_lustre()
24+
let view = view(_, stylesheet)
25+
lustre.application(init, view, update)
26+
|> lustre.start("#root", Nil)
27+
}
28+
29+
/// Inject default styling in the initial stylesheet.
30+
fn setup_sketch_lustre() {
31+
use stylesheet <- sl.construct
32+
stylesheet
33+
|> sketch.global(css.global("body", [css.margin(px(0))]))
34+
|> sketch.global(css.global("html", [css.height(percent(100))]))
35+
// Add any initial style you want here, `sketch.at_rule`, etc.
36+
}
37+
38+
fn view(model: Model, stylesheet: sketch.StyleSheet) {
39+
use <- sl.render(stylesheet, in: [sl.node()])
40+
html.div([], [])
41+
}
42+
```
43+
44+
In a nutshell : if you need to instanciate a quick and easy stylesheet, use
45+
`setup()`, if you need to customize your stylesheet, use `construct()`!
46+
147
## v3.0.0 - 2025-05-09
248

349
Sketch Lustre Experimental is now official, and become the official way to use

sketch_lustre/gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "sketch_lustre"
2-
version = "3.0.0"
2+
version = "3.1.0"
33

44
description = "A Sketch runtime package, made to work with Lustre!"
55
internal_modules = ["sketch/lustre/internals", "sketch/lustre/internals/*"]

sketch_lustre/src/sketch/lustre.gleam

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub opaque type Container {
2626
/// In `sketch_lustre`, there's no notion of persistent or ephemeral stylesheets:
2727
/// all stylesheets will be persisted in the application.
2828
///
29+
/// In case you need to add initial styling directly in your stylesheet, take a
30+
/// look at [`construct`](#construct).
31+
///
2932
/// ```gleam
3033
/// pub fn main() {
3134
/// let assert Ok(stylesheet) = sketch_lustre.setup()
@@ -39,6 +42,34 @@ pub fn setup() -> Result(sketch.StyleSheet, Nil) {
3942
}
4043
}
4144

45+
/// Setup the StyleSheet to use across your application, and let you inject
46+
/// initial styling directly in your stylesheet. If you don't need to add
47+
/// default styling, you can take a look at [`setup`](#setup).
48+
///
49+
/// In `sketch_lustre`, there's no notion of persistent or ephemeral stylesheets:
50+
/// all stylesheets will be persisted in the application.
51+
///
52+
/// ```gleam
53+
/// pub fn main() {
54+
/// let assert Ok(stylesheet) =
55+
/// sketch_lustre.construct(fn (stylesheet) {
56+
/// stylesheet
57+
/// |> sketch.global(css.global("html", [...]))
58+
/// |> sketch.global(css.global("body", [...]))
59+
/// |> sketch.global(css.global(":root", [...]))
60+
/// })
61+
/// // The following code goes there.
62+
/// }
63+
/// ```
64+
pub fn construct(
65+
init: fn(sketch.StyleSheet) -> sketch.StyleSheet,
66+
) -> Result(sketch.StyleSheet, Nil) {
67+
case sketch.stylesheet(strategy: sketch.Persistent) {
68+
Error(_) -> Error(Nil)
69+
Ok(stylesheet) -> global.set_stylesheet(init(stylesheet))
70+
}
71+
}
72+
4273
/// Unref the StyleSheet to let it be garbaged by the runtime. Because stylesheets
4374
/// are memoized to guarantee performance and usability of Sketch Lustre, they
4475
/// should be dereferenced to ensure no memory leaks will happen in the application.

0 commit comments

Comments
 (0)