winduum 0.1.12 → 0.1.14

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "winduum",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,88 +1,99 @@
1
1
  const defaultOptions = {
2
2
  openClass: 'is-lib-dialog-open',
3
+ scrollbarWidthProperty: '--lib-dialog-scrollbar-width',
3
4
  show: {
4
- content: undefined ?? null,
5
- selector: '.lib-dialog.is-inserted' ?? null,
6
- remove: false ?? null,
7
- append: false ?? null,
8
5
  closable: true ?? null,
9
- class: 'lib-dialog' ?? null
6
+ remove: false ?? null
10
7
  },
11
8
  close: {
12
- selector: '.lib-dialog.is-inserted[open]' ?? null,
13
9
  remove: false ?? null
10
+ },
11
+ insert: {
12
+ selector: '.lib-dialog.is-inserted' ?? null,
13
+ class: 'lib-dialog is-inserted' ?? null,
14
+ remove: true ?? null,
15
+ append: false ?? null
14
16
  }
15
17
  }
16
18
 
17
- const dialogSelector = selector => typeof selector === 'string'
18
- ? document.querySelectorAll(selector)[document.querySelectorAll(selector).length - 1] ?? null
19
- : selector
20
-
21
- const dialogDismiss = async (options) => {
22
- await Promise.allSettled(dialogSelector(options.selector).getAnimations().map(animation => animation.finished))
23
- dialogSelector(options.selector).setAttribute('inert', '')
24
- options.remove && dialogSelector(options.selector).remove()
19
+ const dismissDialog = async (selector, options = defaultOptions.close) => {
20
+ await Promise.allSettled(selector.getAnimations().map(animation => animation.finished))
21
+ selector.setAttribute('inert', '')
22
+ options.remove && selector.remove()
25
23
 
26
24
  if (!document.querySelector('dialog[open]')) {
27
25
  document.documentElement.classList.remove(defaultOptions.openClass)
28
26
  }
29
27
  }
30
28
 
31
- const showDialog = async (options = defaultOptions.show) => {
29
+ const showDialog = async (selector, options = defaultOptions.show) => {
32
30
  options = Object.assign({}, defaultOptions.show, options)
33
31
 
34
- if (options.content && !options.append && dialogSelector(options.selector)) {
35
- dialogSelector(options.selector)?.firstElementChild?.remove()
36
- }
32
+ document.documentElement.style.setProperty(defaultOptions.scrollbarWidthProperty, `${window.innerWidth - document.body.clientWidth}px`)
37
33
 
38
- if (options.content && (options.append || !dialogSelector(options.selector))) {
39
- document.body.insertAdjacentHTML('beforeend', `<dialog class="${defaultOptions.show.class} is-inserted"></dialog>`)
40
- }
41
-
42
- if (!dialogSelector(options.selector)._dialogHasEvents && options.closable) {
43
- dialogSelector(options.selector).addEventListener('keydown', async ({ key }) => {
34
+ if (!selector._dialogHasEvents && options.closable) {
35
+ selector.addEventListener('keydown', async ({ key }) => {
44
36
  if (key === 'Escape') {
45
- setTimeout(() => dialogDismiss(options), 1)
37
+ setTimeout(() => dismissDialog(selector, options), 1)
46
38
  }
47
39
  })
48
40
 
49
- dialogSelector(options.selector).addEventListener('click', ({ target }) => {
41
+ selector.addEventListener('click', ({ target }) => {
50
42
  if (target.nodeName === 'DIALOG') {
51
- closeDialog(options)
43
+ closeDialog(selector, options)
52
44
  }
53
45
  })
54
46
 
55
- dialogSelector(options.selector)._dialogHasEvents = true
47
+ selector._dialogHasEvents = true
56
48
  }
57
49
 
58
- dialogSelector(options.selector).removeAttribute('inert')
59
-
60
- options.content &&
61
- dialogSelector(options.selector).insertAdjacentHTML('beforeend', options.content)
50
+ selector.removeAttribute('inert')
62
51
 
63
52
  document.documentElement.classList.add(defaultOptions.openClass)
64
53
 
65
54
  window.HTMLDialogElement
66
- ? dialogSelector(options.selector).showModal()
67
- : dialogSelector(options.selector).setAttribute('open', '')
68
-
69
- document.documentElement.style.setProperty('--lib-dialog-scrollbar-width', `${window.innerWidth - document.body.clientWidth}px`)
55
+ ? selector.showModal()
56
+ : selector.setAttribute('open', '')
70
57
  }
71
58
 
72
- const closeDialog = async (options = defaultOptions.close) => {
59
+ const closeDialog = async (selector, options = defaultOptions.close) => {
73
60
  options = Object.assign({}, defaultOptions.close, options)
74
61
 
75
62
  window.HTMLDialogElement
76
- ? dialogSelector(options.selector).close()
77
- : dialogSelector(options.selector).removeAttribute('open')
63
+ ? selector.close()
64
+ : selector.removeAttribute('open')
65
+
66
+ await dismissDialog(selector, options)
67
+ }
68
+
69
+ const insertDialog = async (content, options = defaultOptions.insert) => {
70
+ options = Object.assign({}, defaultOptions.insert, options)
71
+
72
+ const dialogSelector = selector => document.querySelectorAll(selector)[document.querySelectorAll(selector).length - 1]
78
73
 
79
- await dialogDismiss(options)
74
+ if (!dialogSelector(options.selector) || options.append) {
75
+ document.body.insertAdjacentHTML('beforeend', `<dialog class="${options.class}">${content}</dialog>`)
76
+ } else {
77
+ dialogSelector(options.selector)?.firstElementChild?.remove()
78
+ dialogSelector(options.selector).insertAdjacentHTML('beforeend', content)
79
+ }
80
+
81
+ await showDialog(dialogSelector(options.selector), options)
80
82
  }
81
83
 
82
- const fetchDialog = async ({ url, showOptions = {} }) => {
84
+ const fetchDialog = async ({ url, options = {} }) => {
83
85
  await fetch(url, { headers: { 'X-Requested-With': 'XMLHttpRequest' } })
84
86
  .then(response => response.json())
85
- .then(async ({ content }) => await showDialog({ content, ...showOptions }))
87
+ .then(async ({ content }) => await insertDialog(content, options))
86
88
  }
87
89
 
88
- export { showDialog, closeDialog, fetchDialog, dialogSelector, dialogDismiss }
90
+ export { showDialog, closeDialog, insertDialog, fetchDialog }
91
+
92
+ export default {
93
+ defaults: defaultOptions,
94
+ dismiss: dismissDialog,
95
+ show: showDialog,
96
+ close: closeDialog,
97
+ insert: insertDialog,
98
+ fetch: fetchDialog
99
+ }
@@ -1,27 +1,25 @@
1
- const showRipple = ({ currentTarget, pageX, pageY }) => {
2
- if (currentTarget.querySelector('.lib-ripple') === null) {
1
+ const showRipple = ({ currentTarget, pageX, pageY }, selector = currentTarget.querySelector('.lib-ripple')) => {
2
+ if (!selector) {
3
3
  currentTarget.insertAdjacentHTML('beforeend', "<div class='lib-ripple'></div>")
4
+ selector = currentTarget.querySelector('.lib-ripple')
4
5
  }
5
6
 
6
- const ripple = currentTarget.querySelector('.lib-ripple')
7
+ selector.classList.remove('animation-ripple')
7
8
 
8
- ripple.classList.remove('animation-ripple')
9
-
10
- if (ripple.clientWidth === 0 && ripple.clientHeight === 0) {
9
+ if (selector.clientWidth === 0 && selector.clientHeight === 0) {
11
10
  const d = Math.max(currentTarget.offsetWidth, currentTarget.offsetHeight)
12
11
 
13
- ripple.style.width = d + 'px'
14
- ripple.style.height = d + 'px'
12
+ selector.style.width = d + 'px'
13
+ selector.style.height = d + 'px'
15
14
  }
16
15
 
17
- let x, y
18
-
19
- x = pageX - currentTarget.offsetLeft - (ripple.clientWidth / 2)
20
- y = pageY - currentTarget.offsetTop - (ripple.clientHeight / 2)
21
-
22
- ripple.style.top = y + 'px'
23
- ripple.style.left = x + 'px'
24
- ripple.classList.add('animation-ripple')
16
+ selector.style.top = pageX - currentTarget.offsetLeft - (selector.clientWidth / 2) + 'px'
17
+ selector.style.left = pageY - currentTarget.offsetTop - (selector.clientHeight / 2) + 'px'
18
+ selector.classList.add('animation-ripple')
25
19
  }
26
20
 
27
21
  export { showRipple }
22
+
23
+ export default {
24
+ show: showRipple
25
+ }