({
runtimeScene,
content,
type,
id,
persist,
}: {
runtimeScene: gdjs.RuntimeScene;
content: string;
type: 'success' | 'warning' | 'error';
id?: string;
persist?: boolean;
})
| 582 | * Helper to show a notification to the user, that disappears automatically. |
| 583 | */ |
| 584 | export const showNotification = function ({ |
| 585 | runtimeScene, |
| 586 | content, |
| 587 | type, |
| 588 | id, |
| 589 | persist, |
| 590 | }: { |
| 591 | runtimeScene: gdjs.RuntimeScene; |
| 592 | content: string; |
| 593 | type: 'success' | 'warning' | 'error'; |
| 594 | id?: string; |
| 595 | persist?: boolean; |
| 596 | }) { |
| 597 | // When we show a notification, we add it below the other ones. |
| 598 | // We also remove the oldest one if there are too many > 5. |
| 599 | if (notificationContainerIds.length > 5) { |
| 600 | const oldestNotificationId = notificationContainerIds.shift(); |
| 601 | if (!oldestNotificationId) { |
| 602 | logger.error('No oldest notification ID found.'); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | removeNotificationAndShiftOthers(oldestNotificationId); |
| 607 | } |
| 608 | |
| 609 | // We generate a random ID for the notification, so they can stack. |
| 610 | const notificationId = |
| 611 | id || `notification-${Math.random().toString(36).substring(7)}`; |
| 612 | |
| 613 | const domContainer = runtimeScene |
| 614 | .getGame() |
| 615 | .getRenderer() |
| 616 | .getDomElementContainer(); |
| 617 | if (!domContainer) { |
| 618 | logger.error('No DOM element container found.'); |
| 619 | return; |
| 620 | } |
| 621 | const divContainer = document.createElement('div'); |
| 622 | |
| 623 | divContainer.id = notificationId; |
| 624 | divContainer.style.position = 'absolute'; |
| 625 | divContainer.style.pointerEvents = 'all'; |
| 626 | divContainer.style.backgroundColor = |
| 627 | type === 'success' |
| 628 | ? '#0E062D' |
| 629 | : type === 'warning' |
| 630 | ? '#FFA500' |
| 631 | : '#FF0000'; |
| 632 | // Space the notifications vertically, based on how many there are. |
| 633 | divContainer.style.top = `${12 + notificationContainerIds.length * 32}px`; |
| 634 | divContainer.style.right = '16px'; |
| 635 | divContainer.style.padding = '6px 32px 6px 6px'; |
| 636 | // Use zIndex 1 to make sure it is below the iframe. |
| 637 | divContainer.style.zIndex = '1'; |
| 638 | divContainer.style.display = 'flex'; |
| 639 | divContainer.style.flexDirection = 'row-reverse'; |
| 640 | divContainer.style.justifyContent = 'space-between'; |
| 641 | divContainer.style.alignItems = 'center'; |
no test coverage detected