Skip to content

Commit ab453b3

Browse files
Add actions option to the Panel component
Allows users to component to create consistent markup for the actions users are expected to take when shown an Interruption panel. The actions only show if the Panel is an Interruption panel and can be: - buttons, both `button` and `submit` type (the `attributes` option allows to set `form`, `name` and `value` for the `submit` type) - links, both regular and button-looking links The containing element (`govuk-panel__actions`) can receive custom `classes` and `attributes`, as can each action listed in the `items` list of actions. Having a specific element to target allows us to simplify CSS selectors for adjusting spacing around the panel's actions. Co-authored-by: Nick Colley <2445413+NickColley@users.noreply.github.com>
1 parent 2e28408 commit ab453b3

4 files changed

Lines changed: 312 additions & 40 deletions

File tree

packages/govuk-frontend/src/govuk/components/panel/_mixin.scss

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,29 @@
4343
margin-bottom: base.govuk-spacing(6);
4444
}
4545

46-
// Remove bottom spacing from anything at the end of the Panel's body
46+
// Remove bottom spacing from anything at the end of the Panel's content
4747
// to stop the Panel's padding spacing from becoming too large.
4848
.govuk-panel__title:last-child,
49-
.govuk-panel__body > :last-child {
49+
.govuk-panel__body > :last-child,
50+
.govuk-panel__actions > :last-child {
5051
margin-bottom: 0;
5152
}
5253

54+
// On larger screens if the Panel actions have a button group,
55+
// then we want to adjust the spacing a bit to avoid the button group
56+
// making the Panel's padding spacing being too large.
57+
@media #{base.govuk-from-breakpoint(tablet)} {
58+
.govuk-panel__actions > .govuk-button-group:last-child {
59+
margin-bottom: -1 * base.govuk-spacing(2);
60+
}
61+
}
62+
63+
// When the panel shows some actions, make sure they have some spacing
64+
// from the panel's content
65+
.govuk-panel__body:not(:last-child) {
66+
margin-bottom: base.govuk-spacing(7);
67+
}
68+
5369
.govuk-panel--confirmation {
5470
--govuk-text-colour: #{base.govuk-functional-colour(inverse-text)};
5571
background: base.govuk-functional-colour(success);
@@ -65,21 +81,6 @@
6581
@include base.govuk-font-size($size: 36);
6682
margin-bottom: base.govuk-spacing(3);
6783
}
68-
69-
// When the Button group is at the end of the Panel's body then,
70-
// make sure it has some spacing away from the other body content.
71-
.govuk-panel__body > .govuk-button-group:last-child {
72-
margin-top: base.govuk-spacing(7);
73-
}
74-
75-
// On larger screens if the Interruption panel has a Button group at the end of the Panel's body,
76-
// then we want to adjust the spacing a bit to avoid the Button group
77-
// making the Panel's padding spacing being too large.
78-
@media #{base.govuk-from-breakpoint(tablet)} {
79-
&:has(.govuk-panel__body > .govuk-button-group:last-child) {
80-
padding-bottom: base.govuk-spacing(5);
81-
}
82-
}
8384
}
8485

8586
@media print {

packages/govuk-frontend/src/govuk/components/panel/panel.yaml

Lines changed: 135 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,43 @@ params:
3131
type: object
3232
required: false
3333
description: HTML attributes (for example data attributes) to add to the panel container.
34+
- name: actions
35+
type: object
36+
required: false
37+
description: If creating an Interruption panel, the buttons and links that you want to display. `actions` defaults to `"button"` unless you set `href`, which renders the action as a link.
38+
params:
39+
- name: items
40+
type: array
41+
required: false
42+
params:
43+
- name: text
44+
type: string
45+
required: true
46+
description: The button or link text.
47+
- name: type
48+
type: string
49+
required: false
50+
description: The type of button – `"button"` or `"submit"`. If `href` is provided, set `type` to `"button"` render a link styled as a button.
51+
- name: href
52+
type: string
53+
required: false
54+
description: The `href` for a link. Set `type` to `"button"` and set `href` to render a link styled as a button.
55+
- name: classes
56+
type: string
57+
required: false
58+
description: The additional classes that you want to add to the button or link.
59+
- name: attributes
60+
type: object
61+
required: false
62+
description: The additional attributes that you want to add to the button or link. For example, data attributes.
63+
- name: classes
64+
type: string
65+
required: false
66+
description: The additional classes that you want to add to the `govuk-panel__actions` element that wraps the buttons or links.
67+
- name: attributes
68+
type: object
69+
required: false
70+
description: The additional attributes that you want to add to the `govuk-panel__actions` element that wraps the buttons or links. For example, data attributes.
3471

3572
examples:
3673
- name: default
@@ -44,12 +81,12 @@ examples:
4481
titleText: Is your age correct?
4582
html: |
4683
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
47-
<div class="govuk-button-group">
48-
<button type="submit" class="govuk-button govuk-button--inverse" data-module="govuk-button">
49-
Yes, this is correct
50-
</button>
51-
<a href="#" class="govuk-link govuk-link--inverse">Change my age</a>
52-
</div>
84+
actions:
85+
items:
86+
- type: button
87+
text: Yes, this is correct
88+
- href: '#'
89+
text: Change my age
5390
- name: interruption-with-content-with-long-line-length
5491
options:
5592
classes: govuk-panel--interruption
@@ -59,14 +96,14 @@ examples:
5996
<p class="govuk-body">Previous home address: 1 Willow Lane, Newchurch, Kington, HR5 3QF</p>
6097
<p class="govuk-body">New home address: 9 Elm Street, Whitney-on-Wye, Hereford, HR3 6EH</p>
6198
<p class="govuk-body">You can go back to undo this change.</p>
62-
<div class="govuk-button-group">
63-
<button type="submit" class="govuk-button govuk-button--inverse" data-module="govuk-button">
64-
Are you sure you want to change this?
65-
</button>
66-
<a href="#" class="govuk-link govuk-link--inverse">
67-
Go back to application
68-
</a>
69-
</div>
99+
actions:
100+
items:
101+
- type: submit
102+
text: Are you sure you want to change this?
103+
attributes:
104+
form: the-form-id
105+
- href: '#'
106+
text: Go back to application
70107
- name: interruption-with-headings-content-and-lists
71108
options:
72109
classes: govuk-panel--interruption
@@ -82,15 +119,11 @@ examples:
82119
<li>enter their custody information</li>
83120
<li>update the licence conditions section</li>
84121
</ul>
85-
<div class="govuk-button-group">
86-
<button type="submit" class="govuk-button govuk-button--inverse" data-module="govuk-button">
87-
Continue to other sections
88-
</button>
89-
<a href="#" class="govuk-link govuk-link--inverse">
90-
Go back to application
91-
</a>
92-
</div>
93-
122+
actions:
123+
- type: submit
124+
text: Continue to other sections
125+
- href: '#'
126+
text: Go back to application
94127
# Hidden examples are not shown in the review app, but are used for tests and HTML fixtures
95128
- name: title html as text
96129
hidden: true
@@ -142,3 +175,82 @@ examples:
142175
classes: govuk-panel--interruption extra-class one-more-class
143176
titleText: Are you sure you want to change this?
144177
html: <p class="govuk-body">You've changed the person's address from Wales to England. This means that the referral needs to be cancelled.</p>
178+
- name: confirmation, with actions
179+
hidden: true
180+
options:
181+
titleText: Is your age correct?
182+
actions:
183+
items:
184+
- type: button
185+
text: Yes, this is correct
186+
- href: '#'
187+
text: Change my age
188+
- name: interruption, with actions classes and attributes
189+
hidden: true
190+
options:
191+
classes: govuk-panel--interruption
192+
titleText: Is your age correct?
193+
html: |
194+
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
195+
actions:
196+
classes: extra-class one-more-class
197+
attributes:
198+
'data-test': 1
199+
'data-other-test': 'yes'
200+
items:
201+
- type: button
202+
text: Yes, this is correct
203+
- href: '#'
204+
text: Change my age
205+
- name: interruption, submit action
206+
hidden: true
207+
options:
208+
classes: govuk-panel--interruption
209+
titleText: Is your age correct?
210+
html: |
211+
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
212+
actions:
213+
items:
214+
- type: submit
215+
text: Yes, this is correct
216+
attributes:
217+
form: test-target-element
218+
name: 'acknowledged'
219+
value: ''
220+
classes: 'extra-class one-more-class'
221+
- href: '#'
222+
text: Change my age
223+
- name: interruption, button link
224+
hidden: true
225+
options:
226+
classes: govuk-panel--interruption
227+
titleText: Is your age correct?
228+
html: |
229+
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
230+
actions:
231+
classes: extra-class one-more-class
232+
attributes:
233+
'data-test': 1
234+
'data-other-test': 'yes'
235+
items:
236+
- type: button
237+
text: Yes, this is correct
238+
href: '?acknowledged=true'
239+
- href: '#'
240+
text: Change my age
241+
- name: interruption, no actions
242+
hidden: true
243+
options:
244+
classes: govuk-panel--interruption
245+
titleText: Is your age correct?
246+
html: |
247+
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
248+
- name: interruption, no actions items
249+
hidden: true
250+
options:
251+
classes: govuk-panel--interruption
252+
titleText: Is your age correct?
253+
html: |
254+
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
255+
actions:
256+
classes: 'extra-class'

packages/govuk-frontend/src/govuk/components/panel/template.njk

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% from "../../macros/attributes.njk" import govukAttributes -%}
2+
{% from "../button/macro.njk" import govukButton -%}
23

34
{% set headingLevel = params.headingLevel if params.headingLevel else 1 -%}
45

@@ -7,6 +8,23 @@
78
{#- The Panel component defaults to the Confirmation variant -#}
89
{%- set defaultModifierClassName = "govuk-panel--confirmation" if not isInterruption -%}
910

11+
{%- macro _panelAction(action) -%}
12+
{%- if not action.href or action.type == "button" -%}
13+
{{ govukButton({
14+
"text": action.text,
15+
"type": action.type if action.type else "button",
16+
"classes": "govuk-button--inverse" + (" " + action.classes if action.classes),
17+
"href": action.href,
18+
"attributes": action.attributes
19+
}) }}
20+
{%- else -%}
21+
<a class="govuk-link govuk-link--inverse {%- if action.classes %} {{ action.classes }}{% endif %}" href="{{ action.href }}"
22+
{{- govukAttributes(action.attributes) }}>
23+
{{- action.text -}}
24+
</a>
25+
{%- endif -%}
26+
{%- endmacro -%}
27+
1028
<div class="govuk-panel
1129
{%- if defaultModifierClassName %} {{ defaultModifierClassName }}{% endif %}
1230
{%- if params.classes %} {{ params.classes }}{% endif -%}"
@@ -19,4 +37,20 @@
1937
{{ caller() if caller else (params.html | safe | trim | indent(4) if params.html else params.text) }}
2038
</div>
2139
{% endif %}
40+
{% if isInterruption and params.actions %}
41+
{% set actions = params.actions %}
42+
<div class="govuk-panel__actions
43+
{%- if actions.classes %} {{ actions.classes }}{% endif -%}"
44+
{{- govukAttributes(actions.attributes) }}>
45+
46+
{%- if actions.items.length -%}
47+
<div class="govuk-button-group">
48+
{% for action in actions.items %}
49+
{{ _panelAction(action) | safe | trim | indent(6)}}
50+
{% endfor %}
51+
</div>
52+
{%- endif -%}
53+
54+
</div>
55+
{% endif %}
2256
</div>

0 commit comments

Comments
 (0)