swup 2.0.19 → 3.0.0-rc.1

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.
Files changed (152) hide show
  1. package/dist/helpers.cjs +2 -0
  2. package/dist/helpers.cjs.map +1 -0
  3. package/dist/helpers.modern.js +2 -0
  4. package/dist/helpers.modern.js.map +1 -0
  5. package/dist/helpers.module.js +2 -0
  6. package/dist/helpers.module.js.map +1 -0
  7. package/dist/index.cjs +2 -0
  8. package/dist/index.cjs.map +1 -0
  9. package/dist/index.modern.js +2 -0
  10. package/dist/index.modern.js.map +1 -0
  11. package/dist/index.module.js +2 -0
  12. package/dist/index.module.js.map +1 -0
  13. package/dist/index.umd.js +3 -0
  14. package/dist/index.umd.js.map +1 -0
  15. package/dist/src/config/version.d.ts +5 -0
  16. package/dist/src/helpers/Location.d.ts +31 -0
  17. package/dist/src/helpers/classify.d.ts +2 -0
  18. package/dist/src/helpers/cleanupAnimationClasses.d.ts +2 -0
  19. package/dist/src/helpers/createHistoryRecord.d.ts +2 -0
  20. package/dist/src/helpers/delegateEvent.d.ts +8 -0
  21. package/dist/src/helpers/fetch.d.ts +6 -0
  22. package/dist/src/helpers/getCurrentUrl.d.ts +4 -0
  23. package/dist/src/helpers/getDataFromHtml.d.ts +8 -0
  24. package/dist/src/helpers/index.d.ts +11 -0
  25. package/dist/src/helpers/markSwupElements.d.ts +2 -0
  26. package/dist/src/helpers/updateHistoryRecord.d.ts +2 -0
  27. package/dist/src/helpers/versionSatisfies.d.ts +12 -0
  28. package/dist/src/helpers.d.ts +1 -0
  29. package/dist/src/index.d.ts +97 -0
  30. package/dist/src/modules/Cache.d.ts +20 -0
  31. package/dist/src/modules/enterPage.d.ts +6 -0
  32. package/dist/src/modules/getAnchorElement.d.ts +2 -0
  33. package/dist/src/modules/getAnimationPromises.d.ts +7 -0
  34. package/dist/src/modules/getPageData.d.ts +7 -0
  35. package/dist/src/modules/leavePage.d.ts +7 -0
  36. package/dist/src/modules/loadPage.d.ts +4 -0
  37. package/dist/src/modules/off.d.ts +4 -0
  38. package/dist/src/modules/on.d.ts +4 -0
  39. package/dist/src/modules/plugins.d.ts +13 -0
  40. package/dist/src/modules/renderPage.d.ts +7 -0
  41. package/dist/src/modules/replaceContent.d.ts +17 -0
  42. package/dist/src/modules/triggerEvent.d.ts +4 -0
  43. package/dist/src/modules/updateTransition.d.ts +3 -0
  44. package/dist/src/src/config/version.d.ts +5 -0
  45. package/dist/src/src/helpers/Location.d.ts +31 -0
  46. package/dist/src/src/helpers/classify.d.ts +2 -0
  47. package/dist/src/src/helpers/cleanupAnimationClasses.d.ts +2 -0
  48. package/dist/src/src/helpers/createHistoryRecord.d.ts +2 -0
  49. package/dist/src/src/helpers/delegateEvent.d.ts +8 -0
  50. package/dist/src/src/helpers/fetch.d.ts +6 -0
  51. package/dist/src/src/helpers/getCurrentUrl.d.ts +4 -0
  52. package/dist/src/src/helpers/getDataFromHtml.d.ts +8 -0
  53. package/dist/src/src/helpers/index.d.ts +11 -0
  54. package/dist/src/src/helpers/markSwupElements.d.ts +2 -0
  55. package/dist/src/src/helpers/updateHistoryRecord.d.ts +2 -0
  56. package/dist/src/src/helpers/versionSatisfies.d.ts +12 -0
  57. package/dist/src/src/helpers.d.ts +1 -0
  58. package/dist/src/src/index.d.ts +103 -0
  59. package/dist/src/src/modules/Cache.d.ts +20 -0
  60. package/dist/src/src/modules/enterPage.d.ts +6 -0
  61. package/dist/src/src/modules/getAnchorElement.d.ts +2 -0
  62. package/dist/src/src/modules/getAnimationPromises.d.ts +7 -0
  63. package/dist/src/src/modules/getPageData.d.ts +7 -0
  64. package/dist/src/src/modules/leavePage.d.ts +7 -0
  65. package/dist/src/src/modules/loadPage.d.ts +7 -0
  66. package/dist/src/src/modules/off.d.ts +4 -0
  67. package/dist/src/src/modules/on.d.ts +6 -0
  68. package/dist/src/src/modules/plugins.d.ts +14 -0
  69. package/dist/src/src/modules/renderPage.d.ts +7 -0
  70. package/dist/src/src/modules/replaceContent.d.ts +17 -0
  71. package/dist/src/src/modules/triggerEvent.d.ts +4 -0
  72. package/dist/src/src/modules/updateTransition.d.ts +3 -0
  73. package/dist/src/src/utils/index.d.ts +5 -0
  74. package/dist/src/src/utils.d.ts +1 -0
  75. package/dist/src/types.d.ts +12 -0
  76. package/dist/src/utils/index.d.ts +5 -0
  77. package/dist/src/utils.d.ts +1 -0
  78. package/dist/types.cjs +2 -0
  79. package/dist/types.cjs.map +1 -0
  80. package/dist/types.modern.js +2 -0
  81. package/dist/types.modern.js.map +1 -0
  82. package/dist/types.module.js +2 -0
  83. package/dist/types.module.js.map +1 -0
  84. package/dist/utils.cjs +2 -0
  85. package/dist/utils.cjs.map +1 -0
  86. package/dist/utils.modern.js +2 -0
  87. package/dist/utils.modern.js.map +1 -0
  88. package/dist/utils.module.js +2 -0
  89. package/dist/utils.module.js.map +1 -0
  90. package/package.json +41 -23
  91. package/readme.md +52 -36
  92. package/src/config/version.ts +13 -0
  93. package/src/helpers/Location.ts +44 -0
  94. package/src/helpers/classify.ts +13 -0
  95. package/src/helpers/cleanupAnimationClasses.ts +10 -0
  96. package/src/helpers/createHistoryRecord.ts +14 -0
  97. package/src/helpers/delegateEvent.ts +23 -0
  98. package/src/helpers/fetch.ts +35 -0
  99. package/src/helpers/getCurrentUrl.ts +5 -0
  100. package/src/helpers/getDataFromHtml.ts +41 -0
  101. package/src/helpers/index.ts +11 -0
  102. package/src/helpers/markSwupElements.ts +18 -0
  103. package/src/helpers/updateHistoryRecord.ts +18 -0
  104. package/src/helpers/versionSatisfies.ts +50 -0
  105. package/src/helpers.ts +4 -0
  106. package/src/index.ts +369 -0
  107. package/src/modules/Cache.ts +57 -0
  108. package/src/modules/enterPage.ts +28 -0
  109. package/src/modules/fetchPage.ts +35 -0
  110. package/src/modules/getAnchorElement.ts +19 -0
  111. package/src/modules/getAnimationPromises.ts +176 -0
  112. package/src/modules/getPageData.ts +26 -0
  113. package/src/modules/leavePage.ts +33 -0
  114. package/src/modules/loadPage.ts +54 -0
  115. package/src/modules/off.ts +23 -0
  116. package/src/modules/on.ts +35 -0
  117. package/src/modules/plugins.ts +58 -0
  118. package/src/modules/renderPage.ts +52 -0
  119. package/src/modules/replaceContent.ts +28 -0
  120. package/src/modules/triggerEvent.ts +23 -0
  121. package/src/modules/updateTransition.ts +7 -0
  122. package/src/utils/index.ts +32 -0
  123. package/src/utils.ts +4 -0
  124. package/.editorconfig +0 -19
  125. package/cypress.config.js +0 -14
  126. package/dist/swup.js +0 -1524
  127. package/dist/swup.min.js +0 -1
  128. package/lib/helpers/Link.js +0 -56
  129. package/lib/helpers/classify.js +0 -18
  130. package/lib/helpers/cleanupAnimationClasses.js +0 -18
  131. package/lib/helpers/createHistoryRecord.js +0 -14
  132. package/lib/helpers/fetch.js +0 -41
  133. package/lib/helpers/getCurrentUrl.js +0 -10
  134. package/lib/helpers/getDataFromHtml.js +0 -43
  135. package/lib/helpers/index.js +0 -64
  136. package/lib/helpers/markSwupElements.js +0 -24
  137. package/lib/helpers/normalizeUrl.js +0 -17
  138. package/lib/helpers/transitionEnd.js +0 -14
  139. package/lib/helpers/transitionProperty.js +0 -14
  140. package/lib/index.js +0 -305
  141. package/lib/modules/Cache.js +0 -66
  142. package/lib/modules/getAnchorElement.js +0 -25
  143. package/lib/modules/getAnimationPromises.js +0 -43
  144. package/lib/modules/getPageData.js +0 -26
  145. package/lib/modules/loadPage.js +0 -123
  146. package/lib/modules/off.js +0 -34
  147. package/lib/modules/on.js +0 -14
  148. package/lib/modules/plugins.js +0 -54
  149. package/lib/modules/renderPage.js +0 -76
  150. package/lib/modules/triggerEvent.js +0 -21
  151. package/lib/modules/updateTransition.js +0 -15
  152. package/lib/utils/index.js +0 -32
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.modern.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+
2
+ //# sourceMappingURL=types.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.module.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/dist/utils.cjs ADDED
@@ -0,0 +1,2 @@
1
+ exports.escapeCssIdentifier=function(e){return window.CSS&&window.CSS.escape?CSS.escape(e):e},exports.nextTick=function(e){requestAnimationFrame(function(){requestAnimationFrame(function(){e()})})},exports.query=function(e,r){return void 0===r&&(r=document),r.querySelector(e)},exports.queryAll=function(e,r){return void 0===r&&(r=document),Array.from(r.querySelectorAll(e))},exports.toMs=function(e){return 1e3*Number(e.slice(0,-1).replace(",","."))};
2
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.cjs","sources":["../src/utils/index.ts"],"sourcesContent":["export const query = (selector: string, context: Document | Element = document) => {\n\treturn context.querySelector<HTMLElement>(selector);\n};\n\nexport const queryAll = (\n\tselector: string,\n\tcontext: Document | Element = document\n): HTMLElement[] => {\n\treturn Array.from(context.querySelectorAll(selector));\n};\n\nexport const nextTick = (callback: () => void) => {\n\trequestAnimationFrame(() => {\n\t\trequestAnimationFrame(() => {\n\t\t\tcallback();\n\t\t});\n\t});\n};\n\nexport const escapeCssIdentifier = (ident: string) => {\n\t// @ts-ignore this is for support check, so it's correct that TS complains\n\tif (window.CSS && window.CSS.escape) {\n\t\treturn CSS.escape(ident);\n\t} else {\n\t\treturn ident;\n\t}\n};\n\n// Fix for Chrome below v61 formatting CSS floats with comma in some locales\nexport const toMs = (s: string) => {\n\treturn Number(s.slice(0, -1).replace(',', '.')) * 1000;\n};\n"],"names":["ident","window","CSS","escape","callback","requestAnimationFrame","selector","context","document","querySelector","Array","from","querySelectorAll","s","slice","replace"],"mappings":"4BAmBmC,SAACA,GAEnC,OAAIC,OAAOC,KAAOD,OAAOC,IAAIC,WACjBA,OAAOH,IAIpB,mBAfwB,SAACI,GACxBC,sBAAsB,WACrBA,sBAAsB,WACrBD,GACD,EACD,EACD,gBAjBqB,SAACE,EAAkBC,GACvC,YADuCA,IAAAA,IAAAA,EAA8BC,UAC9DD,EAAQE,cAA2BH,EAC3C,mBAEwB,SACvBA,EACAC,GAEA,YAFAA,IAAAA,IAAAA,EAA8BC,UAEvBE,MAAMC,KAAKJ,EAAQK,iBAAiBN,GAC5C,eAoBoB,SAACO,GACpB,OAAkD,WAApCA,EAAEC,MAAM,GAAI,GAAGC,QAAQ,IAAK,KAC3C"}
@@ -0,0 +1,2 @@
1
+ const e=(e,r=document)=>r.querySelector(e),r=(e,r=document)=>Array.from(r.querySelectorAll(e)),o=e=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{e()})})},t=e=>window.CSS&&window.CSS.escape?CSS.escape(e):e,c=e=>1e3*Number(e.slice(0,-1).replace(",","."));export{t as escapeCssIdentifier,o as nextTick,e as query,r as queryAll,c as toMs};
2
+ //# sourceMappingURL=utils.modern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.modern.js","sources":["../src/utils/index.ts"],"sourcesContent":["export const query = (selector: string, context: Document | Element = document) => {\n\treturn context.querySelector<HTMLElement>(selector);\n};\n\nexport const queryAll = (\n\tselector: string,\n\tcontext: Document | Element = document\n): HTMLElement[] => {\n\treturn Array.from(context.querySelectorAll(selector));\n};\n\nexport const nextTick = (callback: () => void) => {\n\trequestAnimationFrame(() => {\n\t\trequestAnimationFrame(() => {\n\t\t\tcallback();\n\t\t});\n\t});\n};\n\nexport const escapeCssIdentifier = (ident: string) => {\n\t// @ts-ignore this is for support check, so it's correct that TS complains\n\tif (window.CSS && window.CSS.escape) {\n\t\treturn CSS.escape(ident);\n\t} else {\n\t\treturn ident;\n\t}\n};\n\n// Fix for Chrome below v61 formatting CSS floats with comma in some locales\nexport const toMs = (s: string) => {\n\treturn Number(s.slice(0, -1).replace(',', '.')) * 1000;\n};\n"],"names":["query","selector","context","document","querySelector","queryAll","Array","from","querySelectorAll","nextTick","callback","requestAnimationFrame","escapeCssIdentifier","ident","window","CSS","escape","toMs","s","Number","slice","replace"],"mappings":"AAAaA,MAAAA,EAAQ,CAACC,EAAkBC,EAA8BC,aACtDC,cAA2BH,GAG9BI,EAAW,CACvBJ,EACAC,EAA8BC,WAEvBG,MAAMC,KAAKL,EAAQM,iBAAiBP,IAG/BQ,EAAYC,IACxBC,sBAAsB,KACrBA,sBAAsB,KACrBD,GACD,EACD,IAGYE,EAAuBC,GAE/BC,OAAOC,KAAOD,OAAOC,IAAIC,OAClBD,IAACC,OAAOH,GAEXA,EAKII,EAAQC,GAC8B,IAA3CC,OAAOD,EAAEE,MAAM,GAAI,GAAGC,QAAQ,IAAK"}
@@ -0,0 +1,2 @@
1
+ var e=function(e,n){return void 0===n&&(n=document),n.querySelector(e)},n=function(e,n){return void 0===n&&(n=document),Array.from(n.querySelectorAll(e))},r=function(e){requestAnimationFrame(function(){requestAnimationFrame(function(){e()})})},t=function(e){return window.CSS&&window.CSS.escape?CSS.escape(e):e},o=function(e){return 1e3*Number(e.slice(0,-1).replace(",","."))};export{t as escapeCssIdentifier,r as nextTick,e as query,n as queryAll,o as toMs};
2
+ //# sourceMappingURL=utils.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.module.js","sources":["../src/utils/index.ts"],"sourcesContent":["export const query = (selector: string, context: Document | Element = document) => {\n\treturn context.querySelector<HTMLElement>(selector);\n};\n\nexport const queryAll = (\n\tselector: string,\n\tcontext: Document | Element = document\n): HTMLElement[] => {\n\treturn Array.from(context.querySelectorAll(selector));\n};\n\nexport const nextTick = (callback: () => void) => {\n\trequestAnimationFrame(() => {\n\t\trequestAnimationFrame(() => {\n\t\t\tcallback();\n\t\t});\n\t});\n};\n\nexport const escapeCssIdentifier = (ident: string) => {\n\t// @ts-ignore this is for support check, so it's correct that TS complains\n\tif (window.CSS && window.CSS.escape) {\n\t\treturn CSS.escape(ident);\n\t} else {\n\t\treturn ident;\n\t}\n};\n\n// Fix for Chrome below v61 formatting CSS floats with comma in some locales\nexport const toMs = (s: string) => {\n\treturn Number(s.slice(0, -1).replace(',', '.')) * 1000;\n};\n"],"names":["query","selector","context","document","querySelector","queryAll","Array","from","querySelectorAll","nextTick","callback","requestAnimationFrame","escapeCssIdentifier","ident","window","CSS","escape","toMs","s","slice","replace"],"mappings":"AAAaA,IAAAA,EAAQ,SAACC,EAAkBC,GACvC,YADuCA,IAAAA,IAAAA,EAA8BC,UAC9DD,EAAQE,cAA2BH,EAC3C,EAEaI,EAAW,SACvBJ,EACAC,GAEA,YAFAA,IAAAA,IAAAA,EAA8BC,UAEvBG,MAAMC,KAAKL,EAAQM,iBAAiBP,GAC5C,EAEaQ,EAAW,SAACC,GACxBC,sBAAsB,WACrBA,sBAAsB,WACrBD,GACD,EACD,EACD,EAEaE,EAAsB,SAACC,GAEnC,OAAIC,OAAOC,KAAOD,OAAOC,IAAIC,WACjBA,OAAOH,IAIpB,EAGiBI,EAAG,SAACC,GACpB,OAAkD,WAApCA,EAAEC,MAAM,GAAI,GAAGC,QAAQ,IAAK,KAC3C"}
package/package.json CHANGED
@@ -1,22 +1,47 @@
1
1
  {
2
2
  "name": "swup",
3
- "version": "2.0.19",
4
- "description": "Complete, flexible, extensible and easy to use page transition library for your web.",
5
- "main": "lib/index.js",
3
+ "amdName": "Swup",
4
+ "version": "3.0.0-rc.1",
5
+ "description": "Complete, flexible, extensible, and easy-to-use page transition library for your server-side rendered website.",
6
+ "type": "module",
7
+ "source": "src/index.ts",
8
+ "main": "dist/index.cjs",
9
+ "module": "dist/index.module.js",
10
+ "unpkg": "dist/index.umd.js",
11
+ "types": "dist/types/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/types/index.d.ts",
15
+ "require": "./dist/index.cjs",
16
+ "default": "./dist/index.modern.js"
17
+ },
18
+ "./*": {
19
+ "types": "./dist/types/*.d.ts",
20
+ "require": "./dist/*.cjs",
21
+ "default": "./dist/*.modern.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "src",
26
+ "dist"
27
+ ],
6
28
  "scripts": {
7
- "cy:open": "cypress open",
8
- "compile": "babel --presets es2015,stage-0 -d lib/ src/",
9
- "build": "webpack-cli",
10
- "lint": "prettier src/**/*.{js,mjs} --write",
11
- "prepublish": "npm run compile && npm run build",
29
+ "build": "npm run build:module && npm run build:bundle",
30
+ "build:module": "microbundle src/*.ts -f modern,esm,cjs",
31
+ "build:bundle": "microbundle src/index.ts -f umd --external none",
32
+ "dev": "microbundle src/*.ts -w",
33
+ "lint": "prettier 'src/**/*.ts' --write",
34
+ "prepublish": "npm run build",
12
35
  "postinstall": "opencollective-postinstall || true",
13
- "ci": "npm run build && npm run test:instrument && start-server-and-test test:server http://localhost:8274 test:run:record",
14
- "test": "npm run build && npm run test:instrument && start-server-and-test test:server http://localhost:8274 test:run",
15
- "test:headed": "npm run build && npm run test:instrument && start-server-and-test test:server http://localhost:8274 cy:open",
36
+ "ci": "start-server-and-test test:start 8274 cy:run:record",
37
+ "test": "start-server-and-test test:start 8274 cy:run",
38
+ "test:dev": "start-server-and-test test:start 8274 cy:open",
16
39
  "test:instrument": "nyc instrument --compact=false dist test/site/swup",
17
40
  "test:server": "http-server --port 8274 test/site",
18
- "test:run": "cypress run",
19
- "test:run:record": "cypress run --record"
41
+ "test:start": "npm run test:instrument && npm run test:server",
42
+ "cy:run": "cypress run",
43
+ "cy:run:record": "cypress run --record",
44
+ "cy:open": "cypress open"
20
45
  },
21
46
  "author": "Georgy Marchuk",
22
47
  "license": "MIT",
@@ -37,20 +62,13 @@
37
62
  "devDependencies": {
38
63
  "@cypress/code-coverage": "^3.10.0",
39
64
  "@swup/prettier-config": "^1.0.0",
40
- "@swup/webpack-config": "^1.0.0",
41
- "babel-cli": "^6.26.0",
42
- "babel-loader": "^7.1.4",
43
- "babel-preset-es2015": "^6.24.1",
44
- "babel-preset-stage-0": "^6.24.1",
45
65
  "cypress": "^10.0.2",
46
66
  "http-server": "^14.1.1",
47
67
  "istanbul-lib-coverage": "^3.2.0",
68
+ "microbundle": "^0.15.0",
48
69
  "nyc": "^15.1.0",
49
- "prettier": "1.17.0",
50
- "start-server-and-test": "^1.14.0",
51
- "uglifyjs-webpack-plugin": "^1.2.5",
52
- "webpack": "^4.8.3",
53
- "webpack-cli": "^3.3.2"
70
+ "prettier": "1.19.0",
71
+ "start-server-and-test": "^1.14.0"
54
72
  },
55
73
  "collective": {
56
74
  "type": "opencollective",
package/readme.md CHANGED
@@ -1,44 +1,60 @@
1
- <h3 align="center"><a href="https://github.com/swup/swup/discussions/424">We're looking for maintainers!</a></h3>
1
+ <br>
2
2
 
3
- ---
4
-
5
- <p align="center"><img width="420" alt="swup" src="https://swup.js.org/assets/images/swup-logo.svg"></p>
6
3
  <p align="center">
7
- Complete, flexible, extensible and easy to use page transition library for your static web.
4
+ <img width="280" alt="swup" src="https://swup.js.org/assets/images/swup-logo.svg">
8
5
  </p>
6
+
9
7
  <p align="center">
10
- <a href="https://www.npmjs.com/package/swup"><img src="https://img.shields.io/npm/v/swup.svg" alt="npm version"></a>
11
- <img src="https://img.shields.io/bundlephobia/minzip/swup.svg" alt="Gzip Size">
12
- <a href="https://github.com/gmrchk/swup/blob/master/LICENSE"><img src="https://img.shields.io/github/license/gmrchk/swup.svg" alt="License"></a>
13
- <a href="https://www.npmjs.com/package/swup"><img src="https://img.shields.io/npm/dt/swup.svg" alt="npm downloads"></a>
14
- <a href="https://circleci.com/gh/swup/swup"><img src="https://circleci.com/gh/swup/swup.svg?style=svg" alt="CircleCI"></a>
8
+ <a href="https://www.npmjs.com/package/swup"><img src="https://img.shields.io/npm/v/swup.svg" alt="npm version"></a>
9
+ <a href="https://bundlephobia.com/package/swup"><img src="https://img.shields.io/bundlephobia/minzip/swup.svg" alt="Bundle size"></a>
10
+ <a href="https://github.com/gmrchk/swup/blob/master/LICENSE"><img src="https://img.shields.io/github/license/gmrchk/swup.svg" alt="License"></a>
11
+ <a href="https://www.npmjs.com/package/swup"><img src="https://img.shields.io/npm/dt/swup.svg" alt="npm downloads"></a>
12
+ <a href="https://circleci.com/gh/swup/swup"><img src="https://circleci.com/gh/swup/swup.svg?style=shield" alt="Build status"></a>
15
13
  </p>
16
14
 
17
- [Here](https://medium.com/@gmarcuk/introducing-swup-v2-814e40316dee)'s what's new in v2. Check [changelog](https://swup.js.org/other/changelog) for recent changes, see all [swup repositories](https://github.com/swup/) to discover more, or checkout the [discussions](https://github.com/swup/swup/discussions).
18
-
19
- - [Discussions](https://github.com/swup/swup/discussions)
20
- - [Getting Started](https://swup.js.org/getting-started)
21
- - [Example](https://swup.js.org/getting-started/example)
22
- - [Demo](https://swup.js.org/getting-started/demo)
23
- - [Reloading JavaScript](https://swup.js.org/getting-started/reloading-javascript)
24
- - [Video Tutorials](https://swup.js.org/getting-started/videos)
25
- - [Options](https://swup.js.org/options)
26
- - [Events](https://swup.js.org/events)
27
- - Extensions
28
- - [Plugins](https://swup.js.org/plugins)
29
- - [Themes](https://swup.js.org/themes)
30
- - [3rd Party Integrations](https://swup.js.org/third-party-integrations)
31
- - [JavaScript API](https://swup.js.org/api)
32
- - [Swup CLI](https://swup.js.org/cli)
33
- - [CI/CD Integration](https://swup.js.org/ci-cd)
34
- - Other
35
- - [Changelog](https://swup.js.org//other/changelog)
36
- - [Contributions](https://swup.js.org/other/contributions)
37
- - [Repositories](https://github.com/swup)
38
- - [Sites using swup](https://github.com/swup/swup/discussions/333)
39
-
40
- If you're having trouble implementing swup, checkout [Common Issues](https://swup.js.org/other/common-issues), [Closed Issues](https://github.com/gmrchk/swup/issues?q=is%3Aissue+is%3Aclosed) or open a [new one](https://github.com/gmrchk/swup/issues/new).
41
-
42
- [Become a backer or sponsor on Open Collective](https://opencollective.com/swup) or support swup through [GitHub sponsors](https://github.com/sponsors/gmrchk).
15
+ <br>
16
+
17
+ # Swup
18
+
19
+ Complete, flexible, extensible, and easy-to-use page transition library for your server-side rendered website.
20
+
21
+ [Features](#features)
22
+ [Documentation](https://swup.js.org/getting-started)
23
+ [Plugins](https://swup.js.org/plugins)
24
+ [Themes](https://swup.js.org/themes)
25
+ [Discussions](https://github.com/swup/swup/discussions)
26
+
27
+ ## Overview
28
+
29
+ Swup is a library that helps you add page transitions to server-side rendered websites. It handles
30
+ the complete lifecycle of a page visit by intercepting link clicks, loading the new page in the
31
+ background, replacing the content and transitioning between the old and the new page.
32
+
33
+ Its goal is to make adding transitions to a site as simple as possible, while providing lots of
34
+ other quality-of-life improvements.
35
+
36
+ ## Features
37
+
38
+ - Auto-detects [CSS transitions](https://swup.js.org/getting-started/how-it-works) for perfect timing
39
+ - 🔗 Updates URLs and preserves native browser history behavior
40
+ - 📦 Uses a [cache](https://swup.js.org/api/cache) to speed up subsequent page loads
41
+ - 💡 Offers [events](https://swup.js.org/events) for hooking into the lifecycle
42
+ - 🔌 Has a powerful [plugin system](https://swup.js.org/plugins) and many official and third-party plugins
43
+ - 🎨 Provides ready-to-go [themes](https://swup.js.org/themes) to get started quickly
44
+
45
+ ## Examples
43
46
 
44
47
  <img src="https://user-images.githubusercontent.com/9338324/49190360-50125480-f372-11e8-89e9-d2fb091a2240.gif" width="100%">
48
+
49
+ Take a look at [Sites using swup](https://github.com/swup/swup/discussions/333) for more examples.
50
+
51
+ ## Having trouble?
52
+
53
+ If you're having trouble implementing swup, check out the [Common Issues](https://swup.js.org/other/common-issues) section of the docs, look at [closed issues](https://github.com/gmrchk/swup/issues?q=is%3Aissue+is%3Aclosed) or create a [new discussion](https://github.com/swup/swup/discussions/new).
54
+
55
+ ## Want to Contribute?
56
+
57
+ <a href="https://github.com/swup/swup/discussions/424">We're looking for maintainers!</a>   👀
58
+
59
+ Become a sponsor on [Open Collective](https://opencollective.com/swup) or support development through
60
+ [GitHub sponsors](https://github.com/sponsors/gmrchk).
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Import swup's current version from package.json
3
+ */
4
+
5
+ // This will only work in webpack 4
6
+ // export { version as default } from '../../package.json';
7
+
8
+ // This will work in microbundle + webpack 5, but won't treeshake in webpack 4
9
+ // Ignore next line in TypeScript as package.json is outside of rootDir
10
+ // @ts-ignore
11
+ import pckg from '../../package.json';
12
+
13
+ export default pckg.version;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * A helper for creating a Location from either an element
3
+ * or a URL object/string
4
+ *
5
+ * Note: this could be implemented as a class inheriting from URL
6
+ * Except: Babel will add tons of boilerplate for ES6 classes + getters
7
+ * So for now it's implemented as an augmented URL object with custom getter
8
+ *
9
+ * class Location extends URL {
10
+ * get url() {
11
+ * return this.pathname + this.search;
12
+ * }
13
+ * }
14
+ *
15
+ */
16
+
17
+ export default class Location extends URL {
18
+ constructor(url: string, base: string = document.baseURI) {
19
+ super(url.toString(), base);
20
+ }
21
+
22
+ get url() {
23
+ return this.pathname + this.search;
24
+ }
25
+
26
+ /**
27
+ * Instantiate a Location from an element's href attribute
28
+ * @param {Element} el
29
+ * @return new Location instance
30
+ */
31
+ static fromElement(el: HTMLAnchorElement): Location {
32
+ const href = el.getAttribute('href') || el.getAttribute('xlink:href');
33
+ return new Location(href!);
34
+ }
35
+
36
+ /**
37
+ * Instantiate a Location from a URL object or string
38
+ * @param {URL|string} url
39
+ * @return new Location instance
40
+ */
41
+ static fromUrl(url: string): Location {
42
+ return new Location(url);
43
+ }
44
+ }
@@ -0,0 +1,13 @@
1
+ const classify = (text: string, fallback?: string): string => {
2
+ const output = String(text)
3
+ .toLowerCase()
4
+ // .normalize('NFD') // split an accented letter in the base letter and the acent
5
+ // .replace(/[\u0300-\u036f]/g, '') // remove all previously split accents
6
+ .replace(/[\s/_.]+/g, '-') // replace spaces and _./ with '-'
7
+ .replace(/[^\w-]+/g, '') // remove all non-word chars
8
+ .replace(/--+/g, '-') // replace repeating '-' with single '-'
9
+ .replace(/^-+|-+$/, ''); // trim '-' from edges
10
+ return output || fallback || '';
11
+ };
12
+
13
+ export default classify;
@@ -0,0 +1,10 @@
1
+ const isSwupClass = (className: string): boolean =>
2
+ /^to-/.test(className) || ['is-changing', 'is-rendering', 'is-popstate'].includes(className);
3
+
4
+ const cleanupAnimationClasses = (): void => {
5
+ const htmlClasses = document.documentElement.className.split(' ');
6
+ const removeClasses = htmlClasses.filter(isSwupClass);
7
+ document.documentElement.classList.remove(...removeClasses);
8
+ };
9
+
10
+ export default cleanupAnimationClasses;
@@ -0,0 +1,14 @@
1
+ import { default as getCurrentUrl } from './getCurrentUrl.js';
2
+
3
+ const createHistoryRecord = (url: string, customData: Record<string, unknown> = {}): void => {
4
+ url = url || getCurrentUrl({ hash: true });
5
+ const data = {
6
+ url,
7
+ random: Math.random(),
8
+ source: 'swup',
9
+ ...customData
10
+ };
11
+ history.pushState(data, '', url);
12
+ };
13
+
14
+ export default createHistoryRecord;
@@ -0,0 +1,23 @@
1
+ import delegate, { EventType } from 'delegate-it';
2
+ import { ParseSelector } from 'typed-query-selector/parser';
3
+
4
+ export type Unsubscribe = {
5
+ destroy: () => void;
6
+ };
7
+ const delegateEvent = <Selector extends string, TEvent extends EventType>(
8
+ selector: Selector,
9
+ type: TEvent,
10
+ callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEvent]>,
11
+ { base = document, ...eventOptions } = {}
12
+ ): Unsubscribe => {
13
+ const delegation = delegate<string, ParseSelector<Selector, HTMLElement>, TEvent>(
14
+ base,
15
+ selector,
16
+ type,
17
+ callback,
18
+ eventOptions
19
+ );
20
+ return { destroy: () => delegation.destroy() };
21
+ };
22
+
23
+ export default delegateEvent;
@@ -0,0 +1,35 @@
1
+ import { TransitionOptions } from '../modules/loadPage.js';
2
+ import { Options } from '../index.js';
3
+
4
+ const fetch = (
5
+ options: TransitionOptions & { headers: Options['requestHeaders'] },
6
+ callback: (request: XMLHttpRequest) => void
7
+ ): XMLHttpRequest => {
8
+ const defaults = {
9
+ url: window.location.pathname + window.location.search,
10
+ method: 'GET',
11
+ data: null,
12
+ headers: {}
13
+ };
14
+
15
+ const { url, method, headers, data } = { ...defaults, ...options };
16
+
17
+ const request = new XMLHttpRequest();
18
+
19
+ request.onreadystatechange = function() {
20
+ if (request.readyState === 4) {
21
+ // if (request.status === 500) {} else {}
22
+ callback(request);
23
+ }
24
+ };
25
+
26
+ request.open(method, url, true);
27
+ Object.entries(headers).forEach(([key, header]) => {
28
+ request.setRequestHeader(key, header);
29
+ });
30
+ request.send(data);
31
+
32
+ return request;
33
+ };
34
+
35
+ export default fetch;
@@ -0,0 +1,5 @@
1
+ const getCurrentUrl = ({ hash }: { hash?: boolean } = {}): string => {
2
+ return location.pathname + location.search + (hash ? location.hash : '');
3
+ };
4
+
5
+ export default getCurrentUrl;
@@ -0,0 +1,41 @@
1
+ import { query, queryAll } from '../utils.js';
2
+
3
+ export type PageHtmlData = {
4
+ title: string;
5
+ originalContent: string;
6
+ blocks: string[];
7
+ pageClass?: string;
8
+ };
9
+
10
+ const getDataFromHtml = (html: string, containers: string[]): PageHtmlData => {
11
+ let fakeDom = document.createElement('html');
12
+ fakeDom.innerHTML = html;
13
+ let blocks: string[] = [];
14
+
15
+ containers.forEach((selector) => {
16
+ if (query(selector, fakeDom) == null) {
17
+ console.warn(`[swup] Container ${selector} not found on page.`);
18
+ return null;
19
+ } else {
20
+ if (queryAll(selector).length !== queryAll(selector, fakeDom).length) {
21
+ console.warn(`[swup] Mismatched number of containers found on new page.`);
22
+ }
23
+ queryAll(selector).forEach((item, index) => {
24
+ queryAll(selector, fakeDom)[index].setAttribute('data-swup', String(blocks.length));
25
+ blocks.push(queryAll(selector, fakeDom)[index].outerHTML);
26
+ });
27
+ }
28
+ });
29
+
30
+ const title = query('title', fakeDom)?.innerText || '';
31
+ const pageClass = query('body', fakeDom)?.className;
32
+
33
+ // to prevent memory leaks
34
+ fakeDom.innerHTML = '';
35
+ // @ts-ignore don't want to type it as possible null, since it's created at the top of the function always
36
+ fakeDom = null;
37
+
38
+ return { title, pageClass, blocks, originalContent: html };
39
+ };
40
+
41
+ export default getDataFromHtml;
@@ -0,0 +1,11 @@
1
+ export { default as classify } from './classify.js';
2
+ export { default as createHistoryRecord } from './createHistoryRecord.js';
3
+ export { default as updateHistoryRecord } from './updateHistoryRecord.js';
4
+ export { default as delegateEvent } from './delegateEvent.js';
5
+ export { default as getDataFromHtml } from './getDataFromHtml.js';
6
+ export { default as fetch } from './fetch.js';
7
+ export { default as getCurrentUrl } from './getCurrentUrl.js';
8
+ export { default as Location } from './Location.js';
9
+ export { default as markSwupElements } from './markSwupElements.js';
10
+ export { default as cleanupAnimationClasses } from './cleanupAnimationClasses.js';
11
+ export { default as versionSatisfies } from './versionSatisfies.js';
@@ -0,0 +1,18 @@
1
+ import { query, queryAll } from '../utils.js';
2
+
3
+ const markSwupElements = (element: Element, containers: string[]): void => {
4
+ let blocks = 0;
5
+
6
+ containers.forEach((selector) => {
7
+ if (query(selector, element) == null) {
8
+ console.warn(`[swup] Container ${selector} not found on page.`);
9
+ } else {
10
+ queryAll(selector).forEach((item: Element, index: number) => {
11
+ queryAll(selector, element)[index].setAttribute('data-swup', String(blocks));
12
+ blocks++;
13
+ });
14
+ }
15
+ });
16
+ };
17
+
18
+ export default markSwupElements;
@@ -0,0 +1,18 @@
1
+ import { default as getCurrentUrl } from './getCurrentUrl.js';
2
+
3
+ const updateHistoryRecord = (
4
+ url: string | null = null,
5
+ customData: Record<string, unknown> = {}
6
+ ): void => {
7
+ url = url || getCurrentUrl({ hash: true });
8
+ const data = {
9
+ ...history.state,
10
+ url,
11
+ random: Math.random(),
12
+ source: 'swup',
13
+ ...customData
14
+ };
15
+ history.replaceState(data, '', url);
16
+ };
17
+
18
+ export default updateHistoryRecord;
@@ -0,0 +1,50 @@
1
+ type Comparator = '>' | '>=' | '<' | '<=';
2
+
3
+ // Fill versions to exactly 3 decimals
4
+ const normalizeVersion = (version: string): string => {
5
+ return String(version)
6
+ .split('.')
7
+ .concat(['0', '0'])
8
+ .slice(0, 3)
9
+ .join('.');
10
+ };
11
+
12
+ // Numerically compare version strings after normalizing them
13
+ const compareVersion = (a: string, b: string): number => {
14
+ a = normalizeVersion(a);
15
+ b = normalizeVersion(b);
16
+ return a.localeCompare(b, undefined, { numeric: true });
17
+ };
18
+
19
+ // Apply a comparator (equals, greater-than, etc) by its symbol to a sort comparison
20
+ const applyComparator = (comparisonResult: number, comparator: Comparator) => {
21
+ const comparators = {
22
+ '': (r: number) => r === 0,
23
+ '>': (r: number) => r > 0,
24
+ '>=': (r: number) => r >= 0,
25
+ '<': (r: number) => r < 0,
26
+ '<=': (r: number) => r <= 0
27
+ };
28
+ const comparatorFn = comparators[comparator] || comparators[''];
29
+ return comparatorFn(comparisonResult);
30
+ };
31
+
32
+ /**
33
+ * Check if a version satisfies all given version requirements
34
+ *
35
+ * versionSatisfies('2.1.0', ['>=2', '<4']) // true
36
+ * versionSatisfies('2.1.0', ['5']) // false
37
+ *
38
+ * @param {string} installed Installed version
39
+ * @param {Array.<string>} requirements Array of requirements that must be satisfied
40
+ * @returns boolean
41
+ */
42
+ export const versionSatisfies = (installed: string, requirements: string[]) => {
43
+ return requirements.every((required) => {
44
+ const [, comparator, version] = required.match(/^([\D]+)?(.*)$/) || [];
45
+ const comparisonResult = compareVersion(installed, version);
46
+ return applyComparator(comparisonResult, (comparator as Comparator) || '>=');
47
+ });
48
+ };
49
+
50
+ export default versionSatisfies;
package/src/helpers.ts ADDED
@@ -0,0 +1,4 @@
1
+ // Re-export all helpers to allow custom package export path
2
+ // e.g. import { getPageData } from 'swup/helpers'
3
+
4
+ export * from './helpers/index.js';