-
Notifications
You must be signed in to change notification settings - Fork 5
/
product-tiles-with-quick-view-functionality.html
449 lines (366 loc) · 12.6 KB
/
product-tiles-with-quick-view-functionality.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!DOCTYPE html>
<html lang="en">
<head>
<title>Splide - product tiles with quick view functionality accessible boilerplate</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/@splidejs/splide@latest/dist/css/splide.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<meta name="description" content="Pre-built code sample for an accessible carousel of product tiles with quick view functionality using Splide.">
<meta name="keywords" content="accessible, accessibility, WCAG, carousel, slider, Splide, tiles, quick view, popup, template, boilerplate">
<!-- Open Graph -->
<meta property="og:title" content="Splide - product tiles with quick view functionality accessible boilerplate">
<meta property="og:description" content="Pre-built code sample for an accessible carousel of product tiles with quick view functionality using Splide.">
<meta property="og:type" content="website">
<!-- Twitter Card -->
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Splide - product tiles with quick view functionality accessible boilerplate">
<meta name="twitter:description" content="Pre-built code sample for an accessible carousel of product tiles with quick view functionality using Splide.">
<meta name="twitter:creator" content="Accessible_360">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">
</head>
<body>
<main>
<section class="carousel" aria-label="carousel with product tiles">
<p class="sr-only">This is a carousel with rotating tiles that activate modal dialogs. Use the previous and next buttons to navigate, and Enter to activate tiles.</p>
<!-- Previous button -->
<div class="arrow-wrapper previous-button-wrapper">
<button class="previous-button is-control">
<span class="fas fa-angle-left" aria-hidden="true"></span>
<span class="sr-only">Previous tiles</span>
</button>
</div>
<!-- Slide content -->
<div class="splide">
<div class="splide__track">
<div class="splide__list">
<button class="tile splide__slide">
<div class="sr-only">tile 1 of 5,</div>
<img src="../images/galina-n-300x300.jpg" class="product-image" alt="Succulent in planter">
<div class="overlay">
<div class="quick-view-button">Quick view</div>
</div>
</button>
<button class="tile splide__slide">
<div class="sr-only">tile 2 of 5,</div>
<img src="../images/curology-300x300.jpg" class="product-image" alt="Lotion bottle with text Curology on it">
<div class="overlay">
<div class="quick-view-button">Quick view</div>
</div>
</button>
<button class="tile splide__slide">
<div class="sr-only">tile 3 of 5,</div>
<img src="../images/imani-clovis-300x300.jpg" class="product-image" alt="Black running shoe falling downward">
<div class="overlay">
<div class="quick-view-button">Quick view</div>
</div>
</button>
<button class="tile splide__slide">
<div class="sr-only">tile 4 of 5,</div>
<img src="../images/rachit-tank-300x300.jpg" class="product-image" alt="Wristwatch laid on table">
<div class="overlay">
<div class="quick-view-button">Quick view</div>
</div>
</button>
<button class="tile splide__slide">
<div class="sr-only">tile 5 of 5,</div>
<img src="../images/deanna-alys-300x300.jpg" class="product-image" alt="Lotion bottle with succulent and water bottle in background">
<div class="overlay">
<div class="quick-view-button">Quick view</div>
</div>
</button>
</div>
</div>
</div>
<!-- Next button -->
<div class="arrow-wrapper next-button-wrapper">
<button class="next-button is-control">
<span class="fas fa-angle-right" aria-hidden="true"></span>
<span class="sr-only">Next tiles</span>
</button>
</div>
</section>
<a href="https://accessible360.com" class="credits" target="_blank">
Built by <b><u>Accessible360</u></b> <img src="https://assets.codepen.io/3609497/accessible360-logo-black.png" alt="">
</a>
<!-- Quick view dialog - reused for each tile -->
<div class="dialog is-hidden" role="dialog" aria-modal="true">
<div class="overlay">
<button class="close-button first-focusable-element">
<span class="fa fa-times" aria-hidden="true"></span>
<span class="sr-only">Close</span>
</button>
<h2 class="title">Quick view</h2>
<p>This is a fake modal. Press Escape to close, or activate any of the buttons.</p>
<button class="ok-button last-focusable-element">OK</button>
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@splidejs/splide@latest/dist/js/splide.min.js"></script>
<script>
var splide;
var previousButton, nextButton;
var dialog, dialogOverlay, dialogCloseButton, dialogOkButton;
var triggeringElement;
document.addEventListener('DOMContentLoaded', function() {
previousButton = document.querySelector('.carousel .previous-button');
nextButton = document.querySelector('.carousel .next-button');
dialog = document.querySelector('.dialog');
dialogOverlay = dialog.querySelector('.overlay');
dialogCloseButton = dialogOverlay.querySelector('.close-button');
dialogOkButton = dialogOverlay.querySelector('.ok-button');
splide = new Splide('.splide', {
arrows: false,
perPage: 3,
type: 'loop',
pagination: false,
keyboard: false, // Splide listens to key events at the document level and moves ALL carousels when arrow keys are used. Since keyboard and screen reader users use these keys to operate the tabs, this creates a very unusual experience.
slideFocus: false, // removes tabindex="0" from each slide wrapper, since we only want our links inside each slide to receive focus.
gap: '10px',
padding: {
left: '50px',
right: '50px'
},
breakpoints: {
575: {
perPage: 2
},
375: {
perPage: 1
}
}
}).mount();
// To prevent animation issues, let's make every slide visible before a transition happens. Splide will then automatically remove the `.is-visible` class from non-visible slides once the transition is finished.
splide.on('move', function() {
var slides = document.querySelectorAll('.splide .splide__slide');
slides.forEach(function(slide) {
slide.classList.add('is-visible');
});
});
// Go to the previous slide when the Previous button is activated
previousButton.addEventListener('click', function(e) {
splide.go('<');
});
// Go to the next slide when the Next button is activated
nextButton.addEventListener('click', function(e) {
splide.go('>');
});
var tiles = document.querySelectorAll('.splide__slide');
tiles.forEach(function(tile) {
tile.addEventListener('click', openDialog);
});
// Close the dialog when any of its interactive elements are activated
dialogCloseButton.addEventListener('click', closeDialog);
dialogOkButton.addEventListener('click', closeDialog);
// Clicking anywhere outside the dialog content should close the dialog
dialog.addEventListener('click', handleDialogClicks);
dialog.addEventListener('keydown', handleDialogKeypresses);
});
/**
NOTE: Modal window for code demo purposes only!
*/
function openDialog(e) {
e.preventDefault();
triggeringElement = e.target;
dialog.classList.remove('is-hidden');
dialogCloseButton.focus();
}
function closeDialog() {
triggeringElement.focus();
dialog.classList.add('is-hidden');
}
function handleDialogClicks(e) {
if(!dialogOverlay.contains(e.target)) {
closeDialog();
}
}
function handleDialogKeypresses(e) {
switch(e.key) {
case 'Escape':
closeDialog();
break;
case 'Tab':
if(e.shiftKey && document.activeElement === dialogCloseButton) {
e.preventDefault();
dialogOkButton.focus();
} else if(!e.shiftKey && document.activeElement === dialogOkButton) {
e.preventDefault();
dialogCloseButton.focus();
}
break;
}
}
</script>
<style>
/**
Setup for code demo only
*/
:root {
--carousel-height: 175px;
}
body {
margin: 10px;
font-family: Arial, Helvetica, sans-serif;
}
/**
Container
*/
.carousel {
position: relative;
max-width: 700px;
}
/**
Controls (previous/next buttons)
*/
.arrow-wrapper {
position: absolute;
top: 0;
height: 100%;
z-index: 1;
display: flex;
align-items: center;
background-color: white;
}
.previous-button-wrapper { left: 0; }
.next-button-wrapper { right: 0; }
.is-control {
background: 0;
border: 0;
padding: 0 10px;
z-index: 1;
cursor: pointer;
font-size: 40px;
color: rgba(0,0,0,.6);
transition: all .2s linear;
}
.is-control:hover {
color: rgba(0,0,0,1);
}
.is-control:focus {
outline: none;
background-color: rgba(0,0,0,.8);
color: rgba(255,255,255,1);
border-radius: 5px;
}
/**
Single tile
*/
.tile {
visibility: hidden;
height: var(--carousel-height);
position: relative;
padding: 0;
border: 0;
background: none;
cursor: pointer;
}
.tile.is-visible {
visibility: visible;
}
.tile:focus {
outline: 0;
}
.tile .product-image {
width: 100%;
height: 100%;
object-fit: cover;
}
.tile .overlay {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0,0,0,.6);
transition: opacity .1s linear;
opacity: 0;
}
.tile:focus .overlay,
.tile:hover .overlay {
opacity: 1;
}
.tile .overlay .quick-view-button {
padding: 10px 15px;
background: rgba(0,0,0,.7);
border: 3px solid white;
box-shadow: 0 0 15px 0 rgba(0,0,0,.6);
color: white;
font-size: 14px;
text-transform: uppercase;
font-weight: bold;
}
/**
Quick view dialog
*/
.dialog {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,.6);
z-index: 100;
display: flex;
justify-content: center;
align-items: center;
}
.dialog.is-hidden {
display: none;
}
.dialog .overlay {
background-color: white;
border-radius: 10px;
position: relative;
width: 80%;
max-width: 400px;
padding: 20px;
}
.dialog .overlay .close-button {
position: absolute;
top: 15px;
right: 10px;
background: none;
border: 0;
font-size: 18px;
cursor: pointer;
}
.dialog .overlay .title {
margin: 0;
}
.dialog .overlay .ok-button {
padding: 10px 15px;
background: royalblue;
border: 0;
border-radius: 5px;
color: white;
cursor: pointer;
}
.dialog .overlay .ok-button:focus {
outline-offset: 5px;
}
/** Credits at bottom */
.credits {
display: inline-block;
margin-top: 20px;
margin-left: 40px;
padding: 10px;
font-size: 14px;
color: black;
text-decoration: none;
opacity: .7;
}
.credits img {
height: 30px;
margin-left: 5px;
margin-top: -2px;
vertical-align: middle;
}
.credits:hover,
.credits:focus {
opacity: 1;
}
</style>
</body>
</html>