@fruit-ui/router 1.1.1 → 1.1.2

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/README.md CHANGED
@@ -10,6 +10,7 @@ This is a component producer which takes in three props.
10
10
  - `routes` is an object mapping from paths (strings) to `Route`s. `Route`s are objects with a `route` method (which generates the template/component for that route, and is allowed to be asynchronous.) You can also have a 'wildcard' (*) route whose `route` method can take in the name of the current route as a prop. Each `Route` can also have an attribute `title` which will set the window's title for that path.
11
11
  - `scrollOptions` is an optional prop. It is an object describing how scrolling should be handled when the Router component rerenders. It has two attributes: `hashed` and `unhashed`. These respectively describe how scrolling should be handled given hashed (`?page=about#contact`) and unhashed (`?page=about`) paths. In each of these, you may select any options from the [scrollIntoView](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) method, as well as an option `to: {x: number, y: number}`, which can take coordinates (in which case `scrollTo` will be used rather than `scrollIntoView`). Either can also be `false` in which case no scrolling will occur. The default value is `{hashed: {}, unhashed: {to: {x: 0, y: 0}}}`.
12
12
  - `asyncLoader` is an optional prop describing what should be displayed while an asynchronous route is loading. This is only used on the first route visited in a session; in subsequent routes, the router displays the old route until the new async route has fully loaded. The default value is `{}` (an empty `div`).
13
+ - `initFunction` is an optional prop. It is a function that runs inside the router's `state()` function and can be used for initializing paradigms like context.
13
14
 
14
15
  Here is an example router:
15
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fruit-ui/router",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "A basic router component for FRUIT",
5
5
  "main": "src/router.js",
6
6
  "homepage": "https://asantagata.github.io/fruit-ui/?page=router-index",
package/src/router.js CHANGED
@@ -56,13 +56,15 @@ function handleScrolling(element, scrollOptions) {
56
56
  * @param {Object.<string, {route: () => object, title?: string}>} routes - the collection of routes.
57
57
  * @param {object | false} [scrollOptions] - scrolling options.
58
58
  * @param {object} [asyncLoader] - loader to use while processing initial async resources
59
+ * @param {() => void} [initFunction] - function to run on initialization.
59
60
  * @returns {object} a component.
60
61
  */
61
- function Router(routes, scrollOptions = {hashed: {}, unhashed: {to: {x: 0, y: 0}}}, asyncLoader = {}) {
62
+ function Router(routes, scrollOptions = {hashed: {}, unhashed: {to: {x: 0, y: 0}}}, asyncLoader = {}, initFunction = () => {}) {
62
63
  return {
63
64
  state() {
64
65
  const page = getPage();
65
66
  const route = getRoute(routes, page);
67
+ initFunction.call(this);
66
68
  return {
67
69
  element: (() => {
68
70
  if (Object.getPrototypeOf(route.route).constructor.name === "AsyncFunction") {