Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update JavaScript API Reference for v5 #402

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions source/configure-components/index.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ If you're using the Nunjucks macros, you can read about [configuring a component

## Passing JavaScript configuration

Read the [JavaScript API Reference](../javascript-api-reference/) for the configuration accepted by each component.
Read the [JavaScript API Reference](../javascript-api-reference/) for the list of options in each component configuration.

The examples below follow our recommended [Import JavaScript using a bundler](../importing-css-assets-and-javascript/#import-javascript-using-a-bundler) approach and demonstrate how to:

Expand All @@ -34,7 +34,7 @@ Component constructors accept two arguments:
1. The HTML element that represents the component.
2. An optional configuration object.

Although component configuration objects are optional, configuration can still be provided or overridden by [HTML data attributes](#adding-html-data-attributes).
Although JavaScript configuration objects are optional, configuration can still be provided or overridden by [HTML data attributes](#adding-html-data-attributes).

To learn more about how configuration is passed from Nunjucks macros to HTML data attributes, see advanced examples in [the localisation options](../localise-govuk-frontend/).

Expand Down Expand Up @@ -74,13 +74,13 @@ new CharacterCount($element, {
})
```

Read the [JavaScript API Reference](../javascript-api-reference/) for the configuration accepted by each component.
Read the [JavaScript API Reference](../javascript-api-reference/) for the list of options in each component configuration.

### Configure all components using the initAll function

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 with their configuration object values:
You can do this by including key-value pairs of camel-cased component names with their configuration options:

```javascript
import { initAll } from 'govuk-frontend'
Expand Down Expand Up @@ -109,7 +109,7 @@ if ($element) {
}
```

Read the [JavaScript API Reference](../javascript-api-reference/) for the configuration accepted by `initAll`.
Read the [JavaScript API Reference](../javascript-api-reference/) for the list of components in the `initAll` configuration.

### JavaScript errors in the browser console

Expand Down
33 changes: 10 additions & 23 deletions source/javascript-api-reference/index.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,19 @@ weight: 80

# JavaScript API Reference

Some of our components can receive configuration options when you create an
instance in JavaScript. This page lists the available options for these
components. You can find further information on how to [configure these options
in our guidance](../configure-components/).
Some of our components can be passed JavaScript configuration objects. You can do it both:

You can pass these options in an object either as:
- [when configuring individual component instances](../configure-components/#with-configuration-object)
- [when configuring all components using the `initAll` function](../configure-components/#configure-all-components-using-the-initall-function)

- a second argument of the component's constructor
- the value for the key of the component name in an object passed to `initAll`
This page lists the options available for the following components:

For example, to set the `preventDoubleClick` option of a button:

```javascript
import { Button, initAll } from 'govuk-frontend'

// Creating a single instance
const $button = document.querySelector('[data-module="button"]')
new Button($button, { preventDoubleClick: true })

// Or when using initAll
initAll({
button: {
preventDoubleClick: true
}
})
```
- [Accordion](#accordion)
- [Button](#button)
- [CharacterCount](#charactercount)
- [ErrorSummary](#errorsummary)
- [ExitThisPage](#exitthispage)
- [NotificationBanner](#notificationbanner)

## Accordion

Expand Down