create-rasti 0.1.0-alpha.0 → 0.1.0-alpha.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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @typedef {string|number|boolean|null|undefined|Object.<string, *>|Array.<*>} ClassValue
2
+ * @typedef {string|number|boolean|null|undefined|Record<string, *>|Array<*>} ClassValue
3
3
  */
4
4
 
5
5
  /**
@@ -10,7 +10,7 @@
10
10
  const add = (a, b) => a && b ? a + ' ' + b : '' + (a || b);
11
11
 
12
12
  /**
13
- * @param {Array.<*>} classes
13
+ * @param {Array<*>} classes
14
14
  * @returns {string}
15
15
  */
16
16
  const parse = (classes) => {
@@ -46,10 +46,10 @@ In the browser, links opt in to client-side navigation with `data-router`:
46
46
 
47
47
  | Method | Description |
48
48
  |---|---|
49
- | `navigate(url, options?)` | Matches `url`, updates browser history when available, invokes the matched route's `action(location)`, and returns the action result. Async actions return a Promise. Options: `{ addToHistory = true, replaceHistory = false }`. |
49
+ | `navigate(url, options?)` | Matches `url`, updates browser history when available, invokes the matched route's `action(location)`, and returns the action result. Async actions return a Promise. Options: `{ addToHistory = true, replaceHistory = false, resetScroll = true }`. A navigation that adds or replaces a history entry scrolls to the top; pass `resetScroll : false` to keep the position, e.g. for a tab switch that only updates the query. |
50
50
  | `createUrl(path, params?, query?)` | Builds a URL from a route path, route params, and query params. |
51
51
  | `delegateNavigation(rootElement)` | Intercepts left-clicks on descendant `a[data-router]` links and routes them through `navigate`. Returns a cleanup function. |
52
- | `bindHistory()` | Listens for `popstate` and navigates to the current URL without pushing a new history entry. A `popstate` that keeps the path and query — a jump to an in-page anchor — is ignored. Returns a cleanup function. |
52
+ | `bindHistory()` | Listens for `popstate` and navigates to the current URL without pushing a new history entry or scrolling, so the browser restores the position. A `popstate` that keeps the path and query — a jump to an in-page anchor — is ignored. Returns a cleanup function. |
53
53
 
54
54
  **Options:** `{ baseUrl?: string }` — prepended to generated URLs and stripped before matching. It is only stripped as a prefix, on a path segment boundary: with `baseUrl: '/app'`, `/app/about` matches `/about` while `/application/x` and `/en/app-store/x` are left alone.
55
55
 
@@ -2,14 +2,14 @@ import { match, compile } from 'path-to-regexp';
2
2
 
3
3
  /**
4
4
  * Route parameters by name. Wildcard parameters (e.g. `*rest`) are arrays of path segments.
5
- * @typedef {Object.<string, (string|Array.<string>)>} Params
5
+ * @typedef {Record<string, (string|Array<string>)>} Params
6
6
  */
7
7
 
8
8
  /**
9
9
  * @typedef {Object} Location
10
10
  * @property {string} path - The route path pattern (e.g., `/user/:id`).
11
11
  * @property {Params} params - Route parameters decoded from the URL path. Wildcard parameters (e.g. `*rest`) are arrays of path segments. Untrusted input: escape at the point of output.
12
- * @property {Object.<string, string>} query - Query parameters parsed from the URL query string. Untrusted input, same as `params`.
12
+ * @property {Record<string, string>} query - Query parameters parsed from the URL query string. Untrusted input, same as `params`.
13
13
  * @property {(url: string) => boolean} test - A function to test if a given URL matches this route. Returns `true` if the URL matches the route, `false` otherwise.
14
14
  */
15
15
 
@@ -23,12 +23,13 @@ import { match, compile } from 'path-to-regexp';
23
23
  * @typedef {Object} NavigateOptions
24
24
  * @property {boolean} [addToHistory=true] - Whether to add the navigation to the browser's history.
25
25
  * @property {boolean} [replaceHistory=false] - Whether to replace the current history entry instead of adding a new one.
26
+ * @property {boolean} [resetScroll=true] - Whether to scroll to the top when the navigation adds or replaces a history entry. A `popstate` never scrolls: the browser restores the position.
26
27
  */
27
28
 
28
29
  /**
29
30
  * @typedef {Object} Router
30
31
  * @property {(url: string, options?: NavigateOptions) => *} navigate - Navigates to a given URL, matches a route, and returns its action result.
31
- * @property {(path: string, params?: Params, query?: Object.<string, string>) => string} createUrl - Creates a URL from a route path, parameters, and query string.
32
+ * @property {(path: string, params?: Params, query?: Record<string, string>) => string} createUrl - Creates a URL from a route path, parameters, and query string.
32
33
  * @property {(rootElement: HTMLElement) => () => void} delegateNavigation - Delegates navigation events for links with `data-router` attributes. Returns a cleanup function.
33
34
  * @property {() => () => void} bindHistory - Binds browser history changes to the navigation logic. Returns a cleanup function.
34
35
  */
@@ -39,7 +40,7 @@ import { match, compile } from 'path-to-regexp';
39
40
  * `params` and `query` on a `Location` come from the URL and are decoded but not
40
41
  * escaped. Escape them at the point of output, in the context of that output.
41
42
  *
42
- * @param {Array.<Route>} routes - Array of route objects. Each route must have a `path` and an `action` function.
43
+ * @param {Array<Route>} routes - Array of route objects. Each route must have a `path` and an `action` function.
43
44
  * @param {Object} [routerOptions={}] - Options for the router.
44
45
  * @param {string} [routerOptions.baseUrl=''] - A base URL to prepend to all routes and navigation.
45
46
  * @returns {Router} - Router object with navigation, URL creation, and event delegation methods.
@@ -146,13 +147,14 @@ export default function createRouter(routes, routerOptions = {}) {
146
147
  *
147
148
  * History is updated before the action runs, so an action that redirects can
148
149
  * replace the entry it was reached through: `navigate('/login', { replaceHistory : true })`.
150
+ * The page scrolls to the top along with the history update, unless `resetScroll` is `false`.
149
151
  *
150
152
  * @param {string} url - The path to navigate to, including query string.
151
153
  * @param {NavigateOptions} [options={}] - Options for navigation.
152
154
  * @returns {*} The matched route action result, or `undefined` if no route matches. Async actions return a Promise.
153
155
  */
154
156
  const navigate = (url, options = {}) => {
155
- const { addToHistory = true, replaceHistory = false } = options;
157
+ const { addToHistory = true, replaceHistory = false, resetScroll = true } = options;
156
158
  // Match the route and query
157
159
  const matched = matchRoute(url);
158
160
 
@@ -161,6 +163,7 @@ export default function createRouter(routes, routerOptions = {}) {
161
163
  // Handle browser history
162
164
  if (addToHistory && typeof window !== 'undefined') {
163
165
  window.history[replaceHistory ? 'replaceState' : 'pushState']({}, '', url);
166
+ if (resetScroll) window.scrollTo({ top : 0, behavior : 'instant' });
164
167
  }
165
168
  // Call the route's action
166
169
  return matched();
@@ -175,7 +178,7 @@ export default function createRouter(routes, routerOptions = {}) {
175
178
  * Creates a URL from a route path, parameters, and query string.
176
179
  * @param {string} path - The route path (e.g., `/user/:id`).
177
180
  * @param {Params} [params={}] - Parameters to replace in the path (e.g., `{ id : '123' }` for `/user/:id`). Wildcard parameters take an array of segments.
178
- * @param {Object.<string, string>} [query={}] - Query parameters to append to the URL (e.g., `{ page : '1', sort : 'name' }`).
181
+ * @param {Record<string, string>} [query={}] - Query parameters to append to the URL (e.g., `{ page : '1', sort : 'name' }`).
179
182
  * @returns {string} - The generated URL with query parameters.
180
183
  */
181
184
  const createUrl = (path, params = {}, query = {}) => {
@@ -232,7 +235,6 @@ export default function createRouter(routes, routerOptions = {}) {
232
235
  if (url.origin !== window.location.origin) return;
233
236
 
234
237
  event.preventDefault();
235
- window.scrollTo({ top : 0, behavior : 'instant' });
236
238
  navigate(url.pathname + url.search);
237
239
  };
238
240
  // Bind the click event to the root element.
@@ -111,6 +111,7 @@ export function buildComponent(name, svgContent) {
111
111
  ` * @property {string} [label] - Accessible name. Without it the icon is decorative and hidden from assistive technology.`,
112
112
  ` */`,
113
113
  ``,
114
+ `/** @type {typeof Component<IconProps>} */`,
114
115
  `const ${name} = Component.create(/** @this {import('rasti').Component<IconProps>} */ function() {`,
115
116
  ` const { className, ${sizeDefaults}, label } = this.props;`,
116
117
  ``,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-rasti",
3
- "version": "0.1.0-alpha.0",
3
+ "version": "0.1.0-alpha.1",
4
4
  "description": "Create Rasti projects with Vite",
5
5
  "type": "module",
6
6
  "bin": {