nanosplash 3.0.8 → 3.1.0
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/.prettierrc +1 -1
- package/README.md +52 -4
- package/bun.lockb +0 -0
- package/dist/cjs/ns.cjs +1 -2
- package/dist/es/ns.js +42 -340
- package/dist/iife/ns.iife.js +1 -2
- package/docs/.vitepress/.temp/@localSearchIndexroot.Ba2B1gNE.js +4 -0
- package/docs/.vitepress/.temp/Card.Ce4dbGEn.js +30 -0
- package/docs/.vitepress/.temp/VPLocalSearchBox.DV6bivXX.js +382 -0
- package/docs/.vitepress/.temp/api_doc_hide.md.js +25 -0
- package/docs/.vitepress/.temp/api_doc_show.md.js +25 -0
- package/docs/.vitepress/.temp/api_start_customize.md.js +42 -0
- package/docs/.vitepress/.temp/api_start_features.md.js +19 -0
- package/docs/.vitepress/.temp/api_start_install.md.js +35 -0
- package/docs/.vitepress/.temp/api_start_playground.md.js +213 -0
- package/docs/.vitepress/.temp/api_start_usage.md.js +36 -0
- package/docs/.vitepress/.temp/app.js +5090 -0
- package/docs/.vitepress/.temp/assets/style.lcyfX_uc.css +5374 -0
- package/docs/.vitepress/.temp/index.md.js +19 -0
- package/docs/.vitepress/.temp/macos.jpg +0 -0
- package/docs/.vitepress/.temp/package.json +1 -0
- package/docs/.vitepress/.temp/plugin-vue_export-helper.1tPrXgE0.js +10 -0
- package/docs/.vitepress/{config.ts → config.mts} +46 -44
- package/docs/.vitepress/theme/css/style.css +107 -6
- package/docs/.vitepress/theme/vue/CardGrid.vue +1 -1
- package/docs/.vitepress/theme/vue/Exe.vue +58 -28
- package/docs/.vitepress/theme/vue/PlayGround.vue +5 -21
- package/docs/api/doc/hide.md +12 -33
- package/docs/api/doc/show.md +12 -13
- package/docs/api/start/customize.md +30 -0
- package/docs/api/start/features.md +3 -3
- package/docs/api/start/install.md +12 -8
- package/docs/api/start/playground.md +1 -1
- package/docs/api/start/usage.md +4 -7
- package/docs/index.md +25 -25
- package/docs/public/macos.jpg +0 -0
- package/eslint.config.js +9 -0
- package/package.json +54 -48
- package/src/style/ns.sass +98 -73
- package/src/ts/Nanosplash.ts +209 -0
- package/src/ts/main.ts +2 -8
- package/src/ts/types/NanosplashInterface.ts +26 -0
- package/src/ts/types/Types.ts +29 -3
- package/src/ts/types/global.d.ts +9 -9
- package/tests/Nanosplash.spec.ts +33 -0
- package/.prettierignore +0 -1
- package/src/ts/core/DomUtilities.ts +0 -161
- package/src/ts/core/Service.ts +0 -211
- package/src/ts/core/ServiceInterface.ts +0 -58
- package/src/ts/core/Splash.ts +0 -121
- package/src/ts/core/SplashInterface.ts +0 -46
- package/src/ts/core/SplashQueue.ts +0 -49
- package/tests/DomUtilities.spec.ts +0 -117
- package/tests/Guid.spec.ts +0 -21
- package/tests/Service.spec.ts +0 -198
- package/tests/Splash.spec.ts +0 -64
- package/tests/TestUtilities.ts +0 -48
package/docs/api/start/usage.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Usage
|
|
2
2
|
|
|
3
|
-
```js{2,5,8,11,
|
|
3
|
+
```js{2,5,8,11,15,18}
|
|
4
4
|
// Fullscreen spinning wheel
|
|
5
5
|
ns.show()
|
|
6
6
|
|
|
@@ -8,18 +8,15 @@ ns.show()
|
|
|
8
8
|
ns.show('Loading')
|
|
9
9
|
|
|
10
10
|
// Display inside an element
|
|
11
|
-
ns.
|
|
11
|
+
ns.show('Fetching data', '#my-table')
|
|
12
12
|
|
|
13
13
|
// Hide the first created loading screen (FIFO)
|
|
14
14
|
ns.hide()
|
|
15
15
|
|
|
16
|
-
// Hide loading screen inside an element
|
|
17
|
-
ns.hideInside('#my-table')
|
|
18
|
-
|
|
19
16
|
// Hide specific loading screen
|
|
20
17
|
const id = ns.show('Loading')
|
|
21
|
-
ns.
|
|
18
|
+
ns.hide(id)
|
|
22
19
|
|
|
23
20
|
// Hide all loading screens
|
|
24
|
-
ns.
|
|
21
|
+
ns.hide('*')
|
|
25
22
|
```
|
package/docs/index.md
CHANGED
|
@@ -3,31 +3,31 @@
|
|
|
3
3
|
layout: home
|
|
4
4
|
|
|
5
5
|
hero:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
name: 'Nanosplash'
|
|
7
|
+
text: ''
|
|
8
|
+
tagline: The tiny loading screen for web artisans
|
|
9
|
+
actions:
|
|
10
|
+
- theme: brand
|
|
11
|
+
text: Get started
|
|
12
|
+
link: /api/start/install.md
|
|
13
|
+
- theme: alt
|
|
14
|
+
text: Playground
|
|
15
|
+
link: /api/start/playground.md
|
|
16
|
+
- theme: alt
|
|
17
|
+
text: API Documentation
|
|
18
|
+
link: /api/doc/show.md
|
|
19
19
|
|
|
20
20
|
features:
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
21
|
+
- title: ✨ Tiny
|
|
22
|
+
details: Less than 2KB minified and gzipped.
|
|
23
|
+
link: /api/start/features#small-bundle-size
|
|
24
|
+
- title: ✌️ 2 Function API
|
|
25
|
+
details: Start using Nanosplash in seconds.
|
|
26
|
+
link: /api/start/features#simple-api
|
|
27
|
+
- title: 🪴 Spawning
|
|
28
|
+
details: Spawn a splash element anywhere in your DOM.
|
|
29
|
+
link: /api/start/features#spawning
|
|
30
|
+
- title: 🚀 Node recycling
|
|
31
|
+
details: Nanosplash will recycle existing elements.
|
|
32
|
+
link: /api/start/features#element-recycling
|
|
33
33
|
---
|
|
Binary file
|
package/eslint.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,41 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nanosplash",
|
|
3
|
-
"
|
|
4
|
-
"private": false,
|
|
5
|
-
"version": "3.0.8",
|
|
6
|
-
"type": "module",
|
|
3
|
+
"version": "3.1.0",
|
|
7
4
|
"author": {
|
|
8
5
|
"name": "Isak K. Hauge",
|
|
9
6
|
"email": "isakhauge@icloud.com",
|
|
10
7
|
"url": "https://no.linkedin.com/in/isakhauge"
|
|
11
8
|
},
|
|
12
|
-
"keywords": [
|
|
13
|
-
"loading",
|
|
14
|
-
"loader",
|
|
15
|
-
"screen",
|
|
16
|
-
"splash",
|
|
17
|
-
"nano",
|
|
18
|
-
"nanosplash",
|
|
19
|
-
"small",
|
|
20
|
-
"tiny",
|
|
21
|
-
"es",
|
|
22
|
-
"esm",
|
|
23
|
-
"ts",
|
|
24
|
-
"typescript",
|
|
25
|
-
"iife",
|
|
26
|
-
"cdn",
|
|
27
|
-
"vite"
|
|
28
|
-
],
|
|
29
|
-
"homepage": "https://isakhauge.github.io/nanosplash",
|
|
30
9
|
"repository": {
|
|
31
10
|
"type": "git",
|
|
32
|
-
"url": "https://github.com/isakhauge/nanosplash.git"
|
|
11
|
+
"url": "git+https://github.com/isakhauge/nanosplash.git"
|
|
33
12
|
},
|
|
34
|
-
"license": "MIT",
|
|
35
13
|
"main": "./dist/es/ns.js",
|
|
36
14
|
"module": "./dist/es/ns.js",
|
|
37
|
-
"
|
|
38
|
-
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/jsdom": "^21.1.6",
|
|
17
|
+
"@types/node": "^20.11.28",
|
|
18
|
+
"@types/rollup": "^0.54.0",
|
|
19
|
+
"@vitest/coverage-v8": "^1.4.0",
|
|
20
|
+
"@vitest/ui": "^1.4.0",
|
|
21
|
+
"cross-env": "^7.0.3",
|
|
22
|
+
"eslint": "^8.57.0",
|
|
23
|
+
"eslint-config-prettier": "^9.1.0",
|
|
24
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
25
|
+
"jsdom": "^24.0.0",
|
|
26
|
+
"sass": "^1.72.0",
|
|
27
|
+
"stylelint-prettier": "^5.0.0",
|
|
28
|
+
"terser": "^5.29.2",
|
|
29
|
+
"typescript": "5.4.2",
|
|
30
|
+
"typescript-eslint": "^7.2.0",
|
|
31
|
+
"vite": "^5.1.6",
|
|
32
|
+
"vitepress": "1.0.0-rc.45",
|
|
33
|
+
"vitest": "^1.4.0"
|
|
34
|
+
},
|
|
39
35
|
"exports": {
|
|
40
36
|
".": {
|
|
41
37
|
"types": {
|
|
@@ -52,35 +48,45 @@
|
|
|
52
48
|
}
|
|
53
49
|
}
|
|
54
50
|
},
|
|
55
|
-
"
|
|
51
|
+
"description": "The tiny loading screen for web artisans",
|
|
52
|
+
"homepage": "https://isakhauge.github.io/nanosplash",
|
|
53
|
+
"keywords": [
|
|
54
|
+
"loading",
|
|
55
|
+
"loader",
|
|
56
|
+
"screen",
|
|
57
|
+
"splash",
|
|
58
|
+
"nano",
|
|
59
|
+
"nanosplash",
|
|
60
|
+
"small",
|
|
61
|
+
"tiny",
|
|
62
|
+
"es",
|
|
63
|
+
"esm",
|
|
64
|
+
"ts",
|
|
65
|
+
"typescript",
|
|
66
|
+
"iife",
|
|
67
|
+
"cdn",
|
|
68
|
+
"vite"
|
|
69
|
+
],
|
|
70
|
+
"license": "MIT",
|
|
71
|
+
"private": false,
|
|
56
72
|
"scripts": {
|
|
57
73
|
"build": "bun build:all && bun build:docs",
|
|
58
|
-
"build:es": "tsc && cross-env entry=./src/ts/
|
|
59
|
-
"build:cjs": "tsc && cross-env entry=./src/ts/
|
|
60
|
-
"build:iife": "tsc && cross-env entry=./src/ts/main.ts format=iife bun vite build -m production --c vite.config.ts",
|
|
74
|
+
"build:es": "bun x -b tsc && cross-env entry=./src/ts/Nanosplash.ts format=es bun run -b vite build -m production --c vite.config.ts",
|
|
75
|
+
"build:cjs": "bun x -b tsc && cross-env entry=./src/ts/Nanosplash.ts format=cjs bun run -b vite build -m production --c vite.config.ts",
|
|
76
|
+
"build:iife": "bun x -b tsc && cross-env entry=./src/ts/main.ts format=iife bun run -b vite build -m production --c vite.config.ts",
|
|
61
77
|
"build:all": "bun build:es && bun build:cjs && bun build:iife",
|
|
62
78
|
"build:docs": "bun docs:build",
|
|
63
79
|
"preview": "vite preview",
|
|
64
80
|
"test": "vitest",
|
|
65
81
|
"test:ui": "vitest --ui",
|
|
82
|
+
"lint": "bunx eslint src/ts/**/*.ts --fix",
|
|
66
83
|
"coverage": "vitest --coverage",
|
|
67
|
-
"docs:dev": "vitepress dev docs",
|
|
68
|
-
"docs:build": "vitepress build docs",
|
|
69
|
-
"docs:preview": "vitepress preview docs"
|
|
84
|
+
"docs:dev": "bun x vitepress dev docs",
|
|
85
|
+
"docs:build": "bun x vitepress build docs",
|
|
86
|
+
"docs:preview": "bun x vitepress preview docs"
|
|
70
87
|
},
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"@vitest/coverage-v8": "^0.34.6",
|
|
76
|
-
"@vitest/ui": "^0.34.6",
|
|
77
|
-
"cross-env": "^7.0.3",
|
|
78
|
-
"jsdom": "^22.1.0",
|
|
79
|
-
"sass": "^1.69.4",
|
|
80
|
-
"terser": "^5.22.0",
|
|
81
|
-
"typescript": "^5.2.2",
|
|
82
|
-
"vite": "^4.5.0",
|
|
83
|
-
"vitepress": "1.0.0-rc.22",
|
|
84
|
-
"vitest": "^0.34.6"
|
|
85
|
-
}
|
|
88
|
+
"source": "./src/main.ts",
|
|
89
|
+
"type": "module",
|
|
90
|
+
"types": "./src/ts/types/global.d.ts",
|
|
91
|
+
"unpkg": "./dist/iife/ns.iife.js"
|
|
86
92
|
}
|
package/src/style/ns.sass
CHANGED
|
@@ -1,95 +1,120 @@
|
|
|
1
|
+
// Spinner animations:
|
|
2
|
+
@keyframes nsRotate
|
|
3
|
+
100%
|
|
4
|
+
transform: rotate(360deg)
|
|
5
|
+
|
|
6
|
+
@keyframes nsDash
|
|
7
|
+
0%
|
|
8
|
+
stroke-dasharray: 1, 150
|
|
9
|
+
stroke-dashoffset: 0
|
|
10
|
+
50%
|
|
11
|
+
stroke-dasharray: 90, 150
|
|
12
|
+
stroke-dashoffset: -35
|
|
13
|
+
100%
|
|
14
|
+
stroke-dasharray: 90, 150
|
|
15
|
+
stroke-dashoffset: -124
|
|
16
|
+
|
|
17
|
+
@keyframes nsFade
|
|
18
|
+
from
|
|
19
|
+
opacity: 0
|
|
20
|
+
to
|
|
21
|
+
opacity: 1
|
|
22
|
+
|
|
23
|
+
@keyframes nsAscend
|
|
24
|
+
from
|
|
25
|
+
opacity: 0
|
|
26
|
+
transform: translateY(13px)
|
|
27
|
+
to
|
|
28
|
+
opacity: 1
|
|
29
|
+
transform: translateY(0)
|
|
30
|
+
|
|
31
|
+
:root
|
|
32
|
+
--ns-top: 0px
|
|
33
|
+
|
|
1
34
|
%maximize
|
|
2
|
-
|
|
3
|
-
|
|
35
|
+
width: 100%
|
|
36
|
+
height: 100%
|
|
4
37
|
|
|
5
|
-
%
|
|
6
|
-
|
|
7
|
-
|
|
38
|
+
%bottom-left
|
|
39
|
+
bottom: 0
|
|
40
|
+
right: 0
|
|
8
41
|
|
|
9
42
|
%flex-center-xy
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
43
|
+
display: flex
|
|
44
|
+
justify-content: center
|
|
45
|
+
align-items: center
|
|
13
46
|
|
|
14
47
|
// Nanosplash host scoped varibles
|
|
15
48
|
.nsh
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
49
|
+
--color: DarkSlateGray
|
|
50
|
+
--size: 20px
|
|
51
|
+
--relSize: calc(var(--size) * 0.9)
|
|
52
|
+
--font: 'Helvetica', 'Arial', sans-serif
|
|
53
|
+
--weight: 400
|
|
54
|
+
--bg: #{ rgba(White, 0.9) }
|
|
55
|
+
--zIdx: 999999999
|
|
56
|
+
--blur: blur(5px)
|
|
24
57
|
|
|
25
58
|
// Nanosplash host
|
|
26
59
|
.nsh
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
60
|
+
position: relative
|
|
61
|
+
z-index: var(--zIdx)
|
|
62
|
+
&::before // The backdrop
|
|
63
|
+
@extend %maximize
|
|
64
|
+
@extend %bottom-left
|
|
65
|
+
position: absolute
|
|
66
|
+
background-color: var(--bg)
|
|
67
|
+
content: ''
|
|
68
|
+
backdrop-filter: var(--blur)
|
|
69
|
+
-webkit-backdrop-filter: var(--blur)
|
|
70
|
+
z-index: calc(var(--zIdx) + 1)
|
|
71
|
+
border-radius: inherit
|
|
39
72
|
|
|
40
73
|
// When Nanosplash host is the document body
|
|
41
74
|
body.nsh
|
|
42
|
-
|
|
75
|
+
&, &::before
|
|
76
|
+
@extend %maximize
|
|
77
|
+
position: fixed
|
|
78
|
+
top: var(--ns-top)
|
|
79
|
+
left: 0
|
|
43
80
|
|
|
44
81
|
// Nanosplash (main element)
|
|
45
82
|
.ns
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
.nsc
|
|
54
|
-
@extend %flex-center-xy
|
|
55
|
-
filter: drop-shadow(0 0 0.1rem #{ rgba(LightGray, 0.5) })
|
|
83
|
+
@extend %maximize
|
|
84
|
+
@extend %bottom-left
|
|
85
|
+
@extend %flex-center-xy
|
|
86
|
+
position: absolute
|
|
87
|
+
z-index: calc(var(--zIdx) + 2)
|
|
88
|
+
animation: nsFade 2s
|
|
89
|
+
gap: var(--relSize)
|
|
56
90
|
|
|
57
91
|
// Nanosplash text
|
|
58
92
|
.nst
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
93
|
+
color: var(--color)
|
|
94
|
+
font-size: var(--size)
|
|
95
|
+
font-family: var(--font)
|
|
96
|
+
font-weight: var(--weight)
|
|
97
|
+
max-width: 80dvw
|
|
98
|
+
white-space: nowrap
|
|
99
|
+
text-overflow: ellipsis
|
|
100
|
+
overflow: hidden
|
|
101
|
+
text-shadow: 0 0 0.06rem #{ rgba(DarkSlateGray, .25) }
|
|
102
|
+
filter: drop-shadow(0 0 0.07rem #{ rgba(Black, 0.25) })
|
|
103
|
+
animation: nsAscend 0.5s
|
|
65
104
|
|
|
66
105
|
// Nanosplash spinner
|
|
67
106
|
.nss
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
// Spinner animations:
|
|
83
|
-
@keyframes Rotate
|
|
84
|
-
100%
|
|
85
|
-
transform: rotate(360deg)
|
|
86
|
-
@keyframes Dash
|
|
87
|
-
0%
|
|
88
|
-
stroke-dasharray: 1, 150
|
|
89
|
-
stroke-dashoffset: 0
|
|
90
|
-
50%
|
|
91
|
-
stroke-dasharray: 90, 150
|
|
92
|
-
stroke-dashoffset: -35
|
|
93
|
-
100%
|
|
94
|
-
stroke-dasharray: 90, 150
|
|
95
|
-
stroke-dashoffset: -124
|
|
107
|
+
display: block
|
|
108
|
+
width: var(--relSize)
|
|
109
|
+
height: var(--relSize)
|
|
110
|
+
animation: nsAscend 0.5s
|
|
111
|
+
& > svg
|
|
112
|
+
animation: nsRotate 2s linear infinite
|
|
113
|
+
position: relative
|
|
114
|
+
width: inherit
|
|
115
|
+
height: inherit
|
|
116
|
+
stroke-width: 8
|
|
117
|
+
& .path
|
|
118
|
+
stroke: var(--color)
|
|
119
|
+
stroke-linecap: round
|
|
120
|
+
animation: nsDash 1.5s ease-in-out infinite
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// @ts-strict
|
|
2
|
+
import style from '../style/ns.sass?inline'
|
|
3
|
+
import { NanosplashInterface } from './types/NanosplashInterface'
|
|
4
|
+
import {
|
|
5
|
+
DOMSelector,
|
|
6
|
+
ElementReference,
|
|
7
|
+
NSElement,
|
|
8
|
+
ScopedSelectors,
|
|
9
|
+
} from './types/Types'
|
|
10
|
+
import { version } from '../../package.json'
|
|
11
|
+
|
|
12
|
+
const StyleID: string = 'nscss'
|
|
13
|
+
const b = () => document.body
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* # Scope
|
|
17
|
+
* Creates scoped selectors.
|
|
18
|
+
* @param node Node to query.
|
|
19
|
+
* @returns An object containing two, slightly different functions that
|
|
20
|
+
* retrieves elements from the DOM.
|
|
21
|
+
*/
|
|
22
|
+
const scope = (node: ParentNode): ScopedSelectors =>
|
|
23
|
+
(all => ({
|
|
24
|
+
all,
|
|
25
|
+
first: (ref: string) => all(ref)[0] ?? null,
|
|
26
|
+
}))((ref: string) => Array.from(node.querySelectorAll(ref)))
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* # Get all
|
|
30
|
+
* @param selector CSS selector used to match nodes in the DOM.
|
|
31
|
+
* @returns An array of Element nodes.
|
|
32
|
+
*/
|
|
33
|
+
const getAll = (selector: DOMSelector): Element[] =>
|
|
34
|
+
scope(document).all(selector)
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* # Get
|
|
38
|
+
* @param selector CSS selector used to match nodes in the DOM.
|
|
39
|
+
* @returns Firs Element node from the DOM matching the selector.
|
|
40
|
+
*/
|
|
41
|
+
const get = (selector: DOMSelector): Element | null =>
|
|
42
|
+
scope(document).first(selector)
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* # Get All NS
|
|
46
|
+
* @returns An array of all Nanosplash elements in the DOM.
|
|
47
|
+
*/
|
|
48
|
+
const getAllNS = (): NSElement[] => getAll('.ns') as NSElement[]
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* # Parse Ref
|
|
52
|
+
* @param ref Element reference which could be a selector or an Element.
|
|
53
|
+
* @returns The Element node or null.
|
|
54
|
+
*/
|
|
55
|
+
const parseRef = (ref: ElementReference): Element | null =>
|
|
56
|
+
ref instanceof Element ? ref : get(ref)
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* # Div
|
|
60
|
+
* @param className Optional string containing class names.
|
|
61
|
+
* @param children Optional list of Nodes or strings.
|
|
62
|
+
* @returns The newly created HTMLDivElement.
|
|
63
|
+
*/
|
|
64
|
+
const div = (
|
|
65
|
+
className?: string,
|
|
66
|
+
...children: (Node | string)[]
|
|
67
|
+
): HTMLDivElement => {
|
|
68
|
+
const el: HTMLDivElement = document.createElement('div')
|
|
69
|
+
if (className) el.classList.add(className)
|
|
70
|
+
el.append(...children)
|
|
71
|
+
return el
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* # Parse
|
|
76
|
+
* @param html HTML string.
|
|
77
|
+
* @returns Parsed node.
|
|
78
|
+
*/
|
|
79
|
+
const parse = (html: string): Node => {
|
|
80
|
+
const el: HTMLDivElement = div()
|
|
81
|
+
el.innerHTML = html
|
|
82
|
+
return el.firstChild as Node
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* # Create NS
|
|
87
|
+
* @returns Nanpslash DOM element.
|
|
88
|
+
*/
|
|
89
|
+
const createNS = (): NSElement => {
|
|
90
|
+
const circle: string = '<circle class=path cx=25 cy=25 r=20 fill=none />'
|
|
91
|
+
const svg = parse(`<svg viewBox="0 0 50 50">${circle}</svg>`) as Element
|
|
92
|
+
const el = div('ns', div('nst'), div('nss', svg)) as NSElement
|
|
93
|
+
el.nsId = Date.now()
|
|
94
|
+
return el
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* # Peek
|
|
99
|
+
* Collect all Nanosplash elements in the DOM, sort them by their NS ID
|
|
100
|
+
* in ascending order, and return the first element.
|
|
101
|
+
* @returns The front of the queue.
|
|
102
|
+
*/
|
|
103
|
+
const peek = (): NSElement | null =>
|
|
104
|
+
getAllNS().sort((a: NSElement, b: NSElement) => a.nsId - b.nsId)[0] ?? null
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* # Set NS Text
|
|
108
|
+
* Replace the text node to trigger the animation which in turn is invoked
|
|
109
|
+
* by the connected callback in the DOM.
|
|
110
|
+
* @param ns Nanosplash element.
|
|
111
|
+
* @param text Text to display adjacent to spinner.
|
|
112
|
+
*/
|
|
113
|
+
const setNSText = (ns: NSElement, text: string): void => {
|
|
114
|
+
const nsScope = scope(ns)
|
|
115
|
+
const oldNst = nsScope.first('.nst') as Element | null
|
|
116
|
+
if (!text) return oldNst?.remove()
|
|
117
|
+
const newNst = div('nst', text)
|
|
118
|
+
if (oldNst) {
|
|
119
|
+
oldNst.replaceWith(newNst)
|
|
120
|
+
} else {
|
|
121
|
+
ns.insertBefore(newNst, ns.firstChild)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* # Set NS Parent
|
|
127
|
+
* Define where inside the DOM to spawn the Nansoplash.
|
|
128
|
+
* @param ns Nanosplash element.
|
|
129
|
+
* @param parent NS element's parent to be.
|
|
130
|
+
*/
|
|
131
|
+
const setNSParent = (ns: NSElement, parent: Element): void => {
|
|
132
|
+
const child = parent.firstElementChild
|
|
133
|
+
if (child) {
|
|
134
|
+
parent.insertBefore(ns, child)
|
|
135
|
+
}
|
|
136
|
+
parent.append(ns)
|
|
137
|
+
parent.classList.add('nsh')
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* # Show
|
|
142
|
+
* @param text Text to display adjacent to spinner.
|
|
143
|
+
* @param inside Selector or element in which the NS element will be spawned.
|
|
144
|
+
* @returns The ID if the NS element — which is a UTC integer.
|
|
145
|
+
*/
|
|
146
|
+
const show = (text?: string, inside?: string | Element): number | null => {
|
|
147
|
+
const parent: Element | null = (
|
|
148
|
+
inside ? parseRef(inside) : b
|
|
149
|
+
) as Element | null
|
|
150
|
+
let ns: NSElement
|
|
151
|
+
const recycledNs = scope(parent ?? b()).first('& > .ns')
|
|
152
|
+
if (recycledNs) {
|
|
153
|
+
ns = recycledNs as NSElement
|
|
154
|
+
} else {
|
|
155
|
+
ns = createNS()
|
|
156
|
+
setNSParent(ns, parent as Element)
|
|
157
|
+
}
|
|
158
|
+
setNSText(ns, text ?? '')
|
|
159
|
+
const top: string = window.scrollY + 'px'
|
|
160
|
+
b().style.setProperty('--ns-top', top)
|
|
161
|
+
return ns.nsId
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* # Delete NS
|
|
166
|
+
* @param ns Nanosplash elememnt.
|
|
167
|
+
*/
|
|
168
|
+
const deleteNS = (ns?: Element | null): void => {
|
|
169
|
+
ns?.parentElement?.classList.remove('nsh')
|
|
170
|
+
ns?.remove()
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* # Find NS
|
|
175
|
+
* @param id The UTC integer returned from the `show` function.
|
|
176
|
+
* @returns Null or the corresponding Nanosplash element.
|
|
177
|
+
*/
|
|
178
|
+
const findNS = (id: number) => getAllNS().find(v => v.nsId === id) ?? null
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* # Hide
|
|
182
|
+
* Hide agnostically in FIFO order, specifically by ID, or all by the asterisk
|
|
183
|
+
* which will delete all Nanosplashes.
|
|
184
|
+
* @param id Optional ID of a Nanosplash element or '*' .
|
|
185
|
+
*/
|
|
186
|
+
const hide = (id?: number | '*'): void => {
|
|
187
|
+
if (id === '*') getAll('.ns').forEach(deleteNS)
|
|
188
|
+
else deleteNS(typeof id === 'number' ? findNS(id) : peek())
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* # Add style
|
|
193
|
+
*/
|
|
194
|
+
const addStyle = (): void => {
|
|
195
|
+
get('#nscss')?.remove()
|
|
196
|
+
b().append(parse(`<style id=${StyleID}>${style}</style>`))
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* # Use NS
|
|
201
|
+
* @returns Nanosplash API.
|
|
202
|
+
*/
|
|
203
|
+
export const useNs = (): NanosplashInterface =>
|
|
204
|
+
!!new Promise(addStyle) &&
|
|
205
|
+
(window.ns = {
|
|
206
|
+
show,
|
|
207
|
+
hide,
|
|
208
|
+
version: version,
|
|
209
|
+
})
|
package/src/ts/main.ts
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { DOMSelector, UTCInteger } from './Types'
|
|
2
|
+
|
|
3
|
+
export interface NanosplashInterface {
|
|
4
|
+
/**
|
|
5
|
+
* # Show
|
|
6
|
+
* Display Nanosplash inside the window.
|
|
7
|
+
* @param {string | undefined} text Optional text
|
|
8
|
+
* @param {string | Element | undefined} inside Optional element reference
|
|
9
|
+
* @returns {number | null} The internal ID of the created Nanosplash.
|
|
10
|
+
*/
|
|
11
|
+
show(text?: string, inside?: DOMSelector | HTMLElement): UTCInteger | null
|
|
12
|
+
/**
|
|
13
|
+
* # Hide
|
|
14
|
+
* Remove Nanosplash in multiple ways depending on the passed argument.
|
|
15
|
+
* - Pass ID: Remove specific.
|
|
16
|
+
* - Pass '*': Remove all in window.
|
|
17
|
+
* - Pass nothing: Remove in FIFO order.
|
|
18
|
+
* @param id The internal ID, an asterisk, or nothing.
|
|
19
|
+
*/
|
|
20
|
+
hide(id?: UTCInteger | '*'): void
|
|
21
|
+
/**
|
|
22
|
+
* # Version
|
|
23
|
+
* The current version of Nanosplash.
|
|
24
|
+
*/
|
|
25
|
+
version: string
|
|
26
|
+
}
|