loading.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * https://tobiasahlin.com/spinkit
  3. * https://connoratherton.com/loaders
  4. * https://projects.lukehaas.me/css-loaders
  5. * https://matejkustec.github.io/SpinThatShit
  6. */
  7. export function useLoading() {
  8. const className = `loaders-css__square-spin`
  9. const styleContent = `
  10. @keyframes square-spin {
  11. 25% { transform: perspective(100px) rotateX(180deg) rotateY(0); }
  12. 50% { transform: perspective(100px) rotateX(180deg) rotateY(180deg); }
  13. 75% { transform: perspective(100px) rotateX(0) rotateY(180deg); }
  14. 100% { transform: perspective(100px) rotateX(0) rotateY(0); }
  15. }
  16. .${className} > div {
  17. animation-fill-mode: both;
  18. width: 50px;
  19. height: 50px;
  20. background: #fff;
  21. animation: square-spin 3s 0s cubic-bezier(0.09, 0.57, 0.49, 0.9) infinite;
  22. }
  23. .app-loading-wrap {
  24. position: fixed;
  25. top: 0;
  26. left: 0;
  27. width: 100vw;
  28. height: 100vh;
  29. display: flex;
  30. align-items: center;
  31. justify-content: center;
  32. background: #282c34;
  33. z-index: 9;
  34. }
  35. `
  36. const oStyle = document.createElement('style')
  37. const oDiv = document.createElement('div')
  38. oStyle.id = 'app-loading-style'
  39. oStyle.innerHTML = styleContent
  40. oDiv.className = 'app-loading-wrap'
  41. oDiv.innerHTML = `<div class="${className}"><div></div></div>`
  42. return {
  43. appendLoading() {
  44. safe.append(document.head, oStyle)
  45. safe.append(document.body, oDiv)
  46. },
  47. removeLoading() {
  48. safe.remove(document.head, oStyle)
  49. safe.remove(document.body, oDiv)
  50. },
  51. }
  52. }
  53. const safe = {
  54. append(parent: HTMLElement, child: HTMLElement) {
  55. if (!Array.from(parent.children).find(e => e === child)) {
  56. return parent.appendChild(child)
  57. }
  58. },
  59. remove(parent: HTMLElement, child: HTMLElement) {
  60. if (Array.from(parent.children).find(e => e === child)) {
  61. return parent.removeChild(child)
  62. }
  63. },
  64. }