Skip to content

Commit 5b8fe2a

Browse files
authored
Merge pull request apvarun#106 from nickvergessen/feature/105/support-aria-live-attribute
Support aria-live attribute
2 parents 5725170 + e94359e commit 5b8fe2a

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ If `gravity` is equals to `bottom`, it will be pushed from bottom.
153153
| offset | Object | Ability to add some offset to axis | |
154154
| escapeMarkup | boolean | Toggle the default behavior of escaping HTML markup | true |
155155
| style | object | Use the HTML DOM Style properties to add any style directly to toast | |
156+
| ariaLive | string | Announce the toast to screen readers, see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions for options | "polite" |
156157
| oldestFirst | boolean | Set the order in which toasts are stacked in page | true |
157158

158159
> Deprecated properties: `backgroundColor` - use `style.background` option instead

src/toastify-es.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @property {Function} onClick - Invoked when the toast is clicked
2727
* @property {Object} offset - Ability to add some offset to axis
2828
* @property {boolean} escapeMarkup - Toggle the default behavior of escaping HTML markup
29+
* @property {string} ariaLive - Use the HTML DOM style property to add styles to toast
2930
* @property {Object} style - Use the HTML DOM style property to add styles to toast
3031
*/
3132

@@ -52,6 +53,7 @@ class Toastify {
5253
onClick: function() {},
5354
offset: { x: 0, y: 0 },
5455
escapeMarkup: true,
56+
ariaLive: "polite",
5557
style: { background: "" },
5658
};
5759

@@ -160,6 +162,7 @@ class Toastify {
160162
* @param {Function} [options.onClick] - Invoked when the toast is clicked
161163
* @param {Object} [options.offset] - Ability to add some offset to axis
162164
* @param {boolean} [options.escapeMarkup=true] - Toggle the default behavior of escaping HTML markup
165+
* @param {string} [options.ariaLive] - Announce the toast to screen readers
163166
* @param {Object} [options.style] - Use the HTML DOM style property to add styles to toast
164167
* @private
165168
*/
@@ -208,6 +211,11 @@ class Toastify {
208211
divElement.style[property] = this.options.style[property];
209212
}
210213

214+
// Announce the toast to screen readers
215+
if (this.options.ariaLive) {
216+
divElement.setAttribute('aria-live', this.options.ariaLive)
217+
}
218+
211219
// Adding the toast message/node
212220
if (this.options.node && this.options.node.nodeType === Node.ELEMENT_NODE) {
213221
// If we have a valid node, we insert it

src/toastify.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
},
4444
offset: {x: 0, y: 0},
4545
escapeMarkup: true,
46+
ariaLive: 'polite',
4647
style: {background: ''}
4748
};
4849

@@ -83,6 +84,7 @@
8384
this.options.onClick = options.onClick || Toastify.defaults.onClick; // Callback after click
8485
this.options.offset = options.offset || Toastify.defaults.offset; // toast offset
8586
this.options.escapeMarkup = options.escapeMarkup !== undefined ? options.escapeMarkup : Toastify.defaults.escapeMarkup;
87+
this.options.ariaLive = options.ariaLive || Toastify.defaults.ariaLive;
8688
this.options.style = options.style || Toastify.defaults.style;
8789
if(options.backgroundColor) {
8890
this.options.style.background = options.backgroundColor;
@@ -130,6 +132,11 @@
130132
divElement.style[property] = this.options.style[property];
131133
}
132134

135+
// Announce the toast to screen readers
136+
if (this.options.ariaLive) {
137+
divElement.setAttribute('aria-live', this.options.ariaLive)
138+
}
139+
133140
// Adding the toast message/node
134141
if (this.options.node && this.options.node.nodeType === Node.ELEMENT_NODE) {
135142
// If we have a valid node, we insert it

0 commit comments

Comments
 (0)