|
| 1 | +# Tutorial |
| 2 | + |
| 3 | +Let's start by defining a font for our menu items. |
| 4 | + |
| 5 | +```elm |
| 6 | +snippet (context "homepage") |
| 7 | + . MenuItem |
| 8 | + [ fontFamily [ "Georga", "serif" ] |
| 9 | + , fontWeight bold |
| 10 | + ] |
| 11 | +``` |
| 12 | + |
| 13 | +This will compile to the following CSS file: |
| 14 | + |
| 15 | +```css |
| 16 | +.homepageMenuItem { |
| 17 | + font-family: "Georgia", "serif"; |
| 18 | + font-weight: bold; |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +There are a few things to note here. First, `. MenuItem` compiled to |
| 23 | +`.MenuItem`. Why? |
| 24 | + |
| 25 | +The `homepage` part of `.MenuItem` comes from the `context` |
| 26 | +provided as the first argument to `snippet`, which specifies that all |
| 27 | +Class selectors, ID selectors, and animation names in this CSS snippet |
| 28 | +will be prefixed with the given context - in this case, `"homepage"`. This |
| 29 | +lets you use concise class names without clashing with other class names on the |
| 30 | +page. |
| 31 | + |
| 32 | +The `.` part of `.MenuItem` comes from the `.` operator. If we had used |
| 33 | +`#` instead, like so: |
| 34 | + |
| 35 | +```elm |
| 36 | +snippet (context "homepage") |
| 37 | + # MenuItem |
| 38 | + [ fontFamily [ "Georga", "serif" ] |
| 39 | + , fontWeight bold |
| 40 | + ] |
| 41 | +``` |
| 42 | + |
| 43 | +...it would have compiled to this instead: |
| 44 | + |
| 45 | +```css |
| 46 | +#homepageMenuItem { |
| 47 | + font-family: "Georgia", "serif"; |
| 48 | + font-weight: bold; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Finally, the `MenuItem` part of `.MenuItem` came from the `MenuItem` |
| 53 | +that was passed to the `.` operator. `MenuItem` comes from a union type we |
| 54 | +would have defined earlier, perhaps like so: |
| 55 | + |
| 56 | +```elm |
| 57 | +type HomepageClasses |
| 58 | + = MenuItem |
| 59 | + | NavBarEntry |
| 60 | + | FunkyButton |
| 61 | +``` |
| 62 | + |
| 63 | +`elm-css` calls `toString` on the union type `MenuItem` before prepending |
| 64 | +`"homepage"` from the `context` and `"."` from the `.` operator to arrive at |
| 65 | +`.homepageMenuItem`. |
| 66 | + |
| 67 | +Let's add another style. This one will decree that all the links across the land |
| 68 | +shalt henceforth be green and without underlines. |
| 69 | + |
| 70 | +```elm |
| 71 | +snippet (context "homepage") |
| 72 | + . MenuItem |
| 73 | + [ fontFamily [ "Georga", "serif" ] |
| 74 | + , fontWeight bold |
| 75 | + ] |
| 76 | + $ a |
| 77 | + [ color (rgb 0 0 128) |
| 78 | + , textDecoration none |
| 79 | + ] |
| 80 | +``` |
| 81 | + |
| 82 | +The `$` operator creates an element selector. This snippet will compile to |
| 83 | +the following. |
| 84 | + |
| 85 | +```css |
| 86 | +.homepageMenuItem { |
| 87 | + font-family: "Georgia", "serif"; |
| 88 | + font-weight: bold; |
| 89 | +} |
| 90 | + |
| 91 | +a { |
| 92 | + color: rgb(0, 0, 128); |
| 93 | + text-decoration: none; |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Note that `rgb` is a normal Elm function, so we call it as `(rgb 0 0 128)` |
| 98 | +in order to get the output of `rgb(0, 0, 128)`. |
| 99 | + |
| 100 | + |
| 101 | +Next let's add a hover style to `a` that restores the underline. |
| 102 | + |
| 103 | +```elm |
| 104 | +snippet (context "homepage") |
| 105 | + . MenuItem |
| 106 | + [ fontFamily [ "Georga", "serif" ] |
| 107 | + , fontWeight bold |
| 108 | + ] |
| 109 | + $ a |
| 110 | + [ color (rgb 0 0 128) |
| 111 | + , textDecoration none |
| 112 | + ] |
| 113 | + &: hover |
| 114 | + [ textDecoration underline ] |
| 115 | +``` |
| 116 | + |
| 117 | +The `&: hover` declaration means "copy the previous selector and add `:hover`", |
| 118 | +meaning this will create the following output. |
| 119 | + |
| 120 | +```css |
| 121 | +.homepageMenuItem { |
| 122 | + font-family: "Georgia", "serif"; |
| 123 | + font-weight: bold; |
| 124 | +} |
| 125 | + |
| 126 | +a { |
| 127 | + color: rgb(0, 0, 128); |
| 128 | + text-decoration: none; |
| 129 | +} |
| 130 | + |
| 131 | +a:hover { |
| 132 | + text-decoration: underline; |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +There are `&.` and `&#` selectors that work the same way as `&:`, so if we |
| 137 | +wanted to style only `a` tags with the class `NavLink`, we could do it like so: |
| 138 | + |
| 139 | +```elm |
| 140 | +snippet (context "homepage") |
| 141 | + . MenuItem |
| 142 | + [ fontFamily [ "Georga", "serif" ] |
| 143 | + , fontWeight bold |
| 144 | + ] |
| 145 | + $ a |
| 146 | + &. NavLink |
| 147 | + [ color (rgb 0 0 128) |
| 148 | + , textDecoration none |
| 149 | + ] |
| 150 | + &: hover |
| 151 | + [ textDecoration underline ] |
| 152 | +``` |
| 153 | + |
| 154 | +The above would compile to the following: |
| 155 | + |
| 156 | +```css |
| 157 | +.homepageMenuItem { |
| 158 | + font-family: "Georgia", "serif"; |
| 159 | + font-weight: bold; |
| 160 | +} |
| 161 | + |
| 162 | +a.homepageNavLink { |
| 163 | + color: rgb(0, 0, 128); |
| 164 | + text-decoration: none; |
| 165 | +} |
| 166 | + |
| 167 | +a.homepageNavLink:hover { |
| 168 | + text-decoration: underline; |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +However, you should not do this unless you absolutely must! It is best to |
| 173 | +[keep specificity low](https://css-tricks.com/strategies-keeping-css-specificity-low/), |
| 174 | +and if you only want to style your nav links, then make sure they have a |
| 175 | +unique class and write the stylesheet without the `a` selectors: |
| 176 | + |
| 177 | +```elm |
| 178 | +snippet (context "homepage") |
| 179 | + . MenuItem |
| 180 | + [ fontFamily [ "Georga", "serif" ] |
| 181 | + , fontWeight bold |
| 182 | + ] |
| 183 | + . NavLink |
| 184 | + [ color (rgb 0 0 128) |
| 185 | + , textDecoration none |
| 186 | + ] |
| 187 | + &: hover |
| 188 | + [ textDecoration underline ] |
| 189 | +``` |
| 190 | +-} |
0 commit comments