meilisearch-admin/app/components/toast.js

76 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2022-09-12 14:49:25 +00:00
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
2022-09-13 10:57:48 +00:00
import { registerDestructor } from '@ember/destroyable';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
2022-09-13 15:28:41 +00:00
import { htmlSafe } from '@ember/template';
const margin = 16;
2022-09-12 14:49:25 +00:00
export default class ToastComponent extends Component {
2022-09-19 12:36:40 +00:00
@service toaster;
2022-09-13 10:57:48 +00:00
@tracked width;
@tracked height;
2022-09-14 07:56:45 +00:00
get autoCloseDuration() {
return this.args?.autoCloseDuration ?? 3000;
}
2022-09-13 15:28:41 +00:00
get top() {
2022-09-19 12:36:40 +00:00
const queue = this.toaster.queue;
2022-09-13 15:28:41 +00:00
for (let i = 0, height = 0; i < queue.length; i++) {
height += margin;
if (queue[i] === this) {
return `top: ${height}px`;
}
height += queue[i].height;
}
return `top: ${margin}px`;
}
get right() {
return `right: ${margin}px`;
}
get style() {
return htmlSafe([this.top, this.right].join('; '));
}
2022-09-13 14:22:00 +00:00
@action
2022-09-15 07:36:45 +00:00
invokeOnClose() {
2022-09-13 14:22:00 +00:00
this.args.onClose?.();
2022-09-13 10:57:48 +00:00
}
@action
didInsert(el) {
this.registerInQueue();
this.registerResizeObserver(el);
}
registerResizeObserver(el) {
const ro = new ResizeObserver((entries) => {
2022-09-13 15:28:41 +00:00
this.width = entries[0].borderBoxSize[0].inlineSize;
this.height = entries[0].borderBoxSize[0].blockSize;
});
ro.observe(el);
registerDestructor(this, () => {
ro.disconnect();
});
}
2022-09-13 14:27:17 +00:00
registerInQueue() {
2022-09-19 12:36:40 +00:00
this.toaster.add(this);
2022-09-13 14:22:00 +00:00
registerDestructor(this, () => {
2022-09-19 12:36:40 +00:00
this.toaster.remove(this);
2022-09-13 14:22:00 +00:00
});
2022-09-13 10:57:48 +00:00
}
2022-09-12 14:49:25 +00:00
}