@lightningjs/blits 1.30.1 → 1.30.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.
Files changed (149) hide show
  1. package/.editorconfig +7 -7
  2. package/.eslintignore +1 -1
  3. package/.eslintrc.cjs +65 -65
  4. package/.github/workflows/comment-lint-results.yml +124 -124
  5. package/.github/workflows/comment-test-results.yml +108 -108
  6. package/.github/workflows/deploy-docs.yml +27 -27
  7. package/.github/workflows/run-blits-tests.yml +141 -141
  8. package/.github/workflows/run-lint.yml +127 -127
  9. package/.husky/pre-commit +1 -1
  10. package/.vscode/extensions.json +7 -7
  11. package/.vscode/settings.json +9 -9
  12. package/CHANGELOG.md +1019 -1012
  13. package/COPYING +1 -0
  14. package/LICENSE +202 -202
  15. package/NOTICE +12 -12
  16. package/README.md +33 -33
  17. package/bin/index.js +8 -8
  18. package/blits.d.ts +6 -6
  19. package/docs/README.md +19 -19
  20. package/docs/_sidebar.md +37 -37
  21. package/docs/built-in/directives.md +50 -50
  22. package/docs/built-in/for-loop.md +203 -203
  23. package/docs/built-in/layout.md +168 -168
  24. package/docs/components/component_state.md +90 -90
  25. package/docs/components/computed_properties.md +45 -45
  26. package/docs/components/lifecycle_events.md +42 -42
  27. package/docs/components/methods.md +45 -45
  28. package/docs/components/props.md +56 -56
  29. package/docs/components/user_input.md +186 -186
  30. package/docs/components/utility_methods.md +320 -320
  31. package/docs/components/watchers.md +34 -34
  32. package/docs/essentials/application_root.md +19 -19
  33. package/docs/essentials/components.md +68 -68
  34. package/docs/essentials/displaying_images.md +67 -67
  35. package/docs/essentials/displaying_text.md +135 -135
  36. package/docs/essentials/element_attributes.md +195 -195
  37. package/docs/essentials/settings.md +117 -117
  38. package/docs/essentials/template_syntax.md +83 -83
  39. package/docs/getting_started/file_structure.md +58 -58
  40. package/docs/getting_started/getting_started.md +45 -45
  41. package/docs/getting_started/intro.md +20 -20
  42. package/docs/index.html +47 -47
  43. package/docs/performance/lazy-loading.md +161 -161
  44. package/docs/plugins/global_app_state.md +98 -98
  45. package/docs/plugins/language.md +172 -172
  46. package/docs/plugins/text-to-speech-announcer.md +119 -119
  47. package/docs/plugins/theme.md +176 -176
  48. package/docs/router/basics.md +175 -175
  49. package/docs/router/hooks.md +70 -70
  50. package/docs/router/transitions.md +146 -146
  51. package/docs/sidebar.json +171 -171
  52. package/docs/transitions_animations/transitions.md +150 -150
  53. package/index.d.ts +1099 -1099
  54. package/index.js +28 -28
  55. package/jsconfig.json +12 -12
  56. package/package.json +56 -56
  57. package/public/assets/fps_sprite.base64.js +1 -1
  58. package/scripts/postpublish.js +59 -59
  59. package/scripts/prepublishOnly.js +84 -84
  60. package/scripts/prepush.js +78 -78
  61. package/src/announcer/announcer.js +162 -162
  62. package/src/announcer/speechSynthesis.js +107 -107
  63. package/src/application.js +100 -100
  64. package/src/component/base/announcer.js +27 -27
  65. package/src/component/base/events.js +69 -69
  66. package/src/component/base/index.js +33 -33
  67. package/src/component/base/methods.js +244 -244
  68. package/src/component/base/router.js +41 -41
  69. package/src/component/base/timeouts_intervals.js +99 -99
  70. package/src/component/base/utils.js +72 -72
  71. package/src/component/setup/computed.js +48 -48
  72. package/src/component/setup/index.js +62 -62
  73. package/src/component/setup/input.js +30 -30
  74. package/src/component/setup/methods.js +37 -37
  75. package/src/component/setup/props.js +62 -62
  76. package/src/component/setup/props.test.js +238 -238
  77. package/src/component/setup/routes.js +42 -42
  78. package/src/component/setup/state.js +59 -59
  79. package/src/component/setup/watch.js +35 -35
  80. package/src/component.js +437 -437
  81. package/src/component.test.js +388 -388
  82. package/src/components/Circle.js +73 -73
  83. package/src/components/Circle.orig.js +31 -31
  84. package/src/components/FPScounter.js +1510 -1510
  85. package/src/components/FPScounter.orig.js +121 -121
  86. package/src/components/RouterView.js +81 -81
  87. package/src/components/RouterView.orig.js +54 -54
  88. package/src/components/Sprite.js +94 -94
  89. package/src/components/Sprite.orig.js +58 -58
  90. package/src/components/index.js +28 -28
  91. package/src/components/index.orig.js +28 -28
  92. package/src/constants.js +21 -21
  93. package/src/engine.js +1 -1
  94. package/src/engines/L3/element.js +744 -744
  95. package/src/engines/L3/element.test.js +579 -579
  96. package/src/engines/L3/fontLoader.js +38 -38
  97. package/src/engines/L3/index.js +7 -7
  98. package/src/engines/L3/launch.js +123 -123
  99. package/src/engines/L3/shaderLoader.js +12 -12
  100. package/src/engines/index.js +1 -1
  101. package/src/focus.js +90 -90
  102. package/src/helpers/deepEqualArray.js +50 -50
  103. package/src/helpers/deepEqualArray.test.js +127 -127
  104. package/src/helpers/fetchJson.js +34 -34
  105. package/src/helpers/sequence.js +26 -26
  106. package/src/launch.js +123 -123
  107. package/src/lib/blitsfileconverter/blitsfileconverter.js +52 -52
  108. package/src/lib/codegenerator/generator.js +788 -788
  109. package/src/lib/codegenerator/generator.test.js +3801 -3801
  110. package/src/lib/colors/colors.js +92 -92
  111. package/src/lib/colors/colors.test.js +290 -290
  112. package/src/lib/colors/htmlColors.js +168 -168
  113. package/src/lib/componentId.js +49 -49
  114. package/src/lib/componentId.test.js +84 -84
  115. package/src/lib/eventListeners.js +123 -123
  116. package/src/lib/eventListeners.test.js +236 -236
  117. package/src/lib/hooks.js +59 -59
  118. package/src/lib/lifecycle.js +85 -85
  119. package/src/lib/log.js +117 -117
  120. package/src/lib/precompiler/precompiler.js +73 -73
  121. package/src/lib/reactivity/effect.js +93 -93
  122. package/src/lib/reactivity/effect.test.js +35 -35
  123. package/src/lib/reactivity/reactive.js +208 -208
  124. package/src/lib/reactivity/reactive.test.js +27 -27
  125. package/src/lib/reactivityguard/computedprops.js +218 -218
  126. package/src/lib/symbols.js +108 -108
  127. package/src/lib/templateparser/parser.js +386 -386
  128. package/src/lib/templateparser/parser.test.js +1411 -1411
  129. package/src/lib/utils.js +27 -27
  130. package/src/plugin.js +27 -27
  131. package/src/plugins/appstate.js +26 -26
  132. package/src/plugins/index.js +20 -20
  133. package/src/plugins/language.js +152 -152
  134. package/src/plugins/log.js +25 -25
  135. package/src/plugins/theme.js +94 -94
  136. package/src/router/router.js +514 -514
  137. package/src/router/router.test.js +473 -473
  138. package/src/router/transitions/fadeIn.js +27 -27
  139. package/src/router/transitions/fadeInOut.js +33 -33
  140. package/src/router/transitions/index.js +21 -21
  141. package/src/router/transitions/slideIn.js +28 -28
  142. package/src/router/transitions/slideInOut.js +33 -33
  143. package/src/settings.js +40 -40
  144. package/vite/blitsFileConverter.js +67 -67
  145. package/vite/index.js +28 -28
  146. package/vite/msdfGenerator.js +269 -269
  147. package/vite/preCompiler.js +42 -42
  148. package/vite/reactivityGuard.js +45 -45
  149. package/vscode/data/template-attributes.json +589 -589
@@ -1,175 +1,175 @@
1
- # Router Basics
2
-
3
- The Blits framework comes with a built-in, performant router that eliminates the need to pull in any additional dependencies into your App.
4
-
5
- Before we dive into the details, let's first touch base on some key concepts.
6
-
7
- ## Key concepts
8
-
9
- ### Pages are regular Blits Components
10
-
11
- The router in Blits is primarily used to navigate between Pages in your App. In the context of a Blits App, a Page is just a regular Blits Component. Usually it will be a Component that fills the entire screen. But it is also possible to have only a smaller portion of the screen contain dynamic routed content.
12
-
13
- ### URL hash
14
-
15
- When navigating to a page, the hash in the URL of the browser will automatically be updated (i.e. `http://localhost:8080/#my/page`) to match the current page. The hash can also be used to _deep-link_ directly into your application. Upon first load, the router functionality will evaluate the URL hash and try to match that to a defined route. If a matching route is found, it will load that page as deep linked content.
16
-
17
- ## Defining Routes
18
-
19
- Routes are defined in the main Application component, as an Array under the `routes` key of the configuration object.
20
-
21
- Each route in this array, is an Object literal that includes the following key properties:
22
-
23
- - `path` - the url hash to the route (i.e. `/my/page`)
24
- - `component` - the Blits component to render for the route. This can be a direct reference, but also a promise or a dynamic import that resolves to a Blits component
25
- - `hooks` (optional) - hooks such as `before` can be defined to execute code before navigating to the route
26
- - `options` (optional) - additional options defining route behaviour, such as `keepAlive` and `inHistory`
27
- - `transitions` (optional) - used to define custom transitions between pages
28
-
29
- ### Dynamic routes with params
30
-
31
- Besides static routes such as `/account` and `/settings/wifi/advanced`, the Blits router also supports dynamic routes where URI parts can contain params.
32
-
33
- To define a param in a route path, prefix a string with a colon (`:`), followed by the name of the param, e.g. `/movies/:genre/:id`. This route path will match a URI hash such as `#/movies/sci-fi/65281918`. The router will take the params and inject them as a `prop` into the Page that's being navigated to.
34
-
35
- To access these props in the Page component, they must first be defined in the Page component as _props_ first (i.e. `props: ['genre', 'id']`). Once defined, the values (`sci-fi` and `65281918`) in this case, will be available on the `this` scope as `this.genre` and `this.id` (or as `$genre` and `$id` when used in the template), as with any regular Component.
36
-
37
- #### Using query parameters in routes
38
-
39
- According to the browser specifications, query parameters are not part of a URL hash. This means that query parameters should be placed _before_ the URL hash (i.e. `http://localhost:5173?id=100&name=john#/my/page/hash`) and implies that "real" query parameters are _not_ part of the route paths.
40
-
41
- In this case `id` and `name` won't automatically be made available as props inside a Blits component. They can however be retrieved using `document.location.search` in combination with `new URLSearchParams`, and can be used in an App's `index.js` to set dynamic launch settings for example.
42
-
43
- Sometimes you may actually want pass custom data into a Blits component, without it being part of the dynamic route path or `data`-object during navigation. For these cases Blits allows you to use query parameters as part of a route (i.e. `#/series/simpsons/5/10?id=100&name=john`).
44
-
45
- This URL hash will match the route `/series/:show/:season/:episode` and it will pass `id` and `name` as additional props into the Page component. Note: similar to dynamic path params, route query params should also be defined in the Page component as _props_ first (i.e. `props: ['id', 'name']`) in order to be accessed on the `this`-scope.
46
-
47
- ## Router view
48
-
49
- The routes are loaded into a `<RouterView />` component, which acts as a placeholder for the routed Page components.
50
-
51
- The `<RouterView />` component can be positioned and sized as you would expect from a normal Blits Component.
52
-
53
-
54
- ```js
55
- export default Blits.Application({
56
- template: `
57
- <Element>
58
- <RouterView x="300" y="200" w="1520" h="680" />
59
- </Element>
60
- `,
61
- routes: [
62
- { path: '/', component: Home },
63
- { path: '/details', component: () => import('./pages/Details.js'), },
64
- { path: '/account',
65
- component: () => {
66
- // imagine this is an API call or some other async action
67
- return new Promise((resolve) => {
68
- resolve(Account)
69
- })
70
- }
71
- }
72
- ],
73
- // ...
74
- })
75
- ```
76
-
77
- ## Navigation
78
-
79
- Each component in a routed Blits app has a `this.$router` object that provides access to the Router instance. It can be used to programmatically navigate to pages, by calling the `to()` method on it.
80
-
81
- The `this.$router.to()`-method accepts 3 arguments:
82
-
83
- - `path` - the path of the route to navigate to
84
- - `data` (optional) - an object with data to pass into the page as `props`
85
- - `options` (optional) - additional options defining route behaviour, such as `keepAlive` and `inHistory` (these will overwrite any options specified for a route in the routes array)
86
-
87
- ```js
88
- export default Blits.Component('Poster', {
89
- input: {
90
- enter() {
91
- this.$router.to('/movie/details', {id: '1', img: 'details.png'}, {inHistory: false})
92
- }
93
- }
94
- })
95
- ```
96
-
97
- Whenever you navigate to a new page, the URL hash will automatically be updated. Unless specified otherwise, navigating to a new page, will add that route to the history stack. The `back` input action is automatically wired up to navigate back down the history stack.
98
-
99
- ## Deeplinking
100
-
101
- The Router plugin has support for deeplinking. When the App is loaded with a URL hash (i.e. `#/pages/settings/network`), the router will try to match that hash to a defined route. This means that your app can be deep linked into, by simply providing the correct URL hash.
102
-
103
- This is an important feature for several reasons:
104
-
105
- - It will allow external sources (like operators and UIs) to link directly to a specific page in your app (i.e. the details page of a specific movie)
106
- - If dynamic routes are used, the App only loads what is needed, to keep memory usage low and the initial load time fast
107
- - Data provided in the URL hash can still be used to set the initial state of the App
108
-
109
- ## Backtracking
110
-
111
- When a user enters your App via a deeplink, there is technically no history available. By default, this means that a Back keypress goes straight to
112
- the App's `root`-route (i.e. the `Blits.Application()`-component).
113
-
114
- In some cases, you may want walk back down the logical path of the deeplinked page instead, and navigate to the first existing parent route (i.e. go from `/movies/comedy/american-pie` to `/movies/comedy`).
115
-
116
- By setting the route option `backtrack` to `true` in the route definition (or in the `this.$router.to()`-method), the router will recursively remove the last part of the route hash, until it finds a valid path to navigate to.
117
-
118
- ```js
119
- {
120
- path: '/examples/router-hooks/episode/:id',
121
- component: Episode,
122
- transition: PageTransitions.slideInOutUp,
123
- options: {
124
- backtrack: true,
125
- },
126
- },
127
- {
128
- path: '/examples/router-hooks/episode',
129
- component: EpisodesOverview,
130
- },
131
- ```
132
-
133
- In the example above, the `backtrack` option is set to `true` for the `/examples/router-hooks/episode/:id` route. When the user navigates to `/examples/router-hooks/episode/1`, the `back` input action will navigate to `/examples/router-hooks/episode` instead of exiting the App.
134
-
135
- ## Router API
136
-
137
- The Router API provides several useful methods and properties for managing routes and navigation:
138
-
139
- - `this.$router.to()` - navigating to a different location (as discussed above)
140
- - `this.$router.back()` - programmatically navigate back down the history stack
141
- - `this.$router.currentRoute` - retrieve the current route
142
- - `this.$router.routes` - retrieve the list of all routes
143
- - `this.$router.navigating` - retrieve the current navigating state
144
- - `this.$router.state` - reactive router state (see below)
145
-
146
- ### Reactive router state
147
-
148
- The reactive router state (`this.$router.state`) can be useful in situations where you want to hook up reactive changes in your App to Route changes.
149
-
150
- The `state` variable on the `this.$router` object returns a reactive object with 2 keys: `path` and `navigating`. The values of these keys will automatically update when the router navigates from one page to another.
151
-
152
- The router state changes can be used in a Blits template, they can be _watched_ and they can be used in generic business logic, as demonstrated in the example below.
153
-
154
- ```js
155
- export default Blits.Component('MyComponent', {
156
- template: `
157
- <Element w="1920" h="1080">
158
- <!-- dynamically display the current path -->
159
- <Text x="100" h="100" :content="$$router.state.path" />
160
- <!-- show loading text when router is navigating -->
161
- <Text :show="$showLoader">Loading ...</Text>
162
- </Element>
163
- `,
164
- watch: {
165
- '$router.state.path'(v, old) {
166
- Log.info(`Router Path changed from ${old} to ${v}`)
167
- }
168
- },
169
- computed: {
170
- showLoader() {
171
- return this.$router.state.navigating
172
- }
173
- }
174
- })
175
- ```
1
+ # Router Basics
2
+
3
+ The Blits framework comes with a built-in, performant router that eliminates the need to pull in any additional dependencies into your App.
4
+
5
+ Before we dive into the details, let's first touch base on some key concepts.
6
+
7
+ ## Key concepts
8
+
9
+ ### Pages are regular Blits Components
10
+
11
+ The router in Blits is primarily used to navigate between Pages in your App. In the context of a Blits App, a Page is just a regular Blits Component. Usually it will be a Component that fills the entire screen. But it is also possible to have only a smaller portion of the screen contain dynamic routed content.
12
+
13
+ ### URL hash
14
+
15
+ When navigating to a page, the hash in the URL of the browser will automatically be updated (i.e. `http://localhost:8080/#my/page`) to match the current page. The hash can also be used to _deep-link_ directly into your application. Upon first load, the router functionality will evaluate the URL hash and try to match that to a defined route. If a matching route is found, it will load that page as deep linked content.
16
+
17
+ ## Defining Routes
18
+
19
+ Routes are defined in the main Application component, as an Array under the `routes` key of the configuration object.
20
+
21
+ Each route in this array, is an Object literal that includes the following key properties:
22
+
23
+ - `path` - the url hash to the route (i.e. `/my/page`)
24
+ - `component` - the Blits component to render for the route. This can be a direct reference, but also a promise or a dynamic import that resolves to a Blits component
25
+ - `hooks` (optional) - hooks such as `before` can be defined to execute code before navigating to the route
26
+ - `options` (optional) - additional options defining route behaviour, such as `keepAlive` and `inHistory`
27
+ - `transitions` (optional) - used to define custom transitions between pages
28
+
29
+ ### Dynamic routes with params
30
+
31
+ Besides static routes such as `/account` and `/settings/wifi/advanced`, the Blits router also supports dynamic routes where URI parts can contain params.
32
+
33
+ To define a param in a route path, prefix a string with a colon (`:`), followed by the name of the param, e.g. `/movies/:genre/:id`. This route path will match a URI hash such as `#/movies/sci-fi/65281918`. The router will take the params and inject them as a `prop` into the Page that's being navigated to.
34
+
35
+ To access these props in the Page component, they must first be defined in the Page component as _props_ first (i.e. `props: ['genre', 'id']`). Once defined, the values (`sci-fi` and `65281918`) in this case, will be available on the `this` scope as `this.genre` and `this.id` (or as `$genre` and `$id` when used in the template), as with any regular Component.
36
+
37
+ #### Using query parameters in routes
38
+
39
+ According to the browser specifications, query parameters are not part of a URL hash. This means that query parameters should be placed _before_ the URL hash (i.e. `http://localhost:5173?id=100&name=john#/my/page/hash`) and implies that "real" query parameters are _not_ part of the route paths.
40
+
41
+ In this case `id` and `name` won't automatically be made available as props inside a Blits component. They can however be retrieved using `document.location.search` in combination with `new URLSearchParams`, and can be used in an App's `index.js` to set dynamic launch settings for example.
42
+
43
+ Sometimes you may actually want pass custom data into a Blits component, without it being part of the dynamic route path or `data`-object during navigation. For these cases Blits allows you to use query parameters as part of a route (i.e. `#/series/simpsons/5/10?id=100&name=john`).
44
+
45
+ This URL hash will match the route `/series/:show/:season/:episode` and it will pass `id` and `name` as additional props into the Page component. Note: similar to dynamic path params, route query params should also be defined in the Page component as _props_ first (i.e. `props: ['id', 'name']`) in order to be accessed on the `this`-scope.
46
+
47
+ ## Router view
48
+
49
+ The routes are loaded into a `<RouterView />` component, which acts as a placeholder for the routed Page components.
50
+
51
+ The `<RouterView />` component can be positioned and sized as you would expect from a normal Blits Component.
52
+
53
+
54
+ ```js
55
+ export default Blits.Application({
56
+ template: `
57
+ <Element>
58
+ <RouterView x="300" y="200" w="1520" h="680" />
59
+ </Element>
60
+ `,
61
+ routes: [
62
+ { path: '/', component: Home },
63
+ { path: '/details', component: () => import('./pages/Details.js'), },
64
+ { path: '/account',
65
+ component: () => {
66
+ // imagine this is an API call or some other async action
67
+ return new Promise((resolve) => {
68
+ resolve(Account)
69
+ })
70
+ }
71
+ }
72
+ ],
73
+ // ...
74
+ })
75
+ ```
76
+
77
+ ## Navigation
78
+
79
+ Each component in a routed Blits app has a `this.$router` object that provides access to the Router instance. It can be used to programmatically navigate to pages, by calling the `to()` method on it.
80
+
81
+ The `this.$router.to()`-method accepts 3 arguments:
82
+
83
+ - `path` - the path of the route to navigate to
84
+ - `data` (optional) - an object with data to pass into the page as `props`
85
+ - `options` (optional) - additional options defining route behaviour, such as `keepAlive` and `inHistory` (these will overwrite any options specified for a route in the routes array)
86
+
87
+ ```js
88
+ export default Blits.Component('Poster', {
89
+ input: {
90
+ enter() {
91
+ this.$router.to('/movie/details', {id: '1', img: 'details.png'}, {inHistory: false})
92
+ }
93
+ }
94
+ })
95
+ ```
96
+
97
+ Whenever you navigate to a new page, the URL hash will automatically be updated. Unless specified otherwise, navigating to a new page, will add that route to the history stack. The `back` input action is automatically wired up to navigate back down the history stack.
98
+
99
+ ## Deeplinking
100
+
101
+ The Router plugin has support for deeplinking. When the App is loaded with a URL hash (i.e. `#/pages/settings/network`), the router will try to match that hash to a defined route. This means that your app can be deep linked into, by simply providing the correct URL hash.
102
+
103
+ This is an important feature for several reasons:
104
+
105
+ - It will allow external sources (like operators and UIs) to link directly to a specific page in your app (i.e. the details page of a specific movie)
106
+ - If dynamic routes are used, the App only loads what is needed, to keep memory usage low and the initial load time fast
107
+ - Data provided in the URL hash can still be used to set the initial state of the App
108
+
109
+ ## Backtracking
110
+
111
+ When a user enters your App via a deeplink, there is technically no history available. By default, this means that a Back keypress goes straight to
112
+ the App's `root`-route (i.e. the `Blits.Application()`-component).
113
+
114
+ In some cases, you may want walk back down the logical path of the deeplinked page instead, and navigate to the first existing parent route (i.e. go from `/movies/comedy/american-pie` to `/movies/comedy`).
115
+
116
+ By setting the route option `backtrack` to `true` in the route definition (or in the `this.$router.to()`-method), the router will recursively remove the last part of the route hash, until it finds a valid path to navigate to.
117
+
118
+ ```js
119
+ {
120
+ path: '/examples/router-hooks/episode/:id',
121
+ component: Episode,
122
+ transition: PageTransitions.slideInOutUp,
123
+ options: {
124
+ backtrack: true,
125
+ },
126
+ },
127
+ {
128
+ path: '/examples/router-hooks/episode',
129
+ component: EpisodesOverview,
130
+ },
131
+ ```
132
+
133
+ In the example above, the `backtrack` option is set to `true` for the `/examples/router-hooks/episode/:id` route. When the user navigates to `/examples/router-hooks/episode/1`, the `back` input action will navigate to `/examples/router-hooks/episode` instead of exiting the App.
134
+
135
+ ## Router API
136
+
137
+ The Router API provides several useful methods and properties for managing routes and navigation:
138
+
139
+ - `this.$router.to()` - navigating to a different location (as discussed above)
140
+ - `this.$router.back()` - programmatically navigate back down the history stack
141
+ - `this.$router.currentRoute` - retrieve the current route
142
+ - `this.$router.routes` - retrieve the list of all routes
143
+ - `this.$router.navigating` - retrieve the current navigating state
144
+ - `this.$router.state` - reactive router state (see below)
145
+
146
+ ### Reactive router state
147
+
148
+ The reactive router state (`this.$router.state`) can be useful in situations where you want to hook up reactive changes in your App to Route changes.
149
+
150
+ The `state` variable on the `this.$router` object returns a reactive object with 2 keys: `path` and `navigating`. The values of these keys will automatically update when the router navigates from one page to another.
151
+
152
+ The router state changes can be used in a Blits template, they can be _watched_ and they can be used in generic business logic, as demonstrated in the example below.
153
+
154
+ ```js
155
+ export default Blits.Component('MyComponent', {
156
+ template: `
157
+ <Element w="1920" h="1080">
158
+ <!-- dynamically display the current path -->
159
+ <Text x="100" h="100" :content="$$router.state.path" />
160
+ <!-- show loading text when router is navigating -->
161
+ <Text :show="$showLoader">Loading ...</Text>
162
+ </Element>
163
+ `,
164
+ watch: {
165
+ '$router.state.path'(v, old) {
166
+ Log.info(`Router Path changed from ${old} to ${v}`)
167
+ }
168
+ },
169
+ computed: {
170
+ showLoader() {
171
+ return this.$router.state.navigating
172
+ }
173
+ }
174
+ })
175
+ ```
@@ -1,70 +1,70 @@
1
- # Router Hooks
2
-
3
- The Blits router supports hooks that allow you to execute code at specific points during the navigation process.
4
-
5
- Hooks are defined per route in the optional `hooks` key within the Route configuration object.
6
-
7
- > Currently, the Blits router only supports the before hook, with additional hooks planned for future releases.
8
-
9
- ## Before hook
10
-
11
- The before hook is executed before the navigation occurs. It can be used for checks, such as verifying user authentication for a specific route, for pre-fetching data, or for executing logic like sending stats beacons.
12
-
13
- The before hook is defined as a function that receives 2 arguments: the Route object that is being navigated `to`. And the Route object that is being navigated away `from`.
14
-
15
- ```js
16
- export default Blits.Application({
17
- // ...
18
- routes: [
19
- {
20
- path: '/',
21
- component: Home,
22
- hooks: {
23
- before(to, from) {
24
- // custom logic
25
- }
26
- }
27
- }
28
- ],
29
- // ...
30
- })
31
- ```
32
-
33
- The before hook can optionally return a value, which will influence the behaviour of the route change:
34
-
35
- - when `false` is returned, the router will fail to finish the routing, and act as if _no matching route was found_
36
- - when a `string` is returned, the router will interpret this as a _new route path to redirect to_
37
- - when an `object` is returned, the router will interpret this as a route object, allowing to _modify (or completely replace) the route object_ being navigated to
38
-
39
- Returning an `object` is a simple, yet powerful, mechanism to add advanced runtime configuration to your routes. A common scenario is to overwrite certain parts of the route object provided in the `to`-argument, such as the route options (such as `inHistory` or `stayAlive`) or define conditional page transitions.
40
-
41
- It's also possible to add custom data to the route object in the `key` object. Custom data should be an object literal. When the new page loads, the keys in the `data` object are injected into the Component as `props`.
42
-
43
- ```js
44
- export default Blits.Application({
45
- // ...
46
- routes: [
47
- {
48
- path: '/video/details/:id',
49
- component: Details,
50
- hooks: {
51
- before(to, from) {
52
- // redirect to an error page when id is not a number
53
- if(isNaN(to.params.id)) {
54
- return '/not-valid'
55
- }
56
- // set a different transition when navigating from home page
57
- if(from && from.path === '/home') {
58
- to.transition = slideInTransition
59
- }
60
- // add custom data
61
- to.data.title = 'Hello World'
62
- // finally return the modified route object
63
- return to
64
- }
65
- }
66
- }
67
- ],
68
- // ...
69
- })
70
- ```
1
+ # Router Hooks
2
+
3
+ The Blits router supports hooks that allow you to execute code at specific points during the navigation process.
4
+
5
+ Hooks are defined per route in the optional `hooks` key within the Route configuration object.
6
+
7
+ > Currently, the Blits router only supports the before hook, with additional hooks planned for future releases.
8
+
9
+ ## Before hook
10
+
11
+ The before hook is executed before the navigation occurs. It can be used for checks, such as verifying user authentication for a specific route, for pre-fetching data, or for executing logic like sending stats beacons.
12
+
13
+ The before hook is defined as a function that receives 2 arguments: the Route object that is being navigated `to`. And the Route object that is being navigated away `from`.
14
+
15
+ ```js
16
+ export default Blits.Application({
17
+ // ...
18
+ routes: [
19
+ {
20
+ path: '/',
21
+ component: Home,
22
+ hooks: {
23
+ before(to, from) {
24
+ // custom logic
25
+ }
26
+ }
27
+ }
28
+ ],
29
+ // ...
30
+ })
31
+ ```
32
+
33
+ The before hook can optionally return a value, which will influence the behaviour of the route change:
34
+
35
+ - when `false` is returned, the router will fail to finish the routing, and act as if _no matching route was found_
36
+ - when a `string` is returned, the router will interpret this as a _new route path to redirect to_
37
+ - when an `object` is returned, the router will interpret this as a route object, allowing to _modify (or completely replace) the route object_ being navigated to
38
+
39
+ Returning an `object` is a simple, yet powerful, mechanism to add advanced runtime configuration to your routes. A common scenario is to overwrite certain parts of the route object provided in the `to`-argument, such as the route options (such as `inHistory` or `stayAlive`) or define conditional page transitions.
40
+
41
+ It's also possible to add custom data to the route object in the `key` object. Custom data should be an object literal. When the new page loads, the keys in the `data` object are injected into the Component as `props`.
42
+
43
+ ```js
44
+ export default Blits.Application({
45
+ // ...
46
+ routes: [
47
+ {
48
+ path: '/video/details/:id',
49
+ component: Details,
50
+ hooks: {
51
+ before(to, from) {
52
+ // redirect to an error page when id is not a number
53
+ if(isNaN(to.params.id)) {
54
+ return '/not-valid'
55
+ }
56
+ // set a different transition when navigating from home page
57
+ if(from && from.path === '/home') {
58
+ to.transition = slideInTransition
59
+ }
60
+ // add custom data
61
+ to.data.title = 'Hello World'
62
+ // finally return the modified route object
63
+ return to
64
+ }
65
+ }
66
+ }
67
+ ],
68
+ // ...
69
+ })
70
+ ```