Skip to content

Commit dc7072f

Browse files
committed
Update README
1 parent 9f00156 commit dc7072f

File tree

11 files changed

+152
-45
lines changed

11 files changed

+152
-45
lines changed

sketch/README.md

+140-33
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
# Sketch
22

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.
78

89
Sketch supports both runtime of Gleam, and will let you write your CSS without
910
over-thinking about it. Let Sketch handle the hard task for you of CSS caching,
1011
generation and pushing it in the browser. Sketch do the right choices for you,
1112
to maximise performance in the browser and on BEAM.
1213

14+
Write your styles once, use them anywhere you want.
15+
1316
## Distributions
1417

1518
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. \
1821
Sketch package is also made for framework developers, to provide a common
1922
basement, reusable across the entire Gleam ecosystem, letting users reuse their
2023
knowledge no matter what they are coding.
@@ -129,7 +132,9 @@ fn main_style() {
129132
130133
fn view(model: Int) {
131134
html.div(main_style(), [], [
132-
html.div_([], [html.text(int.to_string(model))]),
135+
html.div_([], [
136+
html.text(int.to_string(model)),
137+
]),
133138
])
134139
}
135140
```
@@ -180,23 +185,26 @@ import sketch/redraw as sketch_redraw
180185
181186
pub fn main() {
182187
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+
])
187194
])
188-
]))
195+
)
189196
}
190197
```
191198

192199
### Usage
193200

194201
`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,
196203
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.
200208

201209
```gleam
202210
import redraw/html as h
@@ -231,7 +239,7 @@ seeing something weird, signal the bug!
231239

232240
Because pure CSS generation is straightforward, `sketch_css` does not need a
233241
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
235243
automagically, while providing an abstraction layer written in Gleam, to make
236244
sure you're using the right classes! It's an other way to leverage Sketch core
237245
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!
249257

250258
### Options
251259

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:
254270

255271
- `--dest`, accepting a folder, relative to current directory. It defaults to
256-
`styles`
272+
`styles`.
257273
- `--src`, accepting a folder, relative to current directory. It defaults to
258274
`src`.
259275
- `--interface`, accepting a folder, relative to current directory. It defaults
260276
to `src/sketch/styles`.
261277

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+
262313
### A note on generation algorithm
263314

264315
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
268319
generates the class, with a CSS variable, letting you update it, override it,
269320
etc.
270321

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!
273326

274327
### Example
275328

276329
```gleam
277330
// src/main_styles.gleam
278331
import sketch/css
279332
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)])
284336
}
285337
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) {
287343
css.class([
288344
css.compose(flexer()),
289-
css.flex_direction(flex_direction),
345+
direction(flex_direction),
290346
])
291347
}
292348
```
293349

294350
```css
295351
/* styles/main_styles.css */
296-
.flexer {
352+
.main_styles-flexer {
297353
display: flex;
298354
}
299355

300-
.flexer-direction {
356+
.main_styles-flexer_direction {
357+
display: flex;
301358
flex-direction: var(--flex-direction);
302359
}
303360
```
304361

305362
```gleam
306363
// src/sketch/styles/main_styles.gleam
307-
pub const flexer = "flexer"
364+
pub const flexer = "main_styles-flexer"
308365
309-
pub const flexer_direction = "flexer flexer-direction"
366+
pub const flexer_direction = "main_styles-flexer_direction"
310367
```
311368

312369
## Sketch general usage
313370

314371
At its core, Sketch relies on `sketch.class`, which let you define a class. A
315372
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.
317378

318379
## Using media queries and pseudo-selectors
319380

@@ -477,6 +538,52 @@ fn button(disabled) {
477538
}
478539
```
479540

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+
480587
## Some opinions on properties
481588

482589
All standard widely supported properties are accessible directly through the

sketch/birdie_snapshots/erlang_keyframe_class.accepted

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: erlang_keyframe_class
77
opacity: 1.0;
88
}
99

10-
50.0% {
10+
50% {
1111
opacity: 0.5;
1212
}
1313

sketch/birdie_snapshots/erlang_keyframe_css.accepted

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: erlang_keyframe_css
77
opacity: 1.0;
88
}
99

10-
50.0% {
10+
50% {
1111
opacity: 0.5;
1212
}
1313

sketch/birdie_snapshots/js_keyframe_class.accepted

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: js_keyframe_class
77
opacity: 1.0;
88
}
99

10-
50.0% {
10+
50% {
1111
opacity: 0.5;
1212
}
1313

sketch/birdie_snapshots/js_keyframe_css.accepted

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: js_keyframe_css
77
opacity: 1.0;
88
}
99

10-
50.0% {
10+
50% {
1111
opacity: 0.5;
1212
}
1313

sketch/src/sketch/css/keyframe.gleam

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import gleam/float
1+
import gleam/int
22
import sketch/internals/cache/cache as style
33

44
/// A keyframe is a part of an `@keyframes` rule.
@@ -34,8 +34,8 @@ pub fn to(styles: List(style.Style)) {
3434
/// ---
3535
///
3636
/// [MDN Reference](https://developer.mozilla.org/docs/Web/CSS/@keyframes#percentage)
37-
pub fn at(percentage: Float, styles: List(style.Style)) {
38-
Keyframe(style.named(float.to_string(percentage) <> "%", styles))
37+
pub fn at(percentage: Int, styles: List(style.Style)) {
38+
Keyframe(style.named(int.to_string(percentage) <> "%", styles))
3939
}
4040

4141
/// Internal function, can be used if you need to go from a keyframe to a String

sketch/test/classes/keyframe_css.gleam

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sketch/css/keyframe
44
pub fn keyframe() {
55
css.keyframes("fade-out", [
66
keyframe.from([css.opacity(1.0)]),
7-
keyframe.at(50.0, [css.opacity(0.5)]),
7+
keyframe.at(50, [css.opacity(0.5)]),
88
keyframe.to([css.opacity(0.0)]),
99
])
1010
}

sketch_css/birdie_snapshots/erlang_css_keyframe_css_gleam.accepted

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: erlang_css_keyframe_css.gleam
77
opacity: 1.0;
88
}
99

10-
50.0% {
10+
50% {
1111
opacity: 0.5;
1212
}
1313

sketch_css/birdie_snapshots/js_css_keyframe_css_gleam.accepted

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: js_css_keyframe_css.gleam
77
opacity: 1.0;
88
}
99

10-
50.0% {
10+
50% {
1111
opacity: 0.5;
1212
}
1313

sketch_css/src/sketch_css/module/stylesheet.gleam

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ fn convert_keyframe(
403403
let values = list.try_map(items, convert_expression(_, env, modules))
404404
use values <- result.try(values)
405405
case label, item {
406-
"at", FloatValue(f) ->
406+
"at", IntValue(f) ->
407407
keyframe.at(f, {
408408
use value <- list.filter_map(values)
409409
case value {

sketch_css/test/sketch_css_test/classes/keyframe_css.gleam

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sketch/css/keyframe
44
pub fn keyframe() {
55
css.keyframes("fade-out", [
66
keyframe.from([css.opacity(1.0)]),
7-
keyframe.at(50.0, [css.opacity(0.5)]),
7+
keyframe.at(50, [css.opacity(0.5)]),
88
keyframe.to([css.opacity(0.0)]),
99
])
1010
}

0 commit comments

Comments
 (0)