-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add render then remove snackbar function
This feature introduces a new function for rendering a snackbar message and then automatically removing it after a certain duration. The function combines the rendering and removal processes into a single action, enhancing usability and reducing complexity.
- Loading branch information
1 parent
03e51a7
commit 6e1e4e9
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict' | ||
|
||
import { setAttributes } from '../../utilities/components/set-attributes' | ||
import { isConfigVerified } from '../../utilities/config/config-verifier' | ||
|
||
function renderSnackbar(config) { | ||
if (!isConfigVerified('snackbar', config)) return | ||
|
||
const { message } = config | ||
|
||
return manageSnackbar().displaySnackbar(message) | ||
} | ||
|
||
function manageSnackbar() { | ||
const snackbar_queue = [] | ||
|
||
function displaySnackbar(message) { | ||
const is_snackbar = document.getElementById('snackbar') | ||
|
||
if (is_snackbar) { | ||
snackbar_queue.push(message) | ||
return | ||
} | ||
|
||
const SNACKBAR = createSnackbar(message) | ||
document.body.appendChild(SNACKBAR) | ||
|
||
removeSnackbar(SNACKBAR) | ||
} | ||
|
||
function createSnackbar(message) { | ||
const SNACKBAR = document.createElement('div') | ||
setAttributes(SNACKBAR, { | ||
class: 'snackbar', | ||
id: 'snackbar' | ||
}) | ||
SNACKBAR.textContent = message | ||
|
||
return SNACKBAR | ||
} | ||
|
||
function removeSnackbar(SNACKBAR) { | ||
setTimeout(() => { | ||
SNACKBAR.remove() | ||
|
||
if (snackbar_queue.length > 0) { | ||
const next_message = snackbar_queue.shift() | ||
displaySnackbar(next_message) | ||
} | ||
}, 3000) | ||
} | ||
|
||
return { displaySnackbar } | ||
} | ||
|
||
export { renderSnackbar } |