blob: 43af5310da0ac7880acf81dcf6ec003828ea1194 [file] [log] [blame]
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @module elements-sk/error-toast-sk
* @description <h2><code>error-toast-sk</code></h2>
*
* <p>
* Listens for 'error-sk' events that bubble up to the document and displays them.
* These are normally generated by errorMessage.
* </p>
*
* <p>
* The 'error-sk' event should have 'detail' of the form:
* </p>
*
* <pre>
* {
* message: "The error message to display goes here.",
* duration: Integer, the number of ms to display or 0 for indefinitely.
* Defaults to 10000 (10s)
* }
* </pre>
*
* @example
*
* <footer>
* <error-toast-sk></error-toast-sk>
* </footer>
*/
import { define } from '../define';
import { ToastSk } from '../toast-sk/toast-sk';
import '../toast-sk';
import { ErrorSkEventDetail } from '../errorMessage';
export class ErrorToastSk extends HTMLElement {
private toast: ToastSk | null = null;
private span: HTMLSpanElement | null = null;
connectedCallback(): void {
this.innerHTML =
'<toast-sk><span></span><button title="Close">✗</button></toast-sk>';
this.toast = this.firstElementChild as ToastSk;
this.span = this.toast!.firstElementChild as HTMLSpanElement;
document.addEventListener('error-sk', this);
this.querySelector('button')!.addEventListener('click', (e) =>
this.clickHandler()
);
}
disconnectedCallback(): void {
document.removeEventListener('error-sk', this);
}
clickHandler() {
this.toast!.hide();
}
handleEvent(e: CustomEvent<ErrorSkEventDetail>) {
this.toast!.duration = e.detail.duration;
this.span!.textContent = e.detail.message;
this.toast!.show();
}
}
define('error-toast-sk', ErrorToastSk);