Skip to content

Commit dd1d088

Browse files
authored
Merge pull request #219 from alphagov/javascript-api-reference
Add documentation for configuration and internationalisation
2 parents 3466c6e + 3142b78 commit dd1d088

File tree

4 files changed

+554
-1
lines changed

4 files changed

+554
-1
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
title: Configure components with JavaScript
3+
weight: 76
4+
---
5+
6+
# Configure components with JavaScript
7+
8+
You can configure some of the components in GOV.UK Frontend to customise their behaviour or to [localise their JavaScript to use a language other than English](/localise-govuk-frontend/).
9+
10+
You can configure a component by:
11+
12+
- setting Nunjucks macro options
13+
- using data attributes in the HTML
14+
- passing a JavaScript object when creating an instance of a component
15+
- using the `initAll` function
16+
17+
## Setting Nunjucks macro options
18+
19+
If you're using the Nunjucks macros, you will find all of the configuration options for a component published on the GOV.UK Design System website.
20+
21+
You can find the Nunjucks macro options by selecting the Nunjucks tab in the example box and selecting the Nunjucks macro options.
22+
23+
## Passing configuration using data attributes in HTML
24+
25+
If you're using HTML, you can pass configuration by adding data attributes to the component's outermost element (the element that has the `data-module` attribute). This is how our Nunjucks macros forward the configuration to the JavaScript components in the browser. Data attributes use kebab-case.
26+
27+
Some configuration options are grouped under a namespace to keep related options together. For example, [the localisation options](/localise-govuk-frontend/) are grouped under the `i18n` namespace. When using these options, include the namespace as a prefix followed by a period as part of the attribute name.
28+
29+
For options accepting object values, you'll need to set one attribute for each key of that object. Suffix the attribute name (including any namespace) with a period and the name of the key in the object.
30+
31+
This example shows the opening tag of a character count component with some configuration options including:
32+
33+
- a specific number of characters (non-namespaced configuration)
34+
- a new message for when users reach the specified number of characters (namespaced configuration)
35+
- two plural forms for when users are under the specified limit of characters (namespaced configuration + object value)
36+
37+
```html
38+
<div data-module="govuk-character-count"
39+
data-maxlength="500"
40+
data-i18n.characters-at-limit="No characters left"
41+
data-i18n.characters-under-limit.other="%{count} characters to go"
42+
data-i18n.characters-under-limit.one="%{count} character to go">
43+
```
44+
45+
If your configuration contains [quotes or other reserved HTML characters](https://developer.mozilla.org/en-US/docs/Glossary/Entity#reserved_characters), you'll need to escape those characters.
46+
47+
Configuration is read from data attributes when the component is initialised. Changes to the data attributes made after the component has been initialised will have no effect on the behaviour of the component.
48+
49+
Read the [JavaScript API Reference](/javascript-api-reference/) for the configuration accepted by each component. You'll need to convert the configuration names into kebab-case.
50+
51+
## Passing configuration to a new instance of a component in JavaScript
52+
53+
You can pass a configuration object to the constructor when creating an instance of a component in JavaScript.
54+
55+
Component constructors accept two arguments:
56+
57+
1. The HTML element that represents the component.
58+
2. An optional configuration object.
59+
60+
The object should include key-value pairs. The keys should be written in camelCase.
61+
62+
Components will merge the configuration provided at initialisation with those provided as data-attributes. If the same option is defined in both, the one provided by data-attributes will take precedence.
63+
64+
Some configuration options might accept object values or be grouped under a namespace to keep related things together. For example, [the localisation options](/localise-govuk-frontend/) are grouped under the `i18n` namespace. When using these options, use nested objects. For example:
65+
66+
```javascript
67+
new CharacterCount($element, {
68+
maxlength: 500, // Non namespaced
69+
i18n: { // i18n namespace
70+
charactersAtLimit: "No characters left",
71+
charactersUnderLimit: { // Object value
72+
other: "%{count} characters to go",
73+
one: "%{count} character to go"
74+
}
75+
}
76+
}).init()
77+
```
78+
79+
Read the [JavaScript API Reference](/javascript-api-reference/) to see what configuration each component accepts.
80+
81+
## Passing configuration using the initAll function
82+
83+
You can pass configuration for components when initialising GOV.UK Frontend using the `initAll` function. You can do this by including key-value pairs of camel-cased component names and configuration objects. This is the same method you would use to pass them when creating an instance of the component. For example:
84+
85+
```javascript
86+
window.GOVUKFrontend.initAll({
87+
characterCount: {
88+
maxlength: 500, // Non namespaced
89+
i18n: { // i18n namespace
90+
charactersAtLimit: "No characters left"
91+
}
92+
},
93+
})
94+
```

0 commit comments

Comments
 (0)