Skip to content

Commit 6394a0e

Browse files
romaricpascalNickColleycalvin-lau-sig7
committed
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> Co-authored-by: Calvin Lau <77630796+calvin-lau-sig7@users.noreply.github.com>
1 parent b49d3d1 commit 6394a0e

4 files changed

Lines changed: 313 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: 136 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,44 @@ 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: Can be used when creating an [interruption panel](https://design-system.service.gov.uk/components/panel/#interruption-panel) which will add a button or link. Default is `button` unless you set `href` which will show the action as a link.
38+
params:
39+
- name: items
40+
type: array
41+
required: false
42+
description: The list of buttons or links to display at the end of an Interruption panel.
43+
params:
44+
- name: text
45+
type: string
46+
required: true
47+
description: The button or link text.
48+
- name: type
49+
type: string
50+
required: false
51+
description: The type of button – `"button"` or `"submit"`. If `href` is provided, set `type` to `"button"` render a link styled as a button.
52+
- name: href
53+
type: string
54+
required: false
55+
description: The `href` for a link. Set `type` to `"button"` and set `href` to render a link styled as a button.
56+
- name: classes
57+
type: string
58+
required: false
59+
description: The additional classes that you want to add to the button or link.
60+
- name: attributes
61+
type: object
62+
required: false
63+
description: The additional attributes that you want to add to the button or link. For example, data attributes.
64+
- name: classes
65+
type: string
66+
required: false
67+
description: The additional classes that you want to add to the `govuk-panel__actions` element that wraps the buttons or links.
68+
- name: attributes
69+
type: object
70+
required: false
71+
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.
3472

3573
examples:
3674
- name: default
@@ -44,12 +82,12 @@ examples:
4482
titleText: Is your age correct?
4583
html: |
4684
<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>
85+
actions:
86+
items:
87+
- text: Yes, this is correct
88+
type: button
89+
- text: No, change my age
90+
href: '#'
5391
- name: interruption-with-content-with-long-line-length
5492
options:
5593
classes: govuk-panel--interruption
@@ -59,14 +97,14 @@ examples:
5997
<p class="govuk-body">Previous home address: 1 Willow Lane, Newchurch, Kington, HR5 3QF</p>
6098
<p class="govuk-body">New home address: 9 Elm Street, Whitney-on-Wye, Hereford, HR3 6EH</p>
6199
<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>
100+
actions:
101+
items:
102+
- text: Are you sure you want to change this?
103+
type: submit
104+
attributes:
105+
form: the-form-id
106+
- text: Go back to application
107+
href: '#'
70108
- name: interruption-with-headings-content-and-lists
71109
options:
72110
classes: govuk-panel--interruption
@@ -82,15 +120,11 @@ examples:
82120
<li>enter their custody information</li>
83121
<li>update the licence conditions section</li>
84122
</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-
123+
actions:
124+
- text: Continue to other sections
125+
type: submit
126+
- text: Go back to application
127+
href: '#'
94128
# Hidden examples are not shown in the review app, but are used for tests and HTML fixtures
95129
- name: title html as text
96130
hidden: true
@@ -142,3 +176,82 @@ examples:
142176
classes: govuk-panel--interruption extra-class one-more-class
143177
titleText: Are you sure you want to change this?
144178
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>
179+
- name: confirmation, with actions
180+
hidden: true
181+
options:
182+
titleText: Is your age correct?
183+
actions:
184+
items:
185+
- type: button
186+
text: Yes, this is correct
187+
- href: '#'
188+
text: No, change my age
189+
- name: interruption, with actions classes and attributes
190+
hidden: true
191+
options:
192+
classes: govuk-panel--interruption
193+
titleText: Is your age correct?
194+
html: |
195+
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
196+
actions:
197+
classes: extra-class one-more-class
198+
attributes:
199+
'data-test': 1
200+
'data-other-test': 'yes'
201+
items:
202+
- type: button
203+
text: Yes, this is correct
204+
- href: '#'
205+
text: No, change my age
206+
- name: interruption, submit action
207+
hidden: true
208+
options:
209+
classes: govuk-panel--interruption
210+
titleText: Is your age correct?
211+
html: |
212+
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
213+
actions:
214+
items:
215+
- type: submit
216+
text: Yes, this is correct
217+
attributes:
218+
form: test-target-element
219+
name: 'acknowledged'
220+
value: ''
221+
classes: 'extra-class one-more-class'
222+
- href: '#'
223+
text: No, change my age
224+
- name: interruption, button link
225+
hidden: true
226+
options:
227+
classes: govuk-panel--interruption
228+
titleText: Is your age correct?
229+
html: |
230+
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
231+
actions:
232+
classes: extra-class one-more-class
233+
attributes:
234+
'data-test': 1
235+
'data-other-test': 'yes'
236+
items:
237+
- type: button
238+
text: Yes, this is correct
239+
href: '?acknowledged=true'
240+
- href: '#'
241+
text: No, change my age
242+
- name: interruption, no actions
243+
hidden: true
244+
options:
245+
classes: govuk-panel--interruption
246+
titleText: Is your age correct?
247+
html: |
248+
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
249+
- name: interruption, no actions items
250+
hidden: true
251+
options:
252+
classes: govuk-panel--interruption
253+
titleText: Is your age correct?
254+
html: |
255+
<p class="govuk-body">You entered your age as <strong>109</strong>.</p>
256+
actions:
257+
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)