1
1
# Sketch
2
2
3
- Sketch is a module providing CSS-in-Gleam in its simpler form. Sketch does not
4
- try to add complicated API on top of CSS. If you have CSS knowledge, you'll feel
5
- right at home, with all the niceties offered by Sketch, i.e. type-checking of
6
- sizes and push-to-browser stylesheets of your classes, as well as SSR support.
3
+ Sketch provides CSS support in Gleam in its simpler — yet complete — form.
4
+ Sketch does not add complicated API on top of CSS. If you have CSS knowledge,
5
+ you'll feel right at home, with all the niceties offered by Sketch, i.e.
6
+ type-checking of dimensions and push-to-browser stylesheets of your classes, as
7
+ well as SSR support or CSS files generation.
7
8
8
9
Sketch supports both runtime of Gleam, and will let you write your CSS without
9
10
over-thinking about it. Let Sketch handle the hard task for you of CSS caching,
10
11
generation and pushing it in the browser. Sketch do the right choices for you,
11
12
to maximise performance in the browser and on BEAM.
12
13
14
+ Write your styles once, use them anywhere you want.
15
+
13
16
## Distributions
14
17
15
18
Sketch is thought as bare package, built as a foundation for every CSS packages
16
- that want to leverage it. In the Sketch package, you'll find all CSS properties
17
- accessible, as well as low level generation functions, to go from Sketch to CSS.
19
+ that want to leverage it. In the core package, you'll find all CSS properties
20
+ accessible and a way to convert them directly in plain CSS. \
18
21
Sketch package is also made for framework developers, to provide a common
19
22
basement, reusable across the entire Gleam ecosystem, letting users reuse their
20
23
knowledge no matter what they are coding.
@@ -129,7 +132,9 @@ fn main_style() {
129
132
130
133
fn view(model: Int) {
131
134
html.div(main_style(), [], [
132
- html.div_([], [html.text(int.to_string(model))]),
135
+ html.div_([], [
136
+ html.text(int.to_string(model)),
137
+ ]),
133
138
])
134
139
}
135
140
```
@@ -180,23 +185,26 @@ import sketch/redraw as sketch_redraw
180
185
181
186
pub fn main() {
182
187
let root = client.create_root("root")
183
- client.render(root, redraw.strict_mode([
184
- // Initialise the cache. Sketch Redraw handles the details for you.
185
- sketch_redraw.provider([
186
- // Here comes your components!
188
+ client.render(root,
189
+ redraw.strict_mode([
190
+ // Initialise the cache. Sketch Redraw handles the details for you.
191
+ sketch_redraw.provider([
192
+ // Here comes your components!
193
+ ])
187
194
])
188
- ]) )
195
+ )
189
196
}
190
197
```
191
198
192
199
### Usage
193
200
194
201
` sketch_redraw ` exposes one module to help you build your site, similarly to
195
- redraw: ` sketch/redraw/html ` . ` html ` is simply a supercharged component,
202
+ redraw: ` sketch/redraw/dom/ html ` . ` html ` is simply a supercharged component,
196
203
accepting a ` sketch.Class ` as first argument, and applies that style to the
197
- node. Because it's a simple component, ` sketch/redraw/html ` and ` redraw/html `
198
- can be mixed in the same code without issue! Because of that property,
199
- ` sketch_redraw ` _ does not_ expose ` text ` and ` none ` function at that time.
204
+ node. Because it's a simple component, ` sketch/redraw/dom/html ` and
205
+ ` redraw/html ` can be mixed in the same code without issue! Because of that
206
+ property, ` sketch_redraw ` _ does not_ expose ` text ` and ` none ` function at that
207
+ time.
200
208
201
209
``` gleam
202
210
import redraw/html as h
@@ -231,7 +239,7 @@ seeing something weird, signal the bug!
231
239
232
240
Because pure CSS generation is straightforward, ` sketch_css ` does not need a
233
241
cache to generate correct CSS files. Instead, ` sketch_css ` ships with a CLI
234
- tool, able to read your Gleam styles files, and output corresponding your CSS
242
+ tool, able to read your Gleam styles files, and output corresponding CSS
235
243
automagically, while providing an abstraction layer written in Gleam, to make
236
244
sure you're using the right classes! It's an other way to leverage Sketch core
237
245
and enjoy the styling in Gleam, while taking advantage of all the static CSS
@@ -249,16 +257,59 @@ in `src/sketch/styles`, matching your styles files, to use in your project!
249
257
250
258
### Options
251
259
252
- Sketch CSS generation has strong defaults, but everything can be customised. Use
253
- the CLI flags to configure what you need. CLI exposes 3 flags:
260
+ Sketch CSS generation has strong defaults, but everything can be customised. To
261
+ pass options to Sketch CSS, you have three ways:
262
+
263
+ - Pass them directly on the CLI. Every option has its equivalent exposed in the
264
+ CLI.
265
+ - Write them in a ` sketch_css.toml ` file, at root of your project, next
266
+ ` gleam.toml ` .
267
+ - Write them directly in ` gleam.toml ` , under ` [sketch_css] ` section.
268
+
269
+ Sketch CSS has 3 distinct options:
254
270
255
271
- ` --dest ` , accepting a folder, relative to current directory. It defaults to
256
- ` styles `
272
+ ` styles ` .
257
273
- ` --src ` , accepting a folder, relative to current directory. It defaults to
258
274
` src ` .
259
275
- ` --interface ` , accepting a folder, relative to current directory. It defaults
260
276
to ` src/sketch/styles ` .
261
277
278
+ Write directly the folder, path resolution is done with current working
279
+ directory as root.
280
+
281
+ #### Examples
282
+
283
+ ``` sh
284
+ gleam run -m sketch_css generate --src=" src" --dest=" styles" --interface=" src/sketch/styles"
285
+ ```
286
+
287
+ ``` toml
288
+ # sketch_css.toml
289
+ src = " src"
290
+ dest = " styles"
291
+ interface = " src/sketch/styles"
292
+ ```
293
+
294
+ ``` toml
295
+ # gleam.toml
296
+ name = " name"
297
+ version = " 1.0.0"
298
+
299
+ [sketch_css ]
300
+ src = " src"
301
+ dst = " styles"
302
+ interface = " src/sketch/styles"
303
+
304
+ [dependencies ]
305
+ gleam_stdlib = " >= 0.34.0 and < 2.0.0"
306
+ sketch = " >= 4.0.0 and < 5.0.0"
307
+ sketch_css = " >= 2.0.0 and < 3.0.0"
308
+
309
+ [dev-dependencies ]
310
+ gleeunit = " >= 1.0.0 and < 2.0.0"
311
+ ```
312
+
262
313
### A note on generation algorithm
263
314
264
315
Because a Sketch ` Class ` can be generated in multiple ways, and with variable,
@@ -268,52 +319,62 @@ generated with the variable taken into account! Sketch CSS being opinionated, it
268
319
generates the class, with a CSS variable, letting you update it, override it,
269
320
etc.
270
321
271
- All ` _ ` are also automatically transformed into ` - ` , because CSS classes are
272
- most of the time used with dashes, so Sketch CSS follows that convention!
322
+ Sketch CSS also acts as a basic interpreter. It means you can write basic
323
+ constants or variables, and they will be taking into account. Be sure to write
324
+ classes like you would do in CSS yet: Sketch CSS does not execute your
325
+ functions!
273
326
274
327
### Example
275
328
276
329
``` gleam
277
330
// src/main_styles.gleam
278
331
import sketch/css
279
332
280
- fn flexer() {
281
- css.class([
282
- css.display("flex"),
283
- ])
333
+ pub fn flexer() {
334
+ let display = "flex"
335
+ css.class([css.display(display)])
284
336
}
285
337
286
- fn flexer_direction(flex_direction: String) {
338
+ fn direction(flex_direction: String) {
339
+ css.flex_direction(flex_direction)
340
+ }
341
+
342
+ pub fn flexer_direction(flex_direction: String) {
287
343
css.class([
288
344
css.compose(flexer()),
289
- css.flex_direction (flex_direction),
345
+ direction (flex_direction),
290
346
])
291
347
}
292
348
```
293
349
294
350
``` css
295
351
/* styles/main_styles.css */
296
- .flexer {
352
+ .main_styles- flexer {
297
353
display : flex ;
298
354
}
299
355
300
- .flexer-direction {
356
+ .main_styles-flexer_direction {
357
+ display : flex ;
301
358
flex-direction : var (--flex-direction );
302
359
}
303
360
```
304
361
305
362
``` gleam
306
363
// src/sketch/styles/main_styles.gleam
307
- pub const flexer = "flexer"
364
+ pub const flexer = "main_styles- flexer"
308
365
309
- pub const flexer_direction = "flexer flexer-direction "
366
+ pub const flexer_direction = "main_styles-flexer_direction "
310
367
```
311
368
312
369
## Sketch general usage
313
370
314
371
At its core, Sketch relies on ` sketch.class ` , which let you define a class. A
315
372
class is made of CSS properties. All of those can be accessed in ` sketch `
316
- module. Build your classes, and use them across your codebase!
373
+ module. Build your classes, and use them across your codebase! But a Sketch
374
+ class contains more than CSS properties, it can also contains every piece of
375
+ information used to defined a CSS class. This includes media queries,
376
+ pseudo-selectors & combinators! This allows to think to your styles in
377
+ isolation, without worrying with the global scope.
317
378
318
379
## Using media queries and pseudo-selectors
319
380
@@ -477,6 +538,52 @@ fn button(disabled) {
477
538
}
478
539
```
479
540
541
+ ## Top-level CSS
542
+
543
+ Sometimes, you need to write CSS directly in stylesheets, at the top-level.
544
+ Sketch implements a cherry-picked subset of
545
+ [ @rules ] ( https://developer.mozilla.org/docs/Web/CSS/At-rule ) . You can use them
546
+ directly on stylesheet, and they will be bundled in your resulting stylesheet!
547
+
548
+ ``` gleam
549
+ import sketch
550
+ import sketch/css
551
+
552
+ pub fn main() {
553
+ let assert Ok(stylesheet) = sketch.stylesheet(strategy: sketch.Ephemeral)
554
+ let stylesheet = sketch.at_rule(my_keyframe(), stylesheet)
555
+ let content = stylesheet.render(stylesheet)
556
+ }
557
+
558
+ fn my_keyframe() {
559
+ css.keyframes("fade-out", [
560
+ keyframe.from([css.opacity(1.0)]),
561
+ keyframe.at(50, [css.opacity(0.5)]),
562
+ keyframe.to([css.opacity(0.0)]),
563
+ ])
564
+ }
565
+ ```
566
+
567
+ In the above code, ` content ` will contains the following CSS.
568
+
569
+ ``` css
570
+ @keyframes fade-out {
571
+ from {
572
+ opacity : 1 ;
573
+ }
574
+
575
+ 50% {
576
+ opacity : 0.5 ;
577
+ }
578
+
579
+ to {
580
+ opacity : 0 ;
581
+ }
582
+ }
583
+ ```
584
+
585
+ Similarly, you can use ` @font-face ` to define your own fonts!
586
+
480
587
## Some opinions on properties
481
588
482
589
All standard widely supported properties are accessible directly through the
0 commit comments