@fruit-ui/router 1.0.2 → 1.0.3

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 (3) hide show
  1. package/README.md +18 -3
  2. package/package.json +1 -1
  3. package/src/router.js +36 -2
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This is a basic router component for [FRUIT](https://www.npmjs.com/package/@fruit-ui/core). It uses search params for routes, which minimizes server-side requirements.
4
4
 
5
- This router provides four significant pieces:
5
+ This router provides these significant pieces:
6
6
 
7
7
  ## The Router component
8
8
 
@@ -29,7 +29,11 @@ const router = router.Router({
29
29
 
30
30
  ## The `navigate` function
31
31
 
32
- The `navigate` function takes in a path and navigates to that path. Navigation is done with `history.pushState` so it is compatible with the browser forward/back methods. You can navigate to hashed paths (i.e., `navigate('/about#contact')`) to automatically scroll to a certain element ID, depending on scroll settings.
32
+ The `navigate` function takes in a path and navigates to that path. Navigation is done with `history.pushState` so it is compatible with the browser forward/back methods. You can navigate to hashed paths (i.e., `navigate('/about#contact')`) to automatically scroll to a certain element ID, depending on scroll settings. Navigate does nothing (and no `pagechange` event is dispatched) if the given and existing paths are the same.
33
+
34
+ ### The `navigateHash` function
35
+
36
+ The `navigateHash` function navigates to a different hash on the same page, i.e., `navigate('#contact')`. This is done with `history.pushState` so it is compatible with the browser forward/back methods. If the given and existing hashes are the same, the `hashchange` event is still dispatched (see below).
33
37
 
34
38
  ## The `pagechange` event
35
39
 
@@ -37,7 +41,18 @@ Elements with `data-receive-page-changes` enabled will receive the `pagechange`
37
41
 
38
42
  This attribute can be given using `dataset: {receivePageChanges: true}`.
39
43
 
44
+ ### The `hashchange` event
45
+
46
+ Similarly, elements can have `data-receive-hash-changes` to receive the `hashchange` event, on which `event.detail.hash` can be accessed as the new hash. (This is not an instance of the canonical `hashchange` event, which `navigateHash` and `navigate` do not fire.)
47
+
48
+ Note that if the browser back/forward buttons are used, the `pagechange` event will be fired even if only the hash was changed.
49
+
40
50
  ## The `getPage` function
51
+
41
52
  This returns the current path. Note that hashes and opening slashes are automatically removed, so after `navigate('/about#contact')`, a `getPage()` call will return `'about'`.
42
53
 
43
- More thorough documentation for FRUIT Router will be available shortly [here](https://asantagata.github.io/fruit-ui/?page=router-index).
54
+ ### The `getHash` function
55
+
56
+ This returns the current hash. Note that opening hashes (#) are automatically removed.
57
+
58
+ More thorough documentation for FRUIT Router is available [here](https://asantagata.github.io/fruit-ui/?page=router-index).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fruit-ui/router",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
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
@@ -81,7 +81,7 @@ function Router(routes, scrollOptions = {hashed: {}, unhashed: {to: {x: 0, y: 0}
81
81
  children: typeof this.state.element === 'object' ?
82
82
  {...this.state.element, key: `route-${getPage()}`} : this.state.element,
83
83
  id: 'router',
84
- dataset: { receivePageChanges: true },
84
+ dataset: { receivePageChanges: true, receiveHashChanges: true },
85
85
  on: {
86
86
  pagechange() {
87
87
  const page = getPage();
@@ -99,6 +99,9 @@ function Router(routes, scrollOptions = {hashed: {}, unhashed: {to: {x: 0, y: 0}
99
99
  }
100
100
  document.title = getRoute(routes, getPage()).title ?? document.title;
101
101
  },
102
+ hashchange() {
103
+ handleScrolling(this.element, scrollOptions);
104
+ },
102
105
  mount() {
103
106
  document.title = getRoute(routes, getPage()).title ?? document.title;
104
107
  window.onpopstate = () => broadcastPageChange(getPage());
@@ -149,4 +152,35 @@ function broadcastPageChange(page) {
149
152
  });
150
153
  }
151
154
 
152
- export { navigate, Router, getPage };
155
+ /**
156
+ * Navigates to a hash.
157
+ * @param {string} hash - the hash.
158
+ */
159
+ function navigateHash(hash) {
160
+ if (hash.slice(1) === getHash()) return;
161
+ const url = new URL(window.location.href);
162
+ url.hash = hash;
163
+ window.history.pushState({}, '', url);
164
+ broadcastHashChange(hash);
165
+ }
166
+
167
+ /**
168
+ * Get the current hash.
169
+ * @returns {string}
170
+ */
171
+ function getHash() {
172
+ return window.location.hash.slice(1);
173
+ }
174
+
175
+ /**
176
+ * Broadcasts that a hash updated.
177
+ * @param {string} hash - the hash.
178
+ */
179
+ function broadcastHashChange(hash) {
180
+ const event = new CustomEvent('hashchange', {detail: {hash}});
181
+ Array.from(document.querySelectorAll(`[data-receive-hash-changes]`)).forEach(target => {
182
+ target.dispatchEvent(event);
183
+ });
184
+ }
185
+
186
+ export { navigate, Router, getPage, navigateHash, getHash };