You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/contributing/component-js-configuration-options.md
+24-24
Original file line number
Diff line number
Diff line change
@@ -1,32 +1,32 @@
1
1
# Adding JavaScript configuration options to components
2
2
3
-
Configuration options permit developers to pass additional information into a component's JavaScript, allowing them to change how the component might work or appear without needing to create their own fork.
3
+
Configuration options allow developers to pass additional information into a component's JavaScript. This allows them to change how the component might work or appear without needing to create their own fork.
4
4
5
5
Configuration options straddle the line between Nunjucks, HTML and JavaScript.
6
6
7
7
## Preparing the component's JavaScript
8
8
9
-
First, ensure the entry function for component has a parameter for passing in a configuration object.
9
+
First, make sure the entry function for the component has a parameter for passing in a configuration object.
10
10
11
11
```javascript
12
12
functionAccordion ($module, config) {
13
13
...
14
14
}
15
15
```
16
16
17
-
Within the entry function, you'll then create an object with the default configuration for the component. These are what options will be used if no other options have been provided.
17
+
Within the entry function, you'll then create an object with the default configuration for the component. These are the options the component will use if no other options are provided.
18
18
19
19
```javascript
20
20
var defaultConfig = {
21
21
rememberExpanded:true
22
22
}
23
23
```
24
24
25
-
Next, use the `mergeConfigs` helper to combine the default config and the configuration object. This is assigned globally (using `this`) so that it's accessible anywhere within the component's JavaScript.
25
+
Next, use the `mergeConfigs` helper to combine the default config and the configuration object. The result of the merge is assigned globally (using `this`) so that it's accessible anywhere within the component's JavaScript.
26
26
27
-
The order that variables are written defines their priority, with objects passed sooner being overwritten by those passed later. As we want the user's configuration to take precedence over our defaults, we list our default configuration object first.
27
+
The order in which variables are written defines their priority, with objects passed sooner being overwritten by those passed later. As we want the user's configuration to take precedence over our defaults, we list our default configuration object first.
28
28
29
-
We also do a little safety check here. There is no guarantee that `config` will have any value at all, in which case it'll be `undefined`. We use an OR operator (`||`) to quickly check if this is the case and, if so, we use an empty object instead.
29
+
There is no guarantee `config` will have any value at all, so it'll be `undefined`. We use an OR operator (`||`) as a safety check. If the value is `undefined`, we use an empty object instead.
It's now possible to individually initialising the component with configuration options.
47
+
It's now possible to individually initialise the component with configuration options.
48
48
49
49
```html
50
50
<script>
@@ -65,7 +65,7 @@ In `src/govuk/all.mjs`, update the component's `new` function to pass through a
65
65
newAccordion($accordion, config.accordion).init()
66
66
```
67
67
68
-
It's now possible to pass configuration options for your component, as well as multiple other components, via the `initAll` function.
68
+
It's now possible to pass configuration options for your component, as well as multiple other components, using the `initAll` function.
69
69
70
70
```html
71
71
<script>
@@ -82,13 +82,13 @@ It's now possible to pass configuration options for your component, as well as m
82
82
83
83
## Adding support for HTML `data-*` attributes
84
84
85
-
For convenience, we allow for configuration options to be passed through HTML `data-*` attributes too.
85
+
For convenience, we also allow for configuration options to be passed through HTML `data-*` attributes.
86
86
87
-
`data-*` attributes can be found in JavaScript by looking at an element's `dataset` property. Browsers will helpfully convert the attribute names from HTML's kebab-case to more JavaScript-friendly camelCase when working with `dataset`. See ['Naming configuration options'](#naming-configuration-options) for caveats.
87
+
You can find `data-*` attributes in JavaScript by looking at an element's `dataset` property. Browsers will convert the attribute names from HTML's kebab-case to more JavaScript-friendly camelCase when working with `dataset`. See ['Naming configuration options'](#naming-configuration-options) for exceptions.
88
88
89
-
As we expect configuration-related `data-*` attributes to always be on the component's root element (the same element with the `data-module` attribute), we can access them all quickly using `$module.dataset`.
89
+
As we expect configuration-related `data-*` attributes to always be on the component's root element (the same element with the `data-module` attribute), we can access them all using `$module.dataset`.
90
90
91
-
Let's update our `mergeConfigs` call from earlier to include `$module.dataset` as the highest priority.
91
+
Using the `mergeConfigs` call discussed earlier in this document, update it to include `$module.dataset` as the highest priority.
Here, we pass it through our `normaliseDataset` function. This is because attribute values in dataset are always interpreted as being strings. `normaliseDataset` runs a few simple checks to convert values back to numbers or booleans where appropriate.
104
+
Here, we pass the value of `$module.dataset`through our `normaliseDataset` function. This is because attribute values in dataset are always interpreted as strings. `normaliseDataset` runs a few simple checks to convert values back to numbers or booleans where appropriate.
105
105
106
106
Now, in our HTML, we could pass configuration options by using the kebab-case version of the option's name.
107
107
@@ -114,13 +114,13 @@ Now, in our HTML, we could pass configuration options by using the kebab-case ve
114
114
</div>
115
115
```
116
116
117
-
However, this only works for developers who are writing raw HTML. We include Nunjucks macros for each component with GOV.UK Frontend to make development easier and faster, but this also makes it harder for developers to manually alter the raw HTML. We'll need to add a new parameter to Nunjucks to help them out.
117
+
However, this only works for developers who are writing raw HTML. We include Nunjucks macros for each component with GOV.UK Frontend to make development easier and faster, but this also makes it harder for developers to manually alter the raw HTML. We'll add a new parameter to Nunjucks to help them out.
118
118
119
119
## Adding a Nunjucks parameter
120
120
121
-
Although most components support adding arbitrary attributes and values via the `attributes` parameter, not all of them do. This method is also more verbose compared to having a dedicated Nunjucks parameter.
121
+
Most, but not all, components support adding arbitrary attributes and values through the `attributes` parameter. This method is also more verbose compared to having a dedicated Nunjucks parameter.
122
122
123
-
Let's update our previous bit of HTML to make the `data-*` attribute changeable.
123
+
Using the previous bit of HTML, update it to make the `data-*` attribute changeable.
124
124
125
125
```nunjucks
126
126
<div
@@ -131,7 +131,7 @@ Let's update our previous bit of HTML to make the `data-*` attribute changeable.
131
131
</div>
132
132
```
133
133
134
-
The above checks for the existence of the `rememberExpanded` parameter and adds the `data-remember-expanded` attribute if it's present. It then passes in the developer's defined value, running it through [Nunjucks' native `escape` filter](https://mozilla.github.io/nunjucks/templating.html#escape-aliased-as-e) to ensure that values can't break our HTML.
134
+
The above code checks for the existence of the `rememberExpanded` parameter. If the parameter is present it adds the `data-remember-expanded` attribute. It then passes in the developer's defined value, running it through [Nunjucks' native `escape` filter](https://mozilla.github.io/nunjucks/templating.html#escape-aliased-as-e) to make sure that values can't break our HTML.
135
135
136
136
We can now call the Accordion's Nunjucks macro with our new `rememberExpanded` parameter:
137
137
@@ -151,29 +151,29 @@ And that's pretty much it!
151
151
152
152
### In JavaScript
153
153
154
-
The option in JavaScript should be written in camelCase.
154
+
Use camelCase to write the option in JavaScript.
155
155
156
-
If the option is a boolean option that accepts `true` or `false` as values, favour affirmative language when naming the option. This avoids the use of double-negatives being used to enable something. For example, `rememberExpanded: true` is easier to understand than `forgetExpanded: false`.
156
+
If the option is a boolean option that accepts `true` or `false` as values, favour affirmative language when naming the option. This avoids the use of double-negatives to enable something. For example, `rememberExpanded: true` is easier to understand than `forgetExpanded: false`.
157
157
158
158
### In HTML
159
159
160
-
`data-*` attributes in HTML should use the same name as JavaScript, converted into kebab-case. This is because the `dataset` property in JavaScript automatically converts between these two formats for us.
160
+
`data-*` attributes in HTML should use the same name as JavaScript, converted into kebab-case. This is because the `dataset` property in JavaScript automatically converts between these two formats.
161
161
162
-
> **Warning**: This automatic conversion doesn't happen for numbers, punctuation, or other non-Latin alphabet characters. For this reason, avoid using numbers, or spell them out as words instead, when naming configuration options.
162
+
> **Warning**: This automatic conversion does not happen for numbers, punctuation, or other non-Latin alphabet characters. For this reason, when you're naming configuration options, avoid using numbers or spell them out as words.
163
163
164
164
In our example, `rememberExpanded` becomes `data-remember-expanded`.
165
165
166
166
### In Nunjucks
167
167
168
-
Unlike the `data-*` attribute in HTML and our use of `dataset` in JavaScript, there is no intrinsic link between the Nunjucks parameter name and the names used elsewhere. The Nunjucks parameter can therefore be named different, if convenient.
168
+
Unlike the `data-*` attribute in HTML and our use of `dataset` in JavaScript, there is no intrinsic link between the Nunjucks parameter name and the names used elsewhere. The Nunjucks parameter can therefore be named differently, if convenient.
169
169
170
-
A common case is specifying whether a parameter accepts HTML or only plain text. For example, if a configuration option's value is inserted into the page using `innerText`, you might want to name the Nunjucks parameter something like `sectionLabelText` to indicate that HTML won't be rendered.
170
+
A common case is specifying whether a parameter accepts HTML or only plain text. For example, if a configuration option's value is inserted into the page using `innerText`, you might want to name the Nunjucks parameter something like `sectionLabelText` to indicate that HTML will not be rendered.
171
171
172
172
There is more guidance on naming Nunjucks parameters in [the Nunjucks API documentation](https://github.com/alphagov/govuk-frontend/blob/main/docs/contributing/coding-standards/nunjucks-api.md#naming-options).
173
173
174
174
## Namespacing configuration options
175
175
176
-
Configuration options in JavaScript and HTML can be grouped together by using namespaces; period-separated strings that prefix the configuration option name. Namespaces follow the same formats as other option names, being camelCase in JavaScript and kebab-case in HTML.
176
+
You can group configuration options in JavaScript and HTML together by using namespaces; period-separated strings that prefix the configuration option name. Namespaces follow the same formats as other option names, being camelCase in JavaScript and kebab-case in HTML.
177
177
178
178
These are most commonly used for translation strings, which are usually namespaced under `i18n` (for 'internationalisation').
0 commit comments