@ministryofjustice/frontend 3.3.1 → 3.4.0

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.
Files changed (87) hide show
  1. package/README.md +4 -10
  2. package/govuk-prototype-kit.config.json +5 -16
  3. package/moj/all.jquery.min.js +77 -3
  4. package/moj/all.js +2021 -1436
  5. package/moj/all.scss +2 -0
  6. package/moj/all.spec.js +15 -13
  7. package/moj/components/_all.scss +1 -0
  8. package/moj/components/action-bar/_action-bar.scss +4 -6
  9. package/moj/components/add-another/_add-another.scss +9 -7
  10. package/moj/components/add-another/add-another.js +90 -69
  11. package/moj/components/add-another/add-another.spec.js +165 -0
  12. package/moj/components/alert/README.md +0 -0
  13. package/moj/components/alert/_alert.scss +142 -0
  14. package/moj/components/alert/alert.js +247 -0
  15. package/moj/components/alert/alert.spec.helper.js +67 -0
  16. package/moj/components/alert/alert.spec.js +229 -0
  17. package/moj/components/alert/macro.njk +3 -0
  18. package/moj/components/alert/template.njk +83 -0
  19. package/moj/components/badge/_badge.scss +3 -4
  20. package/moj/components/banner/_banner.scss +5 -10
  21. package/moj/components/button-menu/_button-menu.scss +10 -9
  22. package/moj/components/button-menu/button-menu.js +139 -136
  23. package/moj/components/button-menu/button-menu.spec.js +295 -296
  24. package/moj/components/cookie-banner/_cookie-banner.scss +6 -5
  25. package/moj/components/currency-input/_currency-input.scss +4 -4
  26. package/moj/components/date-picker/README.md +14 -17
  27. package/moj/components/date-picker/_date-picker.scss +122 -106
  28. package/moj/components/date-picker/date-picker.js +473 -471
  29. package/moj/components/date-picker/date-picker.spec.js +971 -923
  30. package/moj/components/filter/README.md +1 -1
  31. package/moj/components/filter/_filter.scss +53 -75
  32. package/moj/components/filter-toggle-button/filter-toggle-button.js +71 -67
  33. package/moj/components/filter-toggle-button/filter-toggle-button.spec.js +203 -205
  34. package/moj/components/form-validator/form-validator.js +117 -109
  35. package/moj/components/header/_header.scss +17 -19
  36. package/moj/components/identity-bar/_identity-bar.scss +5 -5
  37. package/moj/components/interruption-card/_interruption-card.scss +2 -2
  38. package/moj/components/messages/_messages.scss +12 -19
  39. package/moj/components/multi-file-upload/README.md +1 -1
  40. package/moj/components/multi-file-upload/_multi-file-upload.scss +34 -30
  41. package/moj/components/multi-file-upload/multi-file-upload.js +188 -152
  42. package/moj/components/multi-file-upload/multi-file-upload.spec.js +510 -0
  43. package/moj/components/multi-select/_multi-select.scss +4 -3
  44. package/moj/components/multi-select/multi-select.js +55 -50
  45. package/moj/components/multi-select/multi-select.spec.js +72 -79
  46. package/moj/components/notification-badge/_notification-badge.scss +12 -12
  47. package/moj/components/organisation-switcher/_organisation-switcher.scss +1 -1
  48. package/moj/components/page-header-actions/_page-header-actions.scss +3 -2
  49. package/moj/components/pagination/_pagination.scss +26 -31
  50. package/moj/components/password-reveal/_password-reveal.scss +1 -2
  51. package/moj/components/password-reveal/password-reveal.js +22 -21
  52. package/moj/components/password-reveal/password-reveal.spec.js +39 -37
  53. package/moj/components/primary-navigation/_primary-navigation.scss +26 -29
  54. package/moj/components/progress-bar/_progress-bar.scss +21 -26
  55. package/moj/components/rich-text-editor/_rich-text-editor.scss +17 -16
  56. package/moj/components/rich-text-editor/rich-text-editor.js +117 -103
  57. package/moj/components/search/_search.scss +6 -4
  58. package/moj/components/search-toggle/search-toggle.js +29 -30
  59. package/moj/components/search-toggle/search-toggle.scss +21 -15
  60. package/moj/components/search-toggle/search-toggle.spec.js +65 -70
  61. package/moj/components/side-navigation/_side-navigation.scss +12 -21
  62. package/moj/components/sortable-table/_sortable-table.scss +25 -23
  63. package/moj/components/sortable-table/sortable-table.js +139 -117
  64. package/moj/components/sortable-table/sortable-table.spec.js +362 -0
  65. package/moj/components/sub-navigation/_sub-navigation.scss +24 -28
  66. package/moj/components/tag/_tag.scss +8 -9
  67. package/moj/components/task-list/_task-list.scss +8 -7
  68. package/moj/components/ticket-panel/_ticket-panel.scss +14 -6
  69. package/moj/components/timeline/_timeline.scss +18 -20
  70. package/moj/filters/all.js +28 -30
  71. package/moj/filters/prototype-kit-13-filters.js +2 -1
  72. package/moj/helpers/_all.scss +1 -0
  73. package/moj/helpers/_hidden.scss +1 -1
  74. package/moj/helpers/_links.scss +20 -0
  75. package/moj/helpers.js +160 -31
  76. package/moj/helpers.spec.js +235 -0
  77. package/moj/init.js +2 -2
  78. package/moj/moj-frontend.min.css +2 -2
  79. package/moj/moj-frontend.min.js +77 -3
  80. package/moj/namespace.js +2 -1
  81. package/moj/objects/_filter-layout.scss +11 -10
  82. package/moj/objects/_scrollable-pane.scss +11 -14
  83. package/moj/settings/_colours.scss +5 -0
  84. package/moj/settings/_measurements.scss +0 -2
  85. package/moj/utilities/_hidden.scss +3 -3
  86. package/moj/utilities/_width-container.scss +1 -1
  87. package/package.json +1 -1
@@ -0,0 +1,247 @@
1
+ /**
2
+ * @typedef {object} AlertConfig
3
+ * @property {boolean} [dismissible=false] - Can the alert be dismissed by the user
4
+ * @property {string} [dismissText=Dismiss] - the label text for the dismiss button
5
+ * @property {boolean} [disableAutoFocus=false] - whether the alert will be autofocused
6
+ * @property {string} [focusOnDismissSelector] - CSS Selector for element to be focused on dismiss
7
+ */
8
+
9
+ /**
10
+ * @param {HTMLElement} $module - the Alert element
11
+ * @param {AlertConfig} config - configuration options
12
+ * @class
13
+ */
14
+ MOJFrontend.Alert = function ($module, config = {}) {
15
+ if (!$module) {
16
+ return this
17
+ }
18
+
19
+ const schema = Object.freeze({
20
+ properties: {
21
+ dismissible: { type: 'boolean' },
22
+ dismissText: { type: 'string' },
23
+ disableAutoFocus: { type: 'boolean' },
24
+ focusOnDismissSelector: { type: 'string' }
25
+ }
26
+ })
27
+
28
+ const defaults = {
29
+ dismissible: false,
30
+ dismissText: 'Dismiss',
31
+ disableAutoFocus: false
32
+ }
33
+
34
+ // data attributes override JS config, which overrides defaults
35
+ this.config = this.mergeConfigs(
36
+ defaults,
37
+ config,
38
+ this.parseDataset(schema, $module.dataset)
39
+ )
40
+
41
+ this.$module = $module
42
+ }
43
+
44
+ MOJFrontend.Alert.prototype.init = function () {
45
+ /**
46
+ * Focus the alert
47
+ *
48
+ * If `role="alert"` is set, focus the element to help some assistive
49
+ * technologies prioritise announcing it.
50
+ *
51
+ * You can turn off the auto-focus functionality by setting
52
+ * `data-disable-auto-focus="true"` in the component HTML. You might wish to
53
+ * do this based on user research findings, or to avoid a clash with another
54
+ * element which should be focused when the page loads.
55
+ */
56
+ if (
57
+ this.$module.getAttribute('role') === 'alert' &&
58
+ !this.config.disableAutoFocus
59
+ ) {
60
+ MOJFrontend.setFocus(this.$module)
61
+ }
62
+
63
+ this.$dismissButton = this.$module.querySelector('.moj-alert__dismiss')
64
+
65
+ if (this.config.dismissible && this.$dismissButton) {
66
+ this.$dismissButton.innerHTML = this.config.dismissText
67
+ this.$dismissButton.removeAttribute('hidden')
68
+
69
+ this.$module.addEventListener('click', (event) => {
70
+ if (this.$dismissButton.contains(event.target)) {
71
+ this.dimiss()
72
+ }
73
+ })
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Handle dismissing the alert
79
+ */
80
+ MOJFrontend.Alert.prototype.dimiss = function () {
81
+ let $elementToRecieveFocus
82
+
83
+ // If a selector has been provided, attempt to find that element
84
+ if (this.config.focusOnDismissSelector) {
85
+ $elementToRecieveFocus = document.querySelector(
86
+ this.config.focusOnDismissSelector
87
+ )
88
+ }
89
+
90
+ // Is the next sibling another alert
91
+ if (!$elementToRecieveFocus) {
92
+ const $nextSibling = this.$module.nextElementSibling
93
+ if ($nextSibling && $nextSibling.matches('.moj-alert')) {
94
+ $elementToRecieveFocus = $nextSibling
95
+ }
96
+ }
97
+
98
+ // Else try to find any preceding sibling alert or heading
99
+ if (!$elementToRecieveFocus) {
100
+ $elementToRecieveFocus = MOJFrontend.getPreviousSibling(
101
+ this.$module,
102
+ '.moj-alert, h1, h2, h3, h4, h5, h6'
103
+ )
104
+ }
105
+
106
+ // Else find the closest ancestor heading, or fallback to main, or last resort
107
+ // use the body element
108
+ if (!$elementToRecieveFocus) {
109
+ $elementToRecieveFocus = MOJFrontend.findNearestMatchingElement(
110
+ this.$module,
111
+ 'h1, h2, h3, h4, h5, h6, main, body'
112
+ )
113
+ }
114
+
115
+ // If we have an element, place focus on it
116
+ if ($elementToRecieveFocus) {
117
+ MOJFrontend.setFocus($elementToRecieveFocus)
118
+ }
119
+
120
+ // Remove the alert
121
+ this.$module.remove()
122
+ }
123
+
124
+ /**
125
+ * Normalise string
126
+ *
127
+ * 'If it looks like a duck, and it quacks like a duck…' 🦆
128
+ *
129
+ * If the passed value looks like a boolean or a number, convert it to a boolean
130
+ * or number.
131
+ *
132
+ * Designed to be used to convert config passed via data attributes (which are
133
+ * always strings) into something sensible.
134
+ *
135
+ * @internal
136
+ * @param {DOMStringMap[string]} value - The value to normalise
137
+ * @param {SchemaProperty} [property] - Component schema property
138
+ * @returns {string | boolean | number | undefined} Normalised data
139
+ */
140
+ MOJFrontend.Alert.prototype.normaliseString = function (value, property) {
141
+ const trimmedValue = value ? value.trim() : ''
142
+
143
+ let output
144
+ let outputType
145
+ if (property && property.type) {
146
+ outputType = property.type
147
+ }
148
+
149
+ // No schema type set? Determine automatically
150
+ if (!outputType) {
151
+ if (['true', 'false'].includes(trimmedValue)) {
152
+ outputType = 'boolean'
153
+ }
154
+
155
+ // Empty / whitespace-only strings are considered finite so we need to check
156
+ // the length of the trimmed string as well
157
+ if (trimmedValue.length > 0 && isFinite(Number(trimmedValue))) {
158
+ outputType = 'number'
159
+ }
160
+ }
161
+
162
+ switch (outputType) {
163
+ case 'boolean':
164
+ output = trimmedValue === 'true'
165
+ break
166
+
167
+ case 'number':
168
+ output = Number(trimmedValue)
169
+ break
170
+
171
+ default:
172
+ output = value
173
+ }
174
+
175
+ return output
176
+ }
177
+
178
+ /**
179
+ * Parse dataset
180
+ *
181
+ * Loop over an object and normalise each value using {@link normaliseString},
182
+ * optionally expanding nested `i18n.field`
183
+ *
184
+ * @param {Schema} schema - component schema
185
+ * @param {DOMStringMap} dataset - HTML element dataset
186
+ * @returns {object} Normalised dataset
187
+ */
188
+ MOJFrontend.Alert.prototype.parseDataset = function (schema, dataset) {
189
+ const parsed = {}
190
+
191
+ for (const [field, property] of Object.entries(schema.properties)) {
192
+ if (field in dataset) {
193
+ if (dataset[field]) {
194
+ parsed[field] = this.normaliseString(dataset[field], property)
195
+ }
196
+ }
197
+ }
198
+
199
+ return parsed
200
+ }
201
+
202
+ /**
203
+ * Config merging function
204
+ *
205
+ * Takes any number of objects and combines them together, with
206
+ * greatest priority on the LAST item passed in.
207
+ *
208
+ * @param {...{ [key: string]: unknown }} configObjects - Config objects to merge
209
+ * @returns {{ [key: string]: unknown }} A merged config object
210
+ */
211
+ MOJFrontend.Alert.prototype.mergeConfigs = function (...configObjects) {
212
+ const formattedConfigObject = {}
213
+
214
+ // Loop through each of the passed objects
215
+ for (const configObject of configObjects) {
216
+ for (const key of Object.keys(configObject)) {
217
+ const option = formattedConfigObject[key]
218
+ const override = configObject[key]
219
+
220
+ // Push their keys one-by-one into formattedConfigObject. Any duplicate
221
+ // keys with object values will be merged, otherwise the new value will
222
+ // override the existing value.
223
+ if (typeof option === 'object' && typeof override === 'object') {
224
+ // @ts-expect-error Index signature for type 'string' is missing
225
+ formattedConfigObject[key] = this.mergeConfigs(option, override)
226
+ } else {
227
+ formattedConfigObject[key] = override
228
+ }
229
+ }
230
+ }
231
+
232
+ return formattedConfigObject
233
+ }
234
+
235
+ /**
236
+ * Schema for component config
237
+ *
238
+ * @typedef {object} Schema
239
+ * @property {{ [field: string]: SchemaProperty | undefined }} properties - Schema properties
240
+ */
241
+
242
+ /**
243
+ * Schema property for component config
244
+ *
245
+ * @typedef {object} SchemaProperty
246
+ * @property {'string' | 'boolean' | 'number' | 'object'} type - Property type
247
+ */
@@ -0,0 +1,67 @@
1
+ const pageTemplate = `
2
+ <main>
3
+ <div id="alert-1" role="region" class="moj-alert moj-alert--information moj-alert--with-heading" aria-label="information: This contains information" data-module="moj-alert" data-dismissible="true">
4
+ <div>
5
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.2165 3.45151C11.733 2.82332 13.3585 2.5 15 2.5C16.6415 2.5 18.267 2.82332 19.7835 3.45151C21.3001 4.07969 22.6781 5.00043 23.8388 6.16117C24.9996 7.3219 25.9203 8.69989 26.5485 10.2165C27.1767 11.733 27.5 13.3585 27.5 15C27.5 18.3152 26.183 21.4946 23.8388 23.8388C21.4946 26.183 18.3152 27.5 15 27.5C13.3585 27.5 11.733 27.1767 10.2165 26.5485C8.69989 25.9203 7.3219 24.9996 6.16117 23.8388C3.81696 21.4946 2.5 18.3152 2.5 15C2.5 11.6848 3.81696 8.50537 6.16117 6.16117C7.3219 5.00043 8.69989 4.07969 10.2165 3.45151ZM16.3574 22.4121H13.6621V12.95H16.3574V22.4121ZM13.3789 9.20898C13.3789 8.98763 13.4212 8.7793 13.5059 8.58398C13.5905 8.38216 13.7044 8.20964 13.8477 8.06641C13.9974 7.91667 14.1699 7.79948 14.3652 7.71484C14.5605 7.63021 14.7721 7.58789 15 7.58789C15.2214 7.58789 15.4297 7.63021 15.625 7.71484C15.8268 7.79948 15.9993 7.91667 16.1426 8.06641C16.2923 8.20964 16.4095 8.38216 16.4941 8.58398C16.5788 8.7793 16.6211 8.98763 16.6211 9.20898C16.6211 9.43685 16.5788 9.64844 16.4941 9.84375C16.4095 10.0391 16.2923 10.2116 16.1426 10.3613C15.9993 10.5046 15.8268 10.6185 15.625 10.7031C15.4297 10.7878 15.2214 10.8301 15 10.8301C14.7721 10.8301 14.5605 10.7878 14.3652 10.7031C14.1699 10.6185 13.9974 10.5046 13.8477 10.3613C13.7044 10.2116 13.5905 10.0391 13.5059 9.84375C13.4212 9.64844 13.3789 9.43685 13.3789 9.20898Z" fill="currentColor"/></svg>
6
+ </div>
7
+ <div class="moj-alert__content">
8
+ <h2 class="govuk-heading-m">This contains information</h2>
9
+ Content that informs you of a thing. It really is so very informative, that&#39;s why it needs so much content and is really, exceedingly verbose.
10
+ </div>
11
+ <div class="moj-alert__action">
12
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
13
+ </div>
14
+ </div>
15
+
16
+ <h1 id="h1">Heading 1</h1>
17
+ <section>
18
+ <h2 id="h2">heading 2</h2>
19
+ <div id="alert-2" role="region" class="moj-alert moj-alert--information" aria-label="information: You might like to know" data-module="moj-alert" data-dismissible="true">
20
+ <div>
21
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.2165 3.45151C11.733 2.82332 13.3585 2.5 15 2.5C16.6415 2.5 18.267 2.82332 19.7835 3.45151C21.3001 4.07969 22.6781 5.00043 23.8388 6.16117C24.9996 7.3219 25.9203 8.69989 26.5485 10.2165C27.1767 11.733 27.5 13.3585 27.5 15C27.5 18.3152 26.183 21.4946 23.8388 23.8388C21.4946 26.183 18.3152 27.5 15 27.5C13.3585 27.5 11.733 27.1767 10.2165 26.5485C8.69989 25.9203 7.3219 24.9996 6.16117 23.8388C3.81696 21.4946 2.5 18.3152 2.5 15C2.5 11.6848 3.81696 8.50537 6.16117 6.16117C7.3219 5.00043 8.69989 4.07969 10.2165 3.45151ZM16.3574 22.4121H13.6621V12.95H16.3574V22.4121ZM13.3789 9.20898C13.3789 8.98763 13.4212 8.7793 13.5059 8.58398C13.5905 8.38216 13.7044 8.20964 13.8477 8.06641C13.9974 7.91667 14.1699 7.79948 14.3652 7.71484C14.5605 7.63021 14.7721 7.58789 15 7.58789C15.2214 7.58789 15.4297 7.63021 15.625 7.71484C15.8268 7.79948 15.9993 7.91667 16.1426 8.06641C16.2923 8.20964 16.4095 8.38216 16.4941 8.58398C16.5788 8.7793 16.6211 8.98763 16.6211 9.20898C16.6211 9.43685 16.5788 9.64844 16.4941 9.84375C16.4095 10.0391 16.2923 10.2116 16.1426 10.3613C15.9993 10.5046 15.8268 10.6185 15.625 10.7031C15.4297 10.7878 15.2214 10.8301 15 10.8301C14.7721 10.8301 14.5605 10.7878 14.3652 10.7031C14.1699 10.6185 13.9974 10.5046 13.8477 10.3613C13.7044 10.2116 13.5905 10.0391 13.5059 9.84375C13.4212 9.64844 13.3789 9.43685 13.3789 9.20898Z" fill="currentColor"/></svg>
22
+ </div>
23
+ <div class="moj-alert__content">Content that informs you of a thing. <a href="#">More information</a></div>
24
+ <div class="moj-alert__action">
25
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
26
+ </div>
27
+ </div>
28
+
29
+
30
+ <div id="alert-3"role="region" class="moj-alert moj-alert--success" aria-label="success: Woohoo!" data-module="moj-alert" data-dismissible="true">
31
+ <div>
32
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30"><path d="M11.2869 24.6726L2.00415 15.3899L4.62189 12.7722L11.2869 19.4186L25.3781 5.32739L27.9958 7.96369L11.2869 24.6726Z" fill="currentColor"/>
33
+ </svg>
34
+ </div>
35
+ <div class="moj-alert__content">That thing just successfully occurred. <a href="#">Celebrate here</a></div>
36
+ <div class="moj-alert__action">
37
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
38
+ </div>
39
+ </div>
40
+
41
+ <div id="alert-4" role="region" class="moj-alert moj-alert--warning" aria-label="warning: Something&#39;s not quite right" data-module="moj-alert" data-dismissible="true">
42
+ <div>
43
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30"><path fill-rule="evenodd" clip-rule="evenodd" d="M15 2.44922L28.75 26.1992H1.25L15 2.44922ZM13.5107 9.49579H16.4697L16.2431 17.7678H13.7461L13.5107 9.49579ZM13.1299 21.82C13.1299 21.5661 13.1787 21.3285 13.2764 21.1071C13.374 20.8793 13.5075 20.6807 13.6768 20.5114C13.8525 20.3421 14.0544 20.2087 14.2822 20.111C14.5101 20.0134 14.7542 19.9645 15.0146 19.9645C15.2686 19.9645 15.5062 20.0134 15.7275 20.111C15.9554 20.2087 16.154 20.3421 16.3232 20.5114C16.4925 20.6807 16.626 20.8793 16.7236 21.1071C16.8213 21.3285 16.8701 21.5661 16.8701 21.82C16.8701 22.0804 16.8213 22.3246 16.7236 22.5524C16.626 22.7803 16.4925 22.9789 16.3232 23.1481C16.154 23.3174 15.9554 23.4509 15.7275 23.5485C15.5062 23.6462 15.2686 23.695 15.0146 23.695C14.7542 23.695 14.5101 23.6462 14.2822 23.5485C14.0544 23.4509 13.8525 23.3174 13.6768 23.1481C13.5075 22.9789 13.374 22.7803 13.2764 22.5524C13.1787 22.3246 13.1299 22.0804 13.1299 21.82Z" fill="currentColor"/></svg>
44
+ </div>
45
+ <div class="moj-alert__content">You should be aware of this thing. <a href="#">More information</a></div>
46
+ <div class="moj-alert__action">
47
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
48
+ </div>
49
+ </div>
50
+
51
+ <div id="alert-5" role="region" class="moj-alert moj-alert--error" aria-label="error: Woah, hold up!" data-module="moj-alert" data-dismissible="true" data-dismissible="true" data-focus-on-dismiss-selector="#focusOnMe">
52
+ <div>
53
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30"><path fill-rule="evenodd" clip-rule="evenodd" d="M20.1777 2.5H9.82233L2.5 9.82233V20.1777L9.82233 27.5H20.1777L27.5 20.1777V9.82233L20.1777 2.5ZM10.9155 8.87769L15.0001 12.9623L19.0847 8.87771L21.1224 10.9154L17.0378 15L21.1224 19.0846L19.0847 21.1222L15.0001 17.0376L10.9155 21.1223L8.87782 19.0846L12.9624 15L8.87783 10.9153L10.9155 8.87769Z" fill="currentColor"/></svg>
54
+ </div>
55
+ <div class="moj-alert__content">Bad things happened. <a href="#">Contact us</a></div>
56
+ <div class="moj-alert__action">
57
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
58
+ </div>
59
+ </div>
60
+
61
+ <div id="focusOnMe">I will receive focus</div>
62
+ </section>
63
+ </main>
64
+ `
65
+ module.exports = {
66
+ pageTemplate
67
+ }
@@ -0,0 +1,229 @@
1
+ /* eslint-disable no-new */
2
+ const { getByRole, queryByRole, screen } = require('@testing-library/dom')
3
+ const { userEvent } = require('@testing-library/user-event')
4
+
5
+ const { pageTemplate } = require('./alert.spec.helper.js')
6
+ require('../../helpers.js')
7
+ require('./alert.js')
8
+
9
+ const user = userEvent.setup()
10
+
11
+ const kebabize = (str) => {
12
+ return str.replace(
13
+ /[A-Z]+(?![a-z])|[A-Z]/g,
14
+ ($, ofset) => (ofset ? '-' : '') + $.toLowerCase()
15
+ )
16
+ }
17
+
18
+ const configToDataAttributes = (config) => {
19
+ let attributes = ''
20
+ for (const [key, value] of Object.entries(config)) {
21
+ attributes += `data-${kebabize(key)}="${value}" `
22
+ }
23
+ return attributes
24
+ }
25
+
26
+ const createComponent = (options = {}, role = 'region') => {
27
+ const dataAttributes = configToDataAttributes(options)
28
+ const html = `<div role="${role}" class="moj-alert moj-alert--information moj-alert--with-heading" aria-label="information: The finance section has moved" data-module="moj-alert" ${dataAttributes}>
29
+ <div>
30
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30">
31
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M10.2165 3.45151C11.733 2.82332 13.3585 2.5 15 2.5C16.6415 2.5 18.267 2.82332 19.7835 3.45151C21.3001 4.07969 22.6781 5.00043 23.8388 6.16117C24.9996 7.3219 25.9203 8.69989 26.5485 10.2165C27.1767 11.733 27.5 13.3585 27.5 15C27.5 18.3152 26.183 21.4946 23.8388 23.8388C21.4946 26.183 18.3152 27.5 15 27.5C13.3585 27.5 11.733 27.1767 10.2165 26.5485C8.69989 25.9203 7.3219 24.9996 6.16117 23.8388C3.81696 21.4946 2.5 18.3152 2.5 15C2.5 11.6848 3.81696 8.50537 6.16117 6.16117C7.3219 5.00043 8.69989 4.07969 10.2165 3.45151ZM16.3574 22.4121H13.6621V12.95H16.3574V22.4121ZM13.3789 9.20898C13.3789 8.98763 13.4212 8.7793 13.5059 8.58398C13.5905 8.38216 13.7044 8.20964 13.8477 8.06641C13.9974 7.91667 14.1699 7.79948 14.3652 7.71484C14.5605 7.63021 14.7721 7.58789 15 7.58789C15.2214 7.58789 15.4297 7.63021 15.625 7.71484C15.8268 7.79948 15.9993 7.91667 16.1426 8.06641C16.2923 8.20964 16.4095 8.38216 16.4941 8.58398C16.5788 8.7793 16.6211 8.98763 16.6211 9.20898C16.6211 9.43685 16.5788 9.64844 16.4941 9.84375C16.4095 10.0391 16.2923 10.2116 16.1426 10.3613C15.9993 10.5046 15.8268 10.6185 15.625 10.7031C15.4297 10.7878 15.2214 10.8301 15 10.8301C14.7721 10.8301 14.5605 10.7878 14.3652 10.7031C14.1699 10.6185 13.9974 10.5046 13.8477 10.3613C13.7044 10.2116 13.5905 10.0391 13.5059 9.84375C13.4212 9.64844 13.3789 9.43685 13.3789 9.20898Z" fill="currentColor" />
32
+ </svg>
33
+ </div>
34
+ <div class="moj-alert__content">
35
+ <h2 class="govuk-heading-m">The finance section has moved
36
+ </h2>You can now find it in the <a href="#">dashboard</a>.
37
+ </div>
38
+ <div class="moj-alert__action">
39
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
40
+ </div>
41
+ </div>
42
+ `
43
+
44
+ document.body.insertAdjacentHTML('afterbegin', html)
45
+ const component = document.querySelector('[data-module="moj-alert"]')
46
+ return component
47
+ }
48
+
49
+ describe('alert', () => {
50
+ let component
51
+ let dismissButton
52
+
53
+ afterEach(() => {
54
+ document.body.innerHTML = ''
55
+ })
56
+
57
+ test('non-dismissible', () => {
58
+ component = createComponent()
59
+ new MOJFrontend.Alert(component).init()
60
+ dismissButton = queryByRole(component, 'button', { hidden: true })
61
+
62
+ expect(dismissButton).toHaveAttribute('hidden')
63
+ })
64
+
65
+ test('dismissible', () => {
66
+ component = createComponent({ dismissible: 'true' })
67
+ new MOJFrontend.Alert(component).init()
68
+ dismissButton = queryByRole(component, 'button', { hidden: true })
69
+
70
+ expect(dismissButton).not.toHaveAttribute('hidden')
71
+ })
72
+
73
+ test('non-dismissible "false" string', () => {
74
+ component = createComponent({ dismissible: 'false' })
75
+ new MOJFrontend.Alert(component).init()
76
+ dismissButton = queryByRole(component, 'button', { hidden: true })
77
+
78
+ expect(dismissButton).toHaveAttribute('hidden')
79
+ })
80
+
81
+ test('applies custom dismiss text', () => {
82
+ component = createComponent({
83
+ dismissible: true,
84
+ dismissText: 'Close'
85
+ })
86
+ new MOJFrontend.Alert(component).init()
87
+ dismissButton = queryByRole(component, 'button', { name: 'Close' })
88
+
89
+ expect(dismissButton).toBeInTheDocument()
90
+ })
91
+
92
+ test('region role does not receive focus', () => {
93
+ component = createComponent({}, 'region')
94
+ new MOJFrontend.Alert(component).init()
95
+
96
+ expect(component).not.toHaveFocus()
97
+ })
98
+
99
+ test('alert role autofocuses', () => {
100
+ component = createComponent({}, 'alert')
101
+ new MOJFrontend.Alert(component).init()
102
+
103
+ expect(component).toHaveFocus()
104
+ })
105
+
106
+ test('disableAutoFocus prevents autofocus', () => {
107
+ component = createComponent(
108
+ {
109
+ disableAutoFocus: true
110
+ },
111
+ 'alert'
112
+ )
113
+ new MOJFrontend.Alert(component).init()
114
+
115
+ expect(component).not.toHaveFocus()
116
+ })
117
+
118
+ test('dismiss button removes component', async () => {
119
+ component = createComponent({
120
+ dismissible: true
121
+ })
122
+ new MOJFrontend.Alert(component).init()
123
+ dismissButton = queryByRole(component, 'button')
124
+
125
+ await user.click(dismissButton)
126
+
127
+ expect(component).not.toBeInTheDocument()
128
+ })
129
+
130
+ describe('focus on dismiss', () => {
131
+ let firstAlert
132
+ let secondAlert
133
+ let thirdAlert
134
+ let fourthAlert
135
+ let fifthAlert
136
+ let heading1
137
+ let heading2
138
+
139
+ beforeEach(() => {
140
+ document.body.insertAdjacentHTML('afterbegin', pageTemplate)
141
+ const alerts = document.querySelectorAll('[data-module="moj-alert"]')
142
+ alerts.forEach((alert) => {
143
+ new MOJFrontend.Alert(alert).init()
144
+ })
145
+
146
+ firstAlert = document.querySelector('#alert-1')
147
+ secondAlert = document.querySelector('#alert-2')
148
+ thirdAlert = document.querySelector('#alert-3')
149
+ fourthAlert = document.querySelector('#alert-4')
150
+ fifthAlert = document.querySelector('#alert-5')
151
+
152
+ heading1 = document.querySelector('#h1')
153
+ heading2 = document.querySelector('#h2')
154
+ })
155
+
156
+ afterEach(() => {
157
+ document.body.innerHTML = ''
158
+ })
159
+
160
+ test('it moves focus to the element provide by focusOnDismiss', async () => {
161
+ const alert = fifthAlert
162
+ const focusedElement = document.querySelector('#focusOnMe')
163
+ const dismissButton = getByRole(alert, 'button')
164
+
165
+ await user.click(dismissButton)
166
+
167
+ expect(focusedElement).toHaveFocus()
168
+ })
169
+
170
+ test('it moves focus to the next sibling alert if present', async () => {
171
+ const alert = thirdAlert
172
+ const dismissButton = getByRole(alert, 'button')
173
+
174
+ await user.click(dismissButton)
175
+
176
+ expect(fourthAlert).toHaveFocus()
177
+ })
178
+
179
+ test('it moves focus to the previous sibling alert if present', async () => {
180
+ fourthAlert.remove()
181
+ fifthAlert.remove()
182
+ const alert = thirdAlert
183
+ const dismissButton = getByRole(alert, 'button')
184
+
185
+ await user.click(dismissButton)
186
+
187
+ expect(secondAlert).toHaveFocus()
188
+ })
189
+
190
+ test('it moves focus to the previous heading if present', async () => {
191
+ thirdAlert.remove()
192
+ fourthAlert.remove()
193
+ fifthAlert.remove()
194
+ const alert = secondAlert
195
+ const dismissButton = getByRole(alert, 'button')
196
+
197
+ await user.click(dismissButton)
198
+
199
+ expect(heading2).toHaveFocus()
200
+ })
201
+
202
+ test('it moves focus to the parent heading if present', async () => {
203
+ heading2.remove()
204
+ thirdAlert.remove()
205
+ fourthAlert.remove()
206
+ fifthAlert.remove()
207
+ const alert = secondAlert
208
+ const dismissButton = getByRole(alert, 'button')
209
+
210
+ await user.click(dismissButton)
211
+
212
+ expect(heading1).toHaveFocus()
213
+ })
214
+
215
+ test('it moves focus to main if no other element matches', async () => {
216
+ heading2.remove()
217
+ thirdAlert.remove()
218
+ fourthAlert.remove()
219
+ fifthAlert.remove()
220
+ const alert = firstAlert
221
+ const main = screen.getByRole('main')
222
+ const dismissButton = getByRole(alert, 'button')
223
+
224
+ await user.click(dismissButton)
225
+
226
+ expect(main).toHaveFocus()
227
+ })
228
+ })
229
+ })
@@ -0,0 +1,3 @@
1
+ {% macro mojAlert(params) %}
2
+ {%- include "./template.njk" -%}
3
+ {% endmacro %}
@@ -0,0 +1,83 @@
1
+ {% from "govuk/macros/attributes.njk" import govukAttributes %}
2
+
3
+ {#- Valid options for params #}
4
+ {%- set headingTags = ["h2", "h3", "h4"] %}
5
+ {%- set variants = ["information", "success", "warning", "error"] %}
6
+
7
+ {#- Validate and set options for component #}
8
+ {%- set variant = variants | select("eq", params.variant) | join | default("information", true) %}
9
+ {%- set headingTag = headingTags | select("eq", params.headingTag) | join | default("h2", true) %}
10
+ {%- set content = params.html | safe if params.html else params.text %}
11
+ {%- set dismissible = true if params.dismissible and params.dismissible !== "false" else false %}
12
+ {%- set disableAutoFocus = true if params.disableAutoFocus and params.disableAutoFocus !== "false" else false %}
13
+
14
+ {#- Set classes for this component #}
15
+ {%- set classNames = ["moj-alert"] -%}
16
+ {%- set classNames = classNames.concat("moj-alert--"~variant) %}
17
+
18
+ {%- if params.showTitleAsHeading and content %}
19
+ {%- set classNames = classNames.concat("moj-alert--with-heading") %}
20
+ {%- endif %}
21
+
22
+ {%- if params.classes %}
23
+ {%- set classNames = classNames.concat(params.classes) %}
24
+ {%- endif %}
25
+
26
+ {%- set classNames = classNames | join(' ') -%}
27
+
28
+ {#- Set icon for this component #}
29
+ {%- if params.variant == 'success' %}
30
+ {%- set iconPath = '<path d="M11.2869 24.6726L2.00415 15.3899L4.62189 12.7722L11.2869 19.4186L25.3781 5.32739L27.9958 7.96369L11.2869 24.6726Z" fill="currentColor"/>' %}
31
+ {%- elif params.variant == 'warning' %}
32
+ {%- set iconPath = '<path fill-rule="evenodd" clip-rule="evenodd" d="M15 2.44922L28.75 26.1992H1.25L15 2.44922ZM13.5107 9.49579H16.4697L16.2431 17.7678H13.7461L13.5107 9.49579ZM13.1299 21.82C13.1299 21.5661 13.1787 21.3285 13.2764 21.1071C13.374 20.8793 13.5075 20.6807 13.6768 20.5114C13.8525 20.3421 14.0544 20.2087 14.2822 20.111C14.5101 20.0134 14.7542 19.9645 15.0146 19.9645C15.2686 19.9645 15.5062 20.0134 15.7275 20.111C15.9554 20.2087 16.154 20.3421 16.3232 20.5114C16.4925 20.6807 16.626 20.8793 16.7236 21.1071C16.8213 21.3285 16.8701 21.5661 16.8701 21.82C16.8701 22.0804 16.8213 22.3246 16.7236 22.5524C16.626 22.7803 16.4925 22.9789 16.3232 23.1481C16.154 23.3174 15.9554 23.4509 15.7275 23.5485C15.5062 23.6462 15.2686 23.695 15.0146 23.695C14.7542 23.695 14.5101 23.6462 14.2822 23.5485C14.0544 23.4509 13.8525 23.3174 13.6768 23.1481C13.5075 22.9789 13.374 22.7803 13.2764 22.5524C13.1787 22.3246 13.1299 22.0804 13.1299 21.82Z" fill="currentColor"/>' %}
33
+ {%- elif variant == 'error' %}
34
+ {%- set iconPath = '<path fill-rule="evenodd" clip-rule="evenodd" d="M20.1777 2.5H9.82233L2.5 9.82233V20.1777L9.82233 27.5H20.1777L27.5 20.1777V9.82233L20.1777 2.5ZM10.9155 8.87769L15.0001 12.9623L19.0847 8.87771L21.1224 10.9154L17.0378 15L21.1224 19.0846L19.0847 21.1222L15.0001 17.0376L10.9155 21.1223L8.87782 19.0846L12.9624 15L8.87783 10.9153L10.9155 8.87769Z" fill="currentColor"/>' %}
35
+ {%- else %}
36
+ {%- set iconPath = '<path fill-rule="evenodd" clip-rule="evenodd" d="M10.2165 3.45151C11.733 2.82332 13.3585 2.5 15 2.5C16.6415 2.5 18.267 2.82332 19.7835 3.45151C21.3001 4.07969 22.6781 5.00043 23.8388 6.16117C24.9996 7.3219 25.9203 8.69989 26.5485 10.2165C27.1767 11.733 27.5 13.3585 27.5 15C27.5 18.3152 26.183 21.4946 23.8388 23.8388C21.4946 26.183 18.3152 27.5 15 27.5C13.3585 27.5 11.733 27.1767 10.2165 26.5485C8.69989 25.9203 7.3219 24.9996 6.16117 23.8388C3.81696 21.4946 2.5 18.3152 2.5 15C2.5 11.6848 3.81696 8.50537 6.16117 6.16117C7.3219 5.00043 8.69989 4.07969 10.2165 3.45151ZM16.3574 22.4121H13.6621V12.95H16.3574V22.4121ZM13.3789 9.20898C13.3789 8.98763 13.4212 8.7793 13.5059 8.58398C13.5905 8.38216 13.7044 8.20964 13.8477 8.06641C13.9974 7.91667 14.1699 7.79948 14.3652 7.71484C14.5605 7.63021 14.7721 7.58789 15 7.58789C15.2214 7.58789 15.4297 7.63021 15.625 7.71484C15.8268 7.79948 15.9993 7.91667 16.1426 8.06641C16.2923 8.20964 16.4095 8.38216 16.4941 8.58398C16.5788 8.7793 16.6211 8.98763 16.6211 9.20898C16.6211 9.43685 16.5788 9.64844 16.4941 9.84375C16.4095 10.0391 16.2923 10.2116 16.1426 10.3613C15.9993 10.5046 15.8268 10.6185 15.625 10.7031C15.4297 10.7878 15.2214 10.8301 15 10.8301C14.7721 10.8301 14.5605 10.7878 14.3652 10.7031C14.1699 10.6185 13.9974 10.5046 13.8477 10.3613C13.7044 10.2116 13.5905 10.0391 13.5059 9.84375C13.4212 9.64844 13.3789 9.43685 13.3789 9.20898Z" fill="currentColor"/>' %}
37
+ {%- endif %}
38
+
39
+ <div
40
+ role="{{ params.role | default("region", true) }}"
41
+ class="{{ classNames }}"
42
+ aria-label="{{ variant }}: {{ params.title }}"
43
+ data-module="moj-alert"
44
+ {{- govukAttributes({
45
+ "data-dismissible": {
46
+ value: dismissible
47
+ }
48
+ }) if dismissible }}
49
+ {{- govukAttributes({
50
+ "data-disable-auto-focus": {
51
+ value: disableAutoFocus
52
+ }
53
+ }) if disableAutoFocus }}
54
+ {{- govukAttributes({
55
+ "data-focus-on-dismiss-selector": {
56
+ value: params.focusOnDismissSelector
57
+ }
58
+ }) if params.focusOnDismissSelector }}
59
+ {{- govukAttributes(params.attributes) -}}
60
+ >
61
+ <div>
62
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30">
63
+ {{- iconPath | safe }}
64
+ </svg>
65
+ </div>
66
+ <div class="moj-alert__content">
67
+ {%- if params.showTitleAsHeading and content %}
68
+ <{{-headingTag }} class="govuk-heading-m">
69
+ {{- params.title }}
70
+ </{{-headingTag}}>
71
+ {%- endif %}
72
+
73
+ {%- if params.title and not (content) %}
74
+ {{- params.title }}
75
+ {%- endif %}
76
+
77
+ {{- content -}}
78
+ </div>
79
+
80
+ <div class="moj-alert__action">
81
+ <button class="moj-alert__dismiss" hidden>{{ params.dismissText | default("Dismiss", true) }}</button>
82
+ </div>
83
+ </div>
@@ -4,14 +4,14 @@
4
4
 
5
5
  .moj-badge {
6
6
  @include govuk-font($size: 14, $weight: "bold");
7
- padding: 0 govuk-spacing(1);
8
7
  display: inline-block;
8
+ padding: 0 govuk-spacing(1);
9
9
  border: 2px solid $govuk-brand-colour;
10
+ outline: 2px solid transparent;
11
+ outline-offset: -2px;
10
12
  color: $govuk-brand-colour;
11
13
  text-transform: uppercase;
12
14
  vertical-align: middle;
13
- outline: 2px solid transparent;
14
- outline-offset: -2px;
15
15
 
16
16
  &--purple {
17
17
  border-color: govuk-colour("purple");
@@ -51,5 +51,4 @@
51
51
  &--large {
52
52
  @include govuk-font($size: 16, $weight: "bold");
53
53
  }
54
-
55
54
  }