@apolitical/component-library 7.0.5 → 8.0.0-761-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/form/components/form/index.d.ts +1 -0
- package/index.js +31 -31
- package/index.mjs +187 -186
- package/package.json +1 -1
- package/styles/README.md +355 -20
package/package.json
CHANGED
package/styles/README.md
CHANGED
|
@@ -1,56 +1,127 @@
|
|
|
1
1
|
# Styles
|
|
2
|
+
This directory contains the browser reset and base styling for the whole platform, Sass functions and mixins to handle repeated code, and reusable variables for colours and common spacing.
|
|
2
3
|
|
|
3
|
-
- [
|
|
4
|
-
|
|
5
|
-
- [
|
|
4
|
+
We use [Sass](https://sass-lang.com/) for our styling, which gives us the flexibility of using vanilla CSS's powerful features like [layers](#layer) and container queries, while also being able to write less code with variables, mixins, and functions.
|
|
5
|
+
|
|
6
|
+
- [Set-up](#set-up)
|
|
7
|
+
- [Cascade layers (`@layer`)](#layer)
|
|
8
|
+
- [Browser reset](#browser-reset)
|
|
9
|
+
- [Base styling](#base-styling)
|
|
10
|
+
- [Accessibility](#accessibility)
|
|
11
|
+
- [Buttons](#buttons)
|
|
12
|
+
- [Text](#text)
|
|
13
|
+
- [Titles](#titles)
|
|
14
|
+
- [Functions and mixins](#functions-and-mixins)
|
|
15
|
+
- [`get-map`](#get-map)
|
|
16
|
+
- [`px-to-rem`](#px-to-rem)
|
|
17
|
+
- [`image`](#image)
|
|
18
|
+
- [`breakpoint`](#breakpoint)
|
|
19
|
+
- [`animate` and `transition`](#animate-and-transition)
|
|
20
|
+
- [`container-query`](#container-query)
|
|
21
|
+
- [`amend-selector-path`](#amend-selector-path)
|
|
22
|
+
- [Repeated styles](#repeated-styles)
|
|
23
|
+
- [Lesser needed mixins](#lesser-needed-mixins)
|
|
24
|
+
- [`query-touchscreen` and `query-not-touchscreen`](#query-touchscreen-and-query-not-touchscreen)
|
|
25
|
+
- [`target-safari`](#target-safari)
|
|
26
|
+
- [`hidden-element` and `show-hidden-element`](#hidden-element-and-show-hidden-element)
|
|
27
|
+
- [Variables](#variables)
|
|
28
|
+
- [Colours](#colours)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Set-up
|
|
34
|
+
For accessibility, we code all of our sizes in `rem`s. It's important that _every_ size is in `rem`s, otherwise some elements will resize when the user changes their browser's font size, but others won't, leading to a broken layout.
|
|
35
|
+
|
|
36
|
+
In Figma, all of the sizes are in pixels. To make it easier for us to work with (and reason about), we add them as variables in `px` and then convert them to `rem`s using a [custom mixin, `px-to-rem`](#px-to-rem).
|
|
37
|
+
|
|
38
|
+
We place sizes in Sass maps at the top of the file and use a [custom mixin, `get-map`](#get-map), to pull the value. This makes it extremely fast for us to change sizes, as we only need to update the value in one place.
|
|
39
|
+
|
|
40
|
+
See [functions and mixins](#functions-and-mixins) for more information on how these work.
|
|
41
|
+
|
|
42
|
+
<br />
|
|
6
43
|
|
|
7
44
|
---
|
|
8
45
|
|
|
9
|
-
|
|
46
|
+
<br />
|
|
47
|
+
|
|
48
|
+
## Cascade layers (`@layer`)
|
|
10
49
|
|
|
11
50
|
We're using [CSS cascade layers (`@layer`)](https://www.smashingmagazine.com/2022/01/introduction-css-cascade-layers/) to organise our styles, to help us avoid specificity issues and to make it easier to find and update styles.
|
|
12
51
|
|
|
13
|
-
The `./index.scss` file defines the order of our layers, with the
|
|
52
|
+
The `./index.scss` file defines the order of our layers, with the splash styling (the bouncing Apolitical logo we use as a loader) as the lowest.
|
|
14
53
|
|
|
15
54
|
If you need to overwrite CSS, you don't need to create an overly specific (and less performant) rule or add an `!important` (as an aside: please, please [never add an `!important`](https://dev.to/alvaromontoro/using-important-in-css-75c)) - just put the styling in a higher layer.
|
|
16
55
|
|
|
17
56
|
The only 'gotcha' with this is importing external CSS; any styling that _isn't_ in a CSS layer is considered more important than styling in CSS layers, so styling from an external package will overwrite our styling. To change this, we need to write any styling that needs to overwrite external styling outside of CSS layers. (See the `InviteForm` component for an example.)
|
|
18
57
|
|
|
58
|
+
<br />
|
|
59
|
+
|
|
19
60
|
---
|
|
20
61
|
|
|
21
|
-
|
|
62
|
+
<br />
|
|
22
63
|
|
|
23
|
-
|
|
64
|
+
## Browser reset
|
|
24
65
|
|
|
25
|
-
|
|
66
|
+
Every browser has its own styling for elements; to give us full control over the design of the page, we need to remove the default styling before we apply our own.
|
|
26
67
|
|
|
27
|
-
|
|
68
|
+
`@layer reset` is the second layer of our CSS cascade, so it's the first thing applied to the page after the logo loader (`splash`). It's a reset, not a normal CSS file, so it doesn't have any classes or IDs - it just sets the default styling for all elements.
|
|
28
69
|
|
|
29
|
-
|
|
70
|
+
We use a version of [Eric Meyer's browser reset](https://meyerweb.com/eric/tools/css/reset/), though we don't put any kind of `font` overrides in each element; we found this could cause inconsistencies when the base styling was being pulled from `component-library`, as it would override the Next page styling.
|
|
30
71
|
|
|
31
|
-
|
|
72
|
+
The styling lives in `/reset`.
|
|
32
73
|
|
|
33
|
-
|
|
74
|
+
<br />
|
|
34
75
|
|
|
35
|
-
|
|
76
|
+
---
|
|
36
77
|
|
|
37
|
-
|
|
78
|
+
<br />
|
|
38
79
|
|
|
39
|
-
|
|
80
|
+
## Base styling
|
|
81
|
+
|
|
82
|
+
We use base styling to ensure common elements on every page - things like text, headings, buttons, links, and forms - are styled in a single place and don't need to be redefined for each page. When we add a `p` or `button` to the site, it's already styled.
|
|
83
|
+
|
|
84
|
+
It's the third layer in our CSS cascade (`@layer base`), so it's applied after the browser reset, but can be overwritten on a component level if an element inside a particular component needs to look different.
|
|
85
|
+
|
|
86
|
+
Most of our base styling is for elements which we don't have components for - like `p` and `h1` - but we also keep all of the `button` and `form` styling here. While there is a `Button` component, the same styles are often used on elements which aren't interactive, like the connection statuses on profiles, and we have some form components, like `RichTextEditor` and `Search` which don't use the `Form` component; we've pulled the styling out to our base styling, as these are all important building blocks of our UI and aren't strictly linked to their functionality.
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
### Accessibility
|
|
90
|
+
#### Highlighting focus
|
|
91
|
+
Highlighting where the focus is, especially for interactive elements like links, buttons, and inputs, is an important part of the [WCAG AA guidelines](https://www.w3.org/WAI/WCAG21/Understanding/focus-visible.html).
|
|
92
|
+
|
|
93
|
+
In our base styling, we remove the browser's default `outline` on focused elements and add a `box-shadow`, which gives us a bit more control over the look.
|
|
40
94
|
|
|
41
|
-
|
|
95
|
+
Please think twice before removing the `box-shadow` on a focused element as the page its on will immediately fail accessibility.
|
|
42
96
|
|
|
43
|
-
|
|
97
|
+
#### Hiding text visually
|
|
98
|
+
We sometimes want screen readers to receive text that we don't want on the page. While it sometimes makes sense to use an `aria-label` on an element, that will override the existing text. When we want to add text, rather than replacing it, we have the `.hidden` class. It uses the `hidden-element` mixin, which can be called directly if you need more control. It sets the width and height of an element to `0`, rendering it invisible visually but ensuring it's still read out to screen readers. (Using something like `display: none` would also hide the content from screen readers.)
|
|
44
99
|
|
|
45
|
-
|
|
100
|
+
It also has an optional class, `.show-on-focus`, which makes the element show visually if it is navigated to by keyboard. For example, we have a link above our iframes which allows people to skip past the iframe, in case the third party content is not accessible. We don't want to show this text all the time, but we do want to show it to people navigating by keyboard, so they don't click something they don't mean to.
|
|
101
|
+
|
|
102
|
+
The class uses the mixin `show-hidden-element`, which can also be used independently.
|
|
103
|
+
|
|
104
|
+
The component `VisuallyHidden` uses this styling behind-the-scenes; it's best to use that directly if you're doing something simple, but the classes and mixins are available if you need them.
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
### Buttons
|
|
108
|
+
The `Button` component in the component library renders most of the buttons on the site. It can render `button` or `a` components, depending on if we need something to happen on click or if we just want a styled link. But we also sometimes want styling on elements which aren't interactive, like the connection statuses on profiles.
|
|
109
|
+
|
|
110
|
+
See the component library for in-depth documentation on `Button`.
|
|
111
|
+
|
|
112
|
+
#### Variations
|
|
46
113
|
|
|
47
114
|
By default, buttons have `primary` styling (a purple background and white text) and don't need a `primary` class to be styled. Buttons can also be `secondary` (a ghost button with a purple border) or `tertiary` (no border or background color). There are additional variations for `destructive` (only available on primary buttons), `muted` (only available on secondary and tertiary buttons), and `disabled`.
|
|
48
115
|
|
|
49
|
-
|
|
116
|
+
We also have a temporary class, `new`, which renders black buttons; this is used for the homepage, which is in the new design. We should ideally update the styling of `primary`, as this will affect the whole site in a single line of code, rather than needing manual button-by-button work, but we currently need more control over each button.
|
|
117
|
+
|
|
118
|
+
#### Sizes
|
|
50
119
|
|
|
51
120
|
By default, buttons are `medium`. They don't need a class to be styled. Buttons can also be `small` or `large`.
|
|
52
121
|
|
|
53
|
-
|
|
122
|
+
Buttons rendered with `Button` can change their size depending on the width of the viewport. See the component library documentation for an example.
|
|
123
|
+
|
|
124
|
+
#### Icons
|
|
54
125
|
|
|
55
126
|
Buttons can have an icon but - because we can't mix pre-processing and data passed into CSS for file names, like CSS variables or data attributes - icons on buttons need to be set up in advance with classes. The list of available icons is maintained in `./base/buttons/_icons.scss`. To add a new icon, add a new item to the list with arguments for the sizes on large, medium, and small buttons.
|
|
56
127
|
|
|
@@ -61,3 +132,267 @@ The icon file, with a matching name, should be uploaded to the `apolitical-asset
|
|
|
61
132
|
You can also pass `left` or `right` to position the icon - this will add margin between the text and the icon. e.g. `icon heart_empty right`.
|
|
62
133
|
|
|
63
134
|
You can change the icon on hover by adding a second file name, prepended with `hover_`, e.g. `icon heart_empty hover_heart` will change the heart icon from an outline to a filled in heart on hover.
|
|
135
|
+
|
|
136
|
+
See the `base/buttons/_icons.scss` file for the full list of available icons, and details on how to handle different sizes.
|
|
137
|
+
|
|
138
|
+
### Text
|
|
139
|
+
The text used across the site for paragraphs and lists, is styled in `base/_text.scss`.
|
|
140
|
+
|
|
141
|
+
We have three sizes of text used across the site: large (which is our default size, defined in `$default`), medium, and small.
|
|
142
|
+
|
|
143
|
+
These sizes also have different `font-size`s and `line-height`s, depending on the user's viewport.
|
|
144
|
+
|
|
145
|
+
The large size is applied by default. It can be changed to medium or small using a class, `.text-medium` or `.text-small`.
|
|
146
|
+
|
|
147
|
+
For a lot of text, the class should go on the parent element, e.g. on a `ul` or an `article`. If a single `p` is a different size, it can have the class directly.
|
|
148
|
+
|
|
149
|
+
In `base/_text.scss`, this is handled with the `$standard-text-elements` variable, which lists all of the elements which can be styled with the class on the parent and directly. (We also have some special styling, just for `p`s, to handle margins which we don't want on every text element.)
|
|
150
|
+
|
|
151
|
+
`$special-text-elements` is a list of elements which can only take the class directly, such as `a` and `span`; this is because there will often be a lot of them in a component and we don't want them to deviate from their given styles unless they have an explicit class.
|
|
152
|
+
|
|
153
|
+
(We list the elements in variables as they're used in multiple media queries and we don't want to accidentally miss anything.)
|
|
154
|
+
|
|
155
|
+
### Titles
|
|
156
|
+
The titles used across the site for headings and subheadings are styled in `base/_titles.scss`.
|
|
157
|
+
|
|
158
|
+
Our Figma files have six heading styles, named h1-h6, as well as subheading and pre-title styles.
|
|
159
|
+
|
|
160
|
+
By default, we code `h1`s to match the 'h1' styling in Figma, `h2`s to match the 'h2' styling, and so on. However, sometimes, for aesthetic reasons, we may want the styling of the 'h4' heading, but for accessibility and semantic mark-up, the heading would need to be an `h2` or a `p`.
|
|
161
|
+
|
|
162
|
+
All of the styling for each heading lives in a mixin, which can be included on any element, so we can use the styles where we need them.
|
|
163
|
+
|
|
164
|
+
<br />
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
<br />
|
|
169
|
+
|
|
170
|
+
## Functions and mixins
|
|
171
|
+
We have a lot of custom functions and mixins to make our code more maintainable and easier to write.
|
|
172
|
+
|
|
173
|
+
You won't likely need to change these functions but you should be using them in your code, to make it faster, more accessible, and more maintainable.
|
|
174
|
+
|
|
175
|
+
### `get-map`
|
|
176
|
+
Sass has a built-in function to get a value from a map (`map.get`), but it silently fails when the value can't be reached, which means a small typo in a key can mean styling you're expecting is never applied. It's easy to miss and can make us lose time.
|
|
177
|
+
|
|
178
|
+
Instead, we use a custom function, `get-map`, which throws an error if the key can't be found, so we're aware of any issues and can quickly fix them.
|
|
179
|
+
|
|
180
|
+
You shouldn't need to amend the function - just call it wherever you're retrieving a value from a map. It can take two levels of map, e.g.:
|
|
181
|
+
|
|
182
|
+
```
|
|
183
|
+
$map: (
|
|
184
|
+
value: 1,
|
|
185
|
+
key: (
|
|
186
|
+
subkey: 2
|
|
187
|
+
)
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
get-map($map, 'value') // 1
|
|
191
|
+
get-map($map, 'key', 'subkey') // 2
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### `px-to-rem`
|
|
195
|
+
It's important for accessibility that we use `rem`s, which can be scaled for the user's needs (e.g. someone with poor eyesight might need to have the screen zoomed in). Every unit of measurement should be a `rem`, as mixing different units can make the site look broken.
|
|
196
|
+
|
|
197
|
+
`1rem` will always be relative for the user, but `100px` will always be `100px`. If the user resizes their browser, `font-size: 1rem` text inside a `height: 100px` box will break out of the layout. A `6.25rem` box will resize with the text and always contain it.
|
|
198
|
+
|
|
199
|
+
However, `6.25rem` is a lot harder for us to reason about than `100px`; we know how big `100px` is and we know if it matches the Figma designs, which are always presented in `px`.
|
|
200
|
+
|
|
201
|
+
To make it easier for us, we use the same units as Figma and use `px-to-rem` to convert them.
|
|
202
|
+
|
|
203
|
+
`px-to-rem` needs a `$size` value. We use [`get-map`](#get-map) to pull the value from a map, so we only need to update the value in one place, and pass it in, like:
|
|
204
|
+
|
|
205
|
+
```
|
|
206
|
+
$wrapper: ('height': 100);
|
|
207
|
+
|
|
208
|
+
#wrapper {
|
|
209
|
+
height: px-to-rem(get-map($wrapper, 'height'));
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
By default, `px-to-rem` uses `16px` as its baseline - `1rem` is `16px`. If you need to change this, you can pass a second argument, e.g. `px-to-rem(100, 10)` will convert `100px` to `10rem`, but this isn't likely to come up!
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
### `animate` and `transition`
|
|
217
|
+
Anywhere we use CSS animations or transitions on our platform, we need to use the `animate` and `transition` mixins.
|
|
218
|
+
|
|
219
|
+
Some users have conditions like epilepsy which can be triggered by animations; for accessibility, we need to avoid doing any kind of animation if the user has set their device to reduce motion. Devices won't just do this automatically - we are responsible for not triggering animations if the user has this setting.
|
|
220
|
+
|
|
221
|
+
There is a special [media query, `prefers-reduced-motion`](https://www.smashingmagazine.com/2021/10/respecting-users-motion-preferences/) which we can use to target the users who have not opted out of animations; to avoid having to set this up everywhere you need it, and to avoid accidentally missing it and making your component inaccessible, the `animate` and `transition` mixins will do all the work for you.
|
|
222
|
+
|
|
223
|
+
#### `animate`
|
|
224
|
+
`animate` is a very simple mixin which just wraps the code inside a media query. It renders the new styling [`@at-root`](https://sass-lang.com/documentation/at-rules/at-root/), in the root of the document, so you don't have to set up the animation out of context.
|
|
225
|
+
|
|
226
|
+
To use CSS animations, wrap your code in the `animate` mixin, like:
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
.element {
|
|
230
|
+
transform: translateX(0%);
|
|
231
|
+
|
|
232
|
+
@include animate {
|
|
233
|
+
@keyframes loading {
|
|
234
|
+
100% {
|
|
235
|
+
transform: translateX(100%);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// & is the selector to this point - `.element`
|
|
240
|
+
// We're just making sure we're only adding the animation when the user has not
|
|
241
|
+
// asked for reduced motion
|
|
242
|
+
& {
|
|
243
|
+
animation: loading 0.8s infinite;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
You can see the `animate` mixin in action in the `LoadingPlaceholder` component.
|
|
250
|
+
|
|
251
|
+
#### `transition`
|
|
252
|
+
`transition` is a more complex mixin to read, though it's unlikely you will need to make changes to it.
|
|
253
|
+
|
|
254
|
+
It accepts a `$proprerties` argument - the CSS properties you want to transition, like `background` or `color` - and three optional arguments, the duration of the transition, what function to use for the animation, and the delay.
|
|
255
|
+
|
|
256
|
+
For performance reasons, it's important to only pass the properties you need to transition, rather than using `all`. Otherwise, everything will be listened for, which will slow down the page.
|
|
257
|
+
|
|
258
|
+
You can listen for multiple properties on a single element, and each property can have its own duration, function, and delay. They can also use the same, if you'd prefer.
|
|
259
|
+
|
|
260
|
+
This will transition changes to the `color` and `text-decoration-color`, using the default duration, function, and delay:
|
|
261
|
+
```
|
|
262
|
+
a {
|
|
263
|
+
@include transition(color text-decoration-color);
|
|
264
|
+
}
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
This will transition the `color` over `0.2s` with no delay and the `text-decoration-color` over `0.5s` with a delay of `0.3s`. They will both use the `ease-in-out` function:
|
|
268
|
+
```
|
|
269
|
+
a {
|
|
270
|
+
@include transition(color text-decoration-color, 0.2s 0.5s, ease-in-out, 0s 0.3s);
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
The mixin takes all the arguments, breaks them by space, and turns it into the correct transition CSS, and ensures users who have asked for reduced motion don't see it.
|
|
275
|
+
|
|
276
|
+
### `image`
|
|
277
|
+
We use the `image` mixin to handle background images on the site--this lets us easily handle CSS masks without repeating a lot of code, and it lets us have a single reference to our assets bucket, so we can easily change things.
|
|
278
|
+
|
|
279
|
+
The mixin has one required prop, which is the image file (everything after the asset bucket address, which is defined in variables), and optional props for if it's a CSS mask, the size, repeat, and position properties.
|
|
280
|
+
|
|
281
|
+
Passing just the image, like:
|
|
282
|
+
```
|
|
283
|
+
@include image('icons/tick.svg');
|
|
284
|
+
```
|
|
285
|
+
would render the image as a background image. It couldn't be recoloured.
|
|
286
|
+
|
|
287
|
+
Passing `true` as the second argument, like:
|
|
288
|
+
```
|
|
289
|
+
@include image('icons/tick.svg', true);
|
|
290
|
+
```
|
|
291
|
+
would render a CSS mask, so the icon could be recoloured to anything using `background`. This is especially useful for hover and disabled states, where we want to change the color without downloading a new asset.
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
### `breakpoint`
|
|
295
|
+
We have a custom mixin to handle media queries for us, which allows us to use easy shortcuts for the most common viewport sizes. When the design changes, we can remap these names to the new sizes, rather than having to update every media query in the code.
|
|
296
|
+
|
|
297
|
+
The common sizes are defined in `$breakpoint-sizes`. We only name the most common sizes, to keep the map manageable. It's possible to pass a number to the `breakpoint` query instead, to target a viewport we only care about in one-off cases. It's passed in as the second argument.
|
|
298
|
+
|
|
299
|
+
Using the mixin also lets us write less code for more complex media queries.
|
|
300
|
+
|
|
301
|
+
The mixin takes two arguments: the media feature you are targeting, like `max-height` or `min-width` and the viewport size, either as a named variable, like `md` or as a number, like `951`, which will be converted to `px`.
|
|
302
|
+
|
|
303
|
+
This will apply the styles when the viewport width is at least the `md` value, `769px`:
|
|
304
|
+
```
|
|
305
|
+
@include breakpoint(min-width, md) {
|
|
306
|
+
// styles
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
You can pass multiple media features and viewport sizes for media queries which have a minimum and maximum viewport size, like:
|
|
311
|
+
```
|
|
312
|
+
@include breakpoint(min-width max-width, md lg) {
|
|
313
|
+
// styles
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
The styles in this query will apply when the viewport width is at least `769px` and less than `1180px`.
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
### `container-query`
|
|
320
|
+
[Container queries](https://www.smashingmagazine.com/2021/05/complete-guide-css-container-queries/) are very similar to media queries but apply to an element's parent. We may sometimes want to change the style of an element when it's in a smaller area, like a sidebar, which won't always correlate to the viewport being small.
|
|
321
|
+
|
|
322
|
+
For example, on individual article pages, we show a `MarketingBanner` with smaller padding and no purple circle element in the sidebar and in its normal styling after the article--using its parent lets us change things in specific circumstances without needing to add a lot of extra classes.
|
|
323
|
+
|
|
324
|
+
The `container-query` mixin handles this for us. It uses the same named breakpoint sizes as `breakpoint`, so we can easily change the sizes in one place.
|
|
325
|
+
|
|
326
|
+
Instead of a media feature, its first argument is the `container-name` of the container parent, which is set as part of the parent's normal styling alongside the `container-type`. (NB: this is needed for container queries to behave as expected. Be aware container queries don't play nicely with flex styling.)
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
### `amend-selector-path`
|
|
330
|
+
Sass has a built-in function,`@at-root`, which allows you to write styling in context which is rendered in the root of the final CSS file, instead of in the current selector--like a Sass version of `React.Portal`.
|
|
331
|
+
|
|
332
|
+
We can also use it to overwrite the current selector path for a block of styling, which is useful if we have styling which should only be applied when there's a certain parent element in the path. `amend-selector-path` is a mixin which does this for us, to make it more obvious what the code is doing and to make it easier to read.
|
|
333
|
+
|
|
334
|
+
It takes two arguments, the old path and the new one.
|
|
335
|
+
|
|
336
|
+
All styles inside this would only apply to the link inside the sidebar:
|
|
337
|
+
```
|
|
338
|
+
@include amend-selector-path('a', '.sidebar a') {
|
|
339
|
+
// styles
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
### Repeated styles
|
|
345
|
+
We have a lot of repeated styles across the site, which are used across different elements. To avoid duplicating code, we have pulled the common ones out into mixins.
|
|
346
|
+
|
|
347
|
+
- `dashed-border` adds a dashed border with an inline SVG.
|
|
348
|
+
- `line-under-text` adds a colored line beneath a title. It accepts arguments for the colour and size.
|
|
349
|
+
- `full-width-section` makes an element the full width of the user's viewport. This is used in the `FullWidthSection` component (which is the recommended component to use, over this mixin), but the styling is available as a mixin in cases where it might not make sense to render a component wrapper.
|
|
350
|
+
|
|
351
|
+
### Lesser needed mixins
|
|
352
|
+
There are some mixins that aren't used as often, which are worth knowing about.
|
|
353
|
+
|
|
354
|
+
#### `query-touchscreen` and `query-not-touchscreen`
|
|
355
|
+
If you want to apply styling to only touchscreen (or non-touchscreen) devices--for example, applying hover states, you can use these mizins to limit what receives the styling.
|
|
356
|
+
|
|
357
|
+
#### `target-safari`
|
|
358
|
+
Safari has some unique bugs which can make it difficult to work with. We have a mixin, `target-safari`, which can be used to target Safari specifically, to fix these bugs.
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
#### `hidden-element` and `show-hidden-element`
|
|
362
|
+
We sometimes want to add text to the page which we don't want to be visible, but we do want to be read out by screen readers. This is often used for accessibility, to give more context to a user who can't see the screen.
|
|
363
|
+
|
|
364
|
+
We have classes and a component, `VisuallyHidden` (see the ['hiding text visually'](#hiding-text-visually)) section, which utilise these mixins. If you need to, you can call them directly.
|
|
365
|
+
|
|
366
|
+
<br />
|
|
367
|
+
|
|
368
|
+
---
|
|
369
|
+
|
|
370
|
+
<br />
|
|
371
|
+
|
|
372
|
+
## Variables
|
|
373
|
+
The variables that are re-used across the site are defined in `./variables`, such as asset bucket URLs, common sizes, and colours.
|
|
374
|
+
|
|
375
|
+
### Sizes
|
|
376
|
+
We don't store variables for repeated margins/paddings, like `md-padding`, as sizes are unlikely to stay the same between redesigns. Instead, we put those in maps at the top of every component styling file.
|
|
377
|
+
|
|
378
|
+
However, there are a few widths we frequently use for the width of the content of a page which are likely to change together when the site is redesigned. We store these in variables in `./variables/sizes/_common`, so we can manage them from one place.
|
|
379
|
+
|
|
380
|
+
There are also some variables whose sizes we need to be able to access across different components. For example, as our header can animate in and out of the layout, we need to know what height it is in many places, to ensure we're never covering content when it shows. We store the maps the header uses for its sizes in `./variables/sizes/_header`, so we can import them anywhere they're needed.
|
|
381
|
+
|
|
382
|
+
### Colours
|
|
383
|
+
Our colour variables are defined in `./variables/colors`.
|
|
384
|
+
|
|
385
|
+
We have two separate files:
|
|
386
|
+
|
|
387
|
+
- `colors.scss` defines the colours themselves, using the names as they're defined in Figma, like `b700`. **We never call these values directly in the code.**
|
|
388
|
+
|
|
389
|
+
Because the colours are described as colours (`b700`), rather than by what they're used for (`link_hover`), we can't rely on these names. In a future redesign, the links might become pale pink - at which point `b700` would no longer make sense, and would make the code hard to read and reason about. We also can't guarantee that everything which used to be `b700` is now pink in the redesign - some things may become yellow or green instead.
|
|
390
|
+
|
|
391
|
+
We can't rely on these colours making sense in the future, so we don't use them directly in the code.
|
|
392
|
+
|
|
393
|
+
- `./theme/index.scss` defines a `$theme` map which describes where the colours are used, rather than what the colours look like, e.g. `card_bg` for the default background colour of cards and `card_hover_bg` for the background colour cards should change to on hover.
|
|
394
|
+
|
|
395
|
+
Using these names, rather than the direct colour values, future-proofs the code against redesigns and makes it easy to maintain; when we redesign the platform, we can update the colours in one place, rather than having to amend every component.
|
|
396
|
+
|
|
397
|
+
To avoid `theme` being a giant, difficult-to-navigate file, we split out all the values by the component directory they're in (e.g. `base`, `buttons`, `cards`, etc.) and then import them into `./theme/index.scss`. We use a Sass function to pull everything into the `$theme` variable, so we don't need to know exactly what's in each file.
|
|
398
|
+
|