sweetalert2 11.2.1 → 11.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,10 @@
1
- import { isVisible } from '../utils/dom/domUtils.js'
2
- import { getInputValue } from '../utils/dom/inputUtils.js'
3
- import { getDenyButton, getValidationMessage } from '../utils/dom/getters.js'
4
- import { asPromise } from '../utils/utils.js'
5
- import { showLoading } from '../staticMethods/showLoading.js'
6
- import { DismissReason } from '../utils/DismissReason.js'
7
- import privateProps from '../privateProps.js'
1
+ import { isVisible } from './utils/dom/domUtils.js'
2
+ import { getInputValue } from './utils/dom/inputUtils.js'
3
+ import { getDenyButton, getValidationMessage } from './utils/dom/getters.js'
4
+ import { asPromise } from './utils/utils.js'
5
+ import { showLoading } from './staticMethods/showLoading.js'
6
+ import { DismissReason } from './utils/DismissReason.js'
7
+ import privateProps from './privateProps.js'
8
8
 
9
9
  export const handleConfirmButtonClick = (instance) => {
10
10
  const innerParams = privateProps.innerParams.get(instance)
@@ -1,7 +1,10 @@
1
1
  import * as dom from '../utils/dom/index.js'
2
2
  import privateProps from '../privateProps.js'
3
3
 
4
- // Get input element by specified type or, if type isn't specified, by params.input
4
+ /**
5
+ * Gets the input DOM node, this method works with input parameter.
6
+ * @returns {HTMLElement | null}
7
+ */
5
8
  export function getInput (instance) {
6
9
  const innerParams = privateProps.innerParams.get(instance || this)
7
10
  const domCache = privateProps.domCache.get(instance || this)
@@ -1,7 +1,7 @@
1
1
  import * as dom from '../../src/utils/dom/index.js'
2
2
  import { warn } from '../../src/utils/utils.js'
3
- import sweetAlert from '../sweetalert2.js'
4
3
  import privateProps from '../privateProps.js'
4
+ import { isUpdatableParameter } from '../../src/utils/params.js'
5
5
 
6
6
  /**
7
7
  * Updates popup parameters.
@@ -18,7 +18,7 @@ export function update (params) {
18
18
 
19
19
  // assign valid params from `params` to `defaults`
20
20
  Object.keys(params).forEach(param => {
21
- if (sweetAlert.isUpdatableParameter(param)) {
21
+ if (isUpdatableParameter(param)) {
22
22
  validUpdatableParams[param] = params[param]
23
23
  } else {
24
24
  warn(`Invalid parameter to update: "${param}". Updatable params are listed here: https://github.com/sweetalert2/sweetalert2/blob/master/src/utils/params.js\n\nIf you think this parameter should be updatable, request it here: https://github.com/sweetalert2/sweetalert2/issues/new?template=02_feature_request.md`)
@@ -4,6 +4,5 @@ export * from './instanceMethods/close.js'
4
4
  export * from './instanceMethods/enable-disable-elements.js'
5
5
  export * from './instanceMethods/validation-message.js'
6
6
  export * from './instanceMethods/progress-steps.js'
7
- export * from './instanceMethods/_main.js'
8
7
  export * from './instanceMethods/update.js'
9
8
  export * from './instanceMethods/_destroy.js'
@@ -1,8 +1,8 @@
1
- import * as dom from '../utils/dom/index.js'
2
- import { DismissReason } from '../utils/DismissReason.js'
3
- import { callIfFunction } from '../utils/utils.js'
4
- import { clickConfirm } from '../staticMethods/dom.js'
5
- import privateProps from '../privateProps.js'
1
+ import * as dom from './utils/dom/index.js'
2
+ import { DismissReason } from './utils/DismissReason.js'
3
+ import { callIfFunction } from './utils/utils.js'
4
+ import { clickConfirm } from './staticMethods/dom.js'
5
+ import privateProps from './privateProps.js'
6
6
 
7
7
  export const addKeydownHandler = (instance, globalState, innerParams, dismissWith) => {
8
8
  if (globalState.keydownTarget && globalState.keydownHandlerAdded) {
@@ -126,7 +126,7 @@ const handleArrows = (key) => {
126
126
  }
127
127
  const sibling = arrowKeysNextButton.includes(key) ? 'nextElementSibling' : 'previousElementSibling'
128
128
  const buttonToFocus = document.activeElement[sibling]
129
- if (buttonToFocus) {
129
+ if (buttonToFocus instanceof HTMLElement) {
130
130
  buttonToFocus.focus()
131
131
  }
132
132
  }
@@ -1,6 +1,6 @@
1
- import { callIfFunction } from '../utils/utils.js'
2
- import { DismissReason } from '../utils/DismissReason.js'
3
- import privateProps from '../privateProps.js'
1
+ import { callIfFunction } from './utils/utils.js'
2
+ import { DismissReason } from './utils/DismissReason.js'
3
+ import privateProps from './privateProps.js'
4
4
 
5
5
  export const handlePopupClick = (instance, domCache, dismissWith) => {
6
6
  const innerParams = privateProps.innerParams.get(instance)
@@ -22,20 +22,24 @@ const handleToastClick = (instance, domCache, dismissWith) => {
22
22
  // Closing toast by internal click
23
23
  domCache.popup.onclick = () => {
24
24
  const innerParams = privateProps.innerParams.get(instance)
25
- if (
26
- innerParams.showConfirmButton ||
27
- innerParams.showDenyButton ||
28
- innerParams.showCancelButton ||
29
- innerParams.showCloseButton ||
30
- innerParams.timer ||
31
- innerParams.input
32
- ) {
25
+ if (innerParams && (isAnyButtonShown(innerParams) || innerParams.timer || innerParams.input)) {
33
26
  return
34
27
  }
35
28
  dismissWith(DismissReason.close)
36
29
  }
37
30
  }
38
31
 
32
+ /**
33
+ * @param {*} innerParams
34
+ * @returns {boolean}
35
+ */
36
+ const isAnyButtonShown = (innerParams) => {
37
+ return innerParams.showConfirmButton ||
38
+ innerParams.showDenyButton ||
39
+ innerParams.showCancelButton ||
40
+ innerParams.showCloseButton
41
+ }
42
+
39
43
  let ignoreOutsideClick = false
40
44
 
41
45
  const handleModalMousedown = (domCache) => {
@@ -9,7 +9,7 @@ import { swalClasses } from '../utils/classes.js'
9
9
  const showLoading = (buttonToReplace) => {
10
10
  let popup = dom.getPopup()
11
11
  if (!popup) {
12
- Swal.fire()
12
+ new Swal() // eslint-disable-line no-new
13
13
  }
14
14
  popup = dom.getPopup()
15
15
  const loader = dom.getLoader()
@@ -1,6 +1,7 @@
1
1
  import SweetAlert from './SweetAlert.js'
2
2
 
3
3
  const Swal = SweetAlert
4
+ // @ts-ignore
4
5
  Swal.default = Swal
5
6
 
6
7
  export default Swal
@@ -20,7 +20,7 @@ export default class Timer {
20
20
  if (this.running) {
21
21
  this.running = false
22
22
  clearTimeout(this.id)
23
- this.remaining -= new Date() - this.started
23
+ this.remaining -= new Date().getTime() - this.started.getTime()
24
24
  }
25
25
  return this.remaining
26
26
  }
@@ -7,7 +7,14 @@ export const states = {
7
7
  previousBodyPadding: null
8
8
  }
9
9
 
10
- export const setInnerHtml = (elem, html) => { // #1926
10
+ /**
11
+ * Securely set innerHTML of an element
12
+ * https://github.com/sweetalert2/sweetalert2/issues/1926
13
+ *
14
+ * @param {HTMLElement} elem
15
+ * @param {string} html
16
+ */
17
+ export const setInnerHtml = (elem, html) => {
11
18
  elem.textContent = ''
12
19
  if (html) {
13
20
  const parser = new DOMParser()
@@ -21,6 +28,11 @@ export const setInnerHtml = (elem, html) => { // #1926
21
28
  }
22
29
  }
23
30
 
31
+ /**
32
+ * @param {HTMLElement} elem
33
+ * @param {string} className
34
+ * @returns {boolean}
35
+ */
24
36
  export const hasClass = (elem, className) => {
25
37
  if (!className) {
26
38
  return false
@@ -58,6 +70,11 @@ export const applyCustomClass = (elem, params, className) => {
58
70
  }
59
71
  }
60
72
 
73
+ /**
74
+ * @param {HTMLElement} popup
75
+ * @param {string} inputType
76
+ * @returns {HTMLInputElement | null}
77
+ */
61
78
  export const getInput = (popup, inputType) => {
62
79
  if (!inputType) {
63
80
  return null
@@ -66,7 +83,7 @@ export const getInput = (popup, inputType) => {
66
83
  case 'select':
67
84
  case 'textarea':
68
85
  case 'file':
69
- return getChildByClass(popup, swalClasses[inputType])
86
+ return popup.querySelector(`.${swalClasses[inputType]}`)
70
87
  case 'checkbox':
71
88
  return popup.querySelector(`.${swalClasses.checkbox} input`)
72
89
  case 'radio':
@@ -75,10 +92,13 @@ export const getInput = (popup, inputType) => {
75
92
  case 'range':
76
93
  return popup.querySelector(`.${swalClasses.range} input`)
77
94
  default:
78
- return getChildByClass(popup, swalClasses.input)
95
+ return popup.querySelector(`.${swalClasses.input}`)
79
96
  }
80
97
  }
81
98
 
99
+ /**
100
+ * @param {HTMLInputElement} input
101
+ */
82
102
  export const focusInput = (input) => {
83
103
  input.focus()
84
104
 
@@ -91,6 +111,11 @@ export const focusInput = (input) => {
91
111
  }
92
112
  }
93
113
 
114
+ /**
115
+ * @param {HTMLElement | HTMLElement[] | null} target
116
+ * @param {string | string[]} classList
117
+ * @param {boolean} condition
118
+ */
94
119
  export const toggleClass = (target, classList, condition) => {
95
120
  if (!target || !classList) {
96
121
  return
@@ -99,7 +124,7 @@ export const toggleClass = (target, classList, condition) => {
99
124
  classList = classList.split(/\s+/).filter(Boolean)
100
125
  }
101
126
  classList.forEach((className) => {
102
- if (target.forEach) {
127
+ if (Array.isArray(target)) {
103
128
  target.forEach((elem) => {
104
129
  condition ? elem.classList.add(className) : elem.classList.remove(className)
105
130
  })
@@ -109,22 +134,41 @@ export const toggleClass = (target, classList, condition) => {
109
134
  })
110
135
  }
111
136
 
137
+ /**
138
+ * @param {HTMLElement | HTMLElement[] | null} target
139
+ * @param {string | string[]} classList
140
+ */
112
141
  export const addClass = (target, classList) => {
113
142
  toggleClass(target, classList, true)
114
143
  }
115
144
 
145
+ /**
146
+ * @param {HTMLElement | HTMLElement[] | null} target
147
+ * @param {string | string[]} classList
148
+ */
116
149
  export const removeClass = (target, classList) => {
117
150
  toggleClass(target, classList, false)
118
151
  }
119
152
 
153
+ /**
154
+ * @param {HTMLElement} elem
155
+ * @param {string} className
156
+ * @returns {HTMLElement | null}
157
+ */
120
158
  export const getChildByClass = (elem, className) => {
121
- for (let i = 0; i < elem.childNodes.length; i++) {
122
- if (hasClass(elem.childNodes[i], className)) {
123
- return elem.childNodes[i]
159
+ const childNodes = toArray(elem.childNodes)
160
+ for (let i = 0; i < childNodes.length; i++) {
161
+ if (hasClass(childNodes[i], className)) {
162
+ return childNodes[i]
124
163
  }
125
164
  }
126
165
  }
127
166
 
167
+ /**
168
+ * @param {HTMLElement} elem
169
+ * @param {string} property
170
+ * @param {*} value
171
+ */
128
172
  export const applyNumericalStyle = (elem, property, value) => {
129
173
  if (value === `${parseInt(value)}`) {
130
174
  value = parseInt(value)
@@ -136,10 +180,17 @@ export const applyNumericalStyle = (elem, property, value) => {
136
180
  }
137
181
  }
138
182
 
183
+ /**
184
+ * @param {HTMLElement} elem
185
+ * @param {string} display
186
+ */
139
187
  export const show = (elem, display = 'flex') => {
140
188
  elem.style.display = display
141
189
  }
142
190
 
191
+ /**
192
+ * @param {HTMLElement} elem
193
+ */
143
194
  export const hide = (elem) => {
144
195
  elem.style.display = 'none'
145
196
  }
@@ -192,7 +243,7 @@ export const stopTimerProgressBar = () => {
192
243
  timerProgressBar.style.removeProperty('transition')
193
244
  timerProgressBar.style.width = '100%'
194
245
  const timerProgressBarFullWidth = parseInt(window.getComputedStyle(timerProgressBar).width)
195
- const timerProgressBarPercent = parseInt(timerProgressBarWidth / timerProgressBarFullWidth * 100)
246
+ const timerProgressBarPercent = timerProgressBarWidth / timerProgressBarFullWidth * 100
196
247
  timerProgressBar.style.removeProperty('transition')
197
248
  timerProgressBar.style.width = `${timerProgressBarPercent}%`
198
249
  }
@@ -1,7 +1,12 @@
1
1
  import { swalClasses } from '../classes.js'
2
2
  import { uniqueArray, toArray } from '../utils.js'
3
- import { isVisible } from './domUtils.js'
3
+ import { isVisible, hasClass } from './domUtils.js'
4
4
 
5
+ /**
6
+ * Gets the popup container which contains the backdrop and the popup itself.
7
+ *
8
+ * @returns {HTMLElement | null}
9
+ */
5
10
  export const getContainer = () => document.body.querySelector(`.${swalClasses.container}`)
6
11
 
7
12
  export const elementBySelector = (selectorString) => {
@@ -87,11 +92,11 @@ export const getFocusableElements = () => {
87
92
  }
88
93
 
89
94
  export const isModal = () => {
90
- return !isToast() && !document.body.classList.contains(swalClasses['no-backdrop'])
95
+ return !hasClass(document.body, swalClasses['toast-shown']) && !hasClass(document.body, swalClasses['no-backdrop'])
91
96
  }
92
97
 
93
98
  export const isToast = () => {
94
- return document.body.classList.contains(swalClasses['toast-shown'])
99
+ return getPopup() && hasClass(getPopup(), swalClasses.toast)
95
100
  }
96
101
 
97
102
  export const isLoading = () => {
@@ -3,7 +3,7 @@ import { getContainer, getPopup } from './getters.js'
3
3
  import { addClass, removeClass, getChildByClass, setInnerHtml } from './domUtils.js'
4
4
  import { isNodeEnv } from '../isNodeEnv.js'
5
5
  import { error } from '../utils.js'
6
- import sweetAlert from '../../sweetalert2.js'
6
+ import globalState from '../../globalState.js'
7
7
 
8
8
  const sweetHTML = `
9
9
  <div aria-labelledby="${swalClasses.title}" aria-describedby="${swalClasses['html-container']}" class="${swalClasses.popup}" tabindex="-1">
@@ -60,9 +60,7 @@ const resetOldContainer = () => {
60
60
  }
61
61
 
62
62
  const resetValidationMessage = () => {
63
- if (sweetAlert.isVisible()) {
64
- sweetAlert.resetValidationMessage()
65
- }
63
+ globalState.currentInstance.resetValidationMessage()
66
64
  }
67
65
 
68
66
  const addInputChangeListeners = () => {
@@ -157,8 +157,9 @@ renderInputType.radio = (radio) => {
157
157
  }
158
158
 
159
159
  renderInputType.checkbox = (checkboxContainer, params) => {
160
+ /** @type {HTMLInputElement} */
160
161
  const checkbox = dom.getInput(dom.getPopup(), 'checkbox')
161
- checkbox.value = 1
162
+ checkbox.value = '1'
162
163
  checkbox.id = swalClasses.checkbox
163
164
  checkbox.checked = Boolean(params.inputValue)
164
165
  const label = checkboxContainer.querySelector('span')
@@ -17,6 +17,11 @@ export const renderPopup = (instance, params) => {
17
17
  // Padding
18
18
  dom.applyNumericalStyle(popup, 'padding', params.padding)
19
19
 
20
+ // Color
21
+ if (params.color) {
22
+ popup.style.color = params.color
23
+ }
24
+
20
25
  // Background
21
26
  if (params.background) {
22
27
  popup.style.background = params.background
@@ -5,6 +5,7 @@ import { swalClasses } from '../utils/classes.js'
5
5
  // Fix iOS scrolling http://stackoverflow.com/q/39626302
6
6
 
7
7
  export const iOSfix = () => {
8
+ // @ts-ignore
8
9
  const iOS = (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
9
10
  if (iOS && !dom.hasClass(document.body, swalClasses.iosfix)) {
10
11
  const offset = document.body.scrollTop
@@ -23,6 +23,7 @@ export const defaultParams = {
23
23
  },
24
24
  customClass: {},
25
25
  target: 'body',
26
+ color: undefined,
26
27
  backdrop: true,
27
28
  heightAuto: true,
28
29
  allowOutsideClick: true,
@@ -99,6 +100,7 @@ export const updatableParams = [
99
100
  'cancelButtonText',
100
101
  'closeButtonAriaLabel',
101
102
  'closeButtonHtml',
103
+ 'color',
102
104
  'confirmButtonAriaLabel',
103
105
  'confirmButtonColor',
104
106
  'confirmButtonText',
@@ -150,7 +152,7 @@ const toastIncompatibleParams = [
150
152
 
151
153
  /**
152
154
  * Is valid parameter
153
- * @param {String} paramName
155
+ * @param {string} paramName
154
156
  */
155
157
  export const isValidParameter = (paramName) => {
156
158
  return Object.prototype.hasOwnProperty.call(defaultParams, paramName)
@@ -158,7 +160,7 @@ export const isValidParameter = (paramName) => {
158
160
 
159
161
  /**
160
162
  * Is valid parameter for Swal.update() method
161
- * @param {String} paramName
163
+ * @param {string} paramName
162
164
  */
163
165
  export const isUpdatableParameter = (paramName) => {
164
166
  return updatableParams.indexOf(paramName) !== -1
@@ -166,7 +168,7 @@ export const isUpdatableParameter = (paramName) => {
166
168
 
167
169
  /**
168
170
  * Is deprecated parameter
169
- * @param {String} paramName
171
+ * @param {string} paramName
170
172
  */
171
173
  export const isDeprecatedParameter = (paramName) => {
172
174
  return deprecatedParams[paramName]
@@ -29,7 +29,6 @@ function validateCustomTargetElement (params) {
29
29
  * Set type, text and actions on popup
30
30
  *
31
31
  * @param params
32
- * @returns {boolean}
33
32
  */
34
33
  export default function setParameters (params) {
35
34
  setDefaultInputValidators(params)
@@ -51,7 +51,7 @@ $swal2-title-position: relative !default;
51
51
  $swal2-title-max-width: 100% !default;
52
52
  $swal2-title-margin: 0 !default;
53
53
  $swal2-title-padding: .8em 1em 0 !default;
54
- $swal2-title-color: lighten($swal2-black, 35) !default;
54
+ $swal2-title-color: inherit !default;
55
55
  $swal2-title-font-size: 1.875em !default;
56
56
  $swal2-title-font-weight: 600 !default;
57
57
  $swal2-title-text-align: center !default;
@@ -61,7 +61,7 @@ $swal2-html-container-justify-content: center !default;
61
61
  $swal2-html-container-margin: 1em 1.6em .3em !default;
62
62
  $swal2-html-container-padding: 0 !default;
63
63
  $swal2-html-container-overflow: auto !default;
64
- $swal2-html-container-color: lighten($swal2-black, 33) !default;
64
+ $swal2-html-container-color: inherit !default;
65
65
  $swal2-html-container-font-size: 1.125em !default;
66
66
  $swal2-html-container-font-weight: normal !default;
67
67
  $swal2-html-container-line-height: normal !default;
@@ -129,7 +129,7 @@ $swal2-active-step-color: $swal2-white !default;
129
129
  $swal2-footer-margin: 1em 0 0 !default;
130
130
  $swal2-footer-padding: 1em 1em 0 !default;
131
131
  $swal2-footer-border-color: #eee !default;
132
- $swal2-footer-color: lighten($swal2-black, 33) !default;
132
+ $swal2-footer-color: inherit !default;
133
133
  $swal2-footer-font-size: 1em !default;
134
134
 
135
135
  // TIMER POGRESS BAR
package/sweetalert2.d.ts CHANGED
@@ -580,7 +580,14 @@ declare module 'sweetalert2' {
580
580
  padding?: number | string;
581
581
 
582
582
  /**
583
- * Popup background (CSS `background` property).
583
+ * Color for title, content and footer (CSS `color` property). The default color is `#545454`.
584
+ *
585
+ * @default undefined
586
+ */
587
+ color?: string;
588
+
589
+ /**
590
+ * Popup background (CSS `background` property). The default background is `#fff`.
584
591
  *
585
592
  * @default undefined
586
593
  */