Skip to content

Commit

Permalink
feat(a11y): support alt tags for images
Browse files Browse the repository at this point in the history
This PR adds a bit of more a11y support to toast, dropdown and search when it comes to generate images and adding an optional alt-attribute

new option alt for toast (will be used when showImage is used as well)
support for alt key in search response object (will be used if an image key is given as well)
support for alt as dropdown-item key (will be used if an image key is given as well)
  • Loading branch information
lubber-de authored Mar 22, 2024
1 parent 8b6456b commit 4e43130
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4180,6 +4180,7 @@
type: 'type', // type of dropdown element
image: 'image', // optional image path
imageClass: 'imageClass', // optional individual class for image
alt: 'alt', // optional alt text for image
icon: 'icon', // optional icon name
iconClass: 'iconClass', // optional individual class for icon (for example to use flag instead)
class: 'class', // optional individual class for item/header
Expand Down Expand Up @@ -4357,7 +4358,7 @@
html += '<i class="' + (itemType.indexOf('left') !== -1 ? 'left' : '') + ' dropdown icon"></i>';
}
if (option[fields.image]) {
html += '<img class="' + deQuote(option[fields.imageClass] || className.image) + '" src="' + deQuote(option[fields.image]) + '">';
html += '<img class="' + deQuote(option[fields.imageClass] || className.image) + '" src="' + deQuote(option[fields.image]) + (option[fields.alt] ? '" alt="' + deQuote(option[fields.alt]) : '') + '">';
}
if (option[fields.icon]) {
html += '<i class="' + deQuote(option[fields.icon] + ' ' + (option[fields.iconClass] || className.icon)) + '"></i>';
Expand Down
5 changes: 3 additions & 2 deletions src/definitions/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,7 @@
categoryResults: 'results', // array of results (category view)
description: 'description', // result description
image: 'image', // result image
alt: 'alt', // result alt text for image
price: 'price', // result price
results: 'results', // array of results (standard)
title: 'title', // result title
Expand Down Expand Up @@ -1531,7 +1532,7 @@
if (result[fields.image] !== undefined) {
html += ''
+ '<div class="image">'
+ ' <img src="' + result[fields.image].replace(/"/g, '') + '">'
+ ' <img src="' + result[fields.image].replace(/"/g, '') + (result[fields.alt] ? '" alt="' + result[fields.alt].replace(/"/g, '') : '') + '">'
+ '</div>';
}
html += '<div class="content">';
Expand Down Expand Up @@ -1584,7 +1585,7 @@
if (result[fields.image] !== undefined) {
html += ''
+ '<div class="image">'
+ ' <img src="' + result[fields.image].replace(/"/g, '') + '">'
+ ' <img src="' + result[fields.image].replace(/"/g, '') + (result[fields.alt] ? '" alt="' + result[fields.alt].replace(/"/g, '') : '') + '">'
+ '</div>';
}
html += '<div class="content">';
Expand Down
4 changes: 3 additions & 1 deletion src/definitions/modules/toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
$toast.append($('<img>', {
class: className.image + ' ' + settings.classImage,
src: settings.showImage,
alt: settings.alt || '',
}));
}
if (settings.title !== '') {
Expand Down Expand Up @@ -228,7 +229,7 @@
$toast.find(selector.icon).attr('class', iconClass + ' ' + className.icon);
}
if (settings.showImage) {
$toast.find(selector.image).attr('src', settings.showImage);
$toast.find(selector.image).attr('src', settings.showImage).attr('alt', settings.alt || '');
}
if (settings.title !== '') {
$toast.find(selector.title).html(module.helpers.escape(settings.title, settings.preserveHTML));
Expand Down Expand Up @@ -841,6 +842,7 @@
actions: false,
preserveHTML: true,
showImage: false,
alt: false,

// transition settings
transition: {
Expand Down

0 comments on commit 4e43130

Please sign in to comment.