Skip to content

Commit

Permalink
Merge pull request #355 from Oksydan/alert-toast-changes
Browse files Browse the repository at this point in the history
Alert toast changes - rewrite to bootstrap Toast component
  • Loading branch information
Oksydan authored Dec 6, 2023
2 parents 89042ee + 3f75080 commit 7c3610f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 88 deletions.
1 change: 0 additions & 1 deletion _dev/css/theme/components/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
@import "search/index";
@import "page-loader/index";
@import "links-list/index";
@import "alert-toast/index";
11 changes: 0 additions & 11 deletions _dev/css/theme/components/alert-toast/_alert-toast-stack.scss

This file was deleted.

43 changes: 0 additions & 43 deletions _dev/css/theme/components/alert-toast/_alert-toast.scss

This file was deleted.

2 changes: 0 additions & 2 deletions _dev/css/theme/components/alert-toast/_index.scss

This file was deleted.

4 changes: 4 additions & 0 deletions _dev/css/theme/override/bootstrap/_toast.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.toast-container {
width: rem-calc(250px);
}
49 changes: 21 additions & 28 deletions _dev/js/theme/components/useAlertToast.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseToHtml } from '../../utils/DOM/DOMHelpers';
import { one } from '../../utils/event/eventHandler';

let id = 0;

Expand Down Expand Up @@ -29,14 +30,18 @@ const useAlertToast = (params = {}) => {
/**
* Builds the template for an individual toast.
* @param {string} text - The text content of the toast.
* @param {string} type - The type of toast ('info', 'success', 'danger', 'warning').
* @param {string|null} type - The type of toast ('info', 'success', 'danger', 'warning').
* @param {string} toastId - The unique ID for the toast element.
* @returns {HTMLElement} - The constructed toast element.
*/
const buildToastTemplate = (text, type, toastId) => parseToHtml(`
<div class="alert-toast alert-toast--${type} d-none" id=${toastId}>
<div class="alert-toast__content">
${text}
<div class="toast ${type ? `text-bg-${type}` : ''}" id=${toastId}>
<div class="d-flex">
<div class="toast-body">
${text}
</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close">
</button>
</div>
</div>
`);
Expand All @@ -46,7 +51,7 @@ const useAlertToast = (params = {}) => {
* @returns {HTMLElement} - The constructed toast stack container element.
*/
const buildToastStackTemplate = () => parseToHtml(`
<div id="${stackTemplateId}" class="alert-toast-stack">
<div id="${stackTemplateId}" class="toast-container p-3 top-0 end-0 position-fixed">
</div>
`);

Expand All @@ -64,27 +69,13 @@ const useAlertToast = (params = {}) => {
return getElement();
};

/**
* Hides a toast element.
* @param {HTMLElement} toast - The toast element to hide.
*/
const hideToast = (toast) => {
toast.classList.remove('show');

const hideDuration = (parseFloat(window.getComputedStyle(toast).transitionDuration)) * 1000;

setTimeout(() => {
toast.remove();
}, hideDuration);
};

/**
* Displays a toast with the given text, type, and optional timeout duration.
* @param {string} text - The text content of the toast.
* @param {string} type - The type of toast ('info', 'success', 'danger', 'warning').
* @param {string|null} type - The type of toast ('info', 'success', 'danger', 'warning').
* @param {number|boolean} [timeOut=false] - Optional timeout duration for the toast.
*/
const showToast = (text, type, timeOut = false) => {
const showToast = (text, type = null, timeOut = false) => {
const toastId = getId();
const toast = buildToastTemplate(text, type, toastId);
const toastStack = getToastStackTemplate();
Expand All @@ -94,15 +85,17 @@ const useAlertToast = (params = {}) => {

const toastInDOM = document.querySelector(`#${toastId}`);

toastInDOM.classList.remove('d-none');
const instance = window.bootstrap.Toast.getOrCreateInstance(toastInDOM, {
autohide: true,
animation: true,
delay: timeOut,
});

setTimeout(() => {
toastInDOM.classList.add('show');
}, 10);
instance.show();

toastInDOM.dataset.timeoutId = setTimeout(() => {
hideToast(toastInDOM);
}, timeOut);
one(toastInDOM, 'hidden.bs.toast', () => {
toastInDOM.remove();
});
};

/**
Expand Down
2 changes: 1 addition & 1 deletion _dev/js/theme/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import EventEmitter from 'events';

import './windowExpose';
import './components/dynamic-bootstrap-components';
import './core/index';
import './vendors/index';
import './components/dynamic-bootstrap-components';
import bsCustomFileInput from 'bs-custom-file-input';
import './components/header/index';
import './components/customer/index';
Expand Down
2 changes: 0 additions & 2 deletions _dev/js/utils/dynamicImports/useFunctionCallstackMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ const useFunctionCallstackMap = (key) => {
callbackMap.set(key, new Map());
}

callbackMap.set(key, new Map());

const functionsCallMap = callbackMap.get(key);
const currentCallbacks = functionsCallMap.get(elementKey) || [];
const callbacks = [...currentCallbacks, callback];
Expand Down

0 comments on commit 7c3610f

Please sign in to comment.