chromium-dashboard/client-src/elements/chromedash-toast.ts

136 строки
3.2 KiB
TypeScript
Исходник Постоянная ссылка Обычный вид История

import {LitElement, css, html} from 'lit';
import {SHARED_STYLES} from '../css/shared-css.js';
import {customElement, state, property} from 'lit/decorators.js';
2019-04-24 23:24:40 +03:00
const DEFAULT_DURATION = 7000;
2019-04-24 23:24:40 +03:00
@customElement('chromedash-toast')
2019-04-24 23:24:40 +03:00
class ChromedashToast extends LitElement {
@property({type: String})
msg = '';
2019-04-24 23:24:40 +03:00
@property({type: Boolean, reflect: true})
open = false;
@property({attribute: false})
actionLabel = '';
@state()
currentTimeout: number | null = null;
@property({type: Boolean})
waitingForTransition = false;
2019-04-24 23:24:40 +03:00
static get styles() {
return [
...SHARED_STYLES,
css`
2024-04-17 07:52:17 +03:00
:host {
display: flex;
justify-content: space-between;
position: fixed;
background: var(--toast-background);
color: var(--toast-color);
min-height: 48px;
min-width: 288px;
padding: var(--content-padding);
box-sizing: border-box;
box-shadow: var(--card-box-shadow);
border-radius: var(--border-radius);
margin: var(--content-padding);
cursor: default;
transition:
transform 0.3s,
opacity 0.3s;
opacity: 0;
will-change: opacity, transform;
-webkit-transform: translateY(100px);
transform: translateY(100px);
z-index: 3;
bottom: 0;
}
2024-04-17 07:52:17 +03:00
:host([open]) {
opacity: 1;
transform: translateY(-32px);
}
2024-04-17 07:52:17 +03:00
#action {
text-transform: uppercase;
text-decoration: none;
color: var(--toast-action-color);
font-weight: bold;
}
2024-04-17 07:52:17 +03:00
#msg {
margin-right: var(--content-padding);
}
`,
];
}
showMessage(
msg: string,
optAction: string,
optTapHandler: () => void,
optDuration: number
) {
if (this.waitingForTransition) return;
2019-04-24 23:24:40 +03:00
this.msg = msg;
2019-05-22 21:13:09 +03:00
this.actionLabel = optAction;
if (optTapHandler) {
const action = this.shadowRoot!.querySelector('#action');
if (action) {
action.addEventListener(
'click',
e => {
e.preventDefault();
optTapHandler();
},
{once: true}
);
}
}
2019-04-24 23:24:40 +03:00
if (this.open) {
// triggers the previous toast to slide out
this.open = false;
this.waitingForTransition = true;
if (this.currentTimeout !== null) {
clearTimeout(this.currentTimeout);
}
// Don't show the new toast until the transition is over
// (wait for the previous toast to be completely gone)
2024-04-17 07:52:17 +03:00
this.addEventListener(
'transitionend',
() => {
this.show(optDuration);
this.waitingForTransition = false;
},
{once: true}
);
} else {
this.show(optDuration);
}
}
show(optDuration) {
const duration = optDuration || DEFAULT_DURATION;
if (duration > 0) {
this.currentTimeout = window.setTimeout(() => {
this.open = false;
}, duration);
2019-04-24 23:24:40 +03:00
}
this.open = true;
}
render() {
return html`
2019-06-28 19:56:30 +03:00
<span id="msg">${this.msg}</span>
<a href="#" id="action">${this.actionLabel}</a>
2019-04-24 23:24:40 +03:00
`;
}
}