Sweetalert2: Change swal type dynamically without reloading the modal

Created on 4 May 2018  路  3Comments  路  Source: sweetalert2/sweetalert2

Hi

Is there an easy way to change swal type ?

I use swal with EventSource and I update swal with my server message.

When my server finish the job, I want to change info type to success type without reload swal.

I don't find a way to do this.

feature

Most helpful comment

The ability to dynamically change swal type has been shipped in v8.0.0, please read the release notes for the list of breaking changes in the new major release.

Swal.update({
  type: 'success'
})

All 3 comments

@Hydro8 What is/are the reason/reasons you cannot reload the modal?

If it is just the matter of visual presentation, i.e. you want it to appear to the user as one modal, (馃) I have an idea of how to maybe make the reloading (or "second-swal") approach work...

The only real obstacle would be working with the animation: true configuration (which happens to be default)..

Suppose we added support for passing a function for the animation param, so that you can disable animation for the subsequent modal(s) only for the intro. Thus, to the user, it would appear to be one modal.

const MySwal = Swal.mixin({ /* common params */})
MySwal({
  type: 'info',
  text: 'Doing something on the server...'
})
const eventSource = new EventSource('./foo')
eventSource.onmessage = (event) => {
  MySwal({
    type: event.data.isError ? 'error' : 'success',
    text: event.data.message,
    animation: (swal, isOpening) => !isOpening
  })
}

Not sure how I feel about this solution myself, but how would you feel about it? Would you be satisfied?

Or, upgrade to the latest version (7.25.0) and use getIcons() getter to update the icon:

function updateSwalType(newType) {
  const icons = swal.getIcons()

  // hide all icons
  icons.forEach(icon => icon.style.display = 'none')

  // show the new icon
  icons.find(icon => icon.className.match(newType)).style.display = 'flex'
}

Live demo: https://jsfiddle.net/ad3quksn/1603/

The ability to dynamically change swal type has been shipped in v8.0.0, please read the release notes for the list of breaking changes in the new major release.

Swal.update({
  type: 'success'
})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Spidersouris picture Spidersouris  路  3Comments

Xosmond picture Xosmond  路  3Comments

WillGoldstein picture WillGoldstein  路  3Comments

PrestaShark picture PrestaShark  路  3Comments

mayvn10 picture mayvn10  路  4Comments