Commit a0575b9d authored by Jack Steinberg's avatar Jack Steinberg Committed by Commit Bot

Enable persistent toasts by adding functionality for duration Infinity

This change allows users to show() toasts that will persist indefinitely
by passing through duration: Infinity. The change also adds range boundaries
to duration, so the value must be greater than 0.

Bug: 985468, 972945
Change-Id: I694edab74aff7f99e770e7d37373329f5e663c99
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1713684
Commit-Queue: Jack Steinberg <jacksteinberg@chromium.org>
Reviewed-by: default avatarFergal Daly <fergal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#680574}
parent 20a02436
......@@ -67,6 +67,17 @@ export class StdToastElement extends HTMLElement {
#timeoutID;
#actionSlot;
#closeButtonElement;
#setCloseTimeout = duration => {
clearTimeout(this.#timeoutID);
if (duration === Infinity) {
this.#timeoutID = null;
} else {
this.#timeoutID = setTimeout(() => {
this.removeAttribute('open');
}, duration);
}
};
constructor(message) {
super();
......@@ -160,11 +171,12 @@ export class StdToastElement extends HTMLElement {
}
show({duration = DEFAULT_DURATION} = {}) {
if (duration <= 0) {
throw new RangeError(`Invalid Argument: duration must be greater than 0 [${duration} given]`);
}
this.setAttribute('open', '');
clearTimeout(this.#timeoutID);
this.#timeoutID = setTimeout(() => {
this.removeAttribute('open');
}, duration);
this.#setCloseTimeout(duration);
}
hide() {
......@@ -182,8 +194,7 @@ export class StdToastElement extends HTMLElement {
this.dispatchEvent(new Event('show'));
} else if (newValue === null) {
this.dispatchEvent(new Event('hide'));
clearTimeout(this.#timeoutID);
this.#timeoutID = null;
this.#setCloseTimeout(Infinity);
}
break;
case 'closebutton':
......
......@@ -102,22 +102,28 @@ async_test(t => {
});
}, 'showToast closes after user specified 50ms');
// type
test(() => {
const toast = showToast('Message', {type: 'warning'});
assert_equals(toast.type, 'warning');
assert_equals(toast.getAttribute('type'), 'warning');
}, 'passing a valid type option to showToast sets the type property and attribute');
async_test(t => {
const toast = showToast('message', {duration: Infinity});
t.step_timeout(() => {
assertToastShown(toast);
t.done();
}, 50);
t.add_cleanup(function() {
toast.remove();
});
}, 'passing Infinity as the duration leaves the toast open for at least 50ms');
test(() => {
const toast = showToast('Message', {type: 'test'});
assert_equals(toast.type, '');
assert_equals(toast.getAttribute('type'), 'test');
}, 'passing an invalid type option to showToast sets the type attribute, but not the property');
assert_throws(new RangeError(), () => {
const toast = showToast('Message', {duration: 0});
});
}, 'setting the duration to 0 throws a RangeError');
test(() => {
const toast = showToast('Message');
assert_equals(toast.type, '');
assert_false(toast.hasAttribute('type'));
}, 'passing nothing as the type option to showToast does not set the type attribute or property');
assert_throws(new RangeError(), () => {
const toast = showToast('Message', {duration: -5});
});
}, 'setting the duration to a negative number throws a RangeError');
</script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment