-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
4952 lines (4267 loc) · 151 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Dynamic CSS Masterclass</title>
<link href="https://inspirejs.org/inspire.css" rel="stylesheet" />
<link href="https://inspirejs.org/theme.css" rel="stylesheet" />
<link href="https://projects.verou.me/talks/shared/intro-outro.css" rel="stylesheet" />
<link href="css/prism.css" rel="stylesheet" />
<link href="css/theme.css" rel="stylesheet" />
<link href="css/talk.css" rel="stylesheet" />
<link href="https://projects.verou.me/talks/shared/browser-support.css" rel="stylesheet" />
</head>
<body class="language-css"
data-markdown-elements="details.notes, .md, .takeaway.slide > h1, .credit"
data-balance-elements=".slide > h1:only-child, .takeaway.slide > h1, .speech.slide > h1">
<header id="intro" class="slide dont-resize">
<h1>Dynamic CSS</h1>
<h2>…with custom properties</h2>
<p class="attribution">By <a href="http://lea.verou.me">Lea Verou</a> (<a href="http://twitter.com/leaverou">@LeaVerou</a>)</p>
</header>
<article class="slide" id="lea-verou">
<h1>Lea Verou
<a href="https://twitter.com">@leaverou</a>
</h1>
<img src="img/smiling-smaller.jpg" alt="">
<ul>
<li><strong>CSS WG</strong> Invited Expert since 2012</li>
<li><a href="https://lea.verou.me/publications/#specifications">Co-editor of 5 CSS specifications</a></li>
<li>Elected <strong>W3C TAG</strong> Member</li>
<li>Day job: Usable Programming @ <strong>MIT</strong> (research & teaching)</li>
<li>Started <a href="https://github.com/leaverou">dozens of open source projects</a>, some of them used on <a href="https://www.npmjs.com/package/prismjs">millions of websites</a></li>
</ul>
</article>
<article class="slide">
<h1 data-label="Welcome! 👋🏼" style="--label-color: var(--accent1)">Prerequisites</h1>
<ul>
<li>A solid understanding of <strong>modern CSS</strong></li>
<li>Significant experience <strong>solving problems with CSS</strong></li>
<li>A little bit of <strong>Sass</strong> (variables, @for loops)</li>
<li>A little bit of <strong>vanilla JS</strong> (events, basic DOM)</li>
</ul>
</article>
<article class="slide">
<h1>What are we going to learn?</h1>
<ul class="emoji-list">
<li style="list-style-type: '✅';">
How to use custom properties to…
<ul>
<li>…improve maintainability of our CSS</li>
<li>…create style hooks for customizable components</li>
<li>…create transitions and animations not feasible without JS</li>
<li>…cleanly separate style and behavior</li>
</ul>
</li>
<li style="list-style-type: '🤦🏽♀️';">Caveats, gotchas, and workarounds</li>
</ul>
</article>
<article class="slide">
<h1>How will we learn it?</h1>
<ul class="emoji-list">
<li style="list-style-type: '👩🏽🏫'">
Teaching
<ul>
<li>Regular slides</li>
<li>Live coding slides</li>
<li>Mini-quizzes</li>
</ul>
</li>
<li style="list-style-type: '🏋🏽♀️'">Practice</li>
<li style="list-style-type: '📚'">Homework</li>
</ul>
<details class="notes">
There are two main components to this workshop: teaching, and practice.
When you hear "teaching", you may imagine passive listening, but this part of the workshop is hands-on as well.
There are a few more classic slides, but the bulk of what we learn will be taught by interactive live coding demos where I explain as I code, and you can
choose to either watch, or follow along, it’s up to you.
You can code directly in the slides, or open each example in CodePen and work there, if you’re more comfortable with that.
If you run into any issues while doing this, you can always interrupt me to ask.
When we are about to learn something potentially surprising, it is often preceded by "mini-quizzes", where I ask you what result you expect a small snippet of code to produce
and you guess, either freely or between predefined answers.
These quizzes are not testing your knowledge, since to answer correctly they'd require knowing something we haven’t learned yet.
They just aim to demonstrate how surprising aspects of CSS may be, and how code we think we fully understand can often have surprising results
because of concepts we may not know.
There are also exercises, i.e. mini-projects where you practice what we have learned so far on your own, ask any questions along the way,
and after a while we reconvene to code the solution together. This may often involve multiple steps.
A more freeform mini-project will be given as homework during our 2 week break, but there will be no homework between the other sessions, as they are in consecutive days.
There is no over-arching large project that we build gradually throughout this workshop.
While there is value in that approach, it is not my approach of choice.
Those who know me from other workshops, or my book know I prefer smaller, self-contained examples and exercises that are designed to let you practice specific things
and do not involve grunt work that is unrelated to what we're learning.
This means that if for any reason you have trouble with one exercise, it does not affect how you do in the rest.
The breakdown of each session into teaching and practicing is not constant.
The first session is mainly teaching, so we build a solid foundation, whereas the other three are more balanced.
Your companion slides include almost anything that my slides include, except the quizzes (otherwise it wouldn’t be fun).
At the end of the workshop, you will get the full version that includes the quizzes as well.
Please do not skip ahead on your slide deck. Slides are not designed to be standalone, and you’re missing out on what is currently being taught.
</details>
</article>
<article class="slide">
<h1>What will you need?</h1>
<ul class="emoji-list">
<li style="list-style-image: url(img/chrome-logo.svg)">A recent Chrome</li>
<li style="list-style-type: '⌨️';">A functioning keyboard.</li>
<li style="list-style-type: '🤯'">Yup, that’s it!</li>
</ul>
</article>
<article class="confused slide">
<!-- <h1 class="balance-lines">A whole day discussing …CSS variables? Really?!</h1> -->
<h1 class="balance-lines">A whole workshop discussing …CSS variables? Really?!</h1>
</article>
<article class="slide" id="preprocessors" data-steps="1">
<h1>Variables in <span class="swap" style="--content: 'Sass'; --content-1: 'Less'"></span></h1>
<pre><code style="--content: '$'; --content-1: '@'">
<span class="swap"></span>accent-color: #f06;
<span class="swap"></span>heading-font: Fancy Webfont, serif;
h2 {
color: <span class="swap"></span>accent-color;
font-family: <span class="swap"></span>heading-font;
}
</code></pre>
<details class="notes">
You may come from a Sass or Less background, where variables are used like this…
</details>
</article>
<article class="slide" id="preprocessors-scope">
<h1>So many refactor to this…</h1>
<pre><code style="--content: '$'; --content-1: '@'">
:root {
--accent-color: #f06;
--heading-font: Fancy Webfont, serif;
}
h2 {
color: var(--accent-color);
font-family: var(--heading-font);
}
</code></pre>
<div class="note delayed">but CSS variables can do so much more!</div>
<details class="notes">
So people often migrate to CSS variables by just changing syntax.
And it works, for the most part.
But it's like hiring a Michelin star chef to fry eggs.
CSS Custom properties can do so much more!
If you want more resources beyond this workshop, here are a few good introductory articles on CSS variables/custom properties:
- [MDN is always a good resource](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties)
- [Introduction to CSS Custom Properties - BetterProgramming](https://betterprogramming.pub/introduction-to-css-custom-properties-66b9474d3012)
- [A Strategy Guide To CSS Custom Properties - Smashing Magazine](https://www.smashingmagazine.com/2018/05/css-custom-properties-strategy-guide/)
- [A user’s guide to CSS variables - Increment](https://increment.com/frontend/a-users-guide-to-css-variables/)
</details>
</article>
<article class="isolated demo slide" id="cut-corner" style="--division: 65">
<textarea class="language-css">
.cut-corner {
padding: 1em;
clip-path: polygon(
calc(100% - var(--corner-size)) 0,
100% var(--corner-size),
100% 100%, 0 100%, 0 0
);
}
#a {
background: hotpink;
--corner-size: 1em;
}
#b {
background: skyblue;
--corner-size: 10px;
}
</textarea>
<textarea class="language-html">
<p class="cut-corner" id="a">CSS is awesome</p>
<p class="cut-corner" id="b">CSS is awesome</p>
</textarea>
<details class="notes">
CSS variables are scoped on elements, not curly bracket blocks,
taking full advantage of the browser’s DOM structure knowledge.
This means that we can set them and use them in entirely unrelated parts of
our CSS, and things just work beautifully.
Take a look at this example. How would you do it in Sass?
The same code using Sass variables would produce a [syntax error](https://codepen.io/leaverou/pen/3ae47e6e2b1587c66d6b2545ab26de79?editors=1100).
Try setting the corner size via an inline style.
That is definitely something you can't do with preprocessors!
You could also use any unit you want, even viewport units!
-------
*(We revisit this slide after discussing fallbacks)*
Note that these cutout corners can only be 45 degrees, we cannot have
different horizontal and vertical offsets.
How can we change this example so that we can also use
`--corner-size-x` and `--corner-size-y` (either both or just one)?
Does this work like a shorthand? If not, what's the difference?
</details>
</article>
<article class="takeaway slide">
<h1 class="no-md">
Sass variables are scoped on <em><code>{…}</code> blocks</em>,
CSS variables are scoped on <em>elements</em>
<div class="note">(lexical vs dynamic scope)</div>
</h1>
</article>
<article class="confused slide">
<h1 class="balance-lines">
<q>I thought we weren’t supposed to call them CSS variables?</q>
</h1>
<details class="notes bottom-right">
* Short answer: *it doesn't matter, use whatever term you want*
* [The spec itself](https://drafts.csswg.org/css-variables/) uses both.
* Yes, they are custom properties, but they can be used as reactive variables too.
Certain use cases naturally make more sense with one or the other term, others work with both.
* I will be using both terms throughout this workshop.
* Some people have suggested using the term "custom property" when setting (`--foo: ...`)
and "CSS variable" when getting (`var(--foo)`).
Feel free to adopt this if it resonates with you. Or don't. *It doesn't actually matter.*
</details>
</article>
<article class="question slide">
<h1>What’s up with :root?</h1>
</article>
<article class="slide">
<h1>Getting to the root of :root</h1>
<ul>
<li><code>:root</code> = the root element. <code><html></code> in HTML, <code><svg></code> in SVG etc</li>
<li>In HTML, <code>:root</code> = <code>html</code> but with higher specificity</li>
<li>Just a convention. Follow it or don’t.</li>
</ul>
<div style="display: flex; font-size: 70%;" class="delayed-children">
<pre><code>
/* This is fine */
:root {
--accent-color: #f06;
--font-body: FancyFont, serif;
}
html {
background: yellow;
min-height: 100vh;
}
</code></pre>
<pre style="margin: 0"><code>
/* This is also fine */
html {
--accent-color: #f06;
--font-body: FancyFont, serif;
background: yellow;
min-height: 100vh;
}
</code></pre>
<pre style="margin: 0"><code>
/* This is also fine */
:root {
--accent-color: #f06;
--font-body: FancyFont, serif;
background: yellow;
min-height: 100vh;
}
</code></pre>
</div>
<details class="notes">
In the past, one reason to prefer `:root` was also that there were scripts like [postcss-custom-properties](https://github.com/postcss/postcss-custom-properties)
that only preprocessed custom properties in `:root`.
However, these days this is no longer relevant,
not only because browser support for custom properties is great so the need to preprocess is pretty much gone,
but also because there are [PostCSS plugins](https://www.npmjs.com/package/postcss-css-variables) that do heavier transformations
and are not just restricted to `:root`.
</details>
</article>
<article class="slide">
<h1>What CSS variables <em>can’t do</em></h1>
<table>
<thead>
<tr>
<th></th>
<th>Valid Sass code</th>
<th>Invalid CSS code</th>
</tr>
</thead>
<tbody class="delayed-children">
<tr>
<th>Property names</th>
<td>
<pre><code class="language-scss">
$prop: margin-top;
#{$prop}: 10px;
</code></pre>
</td>
<td>
<pre><code>
--prop: margin-top;
var(--prop): 10px;
</code></pre>
</td>
</tr>
<tr>
<th>Selectors</th>
<td>
<pre><code class="language-scss">
$i: 1;
.foo-#{$i} {
...
}
</code></pre>
</td>
<td>
<pre><code>
:root { --i: 1; }
.foo-var(--i) {
...
}
</code></pre>
</td>
</tr>
<tr>
<th>@rules</th>
<td>
<pre><code class="language-scss">
$w: 500px;
@media (min-width: #{$w}) {
...
}
</code></pre>
</td>
<td>
<pre><code>
:root { --w: 500px; }
@media (min-width: var(--w)) {
...
}
</code></pre>
</td>
</tr>
</tbody>
</table>
<details class="notes">
This entire workshop will go in depth about how CSS variables are
more powerful than preprocessor variables.
So it's only fair that before we start we briefly discuss what they *can't* do.
</details>
</article>
<section>
<header class="slide" id="inheritance-header" style="--icon: 💸; background-color: var(--accent2)">
<h1>Inheritance</h1>
</header>
<article class="isolated demo slide" id="inheritance" style="--division: 60">
<textarea class="language-css">
:root {
--primary-color: hsl(250 30% 40%);
--secondary-color: hsl(190 40% 40%);
--tinted-background: hsl(0 0% 100% / .8);
}
h1 {
color: var(--secondary-color);
}
article {
background: var(--primary-color);
}
article h2 { color: white; }
article a {
color: var(--primary-color);
}
article .content {
background: var(--tinted-background);
}
</textarea>
<!--
@media (prefers-color-scheme: dark) {
:root {
--tinted-background: hsl(0 0% 10% / .6);
}
}
body {
color-scheme: light dark;
background: canvas;
color: canvastext;
}
article.alt {
--primary-color: var(--secondary-color);
}
-->
<style class="demo" data-slide media="not all">
body {
font-size: 100%;
line-height: 1.5;
}
article {
border-radius: .3rem;
overflow: hidden;
margin: 1em 0;
}
article h2 {
padding: .3rem .5rem;
margin: 0;
}
article a {
font-weight: bold;
}
article .content {
padding: .1em .5rem;
}
</style>
<textarea class="language-html">
<h1>Lea Verou's blog</h1>
<article>
<h2>Blog Post 1</h2>
<div class="content">
<p>Blog post 1 content with <a href="https://lea.verou.me">link</a></p>
</div>
</article>
<article class="alt">
<h2>Blog Post 2</h2>
<div class="content">
<p>Blog post 2 content with <a href="https://lea.verou.me">link</a></p>
</div>
</article>
</textarea>
<details class="notes">
The browser is aware of our DOM structure, and custom properties inherit by default.
Here we have defined our properties on the `:root` selector,
but they are available on every other element.
But even better, we can *override* variables on individual elements
and it's the more specific value that inherits down.
Let's make any blog post with the `.alt` class use the secondary color instead.
------
One of the major ways we take advantage of this inheritance is media queries.
We can only overide a few variables in the media query and have all our styles adjust
instead of rewriting a bunch of rules.
Let's see how this works in practice with a crude dark mode version of this website.
[Codepen solution](https://codepen.io/leaverou/pen/deee6211f81af64b1a5ebb70f53e044e?editors=1100)
------
Remember the clumsy dark mode we used here? Let's improve it!
</details>
</article>
<article class="takeaway slide">
<h1>CSS Variables are actually <br>
<span class="delayed collapsed">inherited</span> CSS properties</h1>
</article>
<article class="slide" data-insert="#inheritance"></article>
<article class="slide" data-type="Aside">
<div>
<p class="balance-lines">
If <code>color-scheme: light dark</code> is specified,<br>
<code>canvas</code> and <code>canvastext</code> act like this:</p>
<pre><code>
:root {
--canvas: white;
--canvastext: black;
}
@media (prefers-color-scheme: dark) {
:root {
--canvas: black;
--canvastext: white;
}
}
</code></pre>
</div>
<details class="notes">
Related:
- [Initial value of `color` is `canvastext`, not `black`](https://drafts.csswg.org/css-color/#the-color-property)
- [The `color-scheme` property](https://drafts.csswg.org/css-color-adjust/#color-scheme-prop)
</details>
</article>
<article class="question slide">
<h1>What if we don’t want inheritance?</h1>
</article>
<article class="isolated demo slide" id="cut-corner">
<textarea class="language-css">
* {
clip-path: polygon(
calc(100% - var(--corner-size)) 0,
100% var(--corner-size),
100% 100%, 0 100%, 0 0
);
}
p {
padding: 1em;
background: skyblue;
--corner-size: 1em;
}
</textarea>
<textarea class="language-html">
<p>CSS is <mark>awesome</mark></p>
</textarea>
<details class="notes">
Sometimes inheritance is not desirable.
One common pattern for using custom properties more as properties
is to use them on very liberal selectors, e.g. `input` or even `*`
and then specify them only when we want to trigger the corresponding effect.
Often, these effects should not be inherited.
One way to disable inheritance is to just make sure the property always has a non-inherited value.
`initial` is perfect for that: it resets the property to its initial value,
as if it had not been set at all, but still prevents it from inheriting.
We can always opt-in inheritance on a case-by-case basis by using the `inherit` keyword.
--------
There is a newer, more explicit way to disable inheritance: the [@property](https://developer.mozilla.org/en-US/docs/Web/CSS/@property) rule.
It makes custom properties tremendously more powerful, and we will keep revisiting it
throughout this workshop.
</details>
</article>
<article class="takeaway slide">
<h1>
You can disable inheritance by setting the property to `initial` on `*`
</h1>
</article>
<article class="slide" data-insert="#cut-corner-2"></article>
<article class="takeaway slide">
<h1>`@property` allows us to register our properties and control how they behave</h1>
</article>
<article class="slide">
<h1>Remember this?</h1>
<pre><code class="language-javascript">
CSS.registerProperty({
name: "--corner-size",
syntax: "*";
inherits: false
})
</code></pre>
<details class="notes">
Registering custom properties, started as a JS API,
and we got the declarative `@property` rule later.
</details>
</article>
<article class="browser-support slide">
<table>
<thead>
<tr>
<th></th>
<th><img src="img/chrome-logo.svg" alt="Chrome"></th>
<th><img src="img/firefox-logo.svg" alt="Firefox"></th>
<th><img src="img/edge-logo.svg" alt="Edge"></th>
<th><img src="img/safari-logo.png" alt="Safari"></th>
</tr>
</thead>
<tbody>
<tr>
<th><a href="https://caniuse.com/mdn-css_at-rules_property">@property</a></th>
<td>85</td>
<td></td>
<td>85</td>
<td></td>
</tr>
<tr>
<th><a href="https://caniuse.com/mdn-api_css_registerproperty">CSS.registerProperty()</a></th>
<td>79</td>
<td></td>
<td>79</td>
<td></td>
</tr>
</tbody>
</table>
<details class="notes">
However, today there is only a very marginal benefit to using the JS API,
as it's supported by the same browsers,
it just shipped in a slightly older version compared to `@property`.
Given that most users of these browsers have updated to a version that supports `@property`
at this point, you would only be getting a small increase in support,
for a significant increase in maintainability cost.
Also, Safari has [hinted](https://github.com/w3c/css-houdini-drafts/issues/940)
that they may implement `@property` *first* due to its improved performance.
The JS API is still useful for debugging,
because it actually prints out readable errors, while the CSS rule fails silently.
</details>
</article>
<article class="isolated horizontal demo slide">
<textarea class="language-html">
<sl-image-comparer>
<img slot="before" src="img/kittens-bw.jpg" alt="Grayscale version of kittens in a basket looking around.">
<img slot="after" src="img/kittens.jpg" alt="Color version of kittens in a basket looking around.">
</sl-image-comparer>
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/dist/shoelace.js"></script>
</textarea>
<textarea class="language-css">
@import url("https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/dist/themes/light.css");
sl-image-comparer {
--divider-width: 3px;
--handle-size: 2em;
}
</textarea>
<details class="notes">
Since custom properties are inherited properties, they also inherit down Shadow DOM boundaries,
and they are not affected by any potential `all: unset` CSS resets.
Users of Web components cannot directly style them (that’s the whole point of encapsulation),
but custom properties can provide style hooks for customizing aspects of the component.
Here we see how the [Shoelace Image Comparer](https://shoelace.style/components/image-comparer) component
is using CSS variables as style hooks.
Photo by Jari Hytönen on Unsplash
</details>
</article>
<article class="isolated horizontal demo slide">
<textarea class="language-html">
<sl-image-comparer>
<img slot="before" src="img/kittens-bw.jpg" alt="Grayscale version of kittens in a basket looking around.">
<img slot="after" src="img/kittens.jpg" alt="Color version of kittens in a basket looking around.">
</sl-image-comparer>
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/dist/shoelace.js"></script>
</textarea>
<textarea class="language-css">
@import url("https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/dist/themes/light.css");
sl-image-comparer::part(divider),
sl-image-comparer::part(handle) {
background: gold;
}
</textarea>
<details class="notes">
Even as consumers of components, we can use custom properties to create our own higher-level abstractions for styling components.
Let's take a look at this example.
We want to change the divider color, but the component does not expose a custom property for this.
Instead, it exposes two [parts](https://developer.mozilla.org/en-US/docs/Web/CSS/::part) of the component: `divider` and `handle`.
We can define a custom property that allows us to set the color of both at once without having to drill down to the parts.
Note that this technique is not just limited to actual web components, it can be useful for any reusable blob of HTML & CSS (which is essentially what a component is).
</details>
</article>
<article class="isolated demo slide" style="--division: 60">
<textarea class="language-html">
<progress value="30" max="100"></progress>
</textarea>
<textarea class="language-css">
progress {
-webkit-appearance: none;
background: hsl(220 10% 88%);
}
progress::-webkit-progress-value {
background: purple;
}
progress::-moz-progress-bar {
background: purple;
}
</textarea>
<style data-slide media="not all" class="demo">
html {
padding: 1em;
}
progress {
transform: scale(2);
transform-origin: 0 0;
width: 50%;
margin-bottom: 2em;
}
progress::-webkit-progress-bar {
background: transparent;
}
</style>
<details class="notes">
The same technique allows us to define custom properties that style native components,
so we can avoid using messy prefixed pseudo-elements all over the place.
Here we have a rather simple example of styling a `<progress>` element.
Every time we want to change the color of the progress bar, we have to override two separate rules.
Custom properties can help abstract away this ugliness.
</details>
</article>
<article class="takeaway slide">
<h1>
Custom properties inherit down Shadow DOM boundaries
<div class="note">(and are not reset by all: initial)</div>
</h1>
<details class="notes">
Unlike other inheritable properties however,
custom properties cannot be reset by `all: initial`.
</details>
</article>
</section>
<section id="fallbacks">
<header class="slide" style="--icon: 🧗🏽; background-color: var(--accent1)">
<h1>Fallbacks</h1>
</header>
<article class="isolated demo slide" id="gallery" style="--division: 55">
<textarea class="language-html">
<div class="fancy-photos">
<img src="img/cat1.jpg" alt="">
<img src="img/cat2.jpg" alt="">
<img src="img/cat3.jpg" alt="">
<img src="img/cat4.jpg" alt="">
<img src="img/cat5.jpg" alt="">
<img src="img/cat6.jpg" alt="">
<img src="img/cat7.jpg" alt="">
<img src="img/cat8.jpg" alt="">
<img src="img/cat9.jpg" alt="">
<img src="img/cat10.jpg" alt="">
</div>
</textarea>
<textarea class="language-css">
.fancy-photos {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 3vw;
}
.fancy-photos img {
width: 100%;
box-sizing: border-box;
aspect-ratio: 1 / 1;
object-fit: cover;
transform: scale(var(--scale))
rotate(var(--rotate));
transition: .3s;
}
.fancy-photos img:hover {
background: yellow;
}
</textarea>
<style class="demo" data-slide media="not all">
body {
margin: 4vw;
}
.fancy-photos img {
margin: 1vw;
padding: 1vw;
background: white;
box-shadow: 0 .1vw .5vw rgb(0 0 0 / .3);
}
</style>
<!-- TODO translate to show it can have two tokens -->
<details class="notes">
* Often, we use custom properties as optional styling hooks that may or may not be used.
This allows us to create a sort of *"styling API"* for us or even other developers.
* In these cases, we want to provide default values along,
so that people using our code do not need to set every single property.
* Here, we are using `--rotate` and `--scale` properties to allow specifying
transforms individually on these images
(aside: there are [actual standardized properties for this, but support is still limited](https://developer.mozilla.org/en-US/docs/Web/CSS/rotate)).
* Let's try to apply subtle rotations to some images ([Cicada principle](https://lea.verou.me/2020/07/the-cicada-principle-revisited-with-css-variables/))
and also enlarge them slightly on `:hover`.
* Notice that for our transforms to be applied, both need to be set,
defeating the purpose of using a custom property. What to do?
* We *could* set `--scale` and `--rotate` on `img` as well, and it would fix the demo,
but is not ideal because:
- it breaks inheritance
- It forces our API consumers to battle specificity
* Thankfully, the `var()` function allows for a fallback parameter too, which is perfect for this.
* Unlike just setting variables to values, the fallback parameter also allows us to provide
different fallbacks per usage, **for the same custom property**
* Note that an alternative to using these fallbacks is to register our properties and provide initial values.
- We cannot do both. Once we declare an initial value, the fallback is never triggered.
* [Codepen solution](https://codepen.io/leaverou/pen/1f5c9aa156dd2f1902b4932e95716b66?editors=1100)
</details>
</article>
<article class="question slide">
<h1>How do these fallbacks actually work?</h1>
</article>
<article class="slide color-reveal">
<div class="delayed-children">
<br>
<div style="background: yellowgreen">--accent-color: yellowgreen</div>
<br>
<div style="background: orange">No --accent-color set</div>
<br>
<div style="background: orange">--accent-color: initial</div>
<br>
<div style="background: red">No CSS variables support</div>
</div>
<pre style="margin-top: 2em; overflow: visible"><code>
background: red;
background: var(--accent-color, orange);
</code></pre>
</article>
<article class="takeaway slide">
<h1>
The `var()` fallback is applied when there is no value, or when it resolves to `initial`
<div class="note">(and some other cases we'll see later)</div>
</h1>
</article>
<article class="takeaway slide">
<h1>The `var()` fallback is no help in browsers that don’t support CSS variables</h1>
</article>
<article class="browser-support slide">
<table>
<thead>
<tr>
<th></th>
<th><img src="img/chrome-logo.svg" alt="Chrome"></th>
<th><img src="img/firefox-logo.svg" alt="Firefox"></th>
<th><img src="img/edge-logo.svg" alt="Edge"></th>
<th><img src="img/safari-logo.png" alt="Safari"></th>
</tr>
</thead>
<tbody>
<tr>
<th><a href="https://caniuse.com/css-variables" target="_blank">CSS Variables</a></th>
<td>49</td>
<td>31</td>
<td>15</td>
<td>9.1</td>
</tr>
</tbody>
</table>
</article>
<article class="isolated demo slide" id="atsupports" style="--division: 50">
<textarea class="language-css">
body {
background: red;
}
@supports (--css: variables) {
body {
background: green;
}
}
</textarea>
<style class="demo" data-slide media="not all">
body {
min-height: 100vh;
margin: 0;
}
</style>
<details class="notes">
For most cases, providing fallbacks via the cascade
works fine to provide a baseline accessible experience to the
few browsers that still don’t support custom properties.
However, if you need a more elaborate fallback, you can use the
[`@supports`](https://developer.mozilla.org/en-US/docs/Web/CSS/@supports)
rule. Any custom property and any value will do as the test.
When do you need a more elaborate fallback?
Typically when you need to control more properties and/or rules than the ones with
the `var()` reference.
Avoid using its negative form (`@supports not (--css: variables)`),
as that will prevent you from targeting the really old browsers (notably, IE 11 or under)
that don’t even support `@supports`.
----------
Can we use `@supports` to detect `@property` support? Let's try.
</details>
</article>
<article class="slide takeaway">
<h1>Use `@supports` to provide more elaborate fallback styling
<div class="note">And avoid @supports not (…)</div>
</h1>
</article>
<article class="browser-support slide">
<table>
<thead>
<tr>
<th></th>
<th><img src="img/chrome-logo.svg" alt="Chrome"></th>
<th><img src="img/firefox-logo.svg" alt="Firefox"></th>
<th><img src="img/edge-logo.svg" alt="Edge"></th>
<th><img src="img/safari-logo.png" alt="Safari"></th>
</tr>
</thead>
<tbody>
<tr>
<th>CSS Variables</th>
<td>49</td>
<td>31</td>
<td>15</td>
<td>9.1</td>
</tr>
<tr>
<th><a href="https://caniuse.com/css-featurequeries">@supports</a></th>
<td>28</td>
<td>22</td>
<td>13</td>
<td>9</td>
</tr>
</tbody>
</table>
</article>
<!-- <article class="slide takeaway">
<h1>Avoid using `@supports not` for a single test</h1>
<details class="notes">
Why? It only applies what's inside to browsers that
both support `@supports` **and** don't support the thing you're testing.
`not` can be useful in conjunciton with positive tests,
but when you're just testing if a given feature is supported,
go with a positive test and progressive enhancement.
</details>
</article> -->
<article class="slide" data-insert="#atsupports"></article>
<article class="slide takeaway">
<h1>There is currently no way to use `@supports` to detect `@property` support</h1>
<details class="notes">Why does this not work? We'll see in the next section, when we talk about parse time vs computed time values.</details>
</article>
<article class="slide">
<h1 class="balance-lines">How can we detect <code>@property</code> then?</h1>
<pre><code class="language-javascript">
if (window.CSSPropertyRule) {
let root = document.documentElement;