isomorfeus-preact 10.9.0 → 22.9.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (263) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +14 -3
  3. data/README.md +23 -39
  4. data/lib/browser/delegate_native.rb +94 -72
  5. data/lib/browser/document.rb +38 -0
  6. data/lib/browser/element.rb +160 -176
  7. data/lib/browser/event.rb +99 -94
  8. data/lib/browser/history.rb +40 -20
  9. data/lib/browser/location.rb +59 -0
  10. data/lib/browser/window.rb +58 -0
  11. data/lib/data_uri/open_uri.rb +18 -0
  12. data/lib/data_uri/uri.rb +61 -0
  13. data/lib/data_uri.rb +4 -0
  14. data/lib/isomorfeus/preact/config.rb +42 -54
  15. data/lib/isomorfeus/preact/imports.rb +5 -20
  16. data/lib/isomorfeus/preact/ssr.rb +34 -0
  17. data/lib/isomorfeus/preact/thread_local_component_cache.rb +9 -11
  18. data/lib/isomorfeus/preact/version.rb +5 -0
  19. data/lib/isomorfeus/preact/view_helper.rb +72 -0
  20. data/lib/isomorfeus/props/validator.rb +19 -11
  21. data/lib/isomorfeus/top_level.rb +55 -33
  22. data/lib/isomorfeus-preact.rb +78 -103
  23. data/lib/link.rb +52 -0
  24. data/lib/lucid_app.rb +117 -0
  25. data/lib/lucid_component.rb +154 -0
  26. data/lib/nano_css.rb +282 -465
  27. data/lib/preact/component.rb +222 -0
  28. data/lib/preact/component_resolution.rb +49 -83
  29. data/lib/preact/context.rb +68 -0
  30. data/lib/preact/elements.rb +2 -9
  31. data/lib/preact/module_component_resolution.rb +50 -0
  32. data/lib/preact/prop_declaration_mixin.rb +73 -0
  33. data/lib/preact.rb +1548 -257
  34. data/lib/redirect.rb +34 -0
  35. data/lib/route.rb +15 -0
  36. data/lib/router.rb +145 -0
  37. data/lib/switch.rb +70 -0
  38. metadata +42 -309
  39. data/lib/isomorfeus/preact/memcached_component_cache.rb +0 -19
  40. data/lib/isomorfeus/preact/redis_component_cache.rb +0 -19
  41. data/lib/isomorfeus/preact/ssr/history.rb +0 -23
  42. data/lib/isomorfeus/preact/ssr/render_core.rb +0 -117
  43. data/lib/isomorfeus/preact/ssr/top_level.rb +0 -37
  44. data/lib/isomorfeus/preact_view_helper.rb +0 -129
  45. data/lib/isomorfeus_preact/lucid_app/api.rb +0 -38
  46. data/lib/isomorfeus_preact/lucid_app/base.rb +0 -7
  47. data/lib/isomorfeus_preact/lucid_app/mixin.rb +0 -14
  48. data/lib/isomorfeus_preact/lucid_app/native_component_constructor.rb +0 -101
  49. data/lib/isomorfeus_preact/lucid_component/api.rb +0 -123
  50. data/lib/isomorfeus_preact/lucid_component/app_store_proxy.rb +0 -32
  51. data/lib/isomorfeus_preact/lucid_component/base.rb +0 -7
  52. data/lib/isomorfeus_preact/lucid_component/class_store_proxy.rb +0 -37
  53. data/lib/isomorfeus_preact/lucid_component/initializer.rb +0 -9
  54. data/lib/isomorfeus_preact/lucid_component/mixin.rb +0 -13
  55. data/lib/isomorfeus_preact/lucid_component/native_component_constructor.rb +0 -87
  56. data/lib/isomorfeus_preact/lucid_component/styles_wrapper.rb +0 -40
  57. data/lib/isomorfeus_preact/lucid_func/base.rb +0 -7
  58. data/lib/isomorfeus_preact/lucid_func/initializer.rb +0 -8
  59. data/lib/isomorfeus_preact/lucid_func/mixin.rb +0 -10
  60. data/lib/isomorfeus_preact/lucid_func/native_component_constructor.rb +0 -56
  61. data/lib/isomorfeus_preact/preact/function_component/api.rb +0 -164
  62. data/lib/isomorfeus_preact/preact/function_component/base.rb +0 -7
  63. data/lib/isomorfeus_preact/preact/function_component/initializer.rb +0 -6
  64. data/lib/isomorfeus_preact/preact/function_component/mixin.rb +0 -8
  65. data/lib/isomorfeus_preact/preact/function_component/native_component_constructor.rb +0 -47
  66. data/lib/lucid_app/context.rb +0 -24
  67. data/lib/lucid_prop_declaration/mixin.rb +0 -126
  68. data/lib/preact/component/api.rb +0 -137
  69. data/lib/preact/component/base.rb +0 -7
  70. data/lib/preact/component/callbacks.rb +0 -111
  71. data/lib/preact/component/initializer.rb +0 -7
  72. data/lib/preact/component/mixin.rb +0 -11
  73. data/lib/preact/component/native_component_constructor.rb +0 -77
  74. data/lib/preact/context_wrapper.rb +0 -48
  75. data/lib/preact/native_constant_wrapper.rb +0 -29
  76. data/lib/preact/option_hooks.rb +0 -98
  77. data/lib/preact/params.rb +0 -16
  78. data/lib/preact/props.rb +0 -69
  79. data/lib/preact/ref.rb +0 -17
  80. data/lib/preact/state.rb +0 -87
  81. data/lib/preact/version.rb +0 -3
  82. data/node_modules/.package-lock.json +0 -38
  83. data/node_modules/preact/LICENSE +0 -21
  84. data/node_modules/preact/README.md +0 -188
  85. data/node_modules/preact/compat/LICENSE +0 -21
  86. data/node_modules/preact/compat/client.js +0 -19
  87. data/node_modules/preact/compat/client.mjs +0 -17
  88. data/node_modules/preact/compat/dist/compat.js +0 -2
  89. data/node_modules/preact/compat/dist/compat.js.map +0 -1
  90. data/node_modules/preact/compat/dist/compat.mjs +0 -2
  91. data/node_modules/preact/compat/dist/compat.module.js +0 -2
  92. data/node_modules/preact/compat/dist/compat.module.js.map +0 -1
  93. data/node_modules/preact/compat/dist/compat.umd.js +0 -2
  94. data/node_modules/preact/compat/dist/compat.umd.js.map +0 -1
  95. data/node_modules/preact/compat/jsx-dev-runtime.js +0 -3
  96. data/node_modules/preact/compat/jsx-dev-runtime.mjs +0 -3
  97. data/node_modules/preact/compat/jsx-runtime.js +0 -3
  98. data/node_modules/preact/compat/jsx-runtime.mjs +0 -3
  99. data/node_modules/preact/compat/package.json +0 -49
  100. data/node_modules/preact/compat/scheduler.js +0 -15
  101. data/node_modules/preact/compat/scheduler.mjs +0 -23
  102. data/node_modules/preact/compat/server.browser.js +0 -4
  103. data/node_modules/preact/compat/server.js +0 -15
  104. data/node_modules/preact/compat/server.mjs +0 -4
  105. data/node_modules/preact/compat/src/Children.js +0 -21
  106. data/node_modules/preact/compat/src/PureComponent.js +0 -15
  107. data/node_modules/preact/compat/src/forwardRef.js +0 -44
  108. data/node_modules/preact/compat/src/index.d.ts +0 -164
  109. data/node_modules/preact/compat/src/index.js +0 -223
  110. data/node_modules/preact/compat/src/internal.d.ts +0 -47
  111. data/node_modules/preact/compat/src/memo.js +0 -34
  112. data/node_modules/preact/compat/src/portals.js +0 -82
  113. data/node_modules/preact/compat/src/render.js +0 -238
  114. data/node_modules/preact/compat/src/suspense-list.d.ts +0 -14
  115. data/node_modules/preact/compat/src/suspense-list.js +0 -126
  116. data/node_modules/preact/compat/src/suspense.d.ts +0 -15
  117. data/node_modules/preact/compat/src/suspense.js +0 -270
  118. data/node_modules/preact/compat/src/util.js +0 -28
  119. data/node_modules/preact/compat/test-utils.js +0 -1
  120. data/node_modules/preact/debug/LICENSE +0 -21
  121. data/node_modules/preact/debug/dist/debug.js +0 -2
  122. data/node_modules/preact/debug/dist/debug.js.map +0 -1
  123. data/node_modules/preact/debug/dist/debug.mjs +0 -2
  124. data/node_modules/preact/debug/dist/debug.module.js +0 -2
  125. data/node_modules/preact/debug/dist/debug.module.js.map +0 -1
  126. data/node_modules/preact/debug/dist/debug.umd.js +0 -2
  127. data/node_modules/preact/debug/dist/debug.umd.js.map +0 -1
  128. data/node_modules/preact/debug/package.json +0 -26
  129. data/node_modules/preact/debug/src/check-props.js +0 -54
  130. data/node_modules/preact/debug/src/component-stack.js +0 -146
  131. data/node_modules/preact/debug/src/constants.js +0 -3
  132. data/node_modules/preact/debug/src/debug.js +0 -437
  133. data/node_modules/preact/debug/src/index.js +0 -6
  134. data/node_modules/preact/debug/src/internal.d.ts +0 -82
  135. data/node_modules/preact/debug/src/util.js +0 -11
  136. data/node_modules/preact/devtools/LICENSE +0 -21
  137. data/node_modules/preact/devtools/dist/devtools.js +0 -2
  138. data/node_modules/preact/devtools/dist/devtools.js.map +0 -1
  139. data/node_modules/preact/devtools/dist/devtools.mjs +0 -2
  140. data/node_modules/preact/devtools/dist/devtools.module.js +0 -2
  141. data/node_modules/preact/devtools/dist/devtools.module.js.map +0 -1
  142. data/node_modules/preact/devtools/dist/devtools.umd.js +0 -2
  143. data/node_modules/preact/devtools/dist/devtools.umd.js.map +0 -1
  144. data/node_modules/preact/devtools/package.json +0 -25
  145. data/node_modules/preact/devtools/src/devtools.js +0 -10
  146. data/node_modules/preact/devtools/src/index.d.ts +0 -8
  147. data/node_modules/preact/devtools/src/index.js +0 -15
  148. data/node_modules/preact/dist/preact.js +0 -2
  149. data/node_modules/preact/dist/preact.js.map +0 -1
  150. data/node_modules/preact/dist/preact.min.js +0 -2
  151. data/node_modules/preact/dist/preact.min.js.map +0 -1
  152. data/node_modules/preact/dist/preact.mjs +0 -2
  153. data/node_modules/preact/dist/preact.module.js +0 -2
  154. data/node_modules/preact/dist/preact.module.js.map +0 -1
  155. data/node_modules/preact/dist/preact.umd.js +0 -2
  156. data/node_modules/preact/dist/preact.umd.js.map +0 -1
  157. data/node_modules/preact/hooks/LICENSE +0 -21
  158. data/node_modules/preact/hooks/dist/hooks.js +0 -2
  159. data/node_modules/preact/hooks/dist/hooks.js.map +0 -1
  160. data/node_modules/preact/hooks/dist/hooks.mjs +0 -2
  161. data/node_modules/preact/hooks/dist/hooks.module.js +0 -2
  162. data/node_modules/preact/hooks/dist/hooks.module.js.map +0 -1
  163. data/node_modules/preact/hooks/dist/hooks.umd.js +0 -2
  164. data/node_modules/preact/hooks/dist/hooks.umd.js.map +0 -1
  165. data/node_modules/preact/hooks/package.json +0 -35
  166. data/node_modules/preact/hooks/src/index.d.ts +0 -139
  167. data/node_modules/preact/hooks/src/index.js +0 -417
  168. data/node_modules/preact/hooks/src/internal.d.ts +0 -78
  169. data/node_modules/preact/jsx-runtime/LICENSE +0 -21
  170. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.js +0 -2
  171. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.js.map +0 -1
  172. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.mjs +0 -2
  173. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js +0 -2
  174. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js.map +0 -1
  175. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.umd.js +0 -2
  176. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.umd.js.map +0 -1
  177. data/node_modules/preact/jsx-runtime/package.json +0 -28
  178. data/node_modules/preact/jsx-runtime/src/index.d.ts +0 -50
  179. data/node_modules/preact/jsx-runtime/src/index.js +0 -77
  180. data/node_modules/preact/package.json +0 -304
  181. data/node_modules/preact/src/cjs.js +0 -3
  182. data/node_modules/preact/src/clone-element.js +0 -34
  183. data/node_modules/preact/src/component.js +0 -225
  184. data/node_modules/preact/src/constants.js +0 -3
  185. data/node_modules/preact/src/create-context.js +0 -68
  186. data/node_modules/preact/src/create-element.js +0 -98
  187. data/node_modules/preact/src/diff/catch-error.js +0 -40
  188. data/node_modules/preact/src/diff/children.js +0 -335
  189. data/node_modules/preact/src/diff/index.js +0 -533
  190. data/node_modules/preact/src/diff/props.js +0 -158
  191. data/node_modules/preact/src/index.d.ts +0 -317
  192. data/node_modules/preact/src/index.js +0 -13
  193. data/node_modules/preact/src/internal.d.ts +0 -155
  194. data/node_modules/preact/src/jsx.d.ts +0 -1014
  195. data/node_modules/preact/src/options.js +0 -16
  196. data/node_modules/preact/src/render.js +0 -75
  197. data/node_modules/preact/src/util.js +0 -27
  198. data/node_modules/preact/test-utils/dist/testUtils.js +0 -2
  199. data/node_modules/preact/test-utils/dist/testUtils.js.map +0 -1
  200. data/node_modules/preact/test-utils/dist/testUtils.mjs +0 -2
  201. data/node_modules/preact/test-utils/dist/testUtils.module.js +0 -2
  202. data/node_modules/preact/test-utils/dist/testUtils.module.js.map +0 -1
  203. data/node_modules/preact/test-utils/dist/testUtils.umd.js +0 -2
  204. data/node_modules/preact/test-utils/dist/testUtils.umd.js.map +0 -1
  205. data/node_modules/preact/test-utils/package.json +0 -28
  206. data/node_modules/preact/test-utils/src/index.d.ts +0 -3
  207. data/node_modules/preact/test-utils/src/index.js +0 -118
  208. data/node_modules/preact-render-to-string/LICENSE +0 -21
  209. data/node_modules/preact-render-to-string/README.md +0 -102
  210. data/node_modules/preact-render-to-string/dist/commonjs.js +0 -2
  211. data/node_modules/preact-render-to-string/dist/commonjs.js.map +0 -1
  212. data/node_modules/preact-render-to-string/dist/index.d.ts +0 -16
  213. data/node_modules/preact-render-to-string/dist/index.js +0 -1
  214. data/node_modules/preact-render-to-string/dist/index.js.map +0 -1
  215. data/node_modules/preact-render-to-string/dist/index.mjs +0 -2
  216. data/node_modules/preact-render-to-string/dist/index.module.js +0 -2
  217. data/node_modules/preact-render-to-string/dist/index.module.js.map +0 -1
  218. data/node_modules/preact-render-to-string/dist/jsx-entry.js +0 -2
  219. data/node_modules/preact-render-to-string/dist/jsx-entry.js.map +0 -1
  220. data/node_modules/preact-render-to-string/dist/jsx.d.ts +0 -13
  221. data/node_modules/preact-render-to-string/dist/jsx.js +0 -1
  222. data/node_modules/preact-render-to-string/dist/jsx.js.map +0 -1
  223. data/node_modules/preact-render-to-string/dist/jsx.mjs +0 -2
  224. data/node_modules/preact-render-to-string/dist/jsx.modern.js +0 -2
  225. data/node_modules/preact-render-to-string/dist/jsx.modern.js.map +0 -1
  226. data/node_modules/preact-render-to-string/dist/jsx.module.js +0 -2
  227. data/node_modules/preact-render-to-string/dist/jsx.module.js.map +0 -1
  228. data/node_modules/preact-render-to-string/dist/preact-render-to-string-tests.d.ts +0 -1
  229. data/node_modules/preact-render-to-string/jsx.js +0 -1
  230. data/node_modules/preact-render-to-string/package.json +0 -142
  231. data/node_modules/preact-render-to-string/src/index.d.ts +0 -16
  232. data/node_modules/preact-render-to-string/src/index.js +0 -462
  233. data/node_modules/preact-render-to-string/src/jsx.d.ts +0 -13
  234. data/node_modules/preact-render-to-string/src/jsx.js +0 -76
  235. data/node_modules/preact-render-to-string/src/polyfills.js +0 -8
  236. data/node_modules/preact-render-to-string/src/preact-render-to-string-tests.d.ts +0 -1
  237. data/node_modules/preact-render-to-string/src/util.js +0 -78
  238. data/node_modules/preact-render-to-string/typings.json +0 -5
  239. data/node_modules/pretty-format/.npmignore +0 -3
  240. data/node_modules/pretty-format/LICENSE.md +0 -15
  241. data/node_modules/pretty-format/README.md +0 -94
  242. data/node_modules/pretty-format/index.js +0 -343
  243. data/node_modules/pretty-format/package.json +0 -26
  244. data/node_modules/pretty-format/plugins/ReactElement.js +0 -74
  245. data/node_modules/pretty-format/plugins/ReactTestComponent.js +0 -58
  246. data/node_modules/pretty-format/printString.js +0 -7
  247. data/node_modules/wouter-preact/cjs/index.js +0 -180
  248. data/node_modules/wouter-preact/cjs/matcher.js +0 -72
  249. data/node_modules/wouter-preact/cjs/package.json +0 -1
  250. data/node_modules/wouter-preact/cjs/react-deps.js +0 -75
  251. data/node_modules/wouter-preact/cjs/static-location.js +0 -21
  252. data/node_modules/wouter-preact/cjs/use-location.js +0 -94
  253. data/node_modules/wouter-preact/index.d.ts +0 -110
  254. data/node_modules/wouter-preact/index.js +0 -178
  255. data/node_modules/wouter-preact/matcher.d.ts +0 -30
  256. data/node_modules/wouter-preact/matcher.js +0 -66
  257. data/node_modules/wouter-preact/package.json +0 -33
  258. data/node_modules/wouter-preact/react-deps.js +0 -15
  259. data/node_modules/wouter-preact/static-location.d.ts +0 -16
  260. data/node_modules/wouter-preact/static-location.js +0 -17
  261. data/node_modules/wouter-preact/use-location.d.ts +0 -43
  262. data/node_modules/wouter-preact/use-location.js +0 -86
  263. data/package.json +0 -8
@@ -1,58 +0,0 @@
1
- 'use strict';
2
-
3
- const printString = require('../printString');
4
-
5
- const reactTestInstance = Symbol.for('react.test.json');
6
-
7
- function printChildren(children, print, indent, opts) {
8
- return children.map(child => printInstance(child, print, indent, opts)).join(opts.edgeSpacing);
9
- }
10
-
11
- function printProps(props, print, indent, opts) {
12
- return Object.keys(props).sort().map(name => {
13
- const prop = props[name];
14
- let printed = print(prop);
15
-
16
- if (typeof prop !== 'string') {
17
- if (printed.indexOf('\n') !== -1) {
18
- printed = '{' + opts.edgeSpacing + indent(indent(printed) + opts.edgeSpacing + '}');
19
- } else {
20
- printed = '{' + printed + '}';
21
- }
22
- }
23
-
24
- return opts.spacing + indent(name + '=') + printed;
25
- }).join('');
26
- }
27
-
28
- function printInstance(instance, print, indent, opts) {
29
- if (typeof instance == 'number') {
30
- return print(instance);
31
- } else if (typeof instance === 'string') {
32
- return printString(instance);
33
- }
34
-
35
- let result = '<' + instance.type;
36
-
37
- if (instance.props) {
38
- result += printProps(instance.props, print, indent, opts);
39
- }
40
-
41
- if (instance.children) {
42
- const children = printChildren(instance.children, print, indent, opts);
43
- result += '>' + opts.edgeSpacing + indent(children) + opts.edgeSpacing + '</' + instance.type + '>';
44
- } else {
45
- result += ' />';
46
- }
47
-
48
- return result;
49
- }
50
-
51
- module.exports = {
52
- test(object) {
53
- return object && object.$$typeof === reactTestInstance;
54
- },
55
- print(val, print, indent, opts) {
56
- return printInstance(val, print, indent, opts);
57
- }
58
- };
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- const ESCAPED_CHARACTERS = /(\\|\"|\')/g;
4
-
5
- module.exports = function printString(val) {
6
- return val.replace(ESCAPED_CHARACTERS, '\\$1');
7
- }
@@ -1,180 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var useLocation$1 = require('./use-location.js');
6
- var matcher = require('./matcher.js');
7
- var preact = require('preact');
8
- var hooks = require('preact/hooks');
9
-
10
- /*
11
- * Part 1, Hooks API: useRouter, useRoute and useLocation
12
- */
13
-
14
- // one of the coolest features of `createContext`:
15
- // when no value is provided — default object is used.
16
- // allows us to use the router context as a global ref to store
17
- // the implicitly created router (see `useRouter` below)
18
- const RouterCtx = preact.createContext({});
19
-
20
- const buildRouter = ({
21
- hook = useLocation$1['default'],
22
- base = "",
23
- matcher: matcher$1 = matcher['default'](),
24
- } = {}) => ({ hook, base, matcher: matcher$1 });
25
-
26
- const useRouter = () => {
27
- const globalRef = hooks.useContext(RouterCtx);
28
-
29
- // either obtain the router from the outer context (provided by the
30
- // `<Router /> component) or create an implicit one on demand.
31
- return globalRef.v || (globalRef.v = buildRouter());
32
- };
33
-
34
- const useLocation = () => {
35
- const router = useRouter();
36
- return router.hook(router);
37
- };
38
-
39
- const useRoute = (pattern) => {
40
- const [path] = useLocation();
41
- return useRouter().matcher(pattern, path);
42
- };
43
-
44
- // internal hook used by Link and Redirect in order to perform navigation
45
- const useNavigate = (options) => {
46
- const navRef = hooks.useRef();
47
- const [, navigate] = useLocation();
48
-
49
- navRef.current = () => navigate(options.to || options.href, options);
50
- return navRef;
51
- };
52
-
53
- /*
54
- * Part 2, Low Carb Router API: Router, Route, Link, Switch
55
- */
56
-
57
- const Router = (props) => {
58
- const ref = hooks.useRef();
59
-
60
- // this little trick allows to avoid having unnecessary
61
- // calls to potentially expensive `buildRouter` method.
62
- // https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily
63
- const value = ref.current || (ref.current = { v: buildRouter(props) });
64
-
65
- return preact.createElement(RouterCtx.Provider, {
66
- value,
67
- children: props.children,
68
- });
69
- };
70
-
71
- const Route = ({ path, match, component, children }) => {
72
- const useRouteMatch = useRoute(path);
73
-
74
- // `props.match` is present - Route is controlled by the Switch
75
- const [matches, params] = match || useRouteMatch;
76
-
77
- if (!matches) return null;
78
-
79
- // React-Router style `component` prop
80
- if (component) return preact.createElement(component, { params });
81
-
82
- // support render prop or plain children
83
- return typeof children === "function" ? children(params) : children;
84
- };
85
-
86
- const Link = (props) => {
87
- const navRef = useNavigate(props);
88
- const { base } = useRouter();
89
-
90
- let { to, href = to, children, onClick } = props;
91
-
92
- const handleClick = hooks.useCallback(
93
- (event) => {
94
- // ignores the navigation when clicked using right mouse button or
95
- // by holding a special modifier key: ctrl, command, win, alt, shift
96
- if (
97
- event.ctrlKey ||
98
- event.metaKey ||
99
- event.altKey ||
100
- event.shiftKey ||
101
- event.button !== 0
102
- )
103
- return;
104
-
105
- event.preventDefault();
106
- navRef.current();
107
- onClick && onClick(event);
108
- },
109
- // navRef is a ref so it never changes
110
- // eslint-disable-next-line react-hooks/exhaustive-deps
111
- [onClick]
112
- );
113
-
114
- // wraps children in `a` if needed
115
- const extraProps = {
116
- // handle nested routers and absolute paths
117
- href: href[0] === "~" ? href.slice(1) : base + href,
118
- onClick: handleClick,
119
- to: null,
120
- };
121
- const jsx = preact.isValidElement(children) ? children : preact.createElement("a", props);
122
-
123
- return preact.cloneElement(jsx, extraProps);
124
- };
125
-
126
- const flattenChildren = (children) => {
127
- return Array.isArray(children)
128
- ? [].concat(
129
- ...children.map((c) =>
130
- c && c.type === preact.Fragment
131
- ? flattenChildren(c.props.children)
132
- : flattenChildren(c)
133
- )
134
- )
135
- : [children];
136
- };
137
-
138
- const Switch = ({ children, location }) => {
139
- const { matcher } = useRouter();
140
- const [originalLocation] = useLocation();
141
-
142
- for (const element of flattenChildren(children)) {
143
- let match = 0;
144
-
145
- if (
146
- preact.isValidElement(element) &&
147
- // we don't require an element to be of type Route,
148
- // but we do require it to contain a truthy `path` prop.
149
- // this allows to use different components that wrap Route
150
- // inside of a switch, for example <AnimatedRoute />.
151
- (match = element.props.path
152
- ? matcher(element.props.path, location || originalLocation)
153
- : [true, {}])[0]
154
- )
155
- return preact.cloneElement(element, { match });
156
- }
157
-
158
- return null;
159
- };
160
-
161
- const Redirect = (props) => {
162
- const navRef = useNavigate(props);
163
-
164
- // empty array means running the effect once, navRef is a ref so it never changes
165
- hooks.useLayoutEffect(() => {
166
- navRef.current();
167
- }, []); // eslint-disable-line react-hooks/exhaustive-deps
168
-
169
- return null;
170
- };
171
-
172
- exports.Link = Link;
173
- exports.Redirect = Redirect;
174
- exports.Route = Route;
175
- exports.Router = Router;
176
- exports.Switch = Switch;
177
- exports.default = useRoute;
178
- exports.useLocation = useLocation;
179
- exports.useRoute = useRoute;
180
- exports.useRouter = useRouter;
@@ -1,72 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- // creates a matcher function
6
- function makeMatcher(makeRegexpFn = pathToRegexp) {
7
- let cache = {};
8
-
9
- // obtains a cached regexp version of the pattern
10
- const getRegexp = (pattern) =>
11
- cache[pattern] || (cache[pattern] = makeRegexpFn(pattern));
12
-
13
- return (pattern, path) => {
14
- const { regexp, keys } = getRegexp(pattern || "");
15
- const out = regexp.exec(path);
16
-
17
- if (!out) return [false, null];
18
-
19
- // formats an object with matched params
20
- const params = keys.reduce((params, key, i) => {
21
- params[key.name] = out[i + 1];
22
- return params;
23
- }, {});
24
-
25
- return [true, params];
26
- };
27
- }
28
-
29
- // escapes a regexp string (borrowed from path-to-regexp sources)
30
- // https://github.com/pillarjs/path-to-regexp/blob/v3.0.0/index.js#L202
31
- const escapeRx = (str) => str.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
32
-
33
- // returns a segment representation in RegExp based on flags
34
- // adapted and simplified version from path-to-regexp sources
35
- const rxForSegment = (repeat, optional, prefix) => {
36
- let capture = repeat ? "((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*)" : "([^\\/]+?)";
37
- if (optional && prefix) capture = "(?:\\/" + capture + ")";
38
- return capture + (optional ? "?" : "");
39
- };
40
-
41
- const pathToRegexp = (pattern) => {
42
- const groupRx = /:([A-Za-z0-9_]+)([?+*]?)/g;
43
-
44
- let match = null,
45
- lastIndex = 0,
46
- keys = [],
47
- result = "";
48
-
49
- while ((match = groupRx.exec(pattern)) !== null) {
50
- const [_, segment, mod] = match;
51
-
52
- // :foo [1] ( )
53
- // :foo? [0 - 1] ( o)
54
- // :foo+ [1 - ∞] (r )
55
- // :foo* [0 - ∞] (ro)
56
- const repeat = mod === "+" || mod === "*";
57
- const optional = mod === "?" || mod === "*";
58
- const prefix = optional && pattern[match.index - 1] === "/" ? 1 : 0;
59
-
60
- const prev = pattern.substring(lastIndex, match.index - prefix);
61
-
62
- keys.push({ name: segment });
63
- lastIndex = groupRx.lastIndex;
64
-
65
- result += escapeRx(prev) + rxForSegment(repeat, optional, prefix);
66
- }
67
-
68
- result += escapeRx(pattern.substring(lastIndex));
69
- return { keys, regexp: new RegExp("^" + result + "(?:\\/)?$", "i") };
70
- };
71
-
72
- exports.default = makeMatcher;
@@ -1 +0,0 @@
1
- {"type": "commonjs"}
@@ -1,75 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var preact = require('preact');
6
- var hooks = require('preact/hooks');
7
-
8
-
9
-
10
- Object.defineProperty(exports, 'Fragment', {
11
- enumerable: true,
12
- get: function () {
13
- return preact.Fragment;
14
- }
15
- });
16
- Object.defineProperty(exports, 'cloneElement', {
17
- enumerable: true,
18
- get: function () {
19
- return preact.cloneElement;
20
- }
21
- });
22
- Object.defineProperty(exports, 'createContext', {
23
- enumerable: true,
24
- get: function () {
25
- return preact.createContext;
26
- }
27
- });
28
- Object.defineProperty(exports, 'createElement', {
29
- enumerable: true,
30
- get: function () {
31
- return preact.createElement;
32
- }
33
- });
34
- Object.defineProperty(exports, 'isValidElement', {
35
- enumerable: true,
36
- get: function () {
37
- return preact.isValidElement;
38
- }
39
- });
40
- Object.defineProperty(exports, 'useCallback', {
41
- enumerable: true,
42
- get: function () {
43
- return hooks.useCallback;
44
- }
45
- });
46
- Object.defineProperty(exports, 'useContext', {
47
- enumerable: true,
48
- get: function () {
49
- return hooks.useContext;
50
- }
51
- });
52
- Object.defineProperty(exports, 'useEffect', {
53
- enumerable: true,
54
- get: function () {
55
- return hooks.useEffect;
56
- }
57
- });
58
- Object.defineProperty(exports, 'useLayoutEffect', {
59
- enumerable: true,
60
- get: function () {
61
- return hooks.useLayoutEffect;
62
- }
63
- });
64
- Object.defineProperty(exports, 'useRef', {
65
- enumerable: true,
66
- get: function () {
67
- return hooks.useRef;
68
- }
69
- });
70
- Object.defineProperty(exports, 'useState', {
71
- enumerable: true,
72
- get: function () {
73
- return hooks.useState;
74
- }
75
- });
@@ -1,21 +0,0 @@
1
- 'use strict';
2
-
3
- // Generates static `useLocation` hook. The hook always
4
- // responds with initial path provided.
5
- // You can use this for server-side rendering.
6
- var staticLocation = (path = "/", { record = false } = {}) => {
7
- let hook;
8
- const navigate = (to, { replace } = {}) => {
9
- if (record) {
10
- if (replace) {
11
- hook.history.pop();
12
- }
13
- hook.history.push(to);
14
- }
15
- };
16
- hook = () => [path, navigate];
17
- hook.history = [path];
18
- return hook;
19
- };
20
-
21
- module.exports = staticLocation;
@@ -1,94 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- require('preact');
6
- var hooks = require('preact/hooks');
7
-
8
- /**
9
- * History API docs @see https://developer.mozilla.org/en-US/docs/Web/API/History
10
- */
11
- const eventPopstate = "popstate";
12
- const eventPushState = "pushState";
13
- const eventReplaceState = "replaceState";
14
- const events = [eventPopstate, eventPushState, eventReplaceState];
15
-
16
- var locationHook = ({ base = "" } = {}) => {
17
- const [{ path, search }, update] = hooks.useState(() => ({
18
- path: currentPathname(base),
19
- search: location.search,
20
- })); // @see https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
21
- const prevHash = hooks.useRef(path + search);
22
-
23
- hooks.useEffect(() => {
24
- // this function checks if the location has been changed since the
25
- // last render and updates the state only when needed.
26
- // unfortunately, we can't rely on `path` value here, since it can be stale,
27
- // that's why we store the last pathname in a ref.
28
- const checkForUpdates = () => {
29
- const pathname = currentPathname(base);
30
- const search = location.search;
31
- const hash = pathname + search;
32
-
33
- if (prevHash.current !== hash) {
34
- prevHash.current = hash;
35
- update({ path: pathname, search });
36
- }
37
- };
38
-
39
- events.forEach((e) => addEventListener(e, checkForUpdates));
40
-
41
- // it's possible that an update has occurred between render and the effect handler,
42
- // so we run additional check on mount to catch these updates. Based on:
43
- // https://gist.github.com/bvaughn/e25397f70e8c65b0ae0d7c90b731b189
44
- checkForUpdates();
45
-
46
- return () => events.forEach((e) => removeEventListener(e, checkForUpdates));
47
- }, [base]);
48
-
49
- // the 2nd argument of the `useLocation` return value is a function
50
- // that allows to perform a navigation.
51
- //
52
- // the function reference should stay the same between re-renders, so that
53
- // it can be passed down as an element prop without any performance concerns.
54
- const navigate = hooks.useCallback(
55
- (to, { replace = false } = {}) =>
56
- history[replace ? eventReplaceState : eventPushState](
57
- null,
58
- "",
59
- // handle nested routers and absolute paths
60
- to[0] === "~" ? to.slice(1) : base + to
61
- ),
62
- [base]
63
- );
64
-
65
- return [path, navigate];
66
- };
67
-
68
- // While History API does have `popstate` event, the only
69
- // proper way to listen to changes via `push/replaceState`
70
- // is to monkey-patch these methods.
71
- //
72
- // See https://stackoverflow.com/a/4585031
73
- if (typeof history !== "undefined") {
74
- for (const type of [eventPushState, eventReplaceState]) {
75
- const original = history[type];
76
-
77
- history[type] = function () {
78
- const result = original.apply(this, arguments);
79
- const event = new Event(type);
80
- event.arguments = arguments;
81
-
82
- dispatchEvent(event);
83
- return result;
84
- };
85
- }
86
- }
87
-
88
- const currentPathname = (base, path = location.pathname) =>
89
- !path.toLowerCase().indexOf(base.toLowerCase())
90
- ? path.slice(base.length) || "/"
91
- : "~" + path;
92
-
93
- exports.default = locationHook;
94
- exports.events = events;
@@ -1,110 +0,0 @@
1
- // tslint:disable:no-unnecessary-generics
2
-
3
- import {
4
- JSX,
5
- FunctionComponent,
6
- ComponentType,
7
- ComponentChildren,
8
- VNode,
9
- } from "preact";
10
-
11
- import {
12
- Path,
13
- BaseLocationHook,
14
- HookReturnValue,
15
- HookNavigationOptions,
16
- LocationHook,
17
- } from "./use-location";
18
-
19
- import { DefaultParams, Params, Match, MatcherFn } from "./matcher";
20
-
21
- // re-export types from these modules
22
- export * from "./matcher";
23
- export * from "./use-location";
24
-
25
- /*
26
- * Components: <Route />
27
- */
28
-
29
- export interface RouteComponentProps<T extends DefaultParams = DefaultParams> {
30
- params: T;
31
- }
32
-
33
- export interface RouteProps<T extends DefaultParams = DefaultParams> {
34
- children?: ((params: Params<T>) => ComponentChildren) | ComponentChildren;
35
- path?: Path;
36
- component?: ComponentType<RouteComponentProps<T>>;
37
- }
38
- export function Route<T extends DefaultParams = DefaultParams>(
39
- props: RouteProps<T>
40
- ): VNode<any> | null;
41
-
42
- /*
43
- * Components: <Link /> & <Redirect />
44
- */
45
-
46
- export type NavigationalProps<H extends BaseLocationHook = LocationHook> = (
47
- | { to: Path; href?: never }
48
- | { href: Path; to?: never }
49
- ) &
50
- HookNavigationOptions<H>;
51
-
52
- export type LinkProps<H extends BaseLocationHook = LocationHook> = Omit<
53
- JSX.HTMLAttributes,
54
- "href"
55
- > &
56
- NavigationalProps<H>;
57
-
58
- export type RedirectProps<
59
- H extends BaseLocationHook = LocationHook
60
- > = NavigationalProps<H> & {
61
- children?: never;
62
- };
63
-
64
- export function Link<H extends BaseLocationHook = LocationHook>(
65
- props: LinkProps<H>
66
- ): VNode<any> | null;
67
-
68
- export function Redirect<H extends BaseLocationHook = LocationHook>(
69
- props: RedirectProps<H>
70
- ): VNode<any> | null;
71
-
72
- /*
73
- * Components: <Switch />
74
- */
75
-
76
- export interface SwitchProps {
77
- location?: string;
78
- children: Array<VNode<RouteProps>>;
79
- }
80
- export const Switch: FunctionComponent<SwitchProps>;
81
-
82
- /*
83
- * Components: <Router />
84
- */
85
-
86
- export interface RouterProps {
87
- hook: BaseLocationHook;
88
- base: Path;
89
- matcher: MatcherFn;
90
- }
91
- export const Router: FunctionComponent<
92
- Partial<RouterProps> & {
93
- children: ComponentChildren;
94
- }
95
- >;
96
-
97
- /*
98
- * Hooks
99
- */
100
- export function useRouter(): RouterProps;
101
-
102
- export function useRoute<T extends DefaultParams = DefaultParams>(
103
- pattern: Path
104
- ): Match<T>;
105
-
106
- export function useLocation<
107
- H extends BaseLocationHook = LocationHook
108
- >(): HookReturnValue<H>;
109
-
110
- // tslint:enable:no-unnecessary-generics