@solidjs/router
Advanced tools
Comparing version 0.7.1 to 0.8.0
@@ -27,2 +27,45 @@ import { isServer, delegateEvents, createComponent as createComponent$1, spread, mergeProps as mergeProps$1, template } from 'solid-js/web'; | ||
} | ||
function createMemoryHistory() { | ||
const entries = ["/"]; | ||
let index = 0; | ||
const listeners = []; | ||
const go = n => { | ||
// https://github.com/remix-run/react-router/blob/682810ca929d0e3c64a76f8d6e465196b7a2ac58/packages/router/history.ts#L245 | ||
index = Math.max(0, Math.min(index + n, entries.length - 1)); | ||
const value = entries[index]; | ||
listeners.forEach(listener => listener(value)); | ||
}; | ||
return { | ||
get: () => entries[index], | ||
set: ({ | ||
value, | ||
scroll, | ||
replace | ||
}) => { | ||
if (replace) { | ||
entries[index] = value; | ||
} else { | ||
entries.splice(index + 1, entries.length - index, value); | ||
index++; | ||
} | ||
if (scroll) { | ||
scrollToHash(value.split("#")[1] || "", true); | ||
} | ||
}, | ||
back: () => { | ||
go(-1); | ||
}, | ||
forward: () => { | ||
go(1); | ||
}, | ||
go, | ||
listen: listener => { | ||
listeners.push(listener); | ||
return () => { | ||
const index = listeners.indexOf(listener); | ||
listeners.splice(index, 1); | ||
}; | ||
} | ||
}; | ||
} | ||
function createIntegration(get, set, init, utils) { | ||
@@ -119,2 +162,8 @@ let ignore = false; | ||
} | ||
function memoryIntegration() { | ||
const memoryHistory = createMemoryHistory(); | ||
return createIntegration(memoryHistory.get, memoryHistory.set, memoryHistory.listen, { | ||
go: memoryHistory.go | ||
}); | ||
} | ||
@@ -888,2 +937,2 @@ function createBeforeLeave() { | ||
export { A, A as Link, A as NavLink, Navigate, Outlet, Route, Router, Routes, mergeSearchString as _mergeSearchString, createBeforeLeave, createIntegration, hashIntegration, normalizeIntegration, pathIntegration, staticIntegration, useBeforeLeave, useHref, useIsRouting, useLocation, useMatch, useNavigate, useParams, useResolvedPath, useRouteData, useRoutes, useSearchParams }; | ||
export { A, A as Link, A as NavLink, Navigate, Outlet, Route, Router, Routes, mergeSearchString as _mergeSearchString, createBeforeLeave, createIntegration, createMemoryHistory, hashIntegration, memoryIntegration, normalizeIntegration, pathIntegration, staticIntegration, useBeforeLeave, useHref, useIsRouting, useLocation, useMatch, useNavigate, useParams, useResolvedPath, useRouteData, useRoutes, useSearchParams }; |
import type { LocationChange, LocationChangeSignal, RouterIntegration, RouterUtils } from "./types"; | ||
export declare function createMemoryHistory(): { | ||
get: () => string; | ||
set: ({ value, scroll, replace }: LocationChange) => void; | ||
back: () => void; | ||
forward: () => void; | ||
go: (n: number) => void; | ||
listen: (listener: (value: string) => void) => () => void; | ||
}; | ||
export declare function createIntegration(get: () => string | LocationChange, set: (next: LocationChange) => void, init?: (notify: (value?: string | LocationChange) => void) => () => void, utils?: Partial<RouterUtils>): RouterIntegration; | ||
@@ -7,1 +15,2 @@ export declare function normalizeIntegration(integration: RouterIntegration | LocationChangeSignal | undefined): RouterIntegration; | ||
export declare function hashIntegration(): RouterIntegration; | ||
export declare function memoryIntegration(): RouterIntegration; |
@@ -27,2 +27,42 @@ import { createSignal, onCleanup } from "solid-js"; | ||
} | ||
export function createMemoryHistory() { | ||
const entries = ["/"]; | ||
let index = 0; | ||
const listeners = []; | ||
const go = (n) => { | ||
// https://github.com/remix-run/react-router/blob/682810ca929d0e3c64a76f8d6e465196b7a2ac58/packages/router/history.ts#L245 | ||
index = Math.max(0, Math.min(index + n, entries.length - 1)); | ||
const value = entries[index]; | ||
listeners.forEach(listener => listener(value)); | ||
}; | ||
return { | ||
get: () => entries[index], | ||
set: ({ value, scroll, replace }) => { | ||
if (replace) { | ||
entries[index] = value; | ||
} | ||
else { | ||
entries.splice(index + 1, entries.length - index, value); | ||
index++; | ||
} | ||
if (scroll) { | ||
scrollToHash(value.split("#")[1] || "", true); | ||
} | ||
}, | ||
back: () => { | ||
go(-1); | ||
}, | ||
forward: () => { | ||
go(1); | ||
}, | ||
go, | ||
listen: (listener) => { | ||
listeners.push(listener); | ||
return () => { | ||
const index = listeners.indexOf(listener); | ||
listeners.splice(index, 1); | ||
}; | ||
} | ||
}; | ||
} | ||
export function createIntegration(get, set, init, utils) { | ||
@@ -107,1 +147,7 @@ let ignore = false; | ||
} | ||
export function memoryIntegration() { | ||
const memoryHistory = createMemoryHistory(); | ||
return createIntegration(memoryHistory.get, memoryHistory.set, memoryHistory.listen, { | ||
go: memoryHistory.go | ||
}); | ||
} |
@@ -9,3 +9,3 @@ { | ||
"license": "MIT", | ||
"version": "0.7.1", | ||
"version": "0.8.0", | ||
"homepage": "https://github.com/solidjs/solid-router#readme", | ||
@@ -12,0 +12,0 @@ "repository": { |
@@ -21,2 +21,3 @@ <p> | ||
- [Hash Mode Router](#hash-mode-router) | ||
- [Memory Mode Router](#memory-mode-router) | ||
- [Config Based Routing](#config-based-routing) | ||
@@ -423,2 +424,12 @@ - [Router Primitives](#router-primitives) | ||
## Memory Mode Router | ||
You can also use memory mode router for testing purpose. | ||
```jsx | ||
import { Router, memoryIntegration } from '@solidjs/router' | ||
<Router source={memoryIntegration()}><App /></Router> | ||
``` | ||
## Config Based Routing | ||
@@ -425,0 +436,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
95613
2063
618