Skip to content

Commit df87b00

Browse files
kp77tibi2303
andauthored
UCPKN-3880: Pass attributes to modal and toast sections. (#693)
* UCPKN-3879: Allow passing attributes to modal header, body and footer. * UCPKN-3880: Allow passing attributes to toast elements, add custom content. * UCPKN-3880: Add test for toast without close button. * UCPKN-3880: Avoid overriding toast close button aria-label attribute. * UCPKN-3880: Updated tests. --------- Co-authored-by: Tiberiu Dumitru <tiberiu.dumitru23@gmail.com>
1 parent a201c6a commit df87b00

File tree

7 files changed

+151
-35
lines changed

7 files changed

+151
-35
lines changed

src/components/bcl-modal/__snapshots__/modal.test.js.snap

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports[`OE - modal renders correctly 1`] = `
1616
class="modal-content"
1717
>
1818
<div
19-
class="modal-header"
19+
class="test-class modal-header"
2020
>
2121
<h5
2222
class="modal-title"
@@ -32,7 +32,7 @@ exports[`OE - modal renders correctly 1`] = `
3232
/>
3333
</div>
3434
<div
35-
class="modal-body"
35+
class="test-class modal-body"
3636
>
3737
<p>
3838
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fermentum facilisis ex, ac porta massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
@@ -48,7 +48,7 @@ exports[`OE - modal renders correctly 1`] = `
4848
</p>
4949
</div>
5050
<div
51-
class="modal-footer"
51+
class="test-class modal-footer"
5252
>
5353
<button
5454
class="btn btn-secondary"
@@ -86,7 +86,7 @@ exports[`OE - modal renders correctly small 1`] = `
8686
class="modal-content"
8787
>
8888
<div
89-
class="modal-header"
89+
class="test-class modal-header"
9090
>
9191
<h5
9292
class="modal-title"
@@ -102,7 +102,7 @@ exports[`OE - modal renders correctly small 1`] = `
102102
/>
103103
</div>
104104
<div
105-
class="modal-body"
105+
class="test-class modal-body"
106106
>
107107
<p>
108108
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fermentum facilisis ex, ac porta massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
@@ -118,7 +118,7 @@ exports[`OE - modal renders correctly small 1`] = `
118118
</p>
119119
</div>
120120
<div
121-
class="modal-footer"
121+
class="test-class modal-footer"
122122
>
123123
<button
124124
class="btn btn-secondary"
@@ -156,7 +156,7 @@ exports[`OE - modal renders correctly with fullscreen breakpoint 1`] = `
156156
class="modal-content"
157157
>
158158
<div
159-
class="modal-header"
159+
class="test-class modal-header"
160160
>
161161
<h5
162162
class="modal-title"
@@ -172,7 +172,7 @@ exports[`OE - modal renders correctly with fullscreen breakpoint 1`] = `
172172
/>
173173
</div>
174174
<div
175-
class="modal-body"
175+
class="test-class modal-body"
176176
>
177177
<p>
178178
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fermentum facilisis ex, ac porta massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
@@ -188,7 +188,7 @@ exports[`OE - modal renders correctly with fullscreen breakpoint 1`] = `
188188
</p>
189189
</div>
190190
<div
191-
class="modal-footer"
191+
class="test-class modal-footer"
192192
>
193193
<button
194194
class="btn btn-secondary"
@@ -226,7 +226,7 @@ exports[`OE - modal renders correctly with scrollable modal 1`] = `
226226
class="modal-content"
227227
>
228228
<div
229-
class="modal-header"
229+
class="test-class modal-header"
230230
>
231231
<h5
232232
class="modal-title"
@@ -242,7 +242,7 @@ exports[`OE - modal renders correctly with scrollable modal 1`] = `
242242
/>
243243
</div>
244244
<div
245-
class="modal-body"
245+
class="test-class modal-body"
246246
>
247247
<p>
248248
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fermentum facilisis ex, ac porta massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
@@ -258,7 +258,7 @@ exports[`OE - modal renders correctly with scrollable modal 1`] = `
258258
</p>
259259
</div>
260260
<div
261-
class="modal-footer"
261+
class="test-class modal-footer"
262262
>
263263
<button
264264
class="btn btn-secondary"
@@ -297,7 +297,7 @@ exports[`OE - modal renders correctly with static backdrop 1`] = `
297297
class="modal-content"
298298
>
299299
<div
300-
class="modal-header"
300+
class="test-class modal-header"
301301
>
302302
<h5
303303
class="modal-title"
@@ -313,7 +313,7 @@ exports[`OE - modal renders correctly with static backdrop 1`] = `
313313
/>
314314
</div>
315315
<div
316-
class="modal-body"
316+
class="test-class modal-body"
317317
>
318318
<p>
319319
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fermentum facilisis ex, ac porta massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
@@ -329,7 +329,7 @@ exports[`OE - modal renders correctly with static backdrop 1`] = `
329329
</p>
330330
</div>
331331
<div
332-
class="modal-footer"
332+
class="test-class modal-footer"
333333
>
334334
<button
335335
class="btn btn-secondary"
@@ -367,7 +367,7 @@ exports[`OE - modal renders correctly with verticaly centered modal 1`] = `
367367
class="modal-content"
368368
>
369369
<div
370-
class="modal-header"
370+
class="test-class modal-header"
371371
>
372372
<h5
373373
class="modal-title"
@@ -383,7 +383,7 @@ exports[`OE - modal renders correctly with verticaly centered modal 1`] = `
383383
/>
384384
</div>
385385
<div
386-
class="modal-body"
386+
class="test-class modal-body"
387387
>
388388
<p>
389389
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fermentum facilisis ex, ac porta massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
@@ -399,7 +399,7 @@ exports[`OE - modal renders correctly with verticaly centered modal 1`] = `
399399
</p>
400400
</div>
401401
<div
402-
class="modal-footer"
402+
class="test-class modal-footer"
403403
>
404404
<button
405405
class="btn btn-secondary"

src/components/bcl-modal/modal.html.twig

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
- body (block) (default: '')
1515
- footer (block) (default: '')
1616
- attributes (drupal attrs)
17-
17+
- header_attributes (drupal attrs)
18+
- body_attributes (drupal attrs)
19+
- footer_attributes (drupal attrs)
20+
1821
Blocks
1922
- header
2023
- messages
@@ -29,9 +32,12 @@
2932
{% set _verticaly_centered = verticaly_centered ?? false %}
3033
{% set _scrollable = scrollable ?? false %}
3134
{% set _header = header|default('') %}
35+
{% set _header_attributes = header_attributes ?: create_attribute() %}
3236
{% set _messages = messages|default('') %}
3337
{% set _body = body|default('') %}
38+
{% set _body_attributes = body_attributes ?: create_attribute() %}
3439
{% set _footer = footer|default('') %}
40+
{% set _footer_attributes = footer_attributes ?: create_attribute() %}
3541

3642
{% set _classes = ['modal', 'fade'] %}
3743

@@ -62,16 +68,22 @@
6268
>
6369
<div class="modal-content">
6470
{% if header is not empty %}
65-
<div class="modal-header">
71+
<div
72+
{{- _header_attributes.addClass(['modal-header']) -}}
73+
>
6674
{%- block header _header -%}
6775
</div>
6876
{% endif %}
6977
{%- block messages _messages -%}
70-
<div class="modal-body">
78+
<div
79+
{{- _body_attributes.addClass(['modal-body']) -}}
80+
>
7181
{%- block body _body -%}
7282
</div>
7383
{% if footer is not empty %}
74-
<div class="modal-footer">
84+
<div
85+
{{- _footer_attributes.addClass(['modal-footer']) -}}
86+
>
7587
{%- block footer _footer -%}
7688
</div>
7789
{% endif %}

src/components/bcl-toasts/__snapshots__/toasts.test.js.snap

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ exports[`OE - Toasts render correctly with body wrapper classes 1`] = `
88
class="toast"
99
role="alert"
1010
>
11+
<div
12+
class="test-class toast-header"
13+
>
14+
Toast title
15+
<button
16+
aria-label="Close"
17+
class="test-class btn-close"
18+
data-bs-dismiss="toast"
19+
type="button"
20+
/>
21+
</div>
1122
<div
1223
class="wrapper-class another-wrapper-class"
1324
>
1425
<div
15-
class="toast-body"
26+
class="test-class toast-body"
1627
>
1728
Hello, world! This is a toast message.
1829
@@ -35,6 +46,9 @@ exports[`OE - Toasts render correctly with body wrapper classes 1`] = `
3546
</div>
3647
</div>
3748
</div>
49+
<p>
50+
Toast custom content.
51+
</p>
3852
</div>
3953
</jest>
4054
`;
@@ -69,7 +83,7 @@ exports[`OE - Toasts render correctly with multiple toasts 1`] = `
6983
just now
7084
</small>
7185
<button
72-
aria-label="close"
86+
aria-label="Close"
7387
class="btn-close"
7488
data-bs-dismiss="toast"
7589
type="button"
@@ -101,7 +115,7 @@ exports[`OE - Toasts render correctly with multiple toasts 1`] = `
101115
2 seconds ago
102116
</small>
103117
<button
104-
aria-label="close"
118+
aria-label="Close"
105119
class="btn-close"
106120
data-bs-dismiss="toast"
107121
type="button"
@@ -127,7 +141,61 @@ exports[`OE - Toasts renders correctly with a single toast 1`] = `
127141
role="alert"
128142
>
129143
<div
130-
class="toast-body"
144+
class="test-class toast-header"
145+
>
146+
Toast title
147+
<button
148+
aria-label="Close"
149+
class="test-class btn-close"
150+
data-bs-dismiss="toast"
151+
type="button"
152+
/>
153+
</div>
154+
<div
155+
class="test-class toast-body"
156+
>
157+
Hello, world! This is a toast message.
158+
159+
<div
160+
class="mt-2 pt-2 border-top"
161+
>
162+
<button
163+
class="btn btn-primary btn-sm me-1"
164+
type="button"
165+
>
166+
Take action
167+
</button>
168+
<button
169+
class="btn btn-secondary btn-sm"
170+
data-bs-dismiss="toast"
171+
type="button"
172+
>
173+
Close
174+
</button>
175+
</div>
176+
</div>
177+
<p>
178+
Toast custom content.
179+
</p>
180+
</div>
181+
</jest>
182+
`;
183+
184+
exports[`OE - Toasts renders correctly with a single toast without close button 1`] = `
185+
<jest>
186+
<div
187+
aria-atomic="true"
188+
aria-live="assertive"
189+
class="toast"
190+
role="alert"
191+
>
192+
<div
193+
class="test-class toast-header"
194+
>
195+
Toast title
196+
</div>
197+
<div
198+
class="test-class toast-body"
131199
>
132200
Hello, world! This is a toast message.
133201
@@ -149,6 +217,9 @@ exports[`OE - Toasts renders correctly with a single toast 1`] = `
149217
</button>
150218
</div>
151219
</div>
220+
<p>
221+
Toast custom content.
222+
</p>
152223
</div>
153224
</jest>
154225
`;

src/components/bcl-toasts/toasts.html.twig

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
- toasts (object[]) format: [
1111
{
1212
- body (string) (default: '')
13+
- body_attributes (drupal attrs)
14+
- header (string) (default: '')
15+
- header_attributes (drupal attrs)
16+
- button_attributes (drupal attrs)
17+
- custom_content (string) (default: '')
1318
- role (string) (default: 'alert')
1419
- with_close (boolean) (default: true)
1520
- autohide (boolean) (default: false)
@@ -66,6 +71,9 @@
6671
{% set _with_body_wrapper = _toast.with_body_wrapper ?? false %}
6772
{% set _body_wrapper_classes = _toast.body_wrapper_classes|default('') %}
6873
{% set _toast_attributes = _toast.attributes ?: create_attribute() %}
74+
{% set _body_attributes = _toast.body_attributes ?: create_attribute() %}
75+
{% set _header_attributes = _toast.header_attributes ?: create_attribute() %}
76+
{% set _button_attributes = _toast.button_attributes ?: create_attribute() %}
6977
{% set _toast_attributes = _toast_attributes.addClass(_classes).setAttribute('aria-atomic', 'true') %}
7078
{% if _aria_live is not empty %}
7179
{% set _toast_attributes = _toast_attributes.setAttribute('aria-live', _aria_live) %}
@@ -82,16 +90,16 @@
8290
{% if _with_close or
8391
(_toast.header is defined and _toast.header is not empty)
8492
%}
85-
<div class="toast-header">
93+
<div{{ _header_attributes.addClass(['toast-header']) }}>
8694
{{- _toast.header|default('') -}}
8795
{% if _with_close %}
88-
{% set button_attributes = create_attribute()
89-
.addClass(['btn-close'])
90-
.setAttribute('aria-label', 'close')
91-
.setAttribute('data-bs-dismiss', 'toast')
92-
%}
96+
{% if not _button_attributes.hasAttribute('aria-label') %}
97+
{% set _button_attributes = _button_attributes.setAttribute('aria-label', 'Close') %}
98+
{% endif %}
9399
{%- include '@oe-bcl/bcl-button/button.html.twig' with {
94-
attributes: button_attributes,
100+
attributes: _button_attributes
101+
.addClass(['btn-close'])
102+
.setAttribute('data-bs-dismiss', 'toast'),
95103
clean_class: true
96104
} only -%}
97105
{% endif %}
@@ -104,12 +112,13 @@
104112
{% endif %}
105113
>
106114
{% endif %}
107-
<div class="toast-body">
115+
<div{{ _body_attributes.addClass(['toast-body']) }}>
108116
{{- _body -}}
109117
</div>
110118
{% if _with_body_wrapper %}
111119
</div>
112120
{% endif %}
121+
{{- _toast.custom_content|default('') -}}
113122
</div>
114123
{% endfor %}
115124
{% endif %}

0 commit comments

Comments
 (0)