Skip to content

Commit

Permalink
feat: add render then remove snackbar function
Browse files Browse the repository at this point in the history
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
chessurisme committed May 16, 2024
1 parent 03e51a7 commit 6e1e4e9
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/components/snackbar/snackbar-render-then-remove.js
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 }

0 comments on commit 6e1e4e9

Please sign in to comment.