Skip to content

Commit

Permalink
refactor: Switch to scope
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Oct 16, 2024
1 parent 8b04312 commit fe68b8e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/router.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AnyComponent, FunctionComponent, VNode } from 'preact';

export function LocationProvider(props: {
limit?: string | RegExp;
scope?: string | RegExp;
children?: VNode[];
}): VNode;

Expand Down
14 changes: 9 additions & 5 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useContext, useMemo, useReducer, useLayoutEffect, useRef } from 'preact
* @typedef {import('./internal.d.ts').VNode} VNode
*/

let push, limit;
let push, scope;
const UPDATE = (state, url) => {
push = undefined;
if (url && url.type === 'click') {
Expand All @@ -23,9 +23,9 @@ const UPDATE = (state, url) => {
link.origin != location.origin ||
/^#/.test(href) ||
!/^(_?self)?$/i.test(link.target) ||
limit && (typeof limit == 'string'
? !href.startsWith(limit)
: !limit.test(href)
scope && (typeof scope == 'string'
? !href.startsWith(scope)
: !scope.test(href)
)
) {
return state;
Expand Down Expand Up @@ -75,9 +75,13 @@ export const exec = (url, route, matches) => {
return matches;
};

/**
* @type {import('./router.d.ts').LocationProvider}
*/
export function LocationProvider(props) {
// @ts-expect-error - props.url is not implemented correctly & will be removed in the future
const [url, route] = useReducer(UPDATE, props.url || location.pathname + location.search);
if (props.limit) limit = props.limit;
if (props.scope) scope = props.scope;
const wasPush = push === true;

const value = useMemo(() => {
Expand Down
12 changes: 6 additions & 6 deletions test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ describe('Router', () => {
}
});

describe('intercepted VS external links with `limit`', () => {
describe('intercepted VS external links with `scope`', () => {
const shouldIntercept = ['/app', '/app/deeper'];
const shouldNavigate = ['/site', '/site/deeper'];

Expand Down Expand Up @@ -617,9 +617,9 @@ describe('Router', () => {
pushState.resetHistory();
});

it('should intercept clicks on links matching the `limit` props (string)', async () => {
it('should intercept clicks on links matching the `scope` props (string)', async () => {
render(
<LocationProvider limit="/app">
<LocationProvider scope="/app">
<Router>
<Route default />
</Router>
Expand Down Expand Up @@ -647,7 +647,7 @@ describe('Router', () => {

it('should allow default browser navigation for links not matching the `limit` props (string)', async () => {
render(
<LocationProvider limit="app">
<LocationProvider scope="app">
<Router>
<Route default />
</Router>
Expand All @@ -674,7 +674,7 @@ describe('Router', () => {

it('should intercept clicks on links matching the `limit` props (regex)', async () => {
render(
<LocationProvider limit={/^\/app/}>
<LocationProvider scope={/^\/app/}>
<Router>
<Route default />
</Router>
Expand Down Expand Up @@ -702,7 +702,7 @@ describe('Router', () => {

it('should allow default browser navigation for links not matching the `limit` props (regex)', async () => {
render(
<LocationProvider limit={/^\/app/}>
<LocationProvider scope={/^\/app/}>
<Router>
<Route default />
</Router>
Expand Down

0 comments on commit fe68b8e

Please sign in to comment.