-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.xml
1079 lines (1044 loc) · 310 KB
/
rss.xml
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
<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Magnificode]]></title><description><![CDATA[Thoughts from Dominic Magnifico]]></description><link>https://dommagnifi.co</link><generator>GatsbyJS</generator><lastBuildDate>Fri, 31 Mar 2023 19:47:52 GMT</lastBuildDate><item><title><![CDATA[Server Components in Next 13]]></title><description><![CDATA[I just got back from the labs at House of Giants where I was able to take a look at some of the cool new features in Next 13. One of these…]]></description><link>https://dommagnifi.co/2023-03-29-server-components-in-next-13/</link><guid isPermaLink="false">https://dommagnifi.co/2023-03-29-server-components-in-next-13/</guid><pubDate>Wed, 29 Mar 2023 12:44:00 GMT</pubDate><content:encoded><p>I just got back from the labs at <a href="https://houseofgiants.com">House of Giants</a> where I was able to take a look at some of the cool new features in Next 13. One of these promising new features is server components. This new capability promises to change the way we build NextJS applications for the better. In this post, we'll take a closer look at server components, explore their benefits, and discuss best practices for using them in real-world scenarios.</p>
<h2>What are server components?</h2>
<p>Server components allow us to isolate the specific parts of our application that depend on server-side rendered data. By breaking down our pages into smaller, more manageable components, we can load them independently for improved modularity and easier maintenance. Additionally, server components help improve performance by reducing the amount of unnecessary data that needs to be loaded.</p>
<h2>An alternative to existing solutions</h2>
<p>Before we dive into how to create server components, let's take a look at how they compare to other server-side rendering options in NextJS:</p>
<ul>
<li><strong>getStaticProps/getServerSideProps</strong>: These functions allow you to fetch data at build time or request time, respectively. However, they only work for the entire page, not individual components.</li>
<li><strong>API routes</strong>: API routes allow you to fetch data from the server, but they require you to make an extra network request from the client.</li>
<li><strong>Hydration</strong>: Hydration is a technique that allows the client to take over rendering from the server after the initial page load. While this approach can work well for smaller applications, it can become unwieldy as your application grows.</li>
</ul>
<p>Server components offer a more flexible and powerful approach to server-side rendering, allowing you to load only the components you need when you need them.</p>
<h2>What are the benefits</h2>
<p>Server components are a significant improvement over the old way of rendering server side data in NextJS. They provide a more modular approach to managing server side rendering, allowing for better isolation of components that rely on server side data. This not only makes it easier to manage your application, but also reduces bundle sizes by only loading the necessary server components. Additionally, server components can be loaded independently, which leads to faster load times and improved performance overall.</p>
<h2>Show me the code 👨💻</h2>
<p>Let's dive into creating a basic server component:</p>
<div class="gatsby-highlight" data-language="jsx"><pre class="language-jsx"><code class="language-jsx"><span class="token comment">// components/SWPlanets.js</span>
<span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">getPlanets</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> res <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fetch</span><span class="token punctuation">(</span><span class="token string">"https://swapi.dev/api/planets"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> data <span class="token operator">=</span> <span class="token keyword">await</span> res<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token punctuation">{</span> data <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">ServerPlanets</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span> data <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">getPlanets</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token punctuation">{</span>data<span class="token punctuation">.</span>results<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">planet</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">key</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>planet<span class="token punctuation">.</span>name<span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token punctuation">{</span>planet<span class="token punctuation">.</span>name<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre></div>
<p>This component is unique because it uses server-side rendering to fetch the data. It is not tied to any specific page, but rather can be used as a standalone component to retrieve and display the data on the server. By doing so, we can avoid unnecessary network requests from the client and provide a more optimized bundle. Additionally, because server components are isolated, they can be composed together to create more complex features within our application, improving modularity and maintainability. Pretty slick!</p>
<h2><strong>Real-world use cases and best practices for using server components</strong></h2>
<p>Besides obtaining the diameter, population and orbital period of Dagobah, here are a few other real world use cases for server components.</p>
<ul>
<li><strong>Lazy loading</strong>: Use server components to load parts of the page only when they are needed, reducing the initial page load time.</li>
<li><strong>A/B testing</strong>: Use server components to test different variations of a component or page and track user engagement.</li>
<li><strong>Server-side analytics</strong>: Use server components to send data to your analytics platform directly from the server, bypassing the need to track events on the client.</li>
</ul>
<p>When using server components, it's important to follow best practices to ensure optimal performance and maintainability. Keep these tips in mind as you build:</p>
<ul>
<li><strong>Keep components small</strong>: Divide your page into small, reusable components that can be loaded independently.</li>
<li>
<p><strong>Cache data</strong>: Use caching strategies to avoid making unnecessary API calls and improve performance.</p>
<ul>
<li>To this point, we could optimize our example further by leveraging <code class="language-text">getServerSideProps</code> to cache our planet data. See if you can work out how to add that to our example above!</li>
</ul>
</li>
<li><strong>Test thoroughly</strong>: Test your server components thoroughly to ensure they work as expected and don't introduce new bugs.</li>
</ul>
<h2>Wrap it up</h2>
<p>It’s pretty clear that the folks at Next are doing their level best to optimize the amount of JavaScript that is output in our applications in an attempt to reduce the massive weight of web applications these days. Server components and their counterpart client components are a pretty cool step in that direction. Very much looking forward to testing and implementing these features in future builds at <a href="https://houseofgiants.com">House of Giants</a>!</p>
<h3>Post resources:</h3>
<ul>
<li><a href="https://beta.nextjs.org/docs/rendering/server-and-client-components">https://beta.nextjs.org/docs/rendering/server-and-client-components</a></li>
<li><a href="https://swapi.dev/">https://swapi.dev/</a></li>
</ul></content:encoded></item><item><title><![CDATA[Toggle State With React Hooks]]></title><description><![CDATA[Creating a toggle function is probably one thing that you'll always need to know how to do effectively and efficiently in any app that you…]]></description><link>https://dommagnifi.co/2020-12-03-toggle-state-with-react-hooks/</link><guid isPermaLink="false">https://dommagnifi.co/2020-12-03-toggle-state-with-react-hooks/</guid><pubDate>Thu, 03 Dec 2020 16:44:00 GMT</pubDate><content:encoded><p>Creating a toggle function is probably one thing that you'll always need to know how to do effectively and efficiently in any app that you build. Let's dive down the rabbit hole a bit and see what we can explore when it comes to this seemingly simple task.</p>
<h2>What will we need?</h2>
<p>We're going to assume that you've run through the process of setting up your React project. If not, take a second and run through <a href="https://reactjs.org/docs/create-a-new-react-app.html#create-react-app">Create React App</a>.</p>
<p>From there, we're going to create a new component to test our basic toggle!</p>
<h3>React Hooks</h3>
<p><img src="https://media.giphy.com/media/UhFZggMF6xGWk/giphy.gif"></p>
<p>Hooks provide us a super easy way to set the local state of a component within our application. <a href="https://reactjs.org/docs/hooks-intro.html">Hooks solve a variety of different problems</a>. Specifically in our case it allows us to pair state with a function component. Traditionally you'd need to leverage a Class component in order to make your component stateful. With hooks we can define our state like so using <code class="language-text">useState</code>.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">import</span> React<span class="token punctuation">,</span> <span class="token punctuation">{</span> useState <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'react'</span>
<span class="token keyword">const</span> <span class="token function-variable function">MyComponent</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> <span class="token punctuation">[</span>toggle<span class="token punctuation">,</span> setToggle<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">useState</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
<span class="token keyword">return</span><span class="token punctuation">(</span>
<span class="token operator">&lt;</span><span class="token operator">></span>
The Component
<span class="token operator">&lt;</span><span class="token operator">/</span><span class="token operator">></span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre></div>
<p>Let's break down line four a bit.</p>
<p><code class="language-text">const [toggle, setToggle] = useState(false)</code></p>
<p><code class="language-text">toggle</code> is our state variable. This is what we'll reference when we want to get the current state of <code class="language-text">toggle</code>.</p>
<p><code class="language-text">setToggle</code> is the function that we'll use to actually update the state of our component.</p>
<p><code class="language-text">useState(false)</code> this is how we 'hook' into the React feature itself. When we pass something in to this function as an argument (in our case <code class="language-text">false</code> is our argument), React will use that passed argument as the initial state.</p>
<p>All together we have a way to get the current state, set the state, and an initial state for our toggle.</p>
<h3>A Trigger</h3>
<p>We'll now need to define a DOM element that can trigger a state change using our hooks.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">import</span> React<span class="token punctuation">,</span> <span class="token punctuation">{</span> useState <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'react'</span>
<span class="token keyword">const</span> <span class="token function-variable function">MyComponent</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> <span class="token punctuation">[</span>toggle<span class="token punctuation">,</span> setToggle<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">useState</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
<span class="token keyword">return</span><span class="token punctuation">(</span>
<span class="token operator">&lt;</span><span class="token operator">></span>
<span class="token operator">&lt;</span>button onClick<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">setToggle</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token operator">></span>Toggle State<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">/</span><span class="token operator">></span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre></div>
<p>I didn't really think about it too much before writing this post, but I was curious as to why <code class="language-text">setToggle</code> needed to be called within an inline function. Turns out that the inline function is necessary to set the state after the component has rendered. This ensures that the new state we're passing to <code class="language-text">setToggle</code> as an argument is actually applied to the component itself.</p>
<p>Now, when we test this, we should see our state change from <code class="language-text">false</code> to <code class="language-text">true</code> once the button is clicked. Great! However, this is not a toggle just yet. Let's introduce the last piece of the puzzle...</p>
<h3>Setting The State Using The Logical Not (!) or Double Not (!!) Operators</h3>
<p>There are a couple of options that we can leverage to toggle the state, let's understand what these options actually do before we settle on one.</p>
<p><strong>The Logical Not (!)</strong></p>
<p>A single <code class="language-text">!</code> operator in JavaScript gives us the ability to set the opposite of the value immediately adjacent to the operator. We can use this operator to return <code class="language-text">true</code> or <code class="language-text">false</code> depending on whether or not a value is defined. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT#:~:text=The%20logical%20NOT%20(%20!%20),true%20%3B%20otherwise%2C%20returns%20true%20.">From MDN:</a></p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript">n1 <span class="token operator">=</span> <span class="token operator">!</span><span class="token boolean">true</span> <span class="token comment">// !t returns false (the opposite of true)</span>
n2 <span class="token operator">=</span> <span class="token operator">!</span><span class="token boolean">false</span> <span class="token comment">// !f returns true (the opposite of false)</span>
n3 <span class="token operator">=</span> <span class="token operator">!</span><span class="token string">''</span> <span class="token comment">// !f returns true (in JavaScript, an empty string is falsey, thus the opposite of a falsey value here is truthy.)</span>
n4 <span class="token operator">=</span> <span class="token operator">!</span><span class="token string">'Cat'</span> <span class="token comment">// !t returns false (non empty string is truthy, thus opposite is falsy)</span></code></pre></div>
<p>Check out more on <a href="https://developer.mozilla.org/en-US/docs/Glossary/truthy">truthy</a> and <a href="https://developer.mozilla.org/en-US/docs/Glossary/falsy">falsy</a> values.</p>
<p><strong>The Double Not (!!)</strong></p>
<p>The double not operator serves a different purpose. The immediatly adjacent value to a double not operator will be converted to the corresponding boolean based on the truthyness or falsyness of that value. That said, this operator will not return the opposite of anything, in fact it will return the boolean primitive of the adjacent value. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT#:~:text=The%20logical%20NOT%20(%20!%20),true%20%3B%20otherwise%2C%20returns%20true%20.">Again, from MDN</a></p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript">n1 <span class="token operator">=</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token boolean">true</span> <span class="token comment">// !!truthy returns true</span>
n2 <span class="token operator">=</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// !!truthy returns true: any object is truthy...</span>
n3 <span class="token operator">=</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// ...even Boolean objects with a false .valueOf()!</span>
n4 <span class="token operator">=</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token boolean">false</span> <span class="token comment">// !!falsy returns false</span>
n5 <span class="token operator">=</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token string">""</span> <span class="token comment">// !!falsy returns false</span>
n6 <span class="token operator">=</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token comment">// !!falsy returns false</span></code></pre></div>
<p>This section was mostly for my own edification. I wasn't 100% clear on the intent of the double not operator, but now we know! Let's use the logical not operator in our example to set the opposite value of the current state in our toggle button.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">import</span> React<span class="token punctuation">,</span> <span class="token punctuation">{</span> useState <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'react'</span>
<span class="token keyword">const</span> <span class="token function-variable function">MyComponent</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> <span class="token punctuation">[</span>toggle<span class="token punctuation">,</span> setToggle<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">useState</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
<span class="token keyword">return</span><span class="token punctuation">(</span>
<span class="token operator">&lt;</span><span class="token operator">></span>
<span class="token operator">&lt;</span>button onClick<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">setToggle</span><span class="token punctuation">(</span><span class="token operator">!</span>toggle<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token operator">></span>Toggle State<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">/</span><span class="token operator">></span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre></div>
<p>Nice! Now, whatever the state of our toggle state variable is, clicking the button will set it to the opposite boolean value. A true toggle!</p>
<p>With our state being properly toggled we can do some fun things in React. One of the most common things I use a toggle for is hiding or showign markup on the page. For example, a dropdown menu. We don't necessarily need the DOM markup for a hidden dropdown menu on the page on render. But we definitely want that markup when we click the button. This can be achieved like so:</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">import</span> React<span class="token punctuation">,</span> <span class="token punctuation">{</span> useState <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'react'</span>
<span class="token keyword">const</span> <span class="token function-variable function">MyComponent</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> <span class="token punctuation">[</span>toggle<span class="token punctuation">,</span> setToggle<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">useState</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
<span class="token keyword">return</span><span class="token punctuation">(</span>
<span class="token operator">&lt;</span><span class="token operator">></span>
<span class="token operator">&lt;</span>button onClick<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">setToggle</span><span class="token punctuation">(</span><span class="token operator">!</span>toggle<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token operator">></span>Toggle Dropdown Markup<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
<span class="token punctuation">{</span>toggle <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span>
<span class="token operator">&lt;</span>ul<span class="token operator">></span>
<span class="token operator">&lt;</span>li<span class="token operator">></span>Show me<span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span>
<span class="token operator">&lt;</span>li<span class="token operator">></span>Only when<span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span>
<span class="token operator">&lt;</span>li<span class="token operator">></span>Toggle <span class="token operator">===</span> <span class="token boolean">true</span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">/</span>ul<span class="token operator">></span>
<span class="token punctuation">)</span><span class="token punctuation">}</span>
<span class="token operator">&lt;</span><span class="token operator">/</span><span class="token operator">></span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre></div>
<p>In our JSX, we escape into JS using curly braces <code class="language-text">{}</code>. This allows us to write some basic JavaScript within our JSX. Essentially we are saying, if <code class="language-text">toggle</code> is <code class="language-text">true</code>, then render (by way of the parenthesis) our unorded list!</p>
<p>And there you have it. A basic boolean state toggle using React Hooks.</p>
<p><img src="https://media.giphy.com/media/SvOrq4OA7TQTC/giphy.gif"></p></content:encoded></item><item><title><![CDATA[The Many Ways to Include CSS in JavaScript Applications]]></title><link>https://dommagnifi.co/2019-07-08-the-many-ways-to-include-css-in-javascript-applications/</link><guid isPermaLink="false">https://dommagnifi.co/2019-07-08-the-many-ways-to-include-css-in-javascript-applications/</guid><pubDate>Mon, 08 Jul 2019 16:44:00 GMT</pubDate><content:encoded></content:encoded></item><item><title><![CDATA[Basic ES6 Vanilla JS Class Toggle]]></title><description><![CDATA[Wow, almost three years after I posted a lil' blog post about a basic class toggle with Vanilla JS, I suppose I could write out how to do…]]></description><link>https://dommagnifi.co/2019-05-06-basic-class-toggle-with-vanilla-js-es6/</link><guid isPermaLink="false">https://dommagnifi.co/2019-05-06-basic-class-toggle-with-vanilla-js-es6/</guid><pubDate>Mon, 06 May 2019 16:44:00 GMT</pubDate><content:encoded><p>Wow, almost three years after I posted a <a href="/2016-05-16-basic-class-toggle-with-vanilla-js/">lil' blog post</a> about a basic class toggle with Vanilla JS, I suppose I could write out how to do this same thing with ES6.</p>
<p>Since we last visited our class toggle task in 2016, quite a few things have changed. Let's take what we have where we left off and refactor it a bit to be more up to the current standard.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">var</span> el <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.toggle-me'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
el<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
el<span class="token punctuation">.</span>classList<span class="token punctuation">.</span><span class="token function">toggle</span><span class="token punctuation">(</span><span class="token string">'active'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre></div>
<p>Ahh yes. Nothing better than looking at your own old code...🤦. First things first, let's update how we're caching the DOM element using <code class="language-text">const</code>.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">const</span> el <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.toggle-me'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
el<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
el<span class="token punctuation">.</span>classList<span class="token punctuation">.</span><span class="token function">toggle</span><span class="token punctuation">(</span><span class="token string">'active'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre></div>
<p>Super small change here, but keep in mind, when using <code class="language-text">const</code> you want to be sure that whatever you're referencing is not something that will be mutated further down the line. We know that the <code class="language-text">.toggle-me</code> DOM node won't be mutated into something completely different, so <code class="language-text">const</code> is appropriate here.</p>
<p>Next, let's take a look at the function. Currently we're using the <code class="language-text">onclick</code> event handler to detect when a user clicks on our defined element. Now this will work just fine out of the box, but let's pepper in an ES6 arrow function!</p>
<p>As a primer, arrow functions are just a more compact way of writing a regular function like we have in the snippet above. It's worth noting however that <code class="language-text">this</code> behaves quite differently within an arrow function. Tyler McGinnis has a <a href="https://tylermcginnis.com/arrow-functions/">wonderful post</a> that goes into depth about the ins and outs of the arrow function. Feel free to pop over there if you want to dive a little deeper.</p>
<p>For our sake, we can utilize an arrow function to condense our code just a bit:</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">const</span> el <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.toggle-me'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
el<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> el<span class="token punctuation">.</span>classList<span class="token punctuation">.</span><span class="token function">toggle</span><span class="token punctuation">(</span><span class="token string">'active'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
<p>Nice! We're able to trim that down to two very succinct lines of javascript. With our arrow function, we're implicitly running the toggle method on our element. No need to add curly braces around our one line function. This will work perfectly, and remain accessible if the class toggle is a <code class="language-text">&lt;button&gt;</code> or <code class="language-text">&lt;input type=&quot;submit&quot;&gt;</code> element. The <code class="language-text">onclick</code> listener also accounts for users who utilize the keyboard to navigate. The <code class="language-text">enter</code> and <code class="language-text">space</code> keys will trigger a click on these elements. But what about if we need to toggle a class on a <code class="language-text">&lt;div&gt;</code>, <code class="language-text">&lt;span&gt;</code> or another element that is not interactive by default?</p>
<h2>Class toggles on non-interactive elements</h2>
<p><strong>Big ol' disclaimer here. If you have an element on your page that does a thing when you interact with it, you will <em>ALMOST ALWAYS</em> use a button. It's the standard for saying "Hey when you click this thing, something is going to happen on the page you're currently viewing". But, if you have a case where you absolutely cannot use a button or submit input, it's imperative that you make that element accessible for keyboard users.</strong></p>
<p>For non interactive elements like <code class="language-text">&lt;div&gt;</code> and <code class="language-text">&lt;span&gt;</code>, an extra step must be taken to ensure that the interaction is accessible to keyboard users. We're going to add a new event listener to our snippet that will ensure usability for all.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">const</span> el <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.toggle-me'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
el<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> el<span class="token punctuation">.</span>classList<span class="token punctuation">.</span><span class="token function">toggle</span><span class="token punctuation">(</span><span class="token string">'active'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
el<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'keyup'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>keyCode <span class="token operator">===</span> <span class="token number">13</span> <span class="token operator">||</span> event<span class="token punctuation">.</span>keyCode <span class="token operator">===</span> <span class="token number">32</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
el<span class="token punctuation">.</span>classList<span class="token punctuation">.</span><span class="token function">toggle</span><span class="token punctuation">(</span><span class="token string">'active'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
<p>Please also don't forget, that because these elements are not interactive by default, we need to ensure that users can focus the item properly in the DOM. Let's assume that our element is a <code class="language-text">&lt;div&gt;</code>. By adding a <code class="language-text">tabindex</code> attribute to that div, we are able to ensure that keyboard users are able to focus the element.</p>
<div class="gatsby-highlight" data-language="html"><pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>toggle-me<span class="token punctuation">"</span></span> <span class="token attr-name">tabindex</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>0<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Hi!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></code></pre></div>
<p><strong>Note here as well, when using <code class="language-text">tabindex</code> you very rarely will want to take elements out of the tab flow of the document. Using <code class="language-text">tabindex=&quot;0&quot;</code> makes the element focusable, but keeps it in the normal tab flow. A value of anything other than 0 will take it out of the flow, and could potentially cause confusion for keyboard users. Only do this if it's absolutely necessary, and you have a plan to ensure that keyboard users know exactly what's happening</strong>.</p>
<p>The <code class="language-text">keyup</code> event listener will listen for when a keyboard user hits, and releases a key. Within our arrow function, we now need to pass that keyup event so that we can capture the event, and only trigger the class toggle if the enter, or space keys are hit (these are the keyboard keys that indicate a user is trying to perform an action on our element). With that event captured, we can then run our class toggle!</p>
<p>Because we're running the toggle twice, let's consolidate again.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">const</span> el <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.toggle-me'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token function-variable function">handleToggle</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> el<span class="token punctuation">.</span>classList<span class="token punctuation">.</span><span class="token function">toggle</span><span class="token punctuation">(</span><span class="token string">'active'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
el<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">handleToggle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
el<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'keyup'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>keyCode <span class="token operator">===</span> <span class="token number">13</span> <span class="token operator">||</span> event<span class="token punctuation">.</span>keyCode <span class="token operator">===</span> <span class="token number">32</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">handleToggle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
<p>With ES6 we can assign our arrow function to a <code class="language-text">const</code>, and call that function in multiple places.</p>
<p>Spectacular! With that, you should now be able to level up your CSS class toggle to ES6 fairly painlessly for both interactive, and non-interactive elements.</p></content:encoded></item><item><title><![CDATA[React Context API]]></title><description><![CDATA[https://reactjs.org/docs/context.html Since React 16.3.0, we've had access to the React Context API. Traditionally, as Kent C. Dodds has…]]></description><link>https://dommagnifi.co/2019-02-27-the-react-context-api/</link><guid isPermaLink="false">https://dommagnifi.co/2019-02-27-the-react-context-api/</guid><pubDate>Wed, 27 Feb 2019 16:44:00 GMT</pubDate><content:encoded><p><a href="https://reactjs.org/docs/context.html">https://reactjs.org/docs/context.html</a></p>
<p>Since React 16.3.0, we've had access to the React Context API. Traditionally, as Kent C. Dodds <a href="https://blog.kentcdodds.com/prop-drilling-bb62e02cb691">has explained</a>, we've experienced a bit of confusion, and headache when it comes to passing props down to multiple decedents of an application. A term he coined "prop drilling", describes that issue well.</p>
<p>The React Context API aims to solves the prop drilling issue by way of a fairly straight forward <code class="language-text">Provider</code> to <code class="language-text">Consumer</code> relationship. This makes passing data between components that are not necessarily direct descendants of each other much easier.</p>
<h2>Context</h2>
<p>In order to set up the <code class="language-text">Provider</code> to <code class="language-text">Consumer</code> relationship we must first set up a new context. This context acts as a sort of boundary for passing the specific data within this context, to it's child components.</p>
<div class="gatsby-highlight" data-language="jsx"><pre class="language-jsx"><code class="language-jsx"><span class="token keyword">const</span> MyContext <span class="token operator">=</span> React<span class="token punctuation">.</span><span class="token function">createContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
<h2>The Provider Component</h2>
<p>Once we have a context defined, we can create our provider, which is the Component that we'll actually use to wrap our application (or parts of our application), and pass the data. The provider component is just a fairly simple Higher Order Component that contains the state you wish to pass down to various levels of your application. This, simply, is where your data lives.</p>
<div class="gatsby-highlight" data-language="jsx"><pre class="language-jsx"><code class="language-jsx"><span class="token keyword">class</span> <span class="token class-name">MyProvider</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
state <span class="token operator">=</span> <span class="token punctuation">{</span>
name<span class="token operator">:</span> <span class="token string">'Dominic'</span><span class="token punctuation">,</span>
age<span class="token operator">:</span> <span class="token number">28</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token class-name">MyContext.Provider</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span>
state<span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>state
<span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>children<span class="token punctuation">}</span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token class-name">MyContext.Provider</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></div>
<p>The value attribute in the <code class="language-text">MyContext.Provider</code> component is what passes the data down to the child components. So in this case, we pass down the state as an object. This gives us access to the Provider state.</p>
<h2>The Consumer</h2>
<p>Within any child component of our Provider, we'll need to write a consumer to actually get at that data. Instead of traditional <code class="language-text">props</code> the data is passed down via <code class="language-text">render props</code></p>
<div class="gatsby-highlight" data-language="jsx"><pre class="language-jsx"><code class="language-jsx"><span class="token keyword">class</span> <span class="token class-name">Company</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
<span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span><span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>company<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token class-name">MyContext.Consumer</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>
<span class="token comment">//Fragment added here since you can only return one child</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">Welcome to </span><span class="token punctuation">{</span>context<span class="token punctuation">.</span>state<span class="token punctuation">.</span>name<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">We are </span><span class="token punctuation">{</span>context<span class="token punctuation">.</span>state<span class="token punctuation">.</span>age<span class="token punctuation">}</span><span class="token plain-text"> years old!</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token class-name">MyContext.Consumer</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></div>
<h2>Updating State</h2>
<p>In addition to passing the state down from the Provider via the <code class="language-text">value</code> attribute, you can also pass down functions. Much like using Redux, these methods that we pass would be our 'actions'.</p>
<div class="gatsby-highlight" data-language="jsx"><pre class="language-jsx"><code class="language-jsx"><span class="token keyword">class</span> <span class="token class-name">MyProvider</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
state <span class="token operator">=</span> <span class="token punctuation">{</span>
name<span class="token operator">:</span> <span class="token string">'Dominic'</span><span class="token punctuation">,</span>
age<span class="token operator">:</span> <span class="token number">28</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token class-name">MyContext.Provider</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span>
state<span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">,</span>
<span class="token function-variable function">addYear</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
age<span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>age <span class="token operator">+</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>children<span class="token punctuation">}</span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token class-name">MyContext.Provider</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></div>
<p>And within the Consumer, we now have access to that method.</p>
<div class="gatsby-highlight" data-language="jsx"><pre class="language-jsx"><code class="language-jsx"><span class="token keyword">class</span> <span class="token class-name">Company</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
<span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span><span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>company<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token class-name">MyContext.Consumer</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">Welcome to </span><span class="token punctuation">{</span>context<span class="token punctuation">.</span>state<span class="token punctuation">.</span>name<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">We are </span><span class="token punctuation">{</span>context<span class="token punctuation">.</span>state<span class="token punctuation">.</span>age<span class="token punctuation">}</span><span class="token plain-text"> years old!</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">onClick</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>context<span class="token punctuation">.</span>addYear<span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token plain-text">Add Year</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token class-name">MyContext.Consumer</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></div>
<p>Hooray! With that here's what our full <code class="language-text">App.js</code> file should look like:</p>
<div class="gatsby-highlight" data-language="jsx"><pre class="language-jsx"><code class="language-jsx"><span class="token keyword">import</span> React<span class="token punctuation">,</span> <span class="token punctuation">{</span> Component <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'react'</span><span class="token punctuation">;</span>
<span class="token comment">// Create new context</span>
<span class="token keyword">const</span> MyContext <span class="token operator">=</span> React<span class="token punctuation">.</span><span class="token function">createContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Create the Provider that will pass down state and methods to the rest of the application.</span>
<span class="token keyword">class</span> <span class="token class-name">MyProvider</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
state <span class="token operator">=</span> <span class="token punctuation">{</span>
name<span class="token operator">:</span> <span class="token string">'Dominic'</span><span class="token punctuation">,</span>
age<span class="token operator">:</span> <span class="token number">28</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token class-name">MyContext.Provider</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span>
state<span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">,</span>
<span class="token function-variable function">addYear</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
age<span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>age <span class="token operator">+</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>children<span class="token punctuation">}</span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token class-name">MyContext.Provider</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// Create the consumer that will consume the data provided by the Provider.</span>
<span class="token keyword">class</span> <span class="token class-name">Company</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
<span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span><span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>company<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token class-name">MyContext.Consumer</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>
<span class="token comment">//Fragment added here since you can only return one child</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">Welcome to </span><span class="token punctuation">{</span>context<span class="token punctuation">.</span>state<span class="token punctuation">.</span>name<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">We are </span><span class="token punctuation">{</span>context<span class="token punctuation">.</span>state<span class="token punctuation">.</span>age<span class="token punctuation">}</span><span class="token plain-text"> years old!</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">onClick</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>context<span class="token punctuation">.</span>addYear<span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token plain-text">Add Year</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token class-name">MyContext.Consumer</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// We'll nest our Consumer inside another component just to show that we don't need to pass props to each component.</span>
<span class="token keyword">const</span> <span class="token function-variable function">Companies</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token class-name">Company</span></span> <span class="token punctuation">/></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span>
<span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
<span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="token comment">// Ensure the provider wraps all the components you want to share data between.</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token class-name">MyProvider</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>App<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token class-name">Companies</span></span> <span class="token punctuation">/></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span><span class="token plain-text">
</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span><span class="token class-name">MyProvider</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> App<span class="token punctuation">;</span></code></pre></div>
<p>Great job! You’re up and running with the React Context API. This method is a nice first step to attempt to use before reaching for something far more heavy handed like Redux. No third party library, no confusing (it’s still confusing in my head) actions and reducers. Just nice, clean React API.</p></content:encoded></item><item><title><![CDATA[React for People Who Think Things Like React are Weird and Hard]]></title><description><![CDATA[Title shamelessly derived from Chris Coyier's fantastic post about Grunt. In the world of Engineering we tend to get caught up in the…]]></description><link>https://dommagnifi.co/2019-02-24-react-for-people-who-think-things-like-react-are-weird-and-hard/</link><guid isPermaLink="false">https://dommagnifi.co/2019-02-24-react-for-people-who-think-things-like-react-are-weird-and-hard/</guid><pubDate>Sun, 24 Feb 2019 16:44:00 GMT</pubDate><content:encoded><p><em>Title shamelessly derived from <a href="https://24ways.org/2013/grunt-is-not-weird-and-hard/">Chris Coyier's fantastic post about Grunt.</a></em></p>
<p>In the world of Engineering we tend to get caught up in the technical aspects of things. I wanted to write a intro post concerning react to give a little insight to the folks we work with, Project Manages, Account Managers, Marketing folks etc. who may not be as privvy to the technical aspects of React, but who are still curious to understand what it is, what problems it solves, or doesn't solve.</p>
<h2>A Little History First</h2>
<p><img src="https://media.giphy.com/media/HlqvH9JrahLZ6/giphy.gif" alt="Falling asleep"></p>
<p>Yes yes, I know, history is super boring, but I think it's important that we understand a little bit of where React came from. This will provide us with useful insight into what problems React was <em>designed</em> to solve. Let's first use Reacts definition of itself to provide some context:</p>
<p>From the React documentation:</p>
<blockquote>
<p>React is a declarative, efficient, and flexible JavaScript library for
building user interfaces. It lets you compose complex UIs from small and
isolated pieces of code called “components”.</p>
</blockquote>
<p>Before moving on, let me explain what declarative programming means using this very succinct quote from Tyler McGinnis' blog.</p>
<blockquote>
<p>Declarative programming is “the act of programming in languages that conform to
the mental model of the developer rather than the operational model of the
machine”.</p>
</blockquote>
<p>This library was built by the team at Facebook. Specifically within Facebook, their application that handled targeting ads at users became so unwieldy that developers there built a system to make their code more maintainable. This, coupled with Facebook's acquisition of Instagram, pushed the developers to decouple this technology from Facebook, so that it could be open sourced.</p>
<p>React was initially proposed as a response to the emerging MVC (model view controller) methodology that many web applications were heading towards. Angular was super popular, Backbone.js which some of you may remember had it's tentacles reaching into WordPress at various points. React decided to take those methodologies, and focus on being a declarative way for engineers to build User Interfaces.</p>
<h2>Cool, so what is it?</h2>
<p>Now that we know what the developers who build React intended for it, we can explore what it is in practice.</p>
<p>What sets React apart from things like Angular, or Backbone, or any of the other million frameworks is what sets any framework from any other. Adoption. React is incredibly actively maintained, with features coming out frequently that continue to help folks make declarative, dynamic, user interfaces.</p>
<p>Without getting too far into the weeds, React by itself does not inherently provide any performance, or security benefits to a given website. But in conjunction with many other engineering practices, React <em>can</em> help to create a far more maintainable application when complex interactions and data storage are required by the user interface.</p>
<p>Early on in our discovery process, we always sit down with the client and try to understand their business objectives, key performance indicators, and other metrics that we can use to measure success. Let's look at two different scenarios, one where React may not be the best fit, and another where it will.</p>
<h3>Scenario 1: The "Oohhh Shiny" Scenario</h3>
<p><img src="https://media.giphy.com/media/81MHl1DY9kxMI/giphy.gif" alt="Man distracted by something shiny"></p>
<p>You're encountered by Company X. Company X has a large amount of content. Company X wants to utilize our amazing UX and Design teams to find creative ways to display and share that content. They have complex post layouts, some different post types, videos, editorials, maybe a gallery. They approach you and say "Hey! We have all this content, we want a beautiful User Experience, an easy way to edit, publish and share our content. Oh, and also we've heard of React, we like how snappy and fast sites that use React are, let's make sure you build this in React for us!"</p>
<p>Here we find ourselves in a situation where Company X knows just enough about the development ecosystem to recommend React. Which is awesome, and super cool from a community standpoint that a JavaScript framework has such wide name recognition that folks outside of our sphere are even interested in it. It's worth noting that a client recommending a framework or technology should never be scoffed at. It's just our job to help them understand why they're right, or why there's a better solution.</p>
<p>In our case with Company X, React is not the best solution for a few reasons.</p>
<ul>
<li>React by itself does not make a website snappy. There are dozens of other far more nuanced pieces of technology that are being used under the hood to get applications like Facebook, or Instagram, or Pinterest, or Grubhub to be so seemingly snappy.</li>
<li>None of the requirements presented by this client lends itself to needing a framework to build a declarative and dynamic user interface. All of what was described by Client X can be done within WordPress itself, likely with the help of some fancy technology like AJAX, or the Web History API. Site speed, or page transitions, do not inherently mean a website should lean on React.</li>
</ul>
<h3>Scenario 2: The "I don't have a clever name for this" Scenario</h3>
<p>Company Y approaches you. They're a business with similar needs of Company X. Large amounts of content, multiple post layouts and types. The thing that separates Company Y here is that they also allow users to sign up for their website, which gives those users the ability to favorite, react to, organize and even post their own content.</p>
<p>Company Y requires a website where users fundamentally interact with various aspects of their product, which changes the shape of the data that exists within that website. Post #1 has 100 likes, User A has saved Post #1 to their "Awesome Posts" folder that is unique to that user. This data is highly dynamic, and is shaped by the users of the site.</p>
<p>We're now into territory where React, which is a declarative JavaScript library, that can take this dynamic, ever-changing data, and display it on the front end via well designed interface components. Company Y has a <em>possible</em> use case for React. It's important to note here, that we can <strong>absolutely</strong> build these features in a WordPress only context. The question becomes; Will React create a more maintainable, extensible and reactive application than WordPress alone will?</p>
<ul>
<li>React will create an easily manageable way for developers to pass data between various parts of the site without making multiple calls to the database to retrieve that data.</li>
<li>React will make it easy for engineers to build components that are shared between multiple views on the front end. For example, card for each individual post that shows the title, author, number of likes, and reactions to that post that shows up on the home page, in the User's specific "Awesome Posts" folder, and in search results.</li>
<li>React will help developers to use <strong>the same templates for various views while only changing the data when necessary</strong>.</li>
</ul>
<h2>What React <strong>doesn't</strong> do.</h2>
<p>React by itself is just a JavaScript library that makes it easier for developers to build declarative, dynamic user interfaces. It does NOT do the following:</p>
<ul>
<li>Inherently increase site performance</li>
<li>React does not come with a way to easily transition between pages. Consider how WordPress handles navigating between pages. When building a React app, or headless WordPress with React on the front end, the developers <em>must</em> build out the functionality to navigate between pages, posts, custom post types etc. <strong>separately</strong>.</li>
<li>React does not come with a way, by default to consume data. Connections must be made to feed the React application data from a source, whether that's the WP API, or another REST API endpoint. Connections must be made, and the data must be shaped and passed to various parts of the application.</li>
<li>React is not inherently more secure than alternative X. Regardless of the library or framework we choose to use, security can always be improved.</li>
</ul>
<p>All the points above contribute to why, in most cases, estimates for a React application tend to be higher than a WordPress build. There are many aspects of WP that come baked into WP itself. Whereas with a React application, these things we take for granted must be built from (near) scratch.</p>
<h2>Why use React at all?</h2>
<p>From an engineering standpoint, React allows us to much more easily envision a component within the larger whole of the project, abstract out the functionality and the styles, and get that component build in a clean logical way. React, in all reality is more of a gateway into a bunch of other technologies that can provide performance benefits for applications that request, consume and create <em>massive</em> amounts of data.</p>
<p>React is a tool, that helps us developers develop applications with complex logic, and nuanced interactions from the user easier.</p>
<h2>TLDR; How do I know React is right for me?</h2>
<p>I think it's important that before we sell a project we <em>consider</em> the technologies that <em>might</em> help to solve the issue for any given client. Instead of being prescriptive before we're able to do a full discovery, we need to understand the business objectives and full functional requirements for the project. Then, and only then can we prescribe the technology, or set of technologies that we can utilize to make Engineering more efficient, and the site the best it can be. In any case, here are a couple of very broad guidelines that explain where it would be safe to <em>consider</em> React..</p>
<ul>
<li>Does the clients business model dictate that many users will be directly interacting with their data (reactions, likes, account specific sections where users curate their own content)? - Consider React.</li>
<li>Is the User Interface sufficiently complex, reliant and directly affected by data (number of likes or reactions, graphs, charts, content created by users)? - Consider React.</li>
</ul>
<p>Resources used for this post:</p>
<ul>
<li><a href="https://blog.risingstack.com/the-history-of-react-js-on-a-timeline/">https://blog.risingstack.com/the-history-of-react-js-on-a-timeline/</a></li>
<li><a href="https://reactjs.org/docs/getting-started">https://reactjs.org/docs/getting-started</a></li>
<li><a href="https://overreacted.io/react-as-a-ui-runtime/">https://overreacted.io/react-as-a-ui-runtime/</a></li>
<li><a href="https://tylermcginnis.com/imperative-vs-declarative-programming/">https://tylermcginnis.com/imperative-vs-declarative-programming/</a></li>
</ul></content:encoded></item><item><title><![CDATA[Intro to GraphQL Queries]]></title><description><![CDATA[Recently I've been trying to get a better grasp on GraphQL. The majority of my exposure to GraphQL has been in the form of React apps. These…]]></description><link>https://dommagnifi.co/2019-01-01-intro-to-graphql-queries/</link><guid isPermaLink="false">https://dommagnifi.co/2019-01-01-intro-to-graphql-queries/</guid><pubDate>Tue, 01 Jan 2019 16:44:00 GMT</pubDate><content:encoded><p>Recently I've been trying to get a better grasp on GraphQL. The majority of my exposure to GraphQL has been in the form of React apps. These apps tend to be all set up and nicely packaged on the back end. This way, I could focus on consuming that data on the front end.</p>
<p>To get more exposure to how a GraphQL back end is set up, I've been running through the Wes Bos course <a href="https://advancedreact.com/">Fullstack Advanced
React &#x26; GraphQL</a>.</p>
<p>I want to focus on a more narrow topic within this course; the setup of a GraphQL back end, and how to interact with and change that data from the front end.</p>
<p>I won't be going over the setup of these technologies, as that's a separate post entirely. Instead I'm going to focus on how to write and use queries within a working app.</p>
<p>As a Frontend engineer, I've not had much exposure to the work that goes into setting up the GraphQL back end. Let's dive in and set up our data structure.</p>
<h2>Working With Schema</h2>
<hr>
<p>To start lets imagine we want to populate our database with a set of <code class="language-text">people</code>. Each person has an <code class="language-text">id</code>, a <code class="language-text">name</code>, and an <code class="language-text">age</code> to start with.</p>
<p>GraphQL is framework agnostic. What we explain below will also translate to any combination of technologies. We'll use what's called the "GraphQL schema language" to explain the examples below.</p>
<p>To inform GraphQL of how we want our data structured, we have to build the schema using <strong>object types</strong>. This initial schema is created like so:</p>
<div class="gatsby-highlight" data-language="graphql"><pre class="language-graphql"><code class="language-graphql"><span class="token keyword">type</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
<span class="token attr-name">id</span><span class="token punctuation">:</span> ID<span class="token operator">!</span>
<span class="token attr-name">name</span><span class="token punctuation">:</span> String<span class="token operator">!</span>
<span class="token attr-name">age</span><span class="token punctuation">:</span> Int
<span class="token punctuation">}</span></code></pre></div>
<p>Here we're telling GraphQL the following: Our database will consist of many objects of type <code class="language-text">Person</code>. Each <code class="language-text">Person</code> in the database will have an <code class="language-text">id</code> field that expects an <code class="language-text">ID</code> (<em>note: the ID value here is a value that GraphQL knows is a unique identifier</em>). We're also informing GraphQL that this field is required via use of the <code class="language-text">!</code>.</p>
<p>Moving down we explain that each person will have a <code class="language-text">name</code> field that expects a required <code class="language-text">String</code>.</p>
<p>Finally, the <code class="language-text">age</code> field may have an <code class="language-text">Int</code> (integer) value associated with it, but this field is not required.</p>
<p>There are extra identifiers and terms that could be expanded on here, but this gives us the basic sense of how to work with the GraphQL schema language. Much more in depth information about schema can be found in the <a href="https://graphql.org/learn/schema/">GraphQL docs</a>. But for now, let's move on.</p>
<h2>Querying Data</h2>
<hr>
<p>At a low level there are two basic methods for interacting with the GraphQL API. <strong>Queries</strong> and <strong>Mutations</strong>.</p>
<p><strong>Queries</strong> are just that. A way, using the GraphQL API, to query the database. Queries by themselves are simple creatures, and will return a set of fields for you to interact with on the front end. For example, if we wanted to query the database for a specific person, we can do that by writing a simple query, and also pass an argument to the query.</p>
<p>Before we do that, we need to tell GraphQL what to expect from our query.
Within our schema, in addition to defining multiple <code class="language-text">type</code>s of GraphQL services (so far <code class="language-text">Person</code> is the only service we've defined), we can also define specific Queries, and build out how those queries should be handled. For example, if we are to query a single person, we need to tell GraphQL what to return us.</p>
<div class="gatsby-highlight" data-language="graphql"><pre class="language-graphql"><code class="language-graphql"><span class="token keyword">type</span> <span class="token class-name">Query</span> <span class="token punctuation">{</span>
<span class="token attr-name">person</span><span class="token punctuation">:</span> Person
<span class="token punctuation">}</span></code></pre></div>
<p>Here, we're telling GraphQL that when we run a query for a single person, it should return us a JSON object that consists of our Type, <code class="language-text">Person</code>. Now, when we load up a GraphQL playground and query for a specific person, we should be returned the expected Type.</p>
<p>Another powerful feature in GraphQL is the ability for add <strong>arguments</strong> to our queries. This allows us to search the database for a specific person, so long as we know an identifying piece of information stored in the DB about the person. Take the following query:</p>
<div class="gatsby-highlight" data-language="graphql"><pre class="language-graphql"><code class="language-graphql"><span class="token keyword">query</span> <span class="token punctuation">{</span>
person<span class="token punctuation">(</span><span class="token attr-name">id</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
name
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></div>
<p>We are asking GraphQL for a person, whose <code class="language-text">id</code> is <code class="language-text">1</code>. We're then telling GraphQL that we only want to return that person's <code class="language-text">name</code> and nothing else. This is another part of the draw to GraphQL. No matter how much information exists in the DB, we can be quite explicit in the data that we are returned. This creates some super efficient queries that return us only what we need.</p>
<p>Our query above will return the following:</p>
<div class="gatsby-highlight" data-language="json"><pre class="language-json"><code class="language-json"><span class="token punctuation">{</span>
<span class="token property">"data"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">"person"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Alan Watts"</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></div>
<p>All of the data returned is wrapped in the <code class="language-text">data</code> object. This is true for any GraphQL query. What's neat is we can use arguments to query any one of the fields for our the <code class="language-text">Person</code> type that we built out!</p>
<h3>Querying all of a type</h3>
<p>What we've done above is super useful if we know the specific information of the person that we're querying. As long as we're okay being returned one singular <code class="language-text">Person</code> then we're in good shape, but what if we want to query the database for all <code class="language-text">People</code>?</p>
<p>Similar to how we defined our singular <code class="language-text">person</code> query, we will also need to define a <code class="language-text">people</code> query. That will be executed like so:</p>
<div class="gatsby-highlight" data-language="graphql"><pre class="language-graphql"><code class="language-graphql"><span class="token keyword">type</span> <span class="token class-name">Query</span> <span class="token punctuation">{</span>
<span class="token attr-name">person</span><span class="token punctuation">:</span> Person
<span class="token attr-name">people</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>Person<span class="token punctuation">]</span><span class="token operator">!</span>
<span class="token punctuation">}</span></code></pre></div>
<p>In addition to our <code class="language-text">person</code> query, we've added a <code class="language-text">people</code> query, that we expect to return an array of multiple <code class="language-text">People</code> types! I've added an <code class="language-text">!</code> to signify to GraphQL that if we are querying for multiple people, it <em>must</em> return the array. It can be an empty array, but the array itself is required.</p>
<p>Sweet! Now we can query all the people in our database by simply writing:</p>
<div class="gatsby-highlight" data-language="graphql"><pre class="language-graphql"><code class="language-graphql"><span class="token keyword">query</span> <span class="token punctuation">{</span>
people <span class="token punctuation">{</span>
id
name
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></div>
<p>Which will return:</p>
<div class="gatsby-highlight" data-language="json"><pre class="language-json"><code class="language-json"><span class="token property">"data"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">"people"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token property">"id"</span><span class="token operator">:</span> <span class="token number">1</span>
<span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Alan Watts"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
<span class="token property">"id"</span><span class="token operator">:</span> <span class="token number">2</span>
<span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Stanisław Szukalski"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre></div>
<p>This has been a high level look at working with and building Queries in GraphQL, but it should be a good start to get you at least playing around with it. GraphQL is super powerful, and has been very interesting to work with. The major pain point in my eyes though, is getting up and running with GraphQL in the sense that the back end portion of your application needs a connection to the front end. Also, one of the things that GraphQL <em>is not</em> intended to be used for, is altering data before it's passed to the database. No logic, or modification of data can be done with GraphQL alone.</p>
<p>In the Wes Bos course I mentioned at the beginning of this post, he sets up his application using React, Apollo, GraphQL, and GraphQL Yoga. Apollo is the connection between the front end of the application, and the back end, allows you to run queries on the front end of your application, and be returned data that you can work with. GraphQL Yoga, allows you to do logic on data before it's passed to GraphQL on the backend. All of these technologies work great together, but require a bit of setup.</p>
<p>For my own sanity (and possibly for yours if it's useful) I've set up a <a href="https://github.com/magnificode/react-apollo-prisma-yoga-boilerplate">pretty bare bones application framework</a> that has these bits of technology, and a few more, set up and ready to rock. It's the result of the setup steps that Wes Bos goes through in his course, which I highly, highly recommend buying and diving into, because he does a phenomenal job of explaining these technologies and their specific benefits. And of course, it's always really beneficial to understand how to set up an application from scratch.</p></content:encoded></item><item><title><![CDATA[Understanding Grid Display, Tracks And The New fr Unit.]]></title><description><![CDATA[Alright, so I'm going to be writing about the new snazzy CSS Grid property and all it's components. I'm going to try to do this in bite…]]></description><link>https://dommagnifi.co/2017-06-06-understanding-grid-display-tracks-and-the-new-fr-unit/</link><guid isPermaLink="false">https://dommagnifi.co/2017-06-06-understanding-grid-display-tracks-and-the-new-fr-unit/</guid><pubDate>Tue, 06 Jun 2017 16:44:00 GMT</pubDate><content:encoded><p>Alright, so I'm going to be writing about the new snazzy CSS Grid property and all it's components. I'm going to try to do this in bite sized chunks so as to not write a blog post that encompasses all 18 new properties.</p>
<p>Let's get rollin.</p>
<h3>Display</h3>
<p>There's three new values associated with the display property, <code class="language-text">grid</code>, <code class="language-text">inline-grid</code>, and <code class="language-text">subgrid</code>. The first two are reminiscent of what we've seen in the past with flexbox. The <code class="language-text">grid</code> value generates a block level grid element, and <code class="language-text">inline-grid</code> does just what it says, generates an inline-level grid element. Subgrid however, will require a bit of an extended explanation.</p>
<h4>Subgrid</h4>
<p>The short and sweet definition for subgrid is best summarized by the <a href="https://css-tricks.com/snippets/css/complete-guide-grid/">CSS-Tricks Complete Guide to Grid</a></p>
<blockquote>
<p><strong>subgrid</strong> - if your grid container is itself a grid item (i.e. nested grids), you can use this property to indicate that you want the sizes of its rows/columns to be taken from its parent rather than specifying its own.</p>
</blockquote>
<p>Unfortunately, as the CSS Grid Guru Rachel Andrew points out, <a href="https://rachelandrew.co.uk/archives/2017/03/16/subgrid-moved-to-level-2-of-the-css-grid-specification/">Subgrid moved to Level 2 of the CSS Grid specification</a>.</p>
<p>For examples sake, check out this CodePen demo below:</p>
<p data-height="413" data-theme-id="21523" data-slug-hash="bRNGoz" data-default-tab="css,result" data-user="magnificode" data-embed-version="2" data-pen-title="bRNGoz" data-editable="true" className="codepen">See the Pen <a href="https://codepen.io/magnificode/pen/bRNGoz/">bRNGoz</a> by Dominic Magnifico (<a href="https://codepen.io/magnificode">@magnificode</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
<p>What subgrid will effectively do is remove the need for us to define the <code class="language-text">grid-template-columns</code> on each individual <code class="language-text">.grid-item</code>. It will inherit the value that we provided to the <code class="language-text">.grid</code> parent, thus keeping our code nice and slim. Until Grid Level 2 ships however, we'll have to use the method shown above.</p>
<p>Let's tackle the other two properties we see in this example, along with some of the syntax attached.</p>
<h3>grid-template-columns (and rows). AKA Tracks.</h3>
<p>This is the bread and butter of CSS Grid. After we've told a container to be <code class="language-text">display: grid;</code> we need to tell it how we want the grid items within it to behave. By applying <code class="language-text">grid-template-columns</code> or <code class="language-text">grid-template-rows</code> to the wrapper we can essentially define a template for how we want its children to behave. Here's a simple example:</p>
<div class="gatsby-highlight" data-language="html"><pre class="language-html"><code class="language-html"> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>grid<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>grid-item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Sweet Grid!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>grid-item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Sweet Grid!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>grid-item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Sweet Grid!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></code></pre></div>
<p>And the associated CSS.</p>
<div class="gatsby-highlight" data-language="css"><pre class="language-css"><code class="language-css"> <span class="token selector">.grid</span> <span class="token punctuation">{</span>
<span class="token property">display</span><span class="token punctuation">:</span> grid<span class="token punctuation">;</span>
<span class="token property">grid-template-columns</span><span class="token punctuation">:</span> 300px 300px 300px<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre></div>
<p>Each of the <code class="language-text">300px</code> definitions in the CSS above, relates to one column. This format persists for any number of columns or rows (also called tracks in <a href="https://www.w3.org/TR/css-grid-1/#grid-track-concept">CSS Grid-Speak</a>).</p>
<h3>Ugh, so much typing</h3>
<p>I'm sure you're sitting at your computer thinking "Gee that's a lot of typing if you have a bunch of tracks."</p>
<p><img src="https://bukk.it/ugh.gif" alt="ugh"></p>
<p>Boy howdy are you in luck. Within the <code class="language-text">grid-template-columns</code> property we have a nifty function we can utilize called <code class="language-text">repeat()</code>. As you likely saw in the CodePen example above, our grid wrapper had this declaration <code class="language-text">grid-template-columns: repeat(3, 1fr);</code>. I bet you can figure out how to use this function. The first integer in the function represents how many times we'd like to repeat the following track sizing. The second parameter we pass to the function is the size. Which leads us to the next section, on the <code class="language-text">fr</code> unit.</p>
<h3>Pretty <code class="language-text">fr</code>ickin' cool huh?</h3>
<p>It's late, this blog post is getting long, and I'm getting overzealous with the puns. Lets wrap this up eh? The last thing we'll talk about is the new <code class="language-text">fr</code> unit. This unit allows us to define flexible lengths and represents a fraction of the free space within the grid container.</p>
<p><em>One important thing to note</em> with the fr unit. Unlike, px, em, or percentages, the fr unit is not defined as a length. Which means it cannot be used in combination with px, em or percentages within something like the <code class="language-text">calc()</code> function.</p>
<p><a href="https://www.w3.org/TR/css-grid-1/#fr-unit">The spec itself</a> spells out the calculations that are taking place to define the available free space. But what it boils down to in my head is this; Setting our <code class="language-text">grid-template-column</code> track size to <code class="language-text">1fr</code> gives us equally proportional grid items based on the width of the container.</p>
<p>We are able to use decimals in conjunction with the fr unit. In our example, if we change our value to say, <code class="language-text">.5fr</code> nothing changes. This is because each of our three items should still remain proportional. Because there are three items to a track, unless we tell them to be less than <code class="language-text">.33333fr</code>, they will remain proportional.</p>
<p>It's super difficult to talk about CSS grid in a general sense since there is <strong>so much</strong> in the spec. But hopefully this is bit-sized enough for you to digest. And honestly this blog post is as much for me as it is for you, dear reader. It should be fun to write some more of these posts as I stumble my way through the grid.</p>
<p><img src="https://media.giphy.com/media/oSYflamt3IEjm/giphy.gif" alt="the grid"></p></content:encoded></item><item><title><![CDATA[The Case for (or against) the CSS @supports rule]]></title><description><![CDATA[With the increasing support for the feature detection CSS property, I figured it might be a cool idea to dive into this feature a little…]]></description><link>https://dommagnifi.co/2017-03-06-the-case-for-or-against-the-supports-rule/</link><guid isPermaLink="false">https://dommagnifi.co/2017-03-06-the-case-for-or-against-the-supports-rule/</guid><pubDate>Mon, 06 Mar 2017 16:44:00 GMT</pubDate><content:encoded><p>With the increasing support for the feature detection <code class="language-text">@supports</code> CSS property, I figured it might be a cool idea to dive into this feature a little bit. While listening to <a href="http://shoptalkshow.com/episodes/253-rapidfire-84/">Episode 253</a> of ShopTalk show, Mr. Chris Coyier mentioned possibly a GitHub repo or blog post that has some pre-baked use cases for the <code class="language-text">@supports</code> property, and here we are!</p>
<p>Here's a quick little description about what the <code class="language-text">@supports</code> property does; Essentially, <code class="language-text">@supports</code> works just like a media query, but detects certain features, rather than viewport dimensions, media types, etc. A feature query more or less. Using <code class="language-text">@supports</code> in <a href="http://caniuse.com/#feat=css-featurequeries">browsers that support it</a> (Everything except for IE11 and lower at this point) gives us the ability to check wether or not a feature is supported in that particular browser. If it is, the subsequent CSS rules are enforced. If the browser does not support the property, it will just ignore the rule altogether, no harm, no foul.</p>
<p>The theory is that this would give us an opportunity to conditionally apply CSS based on browser support. A neat thought for sure, but in researching for this blog post, I had a hard time coming up with a solid set of recipes for the rule. Let's run through some of the basic scenarios that crossed my mind first.</p>
<h2><code class="language-text">@supports(display: flexbox)</code></h2>
<p>Alright, so a colleague of yours just sent you an awesome design. Nice grid layout. Time to <strong>flex</strong> your CSS muscles and use some flexbox. But oh no! The accounts team just informed you that you have to support IE9. Well shoot, let's play out how this scenario could go utilizing the <code class="language-text">@supports</code> rule. Lets use the classic use case for flexbox; You have multiple things (<code class="language-text">.thing</code>) with a heading and description inside. The description text length is variable, and you want to make sure all the <code class="language-text">.thing</code>s are the same height, regardless of content length. Maybe something like this:</p>
<div class="gatsby-highlight" data-language="scss"><pre class="language-scss"><code class="language-scss"> <span class="token selector">.thing </span><span class="token punctuation">{</span>
<span class="token property">border</span><span class="token punctuation">:</span> 2px solid #efefef<span class="token punctuation">;</span>
<span class="token property">margin</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
<span class="token property">padding</span><span class="token punctuation">:</span> 20px<span class="token punctuation">;</span>
<span class="token comment">// Fallback for IE9 if browser</span>
<span class="token comment">// doesn't support flexbox.</span>
<span class="token property">float</span><span class="token punctuation">:</span> left<span class="token punctuation">;</span>
<span class="token property">width</span><span class="token punctuation">:</span> <span class="token function">calc</span><span class="token punctuation">(</span>33.333333% <span class="token operator">-</span> 20px<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token selector">h2 </span><span class="token punctuation">{</span>
<span class="token property">color</span><span class="token punctuation">:</span> #2b2b2b<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.description </span><span class="token punctuation">{</span>
<span class="token property">font-size</span><span class="token punctuation">:</span> 1rem<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// Insert fanciness here</span>
<span class="token atrule"><span class="token rule">@supports</span> <span class="token punctuation">(</span><span class="token property">display</span><span class="token punctuation">:</span> flexbox<span class="token punctuation">)</span></span> <span class="token punctuation">{</span>
<span class="token selector">.thing-wrapper </span><span class="token punctuation">{</span>
<span class="token property">align-items</span><span class="token punctuation">:</span> stretch<span class="token punctuation">;</span>
<span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.thing </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 0 1 33.333333%<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></div>
<p>Awesome! That looks like a solid use case for <code class="language-text">@supports</code>. We've got a percentage width, and a float applied to the <code class="language-text">.thing</code> as a fallback for if flexbox is not supported. 👍</p>
<p>However, to play devils advocate here, what's to stop us from just doing this:</p>
<div class="gatsby-highlight" data-language="scss"><pre class="language-scss"><code class="language-scss"> <span class="token selector">.thing-wrapper </span><span class="token punctuation">{</span>
<span class="token property">align-items</span><span class="token punctuation">:</span> stretch<span class="token punctuation">;</span>
<span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.thing </span><span class="token punctuation">{</span>
<span class="token property">border</span><span class="token punctuation">:</span> 2px solid #efefef<span class="token punctuation">;</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 0 1 33.333333%<span class="token punctuation">;</span>
<span class="token property">margin</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
<span class="token property">padding</span><span class="token punctuation">:</span> 20px<span class="token punctuation">;</span>
<span class="token comment">//IE9 support</span>
<span class="token property">float</span><span class="token punctuation">:</span> left<span class="token punctuation">;</span>
<span class="token property">width</span><span class="token punctuation">:</span> <span class="token function">calc</span><span class="token punctuation">(</span>33.333333% <span class="token operator">-</span> 60px<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token selector">h2 </span><span class="token punctuation">{</span>
<span class="token property">color</span><span class="token punctuation">:</span> #2b2b2b<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.description </span><span class="token punctuation">{</span>
<span class="token property">font-size</span><span class="token punctuation">:</span> 1rem<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></div>
<p>The <code class="language-text">.thing-wrapper</code> properties will be ignored, as IE9 doesn't recognize them, and the float and width properties will be ignored on newer browsers, as the <code class="language-text">display: flex</code> property takes over. Now, organizationally, it might be nice to throw those properties that older browsers don't support into a nice block. But functionally, the benefit seems a bit limited.</p>
<h2>Hello Operators</h2>
<p>Let's try another example. The <code class="language-text">@support</code> rule also accepts a few additional operators: <code class="language-text">not</code>, <code class="language-text">and</code>, and <code class="language-text">or</code>. You can chain these operators to get some fancy results. For example, you can tell tell the <code class="language-text">@support</code> rule to fire when a browser does not support a CSS property.</p>
<p>Let's say we want to try out some awesome fancy CSS variables (not utilizing SCSS as my example above does). If the browser does not support CSS variables, then fire off some CSS to compensate.</p>
<div class="gatsby-highlight" data-language="css"><pre class="language-css"><code class="language-css"> <span class="token atrule"><span class="token rule">@supports</span> not <span class="token punctuation">(</span><span class="token property">--color-1</span><span class="token punctuation">:</span> #c0ffee<span class="token punctuation">)</span></span> <span class="token punctuation">{</span>
<span class="token selector">.coffee</span> <span class="token punctuation">{</span>
<span class="token property">color</span><span class="token punctuation">:</span> #c0ffee<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></div>
<p>A basic example, but it gets the job done. What we're saying here is if the browser does <em>not</em> support the use of CSS variables, apply the color directly to the coffee div. This sounds pretty good, but now we have to consider if the browsers this declaration will apply to, even support the <code class="language-text">@supports</code> rule. If the browser does not support the <code class="language-text">@supports</code> rule to begin with, this chunk of code will get skipped over, and our coffee div will not get the styling we defined within.</p>
<p>For this specific example, CSS variables <a href="http://caniuse.com/#feat=css-variables">are not supported</a> in Edge v14. However the <code class="language-text">@supports</code> rule is. Therefore, this chunk of code will work! Which is great, this definitely seems like a decent use case in this specific scenario.</p>
<h2>Additional Scenarios</h2>
<p>To try to remain as unbiased as I could after these two original thoughts, I also ran through a few more scenarios that I won't go into as verbosely as I did in the two above.</p>
<p>I tried to find some benefit in the use of the rule for CSS keyframes, which ended with the same issues I had with our first flexbox example. I checked out the new <code class="language-text">display: flow-root</code> property coming down the line (It's <a href="https://css-tricks.com/display-flow-root/">a neat property</a> that replaces the long standing clearfix). This was a good case for the <code class="language-text">not</code> operator, much like our CSS variable example. And lastly I checked out the use case for CSS 3D transforms. And much like our flexbox example, found the same issues.</p>
<h2>TL;DR</h2>
<p>The bottom line here I think, is that the idea of the <code class="language-text">@supports</code> rule was a fantastic one in the days of yore, when browser support for various CSS properties was a vast, convoluted mess. But today, browser vendors are beginning to support properties at roughly the same time. And even if they don't, it's not as if the same logic JavaScript follows, applies here. If a browser doesn't recognize a CSS property, it skips over it and moves on. There's no perceived performance benefit to utilizing the <code class="language-text">@supports</code> rule as far as I can tell. There are certainly a few good cases to utilize <code class="language-text">@supports</code> and the <code class="language-text">not</code> operator. Especially for older versions of Edge that don't support things like CSS variables. However, to me, the <code class="language-text">@supports</code> rule seems mostly cosmetic, and provides a clean way to organize CSS properties that your current project's browser requirements may not allow for you to utilize.</p></content:encoded></item><item><title><![CDATA[Introduction to ARIA Attributes]]></title><description><![CDATA[Making the web accessible for all who use it is a massively important part of our job. Recently I was able to work on a component library…]]></description><link>https://dommagnifi.co/2016-09-23-introduction-to-aria-attributes/</link><guid isPermaLink="false">https://dommagnifi.co/2016-09-23-introduction-to-aria-attributes/</guid><pubDate>Fri, 23 Sep 2016 16:44:00 GMT</pubDate><content:encoded><p>Making the web accessible for all who use it is a massively important part of our job. Recently I was able to work on a component library that had an emphasis on accessibility, and I got to learn a pretty decent amount about ARIA roles and attributes that help browsers and screen readers to properly identify and define various components.</p>
<h3>What is ARIA?</h3>
<blockquote>
<p>"WAI-ARIA, the Accessible Rich Internet Applications Suite, defines a way to make Web content and Web applications more accessible to people with disabilities. It especially helps with dynamic content and advanced user interface controls developed with Ajax, HTML, JavaScript, and related technologies."
— <a href="https://www.w3.org/WAI/intro/aria.php">https://www.w3.org/WAI/intro/aria.php</a></p>
</blockquote>
<p>At it's core ARIA aims to help folks with disabilities, particularly people that rely on a screen reader or cannot use a mouse. Tab targeting and properly defined components on the page help accomplish this.</p>
<h3>How do you use it?</h3>
<p>The spec linked above goes into great detail about the vast spectrum of ARIA roles and attributes. These attributes are added to various DOM nodes just as any other attribute is added. A relatively common example is that of the icon. When you're utilizing some sort of technique that you wouldn't want a screen reader to attempt to decipher, you would tell that screen reader to essentially skip the node.</p>
<div class="gatsby-highlight" data-language="html"><pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>i</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>super-fancy-icon<span class="token punctuation">"</span></span> <span class="token attr-name">aria-hidden</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>i</span><span class="token punctuation">></span></span></code></pre></div>
<p>All the various ARIA attributes are implemented this way. Some of these attributes require an ID to reference back to, others don't come across with the <code class="language-text">aria-</code> prefix. Lets get into some examples to see that in action.</p>
<h3><code class="language-text">role</code></h3>
<p><a href="https://www.w3.org/TR/wai-aria-1.1/#usage_intro">https://www.w3.org/TR/wai-aria-1.1/#usage_intro</a></p>
<p>The ARIA <code class="language-text">role</code> attribute is set on an element in the DOM that normal HTML5 implied semantics are not applied to (the <code class="language-text">&lt;nav&gt;</code> element for example has a 'nav' role implied with it). For more complex pieces of functionality, a <code class="language-text">role</code> must be applied in order for assistive technologies to be able to correctly interpret, and so as to not confuse that element with similar elements. Here's an example:</p>
<div class="gatsby-highlight" data-language="html"><pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">role</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>menubar<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">role</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>menuitem<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>About<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">role</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>menuitem<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Work<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">role</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>menuitem<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Contact<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">></span></span></code></pre></div>
<p>There are various roles that an element cat have, which are <a href="https://www.w3.org/TR/wai-aria/roles#widget_roles">listed here</a>. The ones above are explained by the following:</p>
<h4><code class="language-text">menubar</code></h4>
<p><a href="https://www.w3.org/TR/wai-aria/roles#menubar">https://www.w3.org/TR/wai-aria/roles#menubar</a></p>
<blockquote>
<p>A presentation of menu that usually remains visible and is usually presented horizontally.</p>
</blockquote>
<h4><code class="language-text">menuitem</code></h4>
<p><a href="https://www.w3.org/TR/wai-aria/roles#menuitem">https://www.w3.org/TR/wai-aria/roles#menuitem</a></p>
<blockquote>
<p>An option in a set of choices contained by a menu or menubar.</p>
</blockquote>
<p>There are quite a few other ARIA attributes that can help to define a navigation or a menu on attributes that assistive technologies do not traditionally identify as such.</p>
<h3><code class="language-text">aria-hidden</code></h3>
<p><a href="https://www.w3.org/TR/wai-aria-1.1/#aria-hidden">https://www .w3.org/TR/wai-aria-1.1/#aria-hidden</a></p>
<p>This one is relatively common. I'm sure you have seen a technique or a methodology that utilizes this ARIA attribute. Especially in cases that require the hiding of visibly rendered content. This commonly pops up when you're employing some sort of icon system (SVGs can be made <a href="https://css-tricks.com/accessible-svgs/">very accessible</a>), or image replacement and things of that nature.</p>
<div class="gatsby-highlight" data-language="html"><pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>screen-reader-text icon-codepen<span class="token punctuation">"</span></span> <span class="token attr-name">aria-hidden</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span> <span class="token punctuation">></span></span>CodePen<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">></span></span></code></pre></div>
<p>The spec recommends caution when utilizing this attribute because you're choosing to hide the visibly rendered content from assistive technologies. So be absolutely sure that the content that is output is accessible by those technologies elsewhere on the site.</p>
<h3><code class="language-text">aria-selected</code></h3>
<p><a href="https://www.w3.org/TR/wai-aria-1.1/#aria-selected">https://www.w3.org/TR/wai-aria-1.1/#aria-selected</a></p>
<p>With <code class="language-text">aria-selected</code>, you're telling assistive technologies that a component (or multiple components) on the page are selected. A relatively common use case here is with something like tabs or accordions.</p>
<div class="gatsby-highlight" data-language="html"><pre class="language-html"><code class="language-html"> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>#<span class="token punctuation">"</span></span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-header<span class="token punctuation">"</span></span> <span class="token attr-name">aria-selected</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Selected Accordion Header<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-content<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Super fancy tab content!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>#<span class="token punctuation">"</span></span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-header<span class="token punctuation">"</span></span> <span class="token attr-name">aria-selected</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>false<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Not Selected Accordion Header<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-content<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Super fancy tab content!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>#<span class="token punctuation">"</span></span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-header<span class="token punctuation">"</span></span> <span class="token attr-name">aria-selected</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>false<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Not Selected Accordion Header<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-content<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Super fancy tab content!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></code></pre></div>
<p>In the snippet above, the first accordion item is selected, and likely expanded (check out the next section for an explanation of aria-expanded). As the user navigates through the accordion, the <code class="language-text">aria-selected</code> attribute should be toggled.</p>
<h3><code class="language-text">aria-expanded</code></h3>
<p><a href="https://www.w3.org/TR/wai-aria-1.1/#aria-expanded">https://www.w3.org/TR/wai-aria-1.1/#aria-expanded</a></p>
<p>Much like selected, this one is pretty self explainitory. <code class="language-text">aria-expanded</code> indicates to assistive technologies wether or not an item is expanded or collapsed. We'll expand on our above example.</p>
<div class="gatsby-highlight" data-language="html"><pre class="language-html"><code class="language-html"> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>#<span class="token punctuation">"</span></span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-header<span class="token punctuation">"</span></span> <span class="token attr-name">aria-selected</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span> <span class="token attr-name">aria-expanded</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Selected Accordion Header<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-content<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Super fancy tab content!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>#<span class="token punctuation">"</span></span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-header<span class="token punctuation">"</span></span> <span class="token attr-name">aria-selected</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>false<span class="token punctuation">"</span></span> <span class="token attr-name">aria-expanded</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>false<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Not Selected Accordion Header<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-content<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Super fancy tab content!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>#<span class="token punctuation">"</span></span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-header<span class="token punctuation">"</span></span> <span class="token attr-name">aria-selected</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>false<span class="token punctuation">"</span></span> <span class="token attr-name">aria-expanded</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>false<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Not Selected Accordion Header<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>accordion-content<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Super fancy tab content!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></code></pre></div>
<p>Similarly, the <code class="language-text">aria-expanded</code> attribute should be toggled as the assistive technology navigates through the accordion.</p>
<h3><code class="language-text">aria-labelledby</code></h3>
<p><a href="https://www.w3.org/TR/wai-aria-1.1/#aria-labelledby">https://www.w3.org/TR/wai-aria-1.1/#aria-labelledby</a></p>
<p><code class="language-text">aria-labelledby</code> and the next item, <code class="language-text">aria-describedby</code> are very similar. Both ARIA attributes tell a screen reader how to describe an element on the page. The value passed into this attribute is a space separated list of IDs that link to static text on the page that will be referenced to help describe the element.</p>
<div class="gatsby-highlight" data-language="html"><pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>main</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>main-content<span class="token punctuation">"</span></span> <span class="token attr-name">aria-labelledby</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>main-content-title<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>main-content-title<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>How to get started with accessibility.<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>WAI-ARIA is a technical specification that provides a framework to improve the accessibility and interoperability of web content and applications. This docu...<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>main</span><span class="token punctuation">></span></span></code></pre></div>
<p>In the above example, the <code class="language-text">aria-labelledby</code> attribute value points to the <code class="language-text">#main-content-title</code> which would be a solid short snippet of static text that describes the content in the related element.</p>
<p><em>A note of caution</em>, don't use <code class="language-text">aria-labelledby</code> in the event that you're trying to describe things like an icon, or an individual element. The <a href="https://www.w3.org/TR/wai-aria-1.1/#aria-label">aria-label</a> attribute is used to define a string that labels the current element, whereas the <code class="language-text">aria-labelledby</code> attribute references an ID.</p>
<h3><code class="language-text">aria-describedby</code></h3>
<p><a href="https://www.w3.org/TR/wai-aria-1.1/#aria-describedby">https://www.w3.org/TR/wai-aria-1.1/#aria-describedby</a></p>
<p>Much like the <code class="language-text">aria-labelledby</code> attribute, <code class="language-text">aria-describedby</code> gives the screen reader even further detail about the content associated with a certain element. It works exactly like the former by parsing a space separated list of IDs that describe the element.</p>
<div class="gatsby-highlight" data-language="html"><pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>main</span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>main-content<span class="token punctuation">"</span></span> <span class="token attr-name">aria-labelledby</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>main-content-title<span class="token punctuation">"</span></span> <span class="token attr-name">aria-describedby</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>main-content-excerpt<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>main-content-title<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>How to get started with accessibility.<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>main-content-excerpt<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>In this article we'll explore the basics regarding accessibility, and some of the fancy attributes you can start using today.<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>WAI-ARIA is a technical specification that provides a framework to improve the accessibility and interoperability of web content and applications. This docu...<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>main</span><span class="token punctuation">></span></span></code></pre></div>
<p>As you can see, quite similar, but just identified as a longer description.</p>
<h3>ARIA Done yet?</h3>
<p>Yep! That's a pretty solid start when it comes to the basic ARIA attributes I've run across recently. Next time you're building out a component on your site, give a second to think about how a screen reader would interpret that element, and perhaps these ARIA attributes will help you go forth and make a more accessible web!</p></content:encoded></item><item><title><![CDATA[The State of Web Typography]]></title><description><![CDATA[I don't know about you, but I love to look at sites like Medium, the new Google Fonts site, and any other site that has incredible…]]></description><link>https://dommagnifi.co/2016-06-21-the-state-of-web-typography/</link><guid isPermaLink="false">https://dommagnifi.co/2016-06-21-the-state-of-web-typography/</guid><pubDate>Mon, 20 Jun 2016 16:44:00 GMT</pubDate><content:encoded><p>I don't know about you, but I love to look at sites like <a href="https://medium.com/">Medium</a>, the new <a href="https://fonts.google.com/">Google Fonts</a> site, and any other site that has incredible typography. It feels good when a site is able to bring that familiar feel of print to the web. Furthermore, their amazing ability to control the multitude of typographic nuances like kerning (letter-spacing), leading (line-height) and of course font sizes that end up being perfectly balanced. With <a href="http://caniuse.com/#search=vw">browser support</a> for <code class="language-text">vw</code> and <code class="language-text">vh</code> at an acceptable level, I figured it would be fun to dive into the various methods of achieving typographic bliss.</p>
<h2>The <code class="language-text">:root</code> Method</h2>
<p>Lets start with the method I'm currently using myself. <a href="https://twitter.com/MikeRiethmuller">Mike Riethmuller</a> wrote a post titled <a href="http://madebymike.com.au/writing/precise-control-responsive-typography/">Precise control over responsive typography</a>. This method sets out to give developers control of their typography while utilizing <code class="language-text">vw</code> unit. By themselves this unit of measurement don't really offer us a whole lot of control. If we were to just do something like this:</p>
<div class="gatsby-highlight" data-language="css"><pre class="language-css"><code class="language-css"><span class="token selector">h1</span> <span class="token punctuation">{</span>
<span class="token property">font-size</span><span class="token punctuation">:</span> 5vw<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre></div>
<p>We'd get massive headings on larger browsers, and tiny ones on smaller browsers. The next step here would be to add media queries for various breakpoints and adjust the font size. Furthermore, the scaling of the font would not be smooth, instead at that breakpoint the text would jump from one size to the next. Not ideal.</p>
<h3>calc() the :root</h3>
<p>The beauty of Mike's method lies in setting the global font size, and calculating it based on the <code class="language-text">vw</code>. Once you've done this, you can utilize <code class="language-text">em</code>s on your heading or paragraph tags very consistently throughout your site. Here's Mikes CodePen demo explaining his method:</p>
<p data-height="354" data-theme-id="dark" data-slug-hash="YPJJYv" data-default-tab="css" data-user="MadeByMike" data-embed-version="2" className="codepen">See the Pen <a href="http://codepen.io/MadeByMike/pen/YPJJYv/">Precision responsive typography</a> by Mike (<a href="http://codepen.io/MadeByMike">@MadeByMike</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<script async src="//assets.codepen.io/assets/embed/ei.js"></script>
<p>Since Mikes post, a few variations of this method have popped up.</p>
<h2>Variations</h2>
<p>Zell Liew has <a href="http://zellwk.com/blog/viewport-based-typography/">a great blog post</a> that dives into the same basic principal of setting the font size globally (on the <code class="language-text">html</code> element in his case). However in Zell's variation, he sets the font size once, and then utilizes percentages of that base font size to calculate the responsive font size based on viewport units. Check out this CodePen demo to see how he's handling the calculations:</p>
<p data-height="354" data-theme-id="dark" data-slug-hash="VarLaJ" data-default-tab="css" data-user="indrekpaas" data-embed-version="2" className="codepen">See the Pen <a href="http://codepen.io/indrekpaas/pen/VarLaJ/">Cross-browser fluid type</a> by Indrek Paas (<a href="http://codepen.io/indrekpaas">@indrekpaas</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<script async src="//assets.codepen.io/assets/embed/ei.js"></script>
<p>The great thing about both of these methods is that since the global font size is set dynamically you can create vertical rhythm by utilizing <code class="language-text">em</code>s or even a <a href="https://css-tricks.com/almanac/properties/l/line-height/">unitless line height</a>.</p>
<p>And of course, <a href="https://www.smashingmagazine.com/2016/05/fluid-typography/">Smashing Magazine</a> has a pretty solid variation of this method written up as well.</p>
<h2>Molten Leading</h2>
<p>Let me start by saying, my dad joke sense of humor automatically makes me love this method. Much like the <code class="language-text">:root</code> method and it's variations I spoke about above, this one utilizes some calculations to set the line-height. <a href="http://twitter.com/nicewebtype">Tim Brown</a> has a lovely blog post entitled <a href="http://nicewebtype.com/notes/2012/02/03/molten-leading-or-fluid-line-height/">Molten leading (or, fluid line-height)</a> that started the conversation around this notion. Shortly after Mr. Mat Marquis built <a href="https://github.com/Wilto/Molten-Leading">a small jQuery function</a> that utilized the calculations from Tim's post to set a fluid line-height.</p>
<p>Though the post and the plugin are a bit dated, I still feel like the questions asked here are thought provoking ones. And perhaps with the emergence of viewport units, a calculation can be made in conjunction with the calculations made in the <code class="language-text">:root</code> method to give us even more granular control over the vertical rhythm of our typography.</p>
<h2>Other Resources</h2>
<p>I also want to give a shoutout to this beautiful article from Robin Rendle entitled <a href="https://robinrendle.com/essays/new-web-typography/">The New Web Typography</a>. He goes into great depth about the typographic decisions we make on the web, and the consequences that they yield.</p>
<h2>Wrap it up</h2>
<p>From what I can tell with a little bit of math, and some magic from viewport units, we as Front-End (Frontend, Front End) Developers are now able to think critically about the typography on our sites. Instead of just attributing random units of measurements to our headings, we're able to create some very well thought out and mathematically sound typography. Thanks for reading!</p></content:encoded></item><item><title><![CDATA[Basic Class Toggle with Vanilla JS]]></title><description><![CDATA[✨UPDATE :: New version of this post using ES6 can be found here.✨ One of my goals for this year was to start to grasp vanilla JS a little…]]></description><link>https://dommagnifi.co/2016-05-16-basic-class-toggle-with-vanilla-js/</link><guid isPermaLink="false">https://dommagnifi.co/2016-05-16-basic-class-toggle-with-vanilla-js/</guid><pubDate>Mon, 16 May 2016 16:44:00 GMT</pubDate><content:encoded><p><strong>✨UPDATE :: <a href="/2019-05-06-basic-class-toggle-with-vanilla-js-es6/">New version of this post using ES6 can be found here.</a>✨</strong></p>
<p>One of my <a href="/update/2015/12/29/2015.html">goals for this year</a> was to start to grasp vanilla JS a little bit more. While I was in school, Javascript and jQuery pretty much went hand in hand. Little was taught about vanilla JS, beyond the basics of course, in favor of the glory that was jQuery. It's understandable, that syntactic sugar is pretty irresistible.</p>
<p>I'm going to walk through a pretty common desire when writing Javascript, the class toggle.</p>
<p>Let's take a look at how we would generally do this with jQuery. Beware though, you're going to see this next snippet and think "Why are we even trying to do this in vanilla JS?!". I know...but check out <a href="http://alistapart.com/blog/post/choosing-vanilla-javascript">all</a> <a href="http://gomakethings.com/ditching-jquery-for-vanilla-js/">these</a> <a href="https://teamtreehouse.com/community/pure-javascript-vs-jquery-2">posts</a> that advocate for the performance benefits of vanilla JS. I won't get into that here, this is purely intended to be a reference for when you decide to make the switch.</p>
<p>Alright, so the basic jQuery class toggling function would look something like this:</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">'.toggle-me'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">click</span><span class="token punctuation">(</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toggleClass</span><span class="token punctuation">(</span><span class="token string">'active'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
<p>Pretty straight forward. We'll take this apart piece by piece. The first thing we need to do with vanilla JS is to actually target the element. Now as I'm sure you know there is always the option of utilizing <code class="language-text">getElementByID</code>. But in this case, we're trying to target a class. In reality, we probably want to avoid using an ID if we can since the specificity could cause issues down the road.</p>
<h2>Enter querySelector</h2>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector">MDN states</a> that querySelector:</p>
<blockquote>
<p>Returns the first element within the document that matches the specified group of selectors.</p>
</blockquote>
<p>This is perfect for our case, we currently only have one element on the page with a class name of <code class="language-text">.toggle-me</code>. If you have multiple elements on the page with the same class name, you'll need to look into <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll">querySelectorAll</a>, which I'll dive into in another blog post.</p>
<p>So, lets set our classname to a variable to make it easy to reference.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">var</span> el <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.toggle-me'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
<p>Perfect, now we need some sort of way to detect when that element is clicked. If you were lucky enough to see the days when folks used the inline <code class="language-text">onclick</code> function, then you'll have probably guessed where we're headed.</p>
<h2>The Click</h2>
<p>The <code class="language-text">onclick</code> property allows us to utilize a function expression, or a named function defined elsewhere right within it. For this example I'll utilize a function expression just to keep things brief.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">var</span> el <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.toggle-me'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
el<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'clicked!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre></div>
<p>Sweet! If you check your console you should see our 'clicked!' logging.</p>
<h2>classList</h2>
<p>The last bit of code that we'll need will do the actual toggling of the class. If you've done some searching you'll have noticed that there's not an independent toggle property in Javascript. Luckily, <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/classList">classList</a> comes to the rescue.</p>
<p>The <code class="language-text">classList</code> property by itself is pretty self explanatory. It outputs a collection of the class attributes of the passed element. If we were to run that on our <code class="language-text">.toggle-me</code> class:</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">var</span> el <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.toggle-me'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
el<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>el<span class="token punctuation">.</span>classList<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre></div>
<p>You'll notice that the output in the console provides us with a DOMTokenList object. Expanding that object will show that the first value in the object, is "toggle-me".</p>
<p>With this object we can now use the <code class="language-text">toggle()</code> method associated with the <code class="language-text">classList</code> property. Much like with jQuery, in addition to <code class="language-text">toggle()</code>, the <code class="language-text">classList</code> property gives us access to the <code class="language-text">add()</code>, <code class="language-text">remove()</code>, and <code class="language-text">item()</code> methods. For our purposes, we'll just stick with <code class="language-text">toggle()</code>.</p>
<h2>Result</h2>
<p>Unfortunately, with vanilla JS the <code class="language-text">$(this)</code> property doesn't exist. However, since we are caching the the element we are manipulating, all we need to do is attach the <code class="language-text">classList</code> property to our element, and then chain our <code class="language-text">toggle()</code> method, passing on our active class like so:</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">var</span> el <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.toggle-me'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
el<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
el<span class="token punctuation">.</span>classList<span class="token punctuation">.</span><span class="token function">toggle</span><span class="token punctuation">(</span><span class="token string">'active'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre></div>
<p>And voilà! You're now accomplishing a simple class toggle on an element utilizing pure Javascript. Pat yourself on the back for saving that extra HTTP request and removing your dependency on jQuery.</p></content:encoded></item><item><title><![CDATA[What can we do with ServiceWorker]]></title><description><![CDATA[Before you read on, be sure to check out my first blog post about the Service Worker API. Service workers require a secure connection, and…]]></description><link>https://dommagnifi.co/2016-02-02-what-can-we-do-with-service-workers/</link><guid isPermaLink="false">https://dommagnifi.co/2016-02-02-what-can-we-do-with-service-workers/</guid><pubDate>Tue, 02 Feb 2016 16:44:00 GMT</pubDate><content:encoded><p>Before you read on, be sure to check out <a href="/update/2015/12/20/service-workin-for-the-weekend.html">my first blog post</a> about the Service Worker API. Service workers require a secure connection, and that post will show you how to set your site up with CloudFlare in order to serve your site via https.</p>
<h2>First things First</h2>
<p>Regardless of our end game, we need to register our service worker. We're essentially going to be working in two javascript files. The first one we'll call <code class="language-text">app.js</code> which is where the initial scripts for your site are housed. That is where this first snippet below will be housed.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">'serviceWorker'</span> <span class="token keyword">in</span> navigator<span class="token punctuation">)</span> <span class="token punctuation">{</span>
navigator<span class="token punctuation">.</span>serviceWorker<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">'/your-site/serviceworker.js'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">reg</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Registration succeeded.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Registration failed with '</span> <span class="token operator">+</span> error<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre></div>
<p>Lets break this down and establish what is happening here.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token comment">//Check to see if service worker is supported in the current browser</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">'serviceWorker'</span> <span class="token keyword">in</span> navigator<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Register our service worker. The register function relates</span>
<span class="token comment">// to the javascript file within your site containing the</span>
<span class="token comment">// service worker directives.</span>
navigator<span class="token punctuation">.</span>serviceWorker<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">'/your-site/serviceworker.js'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">reg</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// If the registration worked, success!</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Registration succeeded.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Registration failed.</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Registration failed with '</span> <span class="token operator">+</span> error<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre></div>
<p>The code above is not the service worker itself, rather it is registering our service worker within a scope. That being said, once a page within the scope is loaded, the service worker will run. This happens individually on each page load so long as it is within scope.</p>
<h2>Cache Rules Everything Around Me</h2>
<p>Now we get to the juicy stuff. Now that we have registered a service worker, let's look at how we can utilize the cache to grab the files you'd like to utilize for offline mode on your site. In our second file, which I'm calling <code class="language-text">serviceworker.js</code> place this next snippet.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'install'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
event<span class="token punctuation">.</span><span class="token function">waitUntil</span><span class="token punctuation">(</span>
caches<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">'offline'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">cache</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> cache<span class="token punctuation">.</span><span class="token function">addAll</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
<span class="token string">'/'</span><span class="token punctuation">,</span>
<span class="token string">'/index.html'</span><span class="token punctuation">,</span>
<span class="token string">'/update/2015/12/29/2015.html'</span><span class="token punctuation">,</span>
<span class="token string">'/css'</span><span class="token punctuation">,</span>
<span class="token string">'/css/main.css'</span><span class="token punctuation">,</span>
<span class="token string">'/app.js'</span><span class="token punctuation">,</span>
<span class="token string">'/serviceworker.js'</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
<p>Since we are within the the registered service worker the scope allows us to utilize <code class="language-text">this</code>. The first line of this snippet adds an install event listener. We then chain the <code class="language-text">waitUntil</code> method which delays the proceeding code from running until the ES6 promise has been fulfilled. Then, utilizing the <code class="language-text">caches.open</code> method, we open a new cache titled offline. This is where our cached files will be stored. This returns a promise for the cache, and once that is fulfilled we utilize the <code class="language-text">addAll</code> function which adds the specified files to the cache.</p>
<p>I've added myself the homepage of my site, along with the latest post.</p>
<p>Lastly we're going to need to tell the browser that when it detects that it's offline, to access the cache that we created with the service worker and utilize the files found there.</p>
<div class="gatsby-highlight" data-language="javascript"><pre class="language-javascript"><code class="language-javascript"><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'fetch'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">var</span> response<span class="token punctuation">;</span>
event<span class="token punctuation">.</span><span class="token function">respondWith</span><span class="token punctuation">(</span>caches<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>request<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">fetch</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>request<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">r</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
response <span class="token operator">=</span> r<span class="token punctuation">;</span>
caches<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">'offline'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">cache</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
cache<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> response<span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
<p>We do this by utilizing the fetch event listener which tells the browser to respond with the cache. The first <code class="language-text">.catch</code> function detects if the promise rejects, and returns the default server response. We then utilize the <code class="language-text">.then</code> method to open our offline cache and put the response onto the page.</p>
<p>You can check out my versions of both the <a href="https://github.com/magnificode/magnificode.github.io/blob/master/app.js">app.js</a> and the <a href="https://github.com/magnificode/magnificode.github.io/blob/master/serviceworker.js">serviceworker.js</a> files.</p></content:encoded></item><item><title><![CDATA[👋 2015]]></title><description><![CDATA[I've been seeing these fantastic year in review retro articles pop up all over the place this week. I'm going to take a stab at reviewing my…]]></description><link>https://dommagnifi.co/2015-12-29-👋-2015/</link><guid isPermaLink="false">https://dommagnifi.co/2015-12-29-👋-2015/</guid><pubDate>Tue, 29 Dec 2015 16:44:00 GMT</pubDate><content:encoded><p>I've been seeing these fantastic year in review retro articles pop up all over the place this week. I'm going to take a stab at reviewing my year both professionally and personally, and then set a few goals for myself for the upcoming year. I really love this format as I feel like it will hold me accountable, and help me strive to hit my goals. The last thing I want is to let you down, dear reader!</p>
<h2>Let’s Start Professional</h2>
<blockquote>
<p>I Moved on from my job of almost 4 years with no new job lined up.</p>
</blockquote>
<p>This was a massive one for me, and really falls into both the professional and personal category. The group of folks I worked with were like family, and I <a href="https://dommagnifi.co/update/2015/09/25/being-comfortable-with-being-uncomfortable.html">wrote at length about that.</a></p>
<blockquote>
<p>I Learned some Backbone, Handlebars, and Node.</p>
</blockquote>
<p>What I didn’t get a chance to write about is what followed. A week or so after leaving I got a contracting gig with an awesome agency. I was fortunate enough to work with a pal of mine, and got to learn some Backbone, Handlebars, and Node in the process.</p>
<p>It was really the first time I got to play around with an MV* framework in production, and it was great! Backbone provided a really easy way to set up user signup, and the basic CRUD needed for the project pages associated with the site. The gig only lasted six weeks, but in those six weeks I was able to learn quite a bit. Not to mention the couple of cool components I got to build.</p>
<p data-height="268" data-theme-id="21523" data-slug-hash="GpqGOm" data-default-tab="result" data-user="magnificode" className='codepen'>See the Pen <a href='http://codepen.io/magnificode/pen/GpqGOm/'>Neat Parallax Hero Effect</a> by Dominic Magnifico (<a href='http://codepen.io/magnificode'>@magnificode</a>) on <a href='http://codepen.io'>CodePen</a>.</p>
<script async src="//assets.codepen.io/assets/embed/ei.js"></script>
<p data-height="268" data-theme-id="21523" data-slug-hash="VvyJNQ" data-default-tab="result" data-user="magnificode" className='codepen'>See the Pen <a href='http://codepen.io/magnificode/pen/VvyJNQ/'>Marty McFlyout Nav</a> by Dominic Magnifico (<a href='http://codepen.io/magnificode'>@magnificode</a>) on <a href='http://codepen.io'>CodePen</a>.</p>
<script async src="//assets.codepen.io/assets/embed/ei.js"></script>
<blockquote>
<p>I gave my first ever presentation at a local meetup!</p>
</blockquote>
<p>This was probably one of the more nerve-racking things I’ve had to do. I don’t fancy myself a very apt public speaker, however it turned out great. <a href="https://dommagnifi.co/update/2015/08/17/what-i-learned-from-my-frist-speaking-gig.html">I wrote about what I learned</a>, but the short of it is that speaking to a group of people helped me understand the subject I was talking about even better, and opened the door for me to feel comfortable giving more talks to larger audiences.</p>
<blockquote>
<p>I started writing.</p>
</blockquote>
<p>You’re reading the result of that. I rebuilt my blog with Jekyll and starting writing. The magnificode blog saw a staggering one post in 2014. I increased that number tenfold in 2015! I got super close to averaging one post a month, which I’m pretty happy about.</p>
<blockquote>
<p>I started a new job.</p>
</blockquote>
<p>Last, and certainly not least, is my new job as Senior Front-End (Frontend/Front End/Front-end) Engineer (Developer, Designer, Code Pusher). I could go on and on about how much I’ve learned in the couple months that it’s been since I started, but I’ll try and keep it short.</p>
<p>I’ve learned <a href="https://vip.wordpress.com/2014/06/20/the-importance-of-escaping-all-the-things/">the importance of escaping all the things</a> in WordPress.</p>
<p>I’ve learned how insanely extensible WordPress is by itself through the use of hooks. I almost exclusively used to use Advanced Custom Fields for extend WordPress, but I’ve learned how to eliminate the need for that almost completely. (Repeater fields, and the Flexible Content Type in ACF are still immensely helpful).</p>
<p>I’ve learned how to set up a local environment for enterprise level clients with <a href="https://github.com/Varying-Vagrant-Vagrants/VVV">vvv</a> and how to deploy those sites with <a href="http://beanstalkapp.com/">beanstalk</a>.</p>
<p>And lastly, the company I work for was kind enough to purchase the awesome <a href="https://reactforbeginners.com/">React for Beginners</a> course by <a href="http://wesbos.com/">Wes Bos</a>. I came out of that with two pages of dense notes, a hugely commented project, and a solid beginners understanding of the React framework.</p>
<p>As I write this, I’ve been fighting the feeling that I’m not growing, or expanding quick enough professionally. But the sheer amount that I have written above has proven that worry utterly wrong. In the past 365 days I have learned a ton!</p>
<h2>Gettin’ Personal</h2>
<p>Personally I hit a few milestones that I find to be quite awesome.</p>
<blockquote>
<p>I moved out of a house I was living in with 4 of my closest friends, and into a house with my girlfriend.</p>
</blockquote>
<p>Always a terrifying step in any relationship, but I’m pretty damn lucky to have such an awesome woman. The first half of the year was spent long distance as she finished up school in New York. She then moved into the house with me and my four buddies, a bold step for a girlfriend, but it worked out great. She’s without a doubt one of my closest friends, and the transition was seamless.</p>
<blockquote>
<p>I logged over 200 workouts at the gym, lifting a little over 7.5 million pounds in the process, and started eating much healthier.</p>
</blockquote>
<p>This was a big goal for me. Towards the middle of 2013 I really put a focus on my health ( Before I moved into the house with my friends I lived by myself, and living by yourself has a sneaky way of making it easy to eat EVERYTHING ALWAYS). I made it a point then to go to start going the gym, In the past year I’ve lost almost 10 pounds, and my main lifts continue to increase. Hooray progress!</p>
<blockquote>
<p>I drove from New York to Denver in 2 days.</p>
</blockquote>
<p>I’ve never been out of the country, so any travel that I do get to do is super exciting. Once my girlfriend finished school, I flew out to New York to help her move back to Denver. We drove to Chicago in one day, had a lovely dinner on the river. We then drove from to Chicago to Denver in one shot. We had to avoid some nasty storms and tornadoes in Kansas to boot!</p>
<h2>Looking Ahead</h2>
<p>This is the hard part for me, I have an insatiable desire to learn all the things, and learn them immediately, so setting reasonable goals for myself is tough, but here it goes...my professional and personal goals for 2016.</p>
<ul>
<li>Gain a better understanding of vanilla Javascript</li>
<li>
<p>To go along with that dive into WebGL and libraries like Three.js</p>
<ul>
<li>I want to get to the point where I can make something like the folks at <a href="http://activetheory.net/home">Active Theory</a> have in ther <a href="http://activetheory.net/lab">lab</a>...brilliant, beautiful stuff.</li>
</ul>
</li>
<li>Speak at three or more separate conferences/meetups</li>
<li>
<p>Attend 6 or more conferences/meetups, network, meet people, collaborate!</p>
<ul>
<li>I’m actually part of the way there, the folks at CodePen and myself have organized Denver's first <a href="https://nvite.com/CodePenDenver/">CodePen Meetup</a> in February. You should come!</li>
</ul>
</li>
<li>
<p>Draw a lot more</p>
<ul>
<li>I have a neat little drawing tablet and recently purchased some great pens for line art and illustration. I'm drawing a ton of inspiration <a href="http://www.hydro74.com/portfolio/illustration/">from</a> <a href="https://www.instagram.com/sonywicaksana/">all</a> <a href="http://chasetafoyaart.com/section/196443_Design.html">of</a> <a href="http://tattoosbylaurajade.com/tattoos/">these</a> <a href="https://www.behance.net/gallery/32179625/Fifi-Pascale">talented</a> <a href="https://www.behance.net/JackHarvatt">folks</a>.</li>
</ul>
</li>
<li>
<p>Read more</p>
<ul>
<li>I love books, I just need to make the time to read, at the very least 30 minutes five days a week before bed.</li>
</ul>
</li>
<li>Get <a href="http://img07.deviantart.net/5122/i/2006/134/9/f/goodbye_blue_sky_by_domkantthink.jpg">my 1972 Pontiac LeMans</a> repainted, and back to it’s former glory.</li>
</ul>
<p>2015 has been a year of much learning and much growth. Oh also...you know...<strong>Star Wars</strong>...</p>
<p><img src="https://media.giphy.com/media/yRw8QdcPFRyec/giphy.gif" alt="STAR WARS!"></p></content:encoded></item><item><title><![CDATA[Service Workin' for the Weekend]]></title><description><![CDATA[There's been a decent amount of talk lately about utilizing the ServiceWorker API to serve up a site offline. I believe the scenario has…]]></description><link>https://dommagnifi.co/2015-12-20-service-workin-for-the-weekend/</link><guid isPermaLink="false">https://dommagnifi.co/2015-12-20-service-workin-for-the-weekend/</guid><pubDate>Sun, 20 Dec 2015 16:44:00 GMT</pubDate><content:encoded><p>There's been a decent amount of talk lately about utilizing the ServiceWorker API to serve up a site offline. I believe the scenario has been explained in every post about service workers, but I'll re-iterate it here. Having an offline version of your site is great for folks taking the subway, people with spotty cell service, or anyone who doesn't have an immediate internet connection. In the next couple of blog posts, I'll walk you through my journey into the ServiceWorker API.</p>
<p>This first post will be an overview of what exactly a service worker is, and the prerequisites that exist to start working with them.</p>
<h2>What is a service worker?</h2>
<p>This excerpt is taken from the MDN page on the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API">service worker API</a>.</p>
<blockquote>
<p>Service workers essentially act as proxy servers that sit between web applications, and the browser and network (when available.) They are intended to (amongst other things) enable the creation of effective offline experiences, intercepting network requests and taking appropriate action based on whether the network is available and updated assets reside on the server. They will also allow access to push notifications and background sync APIs.</p>
</blockquote>
<p>That's pretty awesome. The service worker API provides us a means to detect wether or not the user can connect to the network, send them push notifications, and sync APIs in the background. My first thought when I read this definition from MDN was probably the same as yours. It seems like that API could leave someone pretty vulnerable to a man-in-the-middle attack.</p>
<p>If you read a couple paragraphs down on the MDN page, you'll notice that to use the ServiceWorker API, your URL must be behind a valid SSL certificate. Thus ensuring that the request we are making is us, and not some shadowy internet figure.</p>
<p>![Hacking is easy.]({{ "/-/hacking.gif" | prepend: site.baseurl }})</p>
<p>So the first step into the pursuit of an offline available website is to ensure your site is being served via https.</p>
<h2>SSL-ock it down!</h2>
<p>I am utilizing GitHub pages and Jekyll for my blog. What we're going to need to do is tell GitHub pages to use the HTTPS version of our site. So in the root of my site directory i have a file called <code class="language-text">CNAME</code>. In that file we need to define our custom URL.</p>
<p>In my case, the only thing in my CNAME file is the following:</p>
<div class="gatsby-highlight" data-language="html"><pre class="language-html"><code class="language-html">https://dommagnifi.co</code></pre></div>
<p>Now, I'm not a super savvy hosting person, so forgive me if my instructions on the next few steps are a bit fuzzy. Do feel free to shoot me an email (hello [at] dommagnifi.co) with suggestions for improvements and I'll be happy to consider them.</p>
<p><em>This next section involves updating nameservers. This can take a few hours to propagate so be patient.</em></p>
<p>So my next step was to ensure that my domain is using the secure URL. Unfortunately GitHub pages does not allow you to upload an SSL certificate for use. Thus you'll need to use a custom domain, and ensure that the DNS for your domain name is pointing to secure servers.</p>
<p><a href="https://www.cloudflare.com/">CloudFlare</a> offers SSL on their free account, so that's what I'll be using to obtain an SSL certificate for my domain name.</p>
<p>After running through the signup, CloudFlare will automatically detect your DNS records. Once CloudFlare does it's thing, it'll prompt you to change the nameservers. CloudFlare provides you with specific nameservers to switch over to. Log in to your domain registrar, and copy and paste your new nameservers.</p>
<p>Once all is saved, head back over to CloudFlare real quick, click on the "Crypto" tab, and set your SSL certificate to "Flexible". I've attached a screenshot below.</p>
<p>![The CloudFlare Crypto tab.]({{ "/images/2015/12/10/cloudflare.jpg" | prepend: site.baseurl }})</p>
<p>Lastly, it will probably be beneficial to force all traffic to use the HTTPS version of your site. In CloudFlare, navigate to the "Page Rules" tab, and set your URL to always use HTTPS. You can also use this section to forward the www version of your URL to non www, or vice versa, whichever you prefer.</p>
<p><strong>Several minutes later...</strong></p>
<p>![URL bar showing confirmed HTTPS]({{ "/images/2015/12/10/proof.jpg" | prepend: site.baseurl }})</p>
<p>Wonderful! Now my site is being served up via https, and in the next post we can begin diving in to some javascript to get the service worker running.</p></content:encoded></item><item><title><![CDATA[Being Comfortable With Being Uncomfortable]]></title><description><![CDATA[I'm a creature of habit. I think that's probably true for most people, especially developers. We find comfort in the repetition of a well…]]></description><link>https://dommagnifi.co/2015-09-25-being-comfortable-with-being-uncomfortable/</link><guid isPermaLink="false">https://dommagnifi.co/2015-09-25-being-comfortable-with-being-uncomfortable/</guid><pubDate>Fri, 25 Sep 2015 16:44:00 GMT</pubDate><content:encoded><p>I'm a creature of habit. I think that's probably true for most people, especially developers. We find comfort in the repetition of a well written loop. Everything comes out exactly uniform, just how we intended it.</p>
<p>That uniformity, that comfort is something we all look for in times of stress. If things aren't going right we always know we have that thing to be able to fall back on. If things get too uncomfortable we know where to look, and we know where we can go to fall back into the same routine. Quite recently I've realized the importance of reversing that train of thought. I've realized that it's beneficial to be comfortable with being uncomfortable.</p>
<p><em>That's why I left my job.</em></p>
<p>Throughout some of my blog posts you may notice my mention of the company I worked for, Zenman. I want to preface this with the fact that the folks that work at Zenman are among some of the best people I know. Quite truly they were like family, and that's what a good position at a good company does for you. It makes you feel at home. It makes you feel that if the job itself gets stressful, the people you spend your time with for most of your waking hours are the people you can fall back on. And that's the truth about the position I held at Zenman. My colleagues were more than just that, they became friends, some even great friends, and I couldn't be more thankful for that.</p>
<p>Unfortunately there comes a time where you realize that perhaps you're far too comfortable where you're at. Maybe you're not being challenged enough. And that's the point that I got to quite recently. Because I ate and breathed Zenman for the past four years, I was not willing to come to terms with this. I'm 25 years old in a <strong>Senior</strong> Development position. What on earth could I possibly have to complain about? The fact was that I didn't feel challenged enough. And it got to the point where I decided that I needed to do what was best for me, and explore my options.</p>
<p>For anyone out there who may be in a similar position than me let me just say one thing; There's nothing more important than being completely and utterly respectful when you're looking for a new job. Don't let your work slip, don't ditch your responsibilities and just say "Fuck it!". Have the capacity and professionalism to realize that the place you're working at right now was kind enough to hire you. They are still paying you to do quality work. Don't let your peers down.</p>
<p>It's a weird spot to be in, looking out for yourself while still looking out for your colleagues. It certainly felt strange to me. I felt selfish. Since I was in a leadership position perhaps it was my job to burden all this stress for the sake of the team, so that they didn't have to feel it. I rode with that for a while. I remember thinking this exact thought about 6 months ago and just riding it out thinking that it was my responsibility to handle it. But it reached a breaking point and I needed to make a change.</p>
<p>I'll never be able to repay the owner at Zenman for all the kindness he showed to me, or my co-workers for everything that they taught me. That is without a doubt the single biggest thing that I will miss, the people. But, as the title of this blog says, it's time for me to start being comfortable with being uncomfortable. And there's nothing more uncomfortable than starting a new job. But the biggest piece of advice I can offer to you dearest reader, is stick to your guns, realize when you begin to feel yourself slip into the icy grip of comfortability and make a change. Perhaps it doesn't need to be as radical as leaving the company you work for. Perhaps it's best to identify pain points in what you're doing currently and offer solutions that may keep you on your toes a little more.</p>
<p>Some change is good, as hard as it is to recognize. The change that you make will hopefully cultivate new exciting opportunities for you. And if it doesn't...at least you know you tried.</p></content:encoded></item><item><title><![CDATA[What I Learned From My First Speaking Gig]]></title><description><![CDATA[About six months ago one of the organizers of a local meetup group asked if I could give a talk. "We just need more development centered…]]></description><link>https://dommagnifi.co/2015-08-17-what-i-learned-from-my-frist-speaking-gig/</link><guid isPermaLink="false">https://dommagnifi.co/2015-08-17-what-i-learned-from-my-frist-speaking-gig/</guid><pubDate>Mon, 17 Aug 2015 09:00:00 GMT</pubDate><content:encoded><p>About six months ago one of the organizers of a local meetup group asked if I could give a talk.</p>
<p>"We just need more development centered talks."</p>
<p>This was a hell of an opportunity for me to expand as a developer, and in the process help fellow colleagues learn. Of course, I agreed. I didn't realize at the time that six months was not a whole lot of time. With a full time job, and other responsibilities, finding time to sit down and work on my slides was a challenge.</p>
<p>My talk was about process, and the importance it plays in an agency. The topic was at the front of my mind due to some changes happening at work. Some processes were falling through the cracks, and it was effecting everyone. I wanted to remind myself and everyone around me how important it is to stick to process, and how detrimental it can be to stray from it.</p>
<p>I was able to get work done here and there. A couple slides one week, complete with speaker notes, another one the next. When it came down to a couple months before the talk I figured I needed to buckle down. Fast forward a couple of sleepless nights, and a weekend of pounding out slides I was finished. The slides weren't anything complicated, but I had the content there to talk about.</p>
<p>Now, I'm going to admit that I think I'm a pretty sketchy public speaker. I could definitely use some improvement, but I was hopeful this experience would help me do that. Because of that fear of public speaking, I looked over my slides constantly. I tried to memorize my notes, added some clarification here and there, peppered in some gifs to keep people interested. About a week before the talk I began practicing with people I know. My girlfriend was kind enough to let me use her as an audience and give me some feedback. A few friends did the same. I thought I felt pretty darn good.</p>
<p>A couple days before the talk I had a shroud of doubt cast over me. I was looking at my slides for the ten thousandth time, and the flow was off. It didn't look like what I was talking about followed any coherent path. I thought I was jumping around from topic to topic without really explaining the main point. <em>I freaked out a little</em>. What would it take to change the slides? Perhaps I should just scrap it all.</p>
<p>That clearly wasn't the right choice. I took a minute, closed my laptop and realized I had been looking at the slides for too long. I'm curious if most speakers get to this point, or if this was a product of my own making. Either way I was able to step back and take a fresh look the next day.</p>
<p>The night of the talk I gave the slides one last look, practiced what I was going to say to a few co-workers and headed off.</p>
<p>The talk went great.</p>
<p>I opened up with some classic <a href="https://www.youtube.com/watch?v=8To-6VIJZRE">Steve Ballmer</a>. That got everyone interested pretty quick. The talk proceeded and I felt like I knew what I was saying. Everyone seemed pretty interested. I slipped up here and there, caught myself say "um" a little too much, but kept going. At the end there were lots of questions too, which gave me a large bit of relief. People were interested and engaged. Everything ended up pretty well.</p>
<p>The moral of this, and the advice I'd pass on to future first time speakers (even if it is in a small setting such as mine), would be to just follow your gut. You know the topic. You've been asked to speak based on what you know and that's awesome. Regardless of how many times you look at your slides, or if you second guess what you're writing, you know the topic. The slides are the hardest part, don't overthink your notes, and trust your knowledge. Practice as much as you feel you need, but <em>don't stress over it</em>, you know what you want to say, and <strong>you'll find the right way to say it</strong>. <a href="https://dommagnifi.co/update/2015/08/04/on-community.html">The Design and Development Community</a> is an amazing one. And most every person you meet will be happy to help you learn.</p></content:encoded></item><item><title><![CDATA[On Community]]></title><description><![CDATA[It never ceases to amaze me how helpful the web development community is. It's incredible to encounter people who are passionate about their…]]></description><link>https://dommagnifi.co/2015-08-04-on-community/</link><guid isPermaLink="false">https://dommagnifi.co/2015-08-04-on-community/</guid><pubDate>Tue, 04 Aug 2015 07:47:00 GMT</pubDate><content:encoded><p>It never ceases to amaze me how helpful the web development community is. It's incredible to encounter people who are passionate about their craft.</p>
<p>There's been a lot of talk lately about suspending new features on the web. Some folks seem to think that it's becoming too feature rich, too quickly. While I disagree with this sentiment, it's this type of active discussion that I love. There have been countless responses both for and against that idea, and they all have good points! That's what is so fascinating about what we do. We have the ability to disagree with an idea, but what empowers us is our logical mind. As much as we want to yell about how wrong the idea may be, or jump straight to Twitter, we pause and let that logic wash over us.</p>
<p>This fosters a community of discussion and learning for everyone. We see both sides of the same coin and are then empowered to form our own opinion. It would seem that there are few other fields of work where this ecosystem exists. The stigma of the boring 9 to 5, go to work, head down all day, a slave to the clock. But (most of us) don't have that. And even if we do, in reality, we can come to any number of places online and feel challenged. No matter how terrible you think your job is, this community provides an outlet to get back on the right path.</p>
<p>The people (obviously) are what make the community so great. It blows me away how helpful and open to growth we all are. I, much like you I'm sure, have heard "Mind taking a look at this?" or "Think I could bounce a couple of ideas off you?" countless times. I don't know about you, but every time I am asked any variation of that I get excited. Excited to help solve a new problem, or help to propel a colleague towards success in their project, to be part of a larger whole. Granted we all have our off days, I'm not exempt from that. But by and large the fact that anyone is asking you for help, is a huge compliment, and one that should not be taken lightly.</p>
<p>The bottom line is I am thrilled to be a part of this community. There are countless opportunities right around the corner, and a horde of amazing people offering their support.</p></content:encoded></item><item><title><![CDATA[Enhancing Progressive Enhancement]]></title><description><![CDATA[As the famous Dave Rupert says, "It's time for some * Law and Order sound effect here * Hot Drama." Yesterday the internet was abuzz with…]]></description><link>https://dommagnifi.co/2015-06-23-enhancing-progressive-enhancement/</link><guid isPermaLink="false">https://dommagnifi.co/2015-06-23-enhancing-progressive-enhancement/</guid><pubDate>Tue, 23 Jun 2015 15:51:00 GMT</pubDate><content:encoded><p>As the famous Dave Rupert says, "It's time for some * <em>Law and Order sound effect here</em> * Hot Drama."</p>
<p>Yesterday the internet was abuzz with the technicalities of what Progressive Enhancement really is. A few posts popped up here and there of folks defending their respective sides of the argument. Now it goes without saying that this is a great thing, and obviously what the internet is intended to do, which is foster discussion. The two main articles I'll reference in this little discussion are <a href="https://adactio.com/journal/7774">Jeremy Keith's</a> article, which is a response to <a href="https://plus.google.com/+ScottJenson/posts/S23BqQsEuvR">Scott Jenson's</a> post on Google+.</p>
<p>Both of these articles make great points. Scott Jenson, in regards to building a camera app or a chat app, notes:</p>
<blockquote>
<p>Of course you can have it fail gracefully. That is very reasonable but what are you left with, an elegant error dialog?</p>
</blockquote>
<p>This statement cuts away all the mustard and gets at, what I believe to be, the Progressive Enhancement purists. This is the main point of Scott's article. That in some cases Progressive Enhancement may not be an acceptable solution. Specifically when it comes down to the fact that the entire app is rendered useless by this practice, and <em>"...not everything devolves to content."</em>. </p>
<p>While I certainly agree with Scott here, we have to take a step back for a second. This is one of the issues that I see come up time and time again in our industry. Everyone is taking these practices, or these ideologies, at face value. It's either black, or it's white. Zero or one. There is no middle ground. And that is where I think the hang-ups start.</p>
<p>Much like the websites of yesteryear, we as developers need to be fluid. Able to adapt and interpret at the drop of a hat. Yes the description for Progressive Enhancement may be overtly specific, but much like any other process or principal, it's the core of the idea that we need to take away. In regards to Progressive Enhancement that idea is the fact that not everyone is sitting on their MacBook Pro connected to their fancy Google Fiber internet connection. People on trains on their smartphones going through tunnels losing service, people who can't afford the latest and greatest who are ecstatic to receive a donated cellphone or laptop with spotty service and connectivity, those are the people we need to keep in mind when we are developing. Our apps, our blogs, our client work, all of it needs to take into account the end user. We need to figure out how to give them the best experience utilizing the principals that an idea like Progressive Enhancement provides us.</p></content:encoded></item><item><title><![CDATA[The Ellusive Development Process]]></title><description><![CDATA[In my previous posts I discussed how to start setting up processes within your teamand the integration of that process to the whole team…]]></description><link>https://dommagnifi.co/2015-06-16-the-ellusive-development-process/</link><guid isPermaLink="false">https://dommagnifi.co/2015-06-16-the-ellusive-development-process/</guid><pubDate>Tue, 16 Jun 2015 15:51:00 GMT</pubDate><content:encoded><p>In my previous posts I discussed <a href="https://dommagnifi.co/update/2015/05/14/spinning-up-process.html">how to start setting up processes within your team</a>and <a href="https://dommagnifi.co/update/2015/06/01/the-importance-of-company-wide-respect-for-process.html">the integration of that process to the whole team</a>. Feel free to check those posts out as well. This post however is the all about the ellusive development process. Which really is <a href="http://vincentp.me/blog/my-front-end-development-process-start-to-finish/">not</a> <a href="http://blog.chartbeat.com/2014/01/30/modern-front-end-workflow-start-finish/">that</a> <a href="https://www.codeschool.com/blog/2014/11/14/front-end-process/">ellusive</a> <a href="http://code.tutsplus.com/tutorials/essential-tools-for-a-modern-front-end-development-workflow--pre-66083">at</a> <a href="http://bradfrost.com/blog/post/development-is-design/">all</a>. All of these articles are fantastic by the way, and give insight into the front end development process. What follows is the adaptation I have taken from my few years in the industry. It is by no means comprehensive or perfect. As I mentioned previously no process is perfect, and any good process is iterated upon and constantly changing, so do not take this post as gospel, use it, improve it, and tell me all about it. Next to front end development, front end development process is a passion of mine as well.</p>
<h1>The Zenman Development Process</h1>
<p>So I’ve been with Zenman for almost three years now, and I can tell you first hand that our process has grown and evolved tremendously over those three years. Back in the day we were getting printed out documents that had our schedules for the week on them, we weren’t using much of a client management tool, and our development processes were pretty much developer specific. Now don’t get me wrong, there was a method to this, and it worked pretty damn well for the company at the time. But of course, it needed to evolve.</p>
<h1>The Tools</h1>
<p>Of course there is no one set of tools that is perfect. Much like your processes, it’s a long journey of trial and error before you find that sweet spot. I’m going to go over the tools that we use and how they fit into our process.</p>
<h2>Project Management</h2>
<p>As much as we all loved the printer paper schedule, it was very hard for us to be able to keep track of all the individual items that needed to be done for each of our projects. That’s where <a href="http://www.basecamp.com">basecamp</a> comes in.</p>
<p>Basecamp gives us the ability to add projects. Within these projects we can add specific to-do’s with due dates, and assign them to people. In addition we also gain the ability to have active discussions with the client, something that has proved to be quite valuable for us since we are always working with the client in regards to their design. Basecamp has a ton of features, and if you don’t have a project management solution it’s definitely worth checking out.</p>
<p>Now within basecamp we have a specific structure for to-dos, we have a bunch of pre-defined templates that outline everything from our approval process from clients, to items that need to happen in order for us to consider a site launch successful. We have many many process oriented lists in basecamp that help to streamline our jobs, and these are all specific to Zenman, over the years we have established things that work, and things that do not. Not only for us, but for our clients. These to-do lists, and basecamp documents are incredibly helpful for us to stay on track and for us to keep a running paper trail of all communications with clients. We have protocol that forces us to ensure that everything is documented in basecamp to ensure clear, transparent communications with the client. This way, nobody is out of the loop, and anyone can jump into a project and be informed to tackle any issue or request.</p>
<h2>Agile Development</h2>
<p>There are a few tools out there that help to aid shops in staying agile. All of them center around stories, and an acceptance process with the Product Owner.<a href="http://www.pivotaltracker.com">Pivotal Tracker</a> is our tool of choice for our agile projects. This gives us a much more granular view of exactly what prices need to be built into a website.</p>
<p>We start by adding a project. Within this project we import a library of stories that are true for any responsive website. We’ve set up a public instance of an example project <a href="https://www.pivotaltracker.com/n/projects/914224">here</a>. I’ll let that project do most of the explaining, but the main takeaway here is that this gives us a very granular and trackable view of how a project is going. One of our steps in the development of a site is for us to go over the functionality, and all the layouts, and input a tracker story for each feature. This gives us a high level view of the anticipated length of the project, and allows us to see exactly what we can expect to get done that week for that project.</p>
<p>A bit of a side note here, we consider a full ‘day’ to be six hours. There are company wide meetings, and a blocked out period first thing in the morning for developers to tackle small issues that can be resolved quickly. We do these things from 8-9 in the morning so that directly afterwords we can jump right in to the big stuff, and sty focused on it for the whole day.</p>
<p>Each story is assigned a point value, or a level of effort, and based off of that we set a velocity which then anticipates the amount of stories or points that we can complete each week.</p>
<h2>CMS</h2>
<p>Every client that we work with needs to be able to update their content, or utilize a blogging platform. We use <a href="http://www.WordPress.org">WordPress</a> for that. Most of the sites that we develop are WordPress sites, and we have some processes that go along with that.</p>
<p><strong>Starter Theme</strong></p>
<p>If you google “WordPress Starter Theme” you’ll get a million results...Bones, Genesis, _S, Starkers etc. I’ve used a few of these, and Zenman started out with starkers, however a lot of these come with some bloat that we don’t necessarily need. So like all good developers, we built our own tool for the job. <a href="https://github.com/zenman/zemplate">Zemplate</a>. Very minimal, with a sprinkle of mixins, functions, and ideologies that we at Zenman find very useful.</p>
<p>Our template utilizes BEM for class naming structure, which helps to keep things clean and easy to understand regardless of the developer that looks at the project. We implemented a galactic version of Brad Frost’s <a href="http://bradfrost.com/blog/post/atomic-web-design/">Atomic Design System</a> to help keep things modular. We not only modularize our SASS, but we also modularize our template parts, and javascript. Pretty much everything is modular.</p>
<p><strong>ACF</strong></p>
<p>We found that keeping things modular allows us to build bits and pieces separate from everything else so that they can work anywhere on the site. We utilize a plugin called <a href="http://www.advancedcustomfields.com/">Advanced Custom Fields</a>. Within advanced custom fields is a piece called Flexible Content. By default WordPress only has one text area for clients to manage content, and that didn’t work for us. Our layouts are complex enough that it is imperative for clients to be able to edit content throughout the site. ACF and flexible content allow us to do this. Because we build all our components modularly, we are able to build a call to action section once, and then utilize it anywhere on the site. The client has the ability in the backend to add this piece to any page and have it work every single time. This is huge, and if you use WordPress I would highly highly recommend using ACF.</p>
<h2>Local Development</h2>
<p>This one’s pretty easy, and there isn’t much process involved with this. We use <a href="https://www.mamp.info/en/">MAMP</a> to develop locally. This gives you the ability to spin up a server locally and run a WordPress site on your machine. The only real standardization that we have here is our database naming structure. We have our local databases, prefixed with <code class="language-text">l1_</code>, we then utilize git and push to our git server which then pushes out to our development <code class="language-text">d1_</code>, testing <code class="language-text">t1_</code> and staging <code class="language-text">s1_</code> servers.</p>
<p>This utilizes some git hook sorcery that I will not pretend to be privy to. But it’s awesome, and my co-worker <a href="http://www.codepen.io/tcmulder">Tomas Mulder</a> is a git sorcerer. Ask him about it, and it’ll go right over your head.</p>
<h2>Version Control</h2>
<p>Every development team needs some sort of version control to prevent catastrophic overwrites and terrible terrible headaches. Something I’ve definitely never experienced...</p>
<p><a href="https://git-scm.com/">Git</a> is what we use for that. As I mentioned, I’m not the most versed in the land of git, but we have a few very slick git hooks that will push up the databases in addition to the files whenever we make a change. And depending on the branch you are on locally, it will push up to the correct server (dev, test or stage).</p>
<p>The one thing that we do make sure to do process-wise, is write clean, well thought out commit messages.</p>
<h2>Reviews and Testing</h2>
<p>Once a developer has completed the development of the project to the best of his or her knowledge we have a few steps to take before we send the project to the client.</p>
<p><strong>Browser Testing</strong></p>
<p>The developer will go in on their own and browser test their site in our supported browsers (The latest version and two prior major versions of Firefox, Chrome, Safari and Internet Explorer). Once all the bugs are fixed the site moves into Code Audit.</p>
<p><strong>Code Audit</strong></p>
<p>The developer and a Senior Developer will then sit down and go over the code at a high level. This step is crucial to ensure a consistency in the way that our code is written. We’ve found that when working with multiple developers it is crucial to ensure the code can be traversed by any developer at the company. We ensure things are modular, that the BEM class naming structure has been adhered to. We test the site speed and make sure that the site does not choke on slower connections or is unreasonably slow. Is the code commented in places where it makes sense (i.e complex javascript or PHP. Including usage examples). Are you using <code class="language-text">!importants</code>? I’m pretty well known at Zenman for making developers write out explanations when an <code class="language-text">!important</code> is used...I’m very specific about specificity.</p>
<p>These meetings are great because the Senior Dev gets to see the progression of the other developers at the company, but also gets to help shape the quality of code that the company produces. Every single one of these that I have done has yielded a learning experience on both ends of the table. If you can afford one more step in your process, add a Code Audit. It’ll dramatically change the code quality your company produces.</p>
<p><strong>QA</strong></p>
<p>This step gets a fresh set of eyes on the project. This new person will scour the site on all browsers again, and make note of any discrepancies between design and production.</p>
<p><strong>QC</strong></p>
<p>Once QA is complete, one of the folks from the business side of things will ensure that contractually, everything has been addressed and delivered. We again test this in all browsers to make sure we didn’t miss a single thing.</p>
<p><strong>Alpha</strong></p>
<p>Finally the site makes it to the client. We give them a chance to review the site, and provide one round of feedback. It’s important for us to ensure that the client at least has one iteration of back and forth in regards to their site. This also gives us the opportunity to defend some design decisions, and explain to the client exactly why we chose to utilize specific functionality.</p>
<p><strong>Staging and Launch</strong></p>
<p>This is the most process intensive step and varies drastically depending on your companies set up. But this is our chance as developers to check, check again, check one more time, freak out because we have a ‘feeling’ that we missed something, realize we didn’t, close our eyes, and then hit the button.</p>
<p>Here’s a list of items we ensure happen on every stage and launch that we do.</p>
<ul>
<li>Ensure it is before 2:00 MST.</li>
<li>Back up live code and database to Zenman servers.</li>
<li>Identify in changelog that the site is being staged.</li>
<li>Begin staging preparation locally on the <code class="language-text">stage</code> branch.</li>
<li>Confirm Google Analytics code is added (may not start tracking until the site is live).</li>