dxw_govuk_frontend_rails 3.4.0 → 3.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +1 -0
  3. data/Gemfile.lock +5 -4
  4. data/README.md +45 -42
  5. data/dxw_govuk_frontend_rails.gemspec +1 -1
  6. data/lib/dxw_govuk_frontend_rails/version.rb +1 -1
  7. data/package-lock.json +3 -3
  8. data/package.json +1 -1
  9. data/vendor/assets/javascripts/govuk_frontend_rails.js +213 -124
  10. data/vendor/assets/stylesheets/_base.scss +3 -0
  11. data/vendor/assets/stylesheets/all.scss +1 -3
  12. data/vendor/assets/stylesheets/components/_all.scss +31 -29
  13. data/vendor/assets/stylesheets/components/accordion/_accordion.scss +2 -208
  14. data/vendor/assets/stylesheets/components/accordion/_index.scss +206 -0
  15. data/vendor/assets/stylesheets/components/back-link/_back-link.scss +2 -65
  16. data/vendor/assets/stylesheets/components/back-link/_index.scss +110 -0
  17. data/vendor/assets/stylesheets/components/breadcrumbs/_breadcrumbs.scss +2 -118
  18. data/vendor/assets/stylesheets/components/breadcrumbs/_index.scss +137 -0
  19. data/vendor/assets/stylesheets/components/button/_button.scss +2 -290
  20. data/vendor/assets/stylesheets/components/button/_index.scss +280 -0
  21. data/vendor/assets/stylesheets/components/character-count/_character-count.scss +2 -31
  22. data/vendor/assets/stylesheets/components/character-count/_index.scss +24 -0
  23. data/vendor/assets/stylesheets/components/checkboxes/_checkboxes.scss +2 -308
  24. data/vendor/assets/stylesheets/components/checkboxes/_index.scss +305 -0
  25. data/vendor/assets/stylesheets/components/date-input/_date-input.scss +2 -30
  26. data/vendor/assets/stylesheets/components/date-input/_index.scss +26 -0
  27. data/vendor/assets/stylesheets/components/details/_details.scss +2 -88
  28. data/vendor/assets/stylesheets/components/details/_index.scss +83 -0
  29. data/vendor/assets/stylesheets/components/error-message/_error-message.scss +2 -15
  30. data/vendor/assets/stylesheets/components/error-message/_index.scss +11 -0
  31. data/vendor/assets/stylesheets/components/error-summary/_error-summary.scss +2 -59
  32. data/vendor/assets/stylesheets/components/error-summary/_index.scss +53 -0
  33. data/vendor/assets/stylesheets/components/fieldset/_fieldset.scss +2 -68
  34. data/vendor/assets/stylesheets/components/fieldset/_index.scss +64 -0
  35. data/vendor/assets/stylesheets/components/file-upload/_file-upload.scss +2 -81
  36. data/vendor/assets/stylesheets/components/file-upload/_index.scss +50 -0
  37. data/vendor/assets/stylesheets/components/footer/_footer.scss +2 -269
  38. data/vendor/assets/stylesheets/components/footer/_index.scss +234 -0
  39. data/vendor/assets/stylesheets/components/header/_header.scss +2 -318
  40. data/vendor/assets/stylesheets/components/header/_index.scss +309 -0
  41. data/vendor/assets/stylesheets/components/hint/_hint.scss +2 -50
  42. data/vendor/assets/stylesheets/components/hint/_index.scss +44 -0
  43. data/vendor/assets/stylesheets/components/input/_index.scss +187 -0
  44. data/vendor/assets/stylesheets/components/input/_input.scss +2 -103
  45. data/vendor/assets/stylesheets/components/inset-text/_index.scss +24 -0
  46. data/vendor/assets/stylesheets/components/inset-text/_inset-text.scss +2 -28
  47. data/vendor/assets/stylesheets/components/label/_index.scss +41 -0
  48. data/vendor/assets/stylesheets/components/label/_label.scss +2 -45
  49. data/vendor/assets/stylesheets/components/panel/_index.scss +44 -0
  50. data/vendor/assets/stylesheets/components/panel/_panel.scss +2 -44
  51. data/vendor/assets/stylesheets/components/phase-banner/_index.scss +27 -0
  52. data/vendor/assets/stylesheets/components/phase-banner/_phase-banner.scss +2 -31
  53. data/vendor/assets/stylesheets/components/radios/_index.scss +342 -0
  54. data/vendor/assets/stylesheets/components/radios/_radios.scss +2 -346
  55. data/vendor/assets/stylesheets/components/select/_index.scss +49 -0
  56. data/vendor/assets/stylesheets/components/select/_select.scss +2 -57
  57. data/vendor/assets/stylesheets/components/skip-link/_index.scss +34 -0
  58. data/vendor/assets/stylesheets/components/skip-link/_skip-link.scss +2 -31
  59. data/vendor/assets/stylesheets/components/summary-list/_index.scss +145 -0
  60. data/vendor/assets/stylesheets/components/summary-list/_summary-list.scss +2 -157
  61. data/vendor/assets/stylesheets/components/table/_index.scss +50 -0
  62. data/vendor/assets/stylesheets/components/table/_table.scss +2 -54
  63. data/vendor/assets/stylesheets/components/tabs/_index.scss +135 -0
  64. data/vendor/assets/stylesheets/components/tabs/_tabs.scss +2 -142
  65. data/vendor/assets/stylesheets/components/tag/_index.scss +86 -0
  66. data/vendor/assets/stylesheets/components/tag/_tag.scss +2 -44
  67. data/vendor/assets/stylesheets/components/textarea/_index.scss +47 -0
  68. data/vendor/assets/stylesheets/components/textarea/_textarea.scss +2 -55
  69. data/vendor/assets/stylesheets/components/warning-text/_index.scss +57 -0
  70. data/vendor/assets/stylesheets/components/warning-text/_warning-text.scss +2 -60
  71. data/vendor/assets/stylesheets/core/_global-styles.scss +5 -3
  72. data/vendor/assets/stylesheets/core/_links.scss +5 -3
  73. data/vendor/assets/stylesheets/core/_lists.scss +17 -3
  74. data/vendor/assets/stylesheets/core/_section-break.scss +5 -3
  75. data/vendor/assets/stylesheets/core/_template.scss +5 -3
  76. data/vendor/assets/stylesheets/core/_typography.scss +5 -3
  77. data/vendor/assets/stylesheets/helpers/_clearfix.scss +1 -1
  78. data/vendor/assets/stylesheets/helpers/_colour.scss +1 -1
  79. data/vendor/assets/stylesheets/helpers/_device-pixels.scss +3 -3
  80. data/vendor/assets/stylesheets/helpers/_focused.scss +1 -1
  81. data/vendor/assets/stylesheets/helpers/_font-faces.scss +9 -11
  82. data/vendor/assets/stylesheets/helpers/_grid.scss +2 -1
  83. data/vendor/assets/stylesheets/helpers/_links.scss +2 -4
  84. data/vendor/assets/stylesheets/helpers/_media-queries.scss +2 -6
  85. data/vendor/assets/stylesheets/helpers/_shape-arrow.scss +1 -1
  86. data/vendor/assets/stylesheets/helpers/_spacing.scss +7 -6
  87. data/vendor/assets/stylesheets/helpers/_typography.scss +13 -12
  88. data/vendor/assets/stylesheets/helpers/_visually-hidden.scss +30 -30
  89. data/vendor/assets/stylesheets/objects/_form-group.scss +1 -4
  90. data/vendor/assets/stylesheets/objects/_grid.scss +3 -6
  91. data/vendor/assets/stylesheets/objects/_main-wrapper.scss +5 -4
  92. data/vendor/assets/stylesheets/objects/_width-container.scss +12 -9
  93. data/vendor/assets/stylesheets/overrides/_display.scss +12 -4
  94. data/vendor/assets/stylesheets/overrides/_spacing.scss +5 -3
  95. data/vendor/assets/stylesheets/overrides/_typography.scss +5 -3
  96. data/vendor/assets/stylesheets/overrides/_width.scss +6 -3
  97. data/vendor/assets/stylesheets/settings/_colours-applied.scss +2 -5
  98. data/vendor/assets/stylesheets/settings/_colours-organisations.scss +3 -0
  99. data/vendor/assets/stylesheets/settings/_colours-palette.scss +42 -35
  100. data/vendor/assets/stylesheets/settings/_compatibility.scss +0 -1
  101. data/vendor/assets/stylesheets/settings/_ie8.scss +1 -1
  102. data/vendor/assets/stylesheets/settings/_measurements.scss +4 -5
  103. data/vendor/assets/stylesheets/settings/_typography-font-families.scss +2 -2
  104. data/vendor/assets/stylesheets/settings/_typography-font.scss +11 -5
  105. data/vendor/assets/stylesheets/settings/_typography-responsive.scss +6 -2
  106. data/vendor/assets/stylesheets/tools/_compatibility.scss +1 -1
  107. data/vendor/assets/stylesheets/tools/_font-url.scss +1 -4
  108. data/vendor/assets/stylesheets/tools/_ie8.scss +1 -1
  109. data/vendor/assets/stylesheets/tools/_iff.scss +3 -1
  110. data/vendor/assets/stylesheets/tools/_image-url.scss +1 -4
  111. data/vendor/assets/stylesheets/tools/_px-to-em.scss +1 -1
  112. data/vendor/assets/stylesheets/tools/_px-to-rem.scss +1 -1
  113. data/vendor/assets/stylesheets/utilities/_visually-hidden.scss +0 -1
  114. data/vendor/assets/stylesheets/vendor/_sass-mq.scss +0 -4
  115. metadata +36 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 665a2cf46a22c74d9db17906cdc8ad3582ea81bf03d96961f29e3f46ccb4f4a6
4
- data.tar.gz: a734d82b07c896ef3af09306721b8088628801f6931e78f5c142d85ab7be0430
3
+ metadata.gz: 32963462555eea031d11a2c85e7d59aff6534434c5430e522a1468c591d1aa8d
4
+ data.tar.gz: 636e39bce62acc9c6106a2f2156865d26f53bda67e68c7d50e3e5352f0e3bfae
5
5
  SHA512:
6
- metadata.gz: 030eac8904fb2fec2d2a62c378722b22430a70e3b3ff311856d4711bb9d900e9fdc1869638751b1bceed600f2b43468a751ffffdef85aff9a685c480022cf66f
7
- data.tar.gz: 3b554b48352295441e18a1e30a5c5cc1428fa1713c887526b3621d114100b628d594ef3e1bbad9b1bc031eefc76b4ee46c6222abb0d663e3485daee7734e552b
6
+ metadata.gz: 4e5169826e7097a3939119f86b73050355f2330f2e3befa2c9146273a51f01a9ee40d521626f6a372d001590e55e26cafeb2ea8c9c1fd787ff5078c0b52b5725
7
+ data.tar.gz: 3fec0b62d356b58d8cf364d3ebd9d618c092e352fc8cbb6813d047413579a8ec08f36471b46506c6c38c3642d4988dd65b3548c2ec2f0eccd03f045381e65f51
@@ -0,0 +1 @@
1
+ The [dxw standard contributing guide](https://github.com/dxw/.github/blob/main/CONTRIBUTING.md) applies for this repository.
@@ -1,20 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dxw_govuk_frontend_rails (3.4.0)
4
+ dxw_govuk_frontend_rails (3.9.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- rake (10.5.0)
9
+ rake (13.0.1)
10
10
 
11
11
  PLATFORMS
12
12
  ruby
13
+ x86_64-darwin-19
13
14
 
14
15
  DEPENDENCIES
15
16
  bundler (~> 2.0)
16
17
  dxw_govuk_frontend_rails!
17
- rake (~> 10.0)
18
+ rake (~> 13.0)
18
19
 
19
20
  BUNDLED WITH
20
- 2.0.2
21
+ 2.2.1
data/README.md CHANGED
@@ -1,88 +1,91 @@
1
1
  # GOVUK Frontend for Rails
2
2
 
3
- Adds the GOVUK Frontend for Rails using the asset pipeline.
3
+ ## Read this first
4
+
5
+ Although this gem makes it super easy to add the GOVUK Frontend to your
6
+ Rails applications, we would recommend using it as a guide to add the
7
+ GOVUK Frontend yourself.
8
+
9
+ ## About Adds the GOVUK Frontend for Rails using the asset pipeline.
4
10
 
5
11
  https://github.com/alphagov/govuk-frontend
6
12
 
7
13
  ## Breaking Changes in v3.0.0
8
14
 
9
- This release of the GOVUK Frontend has breaking changes! Do not update to this version until you have read and understood:
15
+ This release of the GOVUK Frontend has breaking changes! Do not update to this
16
+ version until you have read and understood:
10
17
 
11
- You will not be required to updated any file paths with govuk as we take care of that for you, all others changes will need to be managed:
18
+ You will not be required to updated any file paths with govuk as we take care of
19
+ that for you, all others changes will need to be managed:
12
20
 
13
21
  https://github.com/alphagov/govuk-frontend/releases/tag/v3.0.0
14
22
 
15
23
  ## Limitations
16
24
 
17
- This gem does not include the ie8 version of GOVUK Frontend, as the service standard no longer requires it:
25
+ This gem does not include the ie8 version of GOVUK Frontend, as the service
26
+ standard no longer requires it:
18
27
 
19
28
  https://www.gov.uk/service-manual/technology/designing-for-different-browsers-and-devices
20
29
 
21
- This gem and its documentation cannot tell you how to use the GOVUK effectively, see the GOVUK Design System documentation for that:
30
+ This gem and its documentation cannot tell you how to use the GOVUK effectively,
31
+ see the GOVUK Design System documentation for that:
22
32
 
23
- https://design-system.service.gov.uk/
24
- ## Installation
33
+ https://design-system.service.gov.uk/ ## Installation
25
34
 
26
35
  Add this line to your application's Gemfile:
27
36
 
28
- ```ruby
29
- gem 'dxw_govuk_frontend_rails'
30
- ```
37
+ ```ruby gem 'dxw_govuk_frontend_rails' ```
31
38
 
32
39
  And then execute:
33
40
 
34
41
  $ bundle
35
42
 
36
- ## Usage
37
- Your Rails app will need to have sass enabled.
43
+ ## Usage Your Rails app will need to have sass enabled.
38
44
 
39
45
  ### Basic usage
40
46
 
41
- If the service you are building does not need to modify or extend the GOVUK Frontend all you need to do is import the main sass and javascript files into your Rails app:
47
+ If the service you are building does not need to modify or extend the GOVUK
48
+ Frontend all you need to do is import the main sass and javascript files into
49
+ your Rails app:
42
50
 
43
- - create a sass file in your application:
44
- ```
45
- app/assets/stylesheets/govuk_frontend_rails.scss
46
- ```
47
- - import the styles into `govuk_frontend_rails.scss`:
48
- ```sass
49
- @import "govuk-frontend-rails";
50
- ```
51
- - require the `govuk-frontend-rails.scss` file in `application.css` or equivalent:
51
+ - create a sass file in your application: ```
52
+ app/assets/stylesheets/govuk_frontend_rails.scss ```
53
+ - import the styles into `govuk_frontend_rails.scss`: ```sass @import
54
+ "govuk-frontend-rails"; ```
55
+ - require the `govuk-frontend-rails.scss` file in `application.css` or
56
+ equivalent:
52
57
 
53
- ```
54
- *= require govuk_frontend_rails
55
- ```
58
+ ``` *= require govuk_frontend_rails ```
56
59
 
57
60
  - require the javascript into `app/assets/javascripts/application.js`:
58
61
 
59
- ```
60
- //= require govuk_frontend_rails
61
- ```
62
- - initialise the GOVUK Frontend either in a .js file on in your application layout:
62
+ ``` //= require govuk_frontend_rails ```
63
+ - initialise the GOVUK Frontend either in a .js file on in your application
64
+ layout:
63
65
 
64
- ```javascript
65
- window.onload = function() {
66
- window.GOVUKFrontend.initAll()
67
- };
68
- ```
66
+ ```javascript window.onload = function() { window.GOVUKFrontend.initAll() }; ```
69
67
  - update your application markup to use the GOVUK Frontend class names
70
68
 
71
- IMPORTANT: You cannot use the GOVUK Frontend without changes if the service you are building is not on GOV.UK, read and understand the guidance here:
69
+ IMPORTANT: You cannot use the GOVUK Frontend without changes if the service you
70
+ are building is not on GOV.UK, read and understand the guidance here:
72
71
 
73
72
  https://www.gov.uk/service-manual/design/making-your-service-look-like-govuk#if-your-service-isnt-on-govuk
74
73
 
75
- ## Tracking version
76
- We aim to tracking the version of GOVUK Frontend.
74
+ ## Tracking version We aim to tracking the version of GOVUK Frontend.
77
75
 
78
76
  ## Updating to a new Version of GOVUK Frontend
79
77
 
80
78
  When a new version of the GOVUK Frontend is released:
81
79
 
82
- - set the new version number in `package.json` and `lib/dxw_govuk_frontend_rails/version.rb`
83
- - run `npm update` to get the new release
84
- - run `bundle rake` to compile the new assets
80
+ - make a new branch with the GOVUK Frontend version number
81
+ - set the new version number in `package.json` and
82
+ `lib/dxw_govuk_frontend_rails/version.rb`
83
+ - run `npm update` to get the new release of the GOVUK Frontend
84
+ - run `bundle exec rake` to compile the new assets
85
85
  - commit the changes
86
86
  - tag with the same release number as GOVUK Frontend
87
- - push the changes
88
- - Github acitons will build the gem and push to Rubygems
87
+ - push the change
88
+ - push the tag
89
+ - merge your branch
90
+ - make a new release of the tag on GitHub
91
+ - GitHub acitons will build the gem and push to Rubygems
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.require_paths = ["lib"]
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 2.0"
26
- spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rake", "~> 13.0"
27
27
  end
@@ -1,3 +1,3 @@
1
1
  module DxwGovukFrontendRails
2
- VERSION = "3.4.0"
2
+ VERSION = "3.9.1"
3
3
  end
@@ -5,9 +5,9 @@
5
5
  "requires": true,
6
6
  "dependencies": {
7
7
  "govuk-frontend": {
8
- "version": "3.4.0",
9
- "resolved": "https://registry.npmjs.org/govuk-frontend/-/govuk-frontend-3.4.0.tgz",
10
- "integrity": "sha512-rmYPtcCtWgz92QBejYwOnfSxbPGYfvSruLwB4CBk/yJtySHRY0whG1e2/iFRRSj0pMx1Bu+zh/IqCTo+84hbFw=="
8
+ "version": "3.9.1",
9
+ "resolved": "https://registry.npmjs.org/govuk-frontend/-/govuk-frontend-3.9.1.tgz",
10
+ "integrity": "sha512-ouOoDUj0QwDA4uCHIBkGCFMpORuTRcSuDscOrz7V1PBcOecntLglxJAZAuNm+j2sPo7anoScHU0ZSeE2QIoeAg=="
11
11
  }
12
12
  }
13
13
  }
@@ -21,6 +21,6 @@
21
21
  },
22
22
  "homepage": "https://github.com/mec/dxw_govuk_frontend_rails#readme",
23
23
  "dependencies": {
24
- "govuk-frontend": "3.4.0"
24
+ "govuk-frontend": "3.9.1"
25
25
  }
26
26
  }
@@ -891,7 +891,7 @@ Accordion.prototype.initHeaderAttributes = function ($headerWrapper, index) {
891
891
  icon.className = this.iconClass;
892
892
  icon.setAttribute('aria-hidden', 'true');
893
893
 
894
- $heading.appendChild(icon);
894
+ $button.appendChild(icon);
895
895
  };
896
896
 
897
897
  // When section toggled, set and store state
@@ -1542,13 +1542,22 @@ CharacterCount.prototype.init = function () {
1542
1542
  // Remove hard limit if set
1543
1543
  $module.removeAttribute('maxlength');
1544
1544
 
1545
- // Bind event changes to the textarea
1546
- var boundChangeEvents = this.bindChangeEvents.bind(this);
1547
- boundChangeEvents();
1545
+ // When the page is restored after navigating 'back' in some browsers the
1546
+ // state of the character count is not restored until *after* the DOMContentLoaded
1547
+ // event is fired, so we need to sync after the pageshow event in browsers
1548
+ // that support it.
1549
+ if ('onpageshow' in window) {
1550
+ window.addEventListener('pageshow', this.sync.bind(this));
1551
+ } else {
1552
+ window.addEventListener('DOMContentLoaded', this.sync.bind(this));
1553
+ }
1554
+
1555
+ this.sync();
1556
+ };
1548
1557
 
1549
- // Update count message
1550
- var boundUpdateCountMessage = this.updateCountMessage.bind(this);
1551
- boundUpdateCountMessage();
1558
+ CharacterCount.prototype.sync = function () {
1559
+ this.bindChangeEvents();
1560
+ this.updateCountMessage();
1552
1561
  };
1553
1562
 
1554
1563
  // Read data attributes
@@ -1596,8 +1605,7 @@ CharacterCount.prototype.checkIfValueChanged = function () {
1596
1605
  if (!this.$textarea.oldValue) this.$textarea.oldValue = '';
1597
1606
  if (this.$textarea.value !== this.$textarea.oldValue) {
1598
1607
  this.$textarea.oldValue = this.$textarea.value;
1599
- var boundUpdateCountMessage = this.updateCountMessage.bind(this);
1600
- boundUpdateCountMessage();
1608
+ this.updateCountMessage();
1601
1609
  }
1602
1610
  };
1603
1611
 
@@ -1666,44 +1674,91 @@ function Checkboxes ($module) {
1666
1674
  this.$inputs = $module.querySelectorAll('input[type="checkbox"]');
1667
1675
  }
1668
1676
 
1677
+ /**
1678
+ * Initialise Checkboxes
1679
+ *
1680
+ * Checkboxes can be associated with a 'conditionally revealed' content block –
1681
+ * for example, a checkbox for 'Phone' could reveal an additional form field for
1682
+ * the user to enter their phone number.
1683
+ *
1684
+ * These associations are made using a `data-aria-controls` attribute, which is
1685
+ * promoted to an aria-controls attribute during initialisation.
1686
+ *
1687
+ * We also need to restore the state of any conditional reveals on the page (for
1688
+ * example if the user has navigated back), and set up event handlers to keep
1689
+ * the reveal in sync with the checkbox state.
1690
+ */
1669
1691
  Checkboxes.prototype.init = function () {
1670
1692
  var $module = this.$module;
1671
1693
  var $inputs = this.$inputs;
1672
1694
 
1673
- /**
1674
- * Loop over all items with [data-controls]
1675
- * Check if they have a matching conditional reveal
1676
- * If they do, assign attributes.
1677
- **/
1678
1695
  nodeListForEach($inputs, function ($input) {
1679
- var controls = $input.getAttribute('data-aria-controls');
1696
+ var target = $input.getAttribute('data-aria-controls');
1680
1697
 
1681
- // Check if input controls anything
1682
- // Check if content exists, before setting attributes.
1683
- if (!controls || !$module.querySelector('#' + controls)) {
1698
+ // Skip checkboxes without data-aria-controls attributes, or where the
1699
+ // target element does not exist.
1700
+ if (!target || !$module.querySelector('#' + target)) {
1684
1701
  return
1685
1702
  }
1686
1703
 
1687
- // If we have content that is controlled, set attributes.
1688
- $input.setAttribute('aria-controls', controls);
1704
+ // Promote the data-aria-controls attribute to a aria-controls attribute
1705
+ // so that the relationship is exposed in the AOM
1706
+ $input.setAttribute('aria-controls', target);
1689
1707
  $input.removeAttribute('data-aria-controls');
1690
- this.setAttributes($input);
1691
- }.bind(this));
1708
+ });
1709
+
1710
+ // When the page is restored after navigating 'back' in some browsers the
1711
+ // state of form controls is not restored until *after* the DOMContentLoaded
1712
+ // event is fired, so we need to sync after the pageshow event in browsers
1713
+ // that support it.
1714
+ if ('onpageshow' in window) {
1715
+ window.addEventListener('pageshow', this.syncAllConditionalReveals.bind(this));
1716
+ } else {
1717
+ window.addEventListener('DOMContentLoaded', this.syncAllConditionalReveals.bind(this));
1718
+ }
1719
+
1720
+ // Although we've set up handlers to sync state on the pageshow or
1721
+ // DOMContentLoaded event, init could be called after those events have fired,
1722
+ // for example if they are added to the page dynamically, so sync now too.
1723
+ this.syncAllConditionalReveals();
1692
1724
 
1693
- // Handle events
1694
1725
  $module.addEventListener('click', this.handleClick.bind(this));
1695
1726
  };
1696
1727
 
1697
- Checkboxes.prototype.setAttributes = function ($input) {
1698
- var inputIsChecked = $input.checked;
1699
- $input.setAttribute('aria-expanded', inputIsChecked);
1728
+ /**
1729
+ * Sync the conditional reveal states for all inputs in this $module.
1730
+ */
1731
+ Checkboxes.prototype.syncAllConditionalReveals = function () {
1732
+ nodeListForEach(this.$inputs, this.syncConditionalRevealWithInputState.bind(this));
1733
+ };
1734
+
1735
+ /**
1736
+ * Sync conditional reveal with the input state
1737
+ *
1738
+ * Synchronise the visibility of the conditional reveal, and its accessible
1739
+ * state, with the input's checked state.
1740
+ *
1741
+ * @param {HTMLInputElement} $input Checkbox input
1742
+ */
1743
+ Checkboxes.prototype.syncConditionalRevealWithInputState = function ($input) {
1744
+ var $target = this.$module.querySelector('#' + $input.getAttribute('aria-controls'));
1745
+
1746
+ if ($target && $target.classList.contains('govuk-checkboxes__conditional')) {
1747
+ var inputIsChecked = $input.checked;
1700
1748
 
1701
- var $content = this.$module.querySelector('#' + $input.getAttribute('aria-controls'));
1702
- if ($content) {
1703
- $content.classList.toggle('govuk-checkboxes__conditional--hidden', !inputIsChecked);
1749
+ $input.setAttribute('aria-expanded', inputIsChecked);
1750
+ $target.classList.toggle('govuk-checkboxes__conditional--hidden', !inputIsChecked);
1704
1751
  }
1705
1752
  };
1706
1753
 
1754
+ /**
1755
+ * Click event handler
1756
+ *
1757
+ * Handle a click within the $module – if the click occurred on a checkbox, sync
1758
+ * the state of any associated conditional reveal with the checkbox state.
1759
+ *
1760
+ * @param {MouseEvent} event Click event
1761
+ */
1707
1762
  Checkboxes.prototype.handleClick = function (event) {
1708
1763
  var $target = event.target;
1709
1764
 
@@ -1711,7 +1766,7 @@ Checkboxes.prototype.handleClick = function (event) {
1711
1766
  var isCheckbox = $target.getAttribute('type') === 'checkbox';
1712
1767
  var hasAriaControls = $target.getAttribute('aria-controls');
1713
1768
  if (isCheckbox && hasAriaControls) {
1714
- this.setAttributes($target);
1769
+ this.syncConditionalRevealWithInputState($target);
1715
1770
  }
1716
1771
  };
1717
1772
 
@@ -1748,7 +1803,7 @@ Checkboxes.prototype.handleClick = function (event) {
1748
1803
 
1749
1804
  if (detect) return
1750
1805
 
1751
- // Polyfill from https://raw.githubusercontent.com/Financial-Times/polyfill-service/1f3c09b402f65bf6e393f933a15ba63f1b86ef1f/packages/polyfill-library/polyfills/Element/prototype/closest/polyfill.js
1806
+ // Polyfill from https://raw.githubusercontent.com/Financial-Times/polyfill-service/1f3c09b402f65bf6e393f933a15ba63f1b86ef1f/packages/polyfill-library/polyfills/Element/prototype/closest/polyfill.js
1752
1807
  Element.prototype.closest = function closest(selector) {
1753
1808
  var node = this;
1754
1809
 
@@ -1906,169 +1961,203 @@ ErrorSummary.prototype.getAssociatedLegendOrLabel = function ($input) {
1906
1961
 
1907
1962
  function Header ($module) {
1908
1963
  this.$module = $module;
1964
+ this.$menuButton = $module && $module.querySelector('.govuk-js-header-toggle');
1965
+ this.$menu = this.$menuButton && $module.querySelector(
1966
+ '#' + this.$menuButton.getAttribute('aria-controls')
1967
+ );
1909
1968
  }
1910
1969
 
1970
+ /**
1971
+ * Initialise header
1972
+ *
1973
+ * Check for the presence of the header, menu and menu button – if any are
1974
+ * missing then there's nothing to do so return early.
1975
+ */
1911
1976
  Header.prototype.init = function () {
1912
- // Check for module
1913
- var $module = this.$module;
1914
- if (!$module) {
1977
+ if (!this.$module || !this.$menuButton || !this.$menu) {
1915
1978
  return
1916
1979
  }
1917
1980
 
1918
- // Check for button
1919
- var $toggleButton = $module.querySelector('.govuk-js-header-toggle');
1920
- if (!$toggleButton) {
1921
- return
1922
- }
1923
-
1924
- // Handle $toggleButton click events
1925
- $toggleButton.addEventListener('click', this.handleClick.bind(this));
1981
+ this.syncState(this.$menu.classList.contains('govuk-header__navigation--open'));
1982
+ this.$menuButton.addEventListener('click', this.handleMenuButtonClick.bind(this));
1926
1983
  };
1927
1984
 
1928
1985
  /**
1929
- * Toggle class
1930
- * @param {object} node element
1931
- * @param {string} className to toggle
1932
- */
1933
- Header.prototype.toggleClass = function (node, className) {
1934
- if (node.className.indexOf(className) > 0) {
1935
- node.className = node.className.replace(' ' + className, '');
1936
- } else {
1937
- node.className += ' ' + className;
1938
- }
1986
+ * Sync menu state
1987
+ *
1988
+ * Sync the menu button class and the accessible state of the menu and the menu
1989
+ * button with the visible state of the menu
1990
+ *
1991
+ * @param {boolean} isVisible Whether the menu is currently visible
1992
+ */
1993
+ Header.prototype.syncState = function (isVisible) {
1994
+ this.$menuButton.classList.toggle('govuk-header__menu-button--open', isVisible);
1995
+ this.$menuButton.setAttribute('aria-expanded', isVisible);
1939
1996
  };
1940
1997
 
1941
1998
  /**
1942
- * An event handler for click event on $toggleButton
1943
- * @param {object} event event
1944
- */
1945
- Header.prototype.handleClick = function (event) {
1946
- var $module = this.$module;
1947
- var $toggleButton = event.target || event.srcElement;
1948
- var $target = $module.querySelector('#' + $toggleButton.getAttribute('aria-controls'));
1949
-
1950
- // If a button with aria-controls, handle click
1951
- if ($toggleButton && $target) {
1952
- this.toggleClass($target, 'govuk-header__navigation--open');
1953
- this.toggleClass($toggleButton, 'govuk-header__menu-button--open');
1954
-
1955
- $toggleButton.setAttribute('aria-expanded', $toggleButton.getAttribute('aria-expanded') !== 'true');
1956
- $target.setAttribute('aria-hidden', $target.getAttribute('aria-hidden') === 'false');
1957
- }
1999
+ * Handle menu button click
2000
+ *
2001
+ * When the menu button is clicked, change the visibility of the menu and then
2002
+ * sync the accessibility state and menu button state
2003
+ */
2004
+ Header.prototype.handleMenuButtonClick = function () {
2005
+ var isVisible = this.$menu.classList.toggle('govuk-header__navigation--open');
2006
+ this.syncState(isVisible);
1958
2007
  };
1959
2008
 
1960
2009
  function Radios ($module) {
1961
2010
  this.$module = $module;
2011
+ this.$inputs = $module.querySelectorAll('input[type="radio"]');
1962
2012
  }
1963
2013
 
2014
+ /**
2015
+ * Initialise Radios
2016
+ *
2017
+ * Radios can be associated with a 'conditionally revealed' content block – for
2018
+ * example, a radio for 'Phone' could reveal an additional form field for the
2019
+ * user to enter their phone number.
2020
+ *
2021
+ * These associations are made using a `data-aria-controls` attribute, which is
2022
+ * promoted to an aria-controls attribute during initialisation.
2023
+ *
2024
+ * We also need to restore the state of any conditional reveals on the page (for
2025
+ * example if the user has navigated back), and set up event handlers to keep
2026
+ * the reveal in sync with the radio state.
2027
+ */
1964
2028
  Radios.prototype.init = function () {
1965
2029
  var $module = this.$module;
1966
- var $inputs = $module.querySelectorAll('input[type="radio"]');
2030
+ var $inputs = this.$inputs;
1967
2031
 
1968
- /**
1969
- * Loop over all items with [data-controls]
1970
- * Check if they have a matching conditional reveal
1971
- * If they do, assign attributes.
1972
- **/
1973
2032
  nodeListForEach($inputs, function ($input) {
1974
- var controls = $input.getAttribute('data-aria-controls');
2033
+ var target = $input.getAttribute('data-aria-controls');
1975
2034
 
1976
- // Check if input controls anything
1977
- // Check if content exists, before setting attributes.
1978
- if (!controls || !$module.querySelector('#' + controls)) {
2035
+ // Skip radios without data-aria-controls attributes, or where the
2036
+ // target element does not exist.
2037
+ if (!target || !$module.querySelector('#' + target)) {
1979
2038
  return
1980
2039
  }
1981
2040
 
1982
- // If we have content that is controlled, set attributes.
1983
- $input.setAttribute('aria-controls', controls);
2041
+ // Promote the data-aria-controls attribute to a aria-controls attribute
2042
+ // so that the relationship is exposed in the AOM
2043
+ $input.setAttribute('aria-controls', target);
1984
2044
  $input.removeAttribute('data-aria-controls');
1985
- this.setAttributes($input);
1986
- }.bind(this));
2045
+ });
2046
+
2047
+ // When the page is restored after navigating 'back' in some browsers the
2048
+ // state of form controls is not restored until *after* the DOMContentLoaded
2049
+ // event is fired, so we need to sync after the pageshow event in browsers
2050
+ // that support it.
2051
+ if ('onpageshow' in window) {
2052
+ window.addEventListener('pageshow', this.syncAllConditionalReveals.bind(this));
2053
+ } else {
2054
+ window.addEventListener('DOMContentLoaded', this.syncAllConditionalReveals.bind(this));
2055
+ }
2056
+
2057
+ // Although we've set up handlers to sync state on the pageshow or
2058
+ // DOMContentLoaded event, init could be called after those events have fired,
2059
+ // for example if they are added to the page dynamically, so sync now too.
2060
+ this.syncAllConditionalReveals();
1987
2061
 
1988
2062
  // Handle events
1989
2063
  $module.addEventListener('click', this.handleClick.bind(this));
1990
2064
  };
1991
2065
 
1992
- Radios.prototype.setAttributes = function ($input) {
1993
- var $content = document.querySelector('#' + $input.getAttribute('aria-controls'));
2066
+ /**
2067
+ * Sync the conditional reveal states for all inputs in this $module.
2068
+ */
2069
+ Radios.prototype.syncAllConditionalReveals = function () {
2070
+ nodeListForEach(this.$inputs, this.syncConditionalRevealWithInputState.bind(this));
2071
+ };
1994
2072
 
1995
- if ($content && $content.classList.contains('govuk-radios__conditional')) {
2073
+ /**
2074
+ * Sync conditional reveal with the input state
2075
+ *
2076
+ * Synchronise the visibility of the conditional reveal, and its accessible
2077
+ * state, with the input's checked state.
2078
+ *
2079
+ * @param {HTMLInputElement} $input Radio input
2080
+ */
2081
+ Radios.prototype.syncConditionalRevealWithInputState = function ($input) {
2082
+ var $target = document.querySelector('#' + $input.getAttribute('aria-controls'));
2083
+
2084
+ if ($target && $target.classList.contains('govuk-radios__conditional')) {
1996
2085
  var inputIsChecked = $input.checked;
1997
2086
 
1998
2087
  $input.setAttribute('aria-expanded', inputIsChecked);
1999
-
2000
- $content.classList.toggle('govuk-radios__conditional--hidden', !inputIsChecked);
2088
+ $target.classList.toggle('govuk-radios__conditional--hidden', !inputIsChecked);
2001
2089
  }
2002
2090
  };
2003
2091
 
2092
+ /**
2093
+ * Click event handler
2094
+ *
2095
+ * Handle a click within the $module – if the click occurred on a radio, sync
2096
+ * the state of the conditional reveal for all radio buttons in the same form
2097
+ * with the same name (because checking one radio could have un-checked a radio
2098
+ * in another $module)
2099
+ *
2100
+ * @param {MouseEvent} event Click event
2101
+ */
2004
2102
  Radios.prototype.handleClick = function (event) {
2005
2103
  var $clickedInput = event.target;
2006
- // We only want to handle clicks for radio inputs
2104
+
2105
+ // Ignore clicks on things that aren't radio buttons
2007
2106
  if ($clickedInput.type !== 'radio') {
2008
2107
  return
2009
2108
  }
2010
- // Because checking one radio can uncheck a radio in another $module,
2011
- // we need to call set attributes on all radios in the same form, or document if they're not in a form.
2012
- //
2013
- // We also only want radios which have aria-controls, as they support conditional reveals.
2109
+
2110
+ // We only need to consider radios with conditional reveals, which will have
2111
+ // aria-controls attributes.
2014
2112
  var $allInputs = document.querySelectorAll('input[type="radio"][aria-controls]');
2113
+
2015
2114
  nodeListForEach($allInputs, function ($input) {
2016
- // Only inputs with the same form owner should change.
2017
2115
  var hasSameFormOwner = ($input.form === $clickedInput.form);
2018
-
2019
- // In radios, only radios with the same name will affect each other.
2020
2116
  var hasSameName = ($input.name === $clickedInput.name);
2117
+
2021
2118
  if (hasSameName && hasSameFormOwner) {
2022
- this.setAttributes($input);
2119
+ this.syncConditionalRevealWithInputState($input);
2023
2120
  }
2024
2121
  }.bind(this));
2025
2122
  };
2026
2123
 
2027
2124
  (function(undefined) {
2028
2125
 
2029
- // Detection from https://github.com/Financial-Times/polyfill-service/pull/1062/files#diff-b09a5d2acf3314b46a6c8f8d0c31b85c
2126
+ // Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-library/master/polyfills/Element/prototype/nextElementSibling/detect.js
2030
2127
  var detect = (
2031
- 'Element' in this && "nextElementSibling" in document.documentElement
2128
+ 'document' in this && "nextElementSibling" in document.documentElement
2032
2129
  );
2033
2130
 
2034
2131
  if (detect) return
2035
2132
 
2036
-
2037
- (function (global) {
2038
-
2039
- // Polyfill from https://github.com/Financial-Times/polyfill-service/pull/1062/files#diff-404b69b4750d18dea4174930a49170fd
2040
- Object.defineProperty(Element.prototype, "nextElementSibling", {
2041
- get: function(){
2042
- var el = this.nextSibling;
2043
- while (el && el.nodeType !== 1) { el = el.nextSibling; }
2044
- return (el.nodeType === 1) ? el : null;
2045
- }
2046
- });
2047
-
2048
- }(this));
2133
+ // Polyfill from https://raw.githubusercontent.com/Financial-Times/polyfill-library/master/polyfills/Element/prototype/nextElementSibling/polyfill.js
2134
+ Object.defineProperty(Element.prototype, "nextElementSibling", {
2135
+ get: function(){
2136
+ var el = this.nextSibling;
2137
+ while (el && el.nodeType !== 1) { el = el.nextSibling; }
2138
+ return el;
2139
+ }
2140
+ });
2049
2141
 
2050
2142
  }).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
2051
2143
 
2052
2144
  (function(undefined) {
2053
2145
 
2054
- // Detection from https://github.com/Financial-Times/polyfill-service/pull/1062/files#diff-a162235fbc9c0dd40d4032265f44942e
2146
+ // Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-library/master/polyfills/Element/prototype/previousElementSibling/detect.js
2055
2147
  var detect = (
2056
- 'Element' in this && 'previousElementSibling' in document.documentElement
2148
+ 'document' in this && "previousElementSibling" in document.documentElement
2057
2149
  );
2058
2150
 
2059
2151
  if (detect) return
2060
2152
 
2061
- (function (global) {
2062
- // Polyfill from https://github.com/Financial-Times/polyfill-service/pull/1062/files#diff-b45a1197b842728cb76b624b6ba7d739
2063
- Object.defineProperty(Element.prototype, 'previousElementSibling', {
2064
- get: function(){
2065
- var el = this.previousSibling;
2066
- while (el && el.nodeType !== 1) { el = el.previousSibling; }
2067
- return (el.nodeType === 1) ? el : null;
2068
- }
2069
- });
2070
-
2071
- }(this));
2153
+ // Polyfill from https://raw.githubusercontent.com/Financial-Times/polyfill-library/master/polyfills/Element/prototype/previousElementSibling/polyfill.js
2154
+ Object.defineProperty(Element.prototype, 'previousElementSibling', {
2155
+ get: function(){
2156
+ var el = this.previousSibling;
2157
+ while (el && el.nodeType !== 1) { el = el.previousSibling; }
2158
+ return el;
2159
+ }
2160
+ });
2072
2161
 
2073
2162
  }).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
2074
2163