Skip to content

Commit 3a8f121

Browse files
committed
Merge pull request #48 from g4rcez/anchor-id
wip: anchor identifier
2 parents 0e71896 + 2daf994 commit 3a8f121

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

src/brouther/brouther.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createContext, Fragment, useCallback, useContext, useEffect, useMemo, useState } from "react";
1+
import React, { createContext, Fragment, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
22
import type { ConfiguredRoute, Location, PathFormat } from "../types";
33
import { BroutherError, NotFoundRoute, UncaughtDataLoader } from "../utils/errors";
44
import { createHref, has, join, mapUrlToQueryStringRecord, transformData, urlEntity } from "../utils/utils";
@@ -255,15 +255,26 @@ export const Outlet = (props: { notFound?: React.ReactElement }) => {
255255
Boolean that represents if it's your action/loader process is loading
256256
@returns string
257257
*/
258-
export const useLoadingState = () => {
259-
const router = useRouter();
260-
return router.loading;
261-
};
258+
export const useLoadingState = () => useRouter().loading;
262259

263260
/*
264261
@private
265262
*/
266-
export const useFlags = () => {
267-
const router = useRouter();
268-
return router.flags;
263+
export const useFlags = () => useRouter().flags;
264+
265+
const useStableRef = <V extends any>(value: V) => {
266+
const v = useRef(value);
267+
useEffect(() => {
268+
v.current = value;
269+
}, [value]);
270+
return v;
271+
};
272+
273+
export const useBeforeUnload = (fn: (event: BeforeUnloadEvent) => void) => {
274+
const func = useStableRef(fn);
275+
useEffect(() => {
276+
const closure = (e: BeforeUnloadEvent) => func.current(e);
277+
window.addEventListener("beforeunload", closure);
278+
return () => window.addEventListener("beforeunload", closure);
279+
}, []);
269280
};

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export {
3030
useQueryString,
3131
useRouteError,
3232
useUrlSearchParams,
33+
useBeforeUnload,
3334
} from "./brouther/brouther";
3435
export { BroutherError, NotFoundRoute } from "./utils/errors";
3536
export { Form } from "./form/form";

src/router/link.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const Link: <TPath extends string>(props: LinkProps<TPath>) => React.Reac
4949
onClick?.(event, { query, paths } as QueryAndPaths<TPath>);
5050
return replace ? _replace(_href, state) : push(_href, state);
5151
};
52+
5253
return <a {...props} target={target} rel={rel} href={_href} onClick={_onClick} ref={ref} />;
5354
}
5455
) as any;

src/router/router.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ const configureRoutes = (
4040
arr: Route[],
4141
basename: string
4242
): {
43-
path: PathFormat;
44-
regex: RegExp;
43+
readonly actions?: Actions;
4544
readonly data?: RouteData;
46-
readonly loader?: Loader<PathFormat, RouteData>;
47-
readonly id: string;
48-
originalPath: PathFormat;
49-
readonly actions?: Actions<PathFormat, RouteData>;
5045
readonly element: React.ReactElement;
46+
readonly id: string;
47+
readonly loader?: Loader;
48+
readonly originalPath: PathFormat;
49+
readonly path: PathFormat;
50+
readonly regex: RegExp;
5151
}[] =>
5252
rankRoutes(arr).map((x) => ({
5353
...x,
@@ -58,13 +58,13 @@ const configureRoutes = (
5858
export const createRoute = <
5959
const Path extends PathFormat,
6060
const Args extends {
61-
id?: string;
62-
element: React.ReactElement;
63-
loader?: Route<Path, Data>["loader"];
6461
actions?: Route<Path, Data>["actions"];
62+
element: React.ReactElement;
6563
errorElement?: Route<Path, Data>["element"];
64+
id?: string;
65+
loader?: Route<Path, Data>["loader"];
6666
},
67-
const Data extends RouteData
67+
const Data extends RouteData,
6868
>(
6969
path: Path,
7070
args: Args,

tests/anchor-target.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ describe("Should test the anchor target", () => {
1010
expect(fetchTarget(true, "https://example.com", "http://localhost:3000")).toBe("_blank");
1111
expect(fetchTarget(true, "https://example.com", "http://localhost:3000")).toBe("_blank");
1212
});
13+
14+
test("Should test different origin but without openExternalLinksInTabs", () => {
15+
expect(fetchTarget(false, "https://example.com", "http://localhost:3000")).not.toBe("_blank");
16+
expect(fetchTarget(false, "https://example.com", "http://localhost:3000")).not.toBe("_blank");
17+
});
1318
});

0 commit comments

Comments
 (0)