@react-navigation/native 7.3.8 → 7.3.9
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.
- package/lib/module/Link.js +23 -6
- package/lib/module/Link.js.map +1 -1
- package/lib/module/createMemoryHistory.js +1 -0
- package/lib/module/createMemoryHistory.js.map +1 -1
- package/lib/module/useDeepStableValue.js +12 -0
- package/lib/module/useDeepStableValue.js.map +1 -0
- package/lib/module/useLinkProps.js +99 -56
- package/lib/module/useLinkProps.js.map +1 -1
- package/lib/typescript/src/Link.d.ts +5 -4
- package/lib/typescript/src/Link.d.ts.map +1 -1
- package/lib/typescript/src/__stubs__/createStackNavigator.d.ts +1 -1
- package/lib/typescript/src/__stubs__/createStackNavigator.d.ts.map +1 -1
- package/lib/typescript/src/createMemoryHistory.d.ts.map +1 -1
- package/lib/typescript/src/useDeepStableValue.d.ts +2 -0
- package/lib/typescript/src/useDeepStableValue.d.ts.map +1 -0
- package/lib/typescript/src/useLinkProps.d.ts +12 -3
- package/lib/typescript/src/useLinkProps.d.ts.map +1 -1
- package/lib/typescript/src/useLinking.native.d.ts +2 -2
- package/package.json +11 -11
- package/src/Link.tsx +30 -12
- package/src/createMemoryHistory.tsx +2 -0
- package/src/useDeepStableValue.tsx +12 -0
- package/src/useLinkProps.tsx +192 -91
package/lib/module/Link.js
CHANGED
|
@@ -11,7 +11,7 @@ import { useLinkProps } from "./useLinkProps.js";
|
|
|
11
11
|
* @param props.screen Name of the screen to navigate to (e.g. `'Feeds'`).
|
|
12
12
|
* @param props.params Params to pass to the screen to navigate to (e.g. `{ sort: 'hot' }`).
|
|
13
13
|
* @param props.href Optional absolute path to use for the href (e.g. `/feeds/hot`).
|
|
14
|
-
* @param props.action Optional action to
|
|
14
|
+
* @param props.action Optional action to override the in-page navigation. The `href` is still derived from `screen`, so this can be used to render a link while dispatching a different action (e.g. a `replace`).
|
|
15
15
|
* @param props.children Child elements to render the content.
|
|
16
16
|
*/
|
|
17
17
|
export function Link({
|
|
@@ -20,12 +20,9 @@ export function Link({
|
|
|
20
20
|
action,
|
|
21
21
|
href,
|
|
22
22
|
style,
|
|
23
|
+
target,
|
|
23
24
|
...rest
|
|
24
25
|
}) {
|
|
25
|
-
const {
|
|
26
|
-
colors,
|
|
27
|
-
fonts
|
|
28
|
-
} = useTheme();
|
|
29
26
|
// @ts-expect-error: This is already type-checked by the prop types
|
|
30
27
|
const props = useLinkProps({
|
|
31
28
|
screen,
|
|
@@ -33,7 +30,19 @@ export function Link({
|
|
|
33
30
|
action,
|
|
34
31
|
href
|
|
35
32
|
});
|
|
33
|
+
|
|
34
|
+
// Keep usage of `useTheme` after `useLinkProps`
|
|
35
|
+
// This ensures proper error when used outside of navigation container
|
|
36
|
+
const {
|
|
37
|
+
colors,
|
|
38
|
+
fonts
|
|
39
|
+
} = useTheme();
|
|
36
40
|
const onPress = e => {
|
|
41
|
+
if (rest.disabled) {
|
|
42
|
+
e.preventDefault();
|
|
43
|
+
e.stopPropagation();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
37
46
|
if ('onPress' in rest) {
|
|
38
47
|
rest.onPress?.(e);
|
|
39
48
|
}
|
|
@@ -48,7 +57,15 @@ export function Link({
|
|
|
48
57
|
...rest,
|
|
49
58
|
...Platform.select({
|
|
50
59
|
web: {
|
|
51
|
-
|
|
60
|
+
'aria-disabled': rest.disabled,
|
|
61
|
+
'onAuxClick': rest.disabled ? e => {
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
e.stopPropagation();
|
|
64
|
+
} : undefined,
|
|
65
|
+
'onClick': onPress,
|
|
66
|
+
'hrefAttrs': {
|
|
67
|
+
target
|
|
68
|
+
}
|
|
52
69
|
},
|
|
53
70
|
default: {
|
|
54
71
|
onPress
|
package/lib/module/Link.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useTheme","React","Platform","Text","useLinkProps","Link","screen","params","action","href","style","rest","
|
|
1
|
+
{"version":3,"names":["useTheme","React","Platform","Text","useLinkProps","Link","screen","params","action","href","style","target","rest","props","colors","fonts","onPress","e","disabled","preventDefault","stopPropagation","defaultPrevented","createElement","select","web","undefined","default","color","primary","regular"],"sourceRoot":"../../src","sources":["Link.tsx"],"mappings":";;AAAA,SAASA,QAAQ,QAAQ,wBAAwB;AACjD,OAAO,KAAKC,KAAK,MAAM,OAAO;AAC9B,SAEEC,QAAQ,EACRC,IAAI,QAEC,cAAc;AAErB,SAAyBC,YAAY,QAAQ,mBAAgB;AAe7D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,IAAIA,CAAkD;EACpEC,MAAM;EACNC,MAAM;EACNC,MAAM;EACNC,IAAI;EACJC,KAAK;EACLC,MAAM;EACN,GAAGC;AACa,CAAC,EAAE;EACnB;EACA,MAAMC,KAAK,GAAGT,YAAY,CAAY;IAAEE,MAAM;IAAEC,MAAM;IAAEC,MAAM;IAAEC;EAAK,CAAC,CAAC;;EAEvE;EACA;EACA,MAAM;IAAEK,MAAM;IAAEC;EAAM,CAAC,GAAGf,QAAQ,CAAC,CAAC;EAEpC,MAAMgB,OAAO,GAAIC,CAAa,IAAK;IACjC,IAAIL,IAAI,CAACM,QAAQ,EAAE;MACjBD,CAAC,CAACE,cAAc,CAAC,CAAC;MAClBF,CAAC,CAACG,eAAe,CAAC,CAAC;MACnB;IACF;IAEA,IAAI,SAAS,IAAIR,IAAI,EAAE;MACrBA,IAAI,CAACI,OAAO,GAAGC,CAAC,CAAC;IACnB;;IAEA;IACA,IAAI,CAACA,CAAC,CAACI,gBAAgB,EAAE;MACvBR,KAAK,CAACG,OAAO,CAACC,CAAC,CAAC;IAClB;EACF,CAAC;EAED,oBAAOhB,KAAK,CAACqB,aAAa,CAACnB,IAAI,EAAE;IAC/B,GAAGU,KAAK;IACR,GAAGD,IAAI;IACP,GAAGV,QAAQ,CAACqB,MAAM,CAAC;MACjBC,GAAG,EAAE;QACH,eAAe,EAAEZ,IAAI,CAACM,QAAQ;QAC9B,YAAY,EAAEN,IAAI,CAACM,QAAQ,GACtBD,CAAkD,IAAK;UACtDA,CAAC,CAACE,cAAc,CAAC,CAAC;UAClBF,CAAC,CAACG,eAAe,CAAC,CAAC;QACrB,CAAC,GACDK,SAAS;QACb,SAAS,EAAET,OAAO;QAClB,WAAW,EAAE;UAAEL;QAAO;MACxB,CAAC;MACDe,OAAO,EAAE;QAAEV;MAAQ;IACrB,CAAC,CAAC;IACFN,KAAK,EAAE,CAAC;MAAEiB,KAAK,EAAEb,MAAM,CAACc;IAAQ,CAAC,EAAEb,KAAK,CAACc,OAAO,EAAEnB,KAAK;EACzD,CAAC,CAAC;AACJ","ignoreList":[]}
|
|
@@ -189,6 +189,7 @@ export function createMemoryHistory() {
|
|
|
189
189
|
// But on Firefox, it seems to take much longer, around 50ms from our testing
|
|
190
190
|
// We're using a hacky timeout since there doesn't seem to be way to know for sure
|
|
191
191
|
const timer = setTimeout(() => {
|
|
192
|
+
window.removeEventListener('popstate', onPopState);
|
|
192
193
|
const foundIndex = pending.findIndex(it => it.ref === done);
|
|
193
194
|
if (foundIndex > -1) {
|
|
194
195
|
pending[foundIndex]?.cb();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["nanoid","getPathWithoutHash","path","split","createMemoryHistory","index","items","pending","interrupt","forEach","it","cb","history","id","window","state","findIndex","item","get","backIndex","pathWithoutHash","i","push","slice","length","pushState","replace","pathWithHash","hash","includes","location","replaceState","go","n","nextIndex","lastItemIndex","targetId","Promise","resolve","reject","done","interrupted","clearTimeout","timer","Error","title","document","ref","setTimeout","
|
|
1
|
+
{"version":3,"names":["nanoid","getPathWithoutHash","path","split","createMemoryHistory","index","items","pending","interrupt","forEach","it","cb","history","id","window","state","findIndex","item","get","backIndex","pathWithoutHash","i","push","slice","length","pushState","replace","pathWithHash","hash","includes","location","replaceState","go","n","nextIndex","lastItemIndex","targetId","Promise","resolve","reject","done","interrupted","clearTimeout","timer","Error","title","document","ref","setTimeout","removeEventListener","onPopState","foundIndex","splice","last","pop","addEventListener","listen","listener"],"sourceRoot":"../../src","sources":["createMemoryHistory.tsx"],"mappings":";;AACA,SAASA,MAAM,QAAQ,mBAAmB;AAW1C,MAAMC,kBAAkB,GAAIC,IAAY,IAAKA,IAAI,CAACC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE;AAErE,OAAO,SAASC,mBAAmBA,CAAA,EAAG;EACpC,IAAIC,KAAK,GAAG,CAAC;EACb,IAAIC,KAAsB,GAAG,EAAE;;EAE/B;EACA;EACA,MAAMC,OAAgE,GAAG,EAAE;EAE3E,MAAMC,SAAS,GAAGA,CAAA,KAAM;IACtB;IACA;IACA;IACAD,OAAO,CAACE,OAAO,CAAEC,EAAE,IAAK;MACtB,MAAMC,EAAE,GAAGD,EAAE,CAACC,EAAE;MAChBD,EAAE,CAACC,EAAE,GAAG,MAAMA,EAAE,CAAC,IAAI,CAAC;IACxB,CAAC,CAAC;EACJ,CAAC;EAED,MAAMC,OAAO,GAAG;IACd,IAAIP,KAAKA,CAAA,EAAW;MAClB;MACA;MACA,MAAMQ,EAAE,GAAGC,MAAM,CAACF,OAAO,CAACG,KAAK,EAAEF,EAAE;MAEnC,IAAIA,EAAE,EAAE;QACN,MAAMR,KAAK,GAAGC,KAAK,CAACU,SAAS,CAAEC,IAAI,IAAKA,IAAI,CAACJ,EAAE,KAAKA,EAAE,CAAC;QAEvD,OAAOR,KAAK,GAAG,CAAC,CAAC,GAAGA,KAAK,GAAG,CAAC;MAC/B;MAEA,OAAO,CAAC;IACV,CAAC;IAEDa,GAAGA,CAACb,KAAa,EAAE;MACjB,OAAOC,KAAK,CAACD,KAAK,CAAC;IACrB,CAAC;IAEDc,SAASA,CAAC;MAAEjB;IAAuB,CAAC,EAAE;MACpC,MAAMkB,eAAe,GAAGnB,kBAAkB,CAACC,IAAI,CAAC;;MAEhD;MACA,KAAK,IAAImB,CAAC,GAAGhB,KAAK,GAAG,CAAC,EAAEgB,C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GAAG,IAAI,CAACA,KAAK;QACpB,CAAC,EAAE,GAAG,CAAC;QAEP,MAAM6C,UAAU,GAAGA,CAAA,KAAM;UACvB;UACA;UACA7C,KAAK,GAAG,IAAI,CAACA,KAAK;UAElB,MAAMgD,IAAI,GAAG9C,OAAO,CAAC+C,GAAG,CAAC,CAAC;UAE1BxC,MAAM,CAACmC,mBAAmB,CAAC,UAAU,EAAEC,UAAU,CAAC;UAClDG,IAAI,EAAE1C,EAAE,CAAC,CAAC;QACZ,CAAC;QAEDG,MAAM,CAACyC,gBAAgB,CAAC,UAAU,EAAEL,UAAU,CAAC;QAC/CpC,MAAM,CAACF,OAAO,CAACoB,EAAE,CAACC,CAAC,CAAC;MACtB,CAAC,CAAC;IACJ,CAAC;IAED;IACA;IACA;IACAuB,MAAMA,CAACC,QAAoB,EAAE;MAC3B,MAAMP,UAAU,GAAGA,CAAA,KAAM;QACvB;QACA;QACA7C,KAAK,GAAG,IAAI,CAACA,KAAK;QAElB,IAAIE,OAAO,CAACiB,MAAM,EAAE;UAClB;UACA;QACF;QAEAiC,QAAQ,CAAC,CAAC;MACZ,CAAC;MAED3C,MAAM,CAACyC,gBAAgB,CAAC,UAAU,EAAEL,UAAU,CAAC;MAE/C,OAAO,MAAMpC,MAAM,CAACmC,mBAAmB,CAAC,UAAU,EAAEC,UAAU,CAAC;IACjE;EACF,CAAC;EAED,OAAOtC,OAAO;AAChB","ignoreList":[]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import isEqual from 'fast-deep-equal';
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
export function useDeepStableValue(value) {
|
|
6
|
+
const valueRef = React.useRef(value);
|
|
7
|
+
if (!isEqual(valueRef.current, value)) {
|
|
8
|
+
valueRef.current = value;
|
|
9
|
+
}
|
|
10
|
+
return valueRef.current;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=useDeepStableValue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["isEqual","React","useDeepStableValue","value","valueRef","useRef","current"],"sourceRoot":"../../src","sources":["useDeepStableValue.tsx"],"mappings":";;AAAA,OAAOA,OAAO,MAAM,iBAAiB;AACrC,OAAO,KAAKC,KAAK,MAAM,OAAO;AAE9B,OAAO,SAASC,kBAAkBA,CAAIC,KAAQ,EAAK;EACjD,MAAMC,QAAQ,GAAGH,KAAK,CAACI,MAAM,CAACF,KAAK,CAAC;EAEpC,IAAI,CAACH,OAAO,CAACI,QAAQ,CAACE,OAAO,EAAEH,KAAK,CAAC,EAAE;IACrCC,QAAQ,CAACE,OAAO,GAAGH,KAAK;EAC1B;EAEA,OAAOC,QAAQ,CAACE,OAAO;AACzB","ignoreList":[]}
|
|
@@ -1,24 +1,69 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
import { getPathFromState, NavigationContainerRefContext, NavigationHelpersContext } from '@react-navigation/core';
|
|
3
|
+
import { CommonActions, getPathFromState, NavigationContainerRefContext, NavigationHelpersContext } from '@react-navigation/core';
|
|
4
4
|
import * as React from 'react';
|
|
5
5
|
import { Platform } from 'react-native';
|
|
6
|
+
import useLatestCallback from 'use-latest-callback';
|
|
6
7
|
import { LinkingContext } from "./LinkingContext.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
import { useDeepStableValue } from "./useDeepStableValue.js";
|
|
9
|
+
const NESTED_KEYS = ['payload', 'params', 'state', 'routes'];
|
|
10
|
+
const NESTED_PARAMS_KEYS = ['params', 'state'];
|
|
11
|
+
const NAVIGATE_ACTION_TYPES = ['NAVIGATE', 'PUSH', 'REPLACE', 'POP_TO', 'JUMP_TO'];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Nested params are tracked as consumed after handling
|
|
15
|
+
* So navigating with same params again won't work
|
|
16
|
+
* This can happen if the action or params are memoized
|
|
17
|
+
* Or component hasn't re-renderd to re-create inline objects
|
|
18
|
+
* So we clone the action and params when necessary before dispatch
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
function clone(value, keys = NESTED_KEYS, clones) {
|
|
22
|
+
if (typeof value !== 'object' || value == null) {
|
|
23
|
+
return value;
|
|
10
24
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
25
|
+
const existing = clones?.get(value);
|
|
26
|
+
if (existing) {
|
|
27
|
+
return existing;
|
|
28
|
+
}
|
|
29
|
+
const isArray = Array.isArray(value);
|
|
30
|
+
if (keys === NESTED_PARAMS_KEYS && 'screen' in value && typeof value.screen !== 'string' && !('state' in value && typeof value.state === 'object' && value.state != null && 'routes' in value.state && Array.isArray(value.state.routes))) {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
let copy = keys === NESTED_PARAMS_KEYS ? {
|
|
34
|
+
...value
|
|
35
|
+
} : undefined;
|
|
36
|
+
clones ??= new WeakMap();
|
|
37
|
+
clones.set(value, copy ?? value);
|
|
38
|
+
for (const key of isArray ? value.keys() : keys) {
|
|
39
|
+
const nested = Reflect.get(value, key);
|
|
40
|
+
const cloned = clone(nested, key === 'params' ? NESTED_PARAMS_KEYS : NESTED_KEYS, clones);
|
|
41
|
+
if (cloned !== nested) {
|
|
42
|
+
copy ??= isArray ? [...value] : {
|
|
43
|
+
...value
|
|
44
|
+
};
|
|
45
|
+
clones.set(value, copy);
|
|
46
|
+
Object.assign(copy, {
|
|
47
|
+
[key]: cloned
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return copy ?? value;
|
|
52
|
+
}
|
|
53
|
+
const getTargetFromAction = (action, screens) => {
|
|
54
|
+
if (action === undefined || screens === undefined) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const {
|
|
58
|
+
payload
|
|
59
|
+
} = action;
|
|
60
|
+
if (!NAVIGATE_ACTION_TYPES.includes(action.type) || payload === undefined || !('name' in payload) || typeof payload.name !== 'string' || !(payload.name in screens)) {
|
|
61
|
+
return undefined;
|
|
20
62
|
}
|
|
21
|
-
return
|
|
63
|
+
return {
|
|
64
|
+
screen: payload.name,
|
|
65
|
+
params: 'params' in payload && typeof payload.params === 'object' && payload.params != null ? payload.params : undefined
|
|
66
|
+
};
|
|
22
67
|
};
|
|
23
68
|
|
|
24
69
|
/**
|
|
@@ -27,65 +72,63 @@ const getStateFromParams = params => {
|
|
|
27
72
|
* @param props.screen Name of the screen to navigate to (e.g. `'Feeds'`).
|
|
28
73
|
* @param props.params Params to pass to the screen to navigate to (e.g. `{ sort: 'hot' }`).
|
|
29
74
|
* @param props.href Optional absolute path to use for the href (e.g. `/feeds/hot`).
|
|
30
|
-
* @param props.action Optional action to
|
|
75
|
+
* @param props.action Optional action to override the in-page navigation. The `href` is still derived from `screen`, so this can be used to render a link while dispatching a different action (e.g. a `replace`).
|
|
31
76
|
*/
|
|
32
77
|
export function useLinkProps({
|
|
33
78
|
screen,
|
|
34
79
|
params,
|
|
35
|
-
|
|
36
|
-
|
|
80
|
+
action,
|
|
81
|
+
...rest
|
|
37
82
|
}) {
|
|
38
83
|
const root = React.useContext(NavigationContainerRefContext);
|
|
39
|
-
const navigation = React.useContext(NavigationHelpersContext);
|
|
84
|
+
const navigation = React.useContext(NavigationHelpersContext) ?? root;
|
|
85
|
+
if (navigation == null) {
|
|
86
|
+
throw new Error("Couldn't find a navigation object. Is your component inside NavigationContainer?");
|
|
87
|
+
}
|
|
40
88
|
const {
|
|
41
89
|
options
|
|
42
90
|
} = React.useContext(LinkingContext);
|
|
43
|
-
const onPress = e => {
|
|
44
|
-
|
|
45
|
-
if (Platform.OS !== 'web' || !e) {
|
|
46
|
-
e?.preventDefault?.();
|
|
47
|
-
shouldHandle = true;
|
|
48
|
-
} else {
|
|
49
|
-
// ignore clicks with modifier keys
|
|
91
|
+
const onPress = useLatestCallback(e => {
|
|
92
|
+
if (Platform.OS === 'web' && e) {
|
|
50
93
|
const hasModifierKey = 'metaKey' in e && e.metaKey || 'altKey' in e && e.altKey || 'ctrlKey' in e && e.ctrlKey || 'shiftKey' in e && e.shiftKey;
|
|
51
|
-
|
|
52
|
-
// only handle left clicks
|
|
53
94
|
const isLeftClick = 'button' in e ? e.button == null || e.button === 0 : true;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (!hasModifierKey && isLeftClick && isSelfTarget) {
|
|
58
|
-
e.preventDefault?.();
|
|
59
|
-
shouldHandle = true;
|
|
95
|
+
const isSelfTarget = e.currentTarget && 'target' in e.currentTarget ? [undefined, null, '', '_self'].includes(e.currentTarget.target) : true;
|
|
96
|
+
if (hasModifierKey || !isLeftClick || !isSelfTarget) {
|
|
97
|
+
return;
|
|
60
98
|
}
|
|
61
99
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
throw new Error("Couldn't find a navigation object. Is your component inside NavigationContainer?");
|
|
70
|
-
}
|
|
71
|
-
} else {
|
|
72
|
-
// @ts-expect-error This is already type-checked by the prop types
|
|
73
|
-
navigation?.navigate(screen, params);
|
|
100
|
+
e?.preventDefault();
|
|
101
|
+
let cloned;
|
|
102
|
+
if (action != null) {
|
|
103
|
+
cloned = clone(action);
|
|
104
|
+
} else {
|
|
105
|
+
if (screen == null) {
|
|
106
|
+
throw new Error("Couldn't find a screen to navigate to. Make sure to provide a screen name.");
|
|
74
107
|
}
|
|
108
|
+
cloned = clone(
|
|
109
|
+
// @ts-expect-error This is already type-checked by the prop types
|
|
110
|
+
CommonActions.navigate(screen, params));
|
|
75
111
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
112
|
+
navigation.dispatch(cloned);
|
|
113
|
+
});
|
|
114
|
+
const target = useDeepStableValue(Platform.OS === 'web' ? typeof screen === 'string' ? {
|
|
115
|
+
screen,
|
|
116
|
+
params: typeof params === 'object' && params != null ? params : undefined
|
|
117
|
+
} : getTargetFromAction(action, options?.config?.screens) : undefined);
|
|
118
|
+
const href = React.useMemo(() => {
|
|
119
|
+
if (Platform.OS !== 'web' || rest.href != null || target == null) {
|
|
120
|
+
return rest.href;
|
|
121
|
+
}
|
|
122
|
+
const getPath = options?.getPathFromState ?? getPathFromState;
|
|
123
|
+
return getPath({
|
|
80
124
|
routes: [{
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
// @ts-expect-error this is fine 🔥
|
|
84
|
-
params: params,
|
|
85
|
-
// @ts-expect-error this is fine 🔥
|
|
86
|
-
state: getStateFromParams(params)
|
|
125
|
+
name: target.screen,
|
|
126
|
+
params: target.params
|
|
87
127
|
}]
|
|
88
|
-
}, options?.config)
|
|
128
|
+
}, options?.config);
|
|
129
|
+
}, [rest.href, target, options?.getPathFromState, options?.config]);
|
|
130
|
+
return {
|
|
131
|
+
href,
|
|
89
132
|
role: 'link',
|
|
90
133
|
onPress
|
|
91
134
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["getPathFromState","NavigationContainerRefContext","NavigationHelpersContext","React","Platform","LinkingContext","
|
|
1
|
+
{"version":3,"names":["CommonActions","getPathFromState","NavigationContainerRefContext","NavigationHelpersContext","React","Platform","useLatestCallback","LinkingContext","useDeepStableValue","NESTED_KEYS","NESTED_PARAMS_KEYS","NAVIGATE_ACTION_TYPES","clone","value","keys","clones","existing","get","isArray","Array","screen","state","routes","copy","undefined","WeakMap","set","key","nested","Reflect","cloned","Object","assign","getTargetFromAction","action","screens","payload","includes","type","name","params","useLinkProps","rest","root","useContext","navigation","Error","options","onPress","e","OS","hasModifierKey","metaKey","altKey","ctrlKey","shiftKey","isLeftClick","button","isSelfTarget","currentTarget","target","preventDefault","navigate","dispatch","config","href","useMemo","getPath","role"],"sourceRoot":"../../src","sources":["useLinkProps.tsx"],"mappings":";;AAAA,SACEA,aAAa,EACbC,gBAAgB,EAEhBC,6BAA6B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AACuB,IAAI;IAClB;IAEA,MAAME,OAAO,GAAGpB,OAAO,EAAE9C,gBAAgB,IAAIA,gBAAgB;IAE7D,OAAOkE,OAAO,CACZ;MACE7C,MAAM,EAAE,CACN;QACEiB,IAAI,EAAEqB,MAAM,CAACxC,MAAM;QACnBoB,MAAM,EAAEoB,MAAM,CAACpB;MACjB,CAAC;IAEL,CAAC,EACDO,OAAO,EAAEiB,MACX,CAAC;EACH,CAAC,EAAE,CAACtB,IAAI,CAACuB,IAAI,EAAEL,MAAM,EAAEb,OAAO,EAAE9C,gBAAgB,EAAE8C,OAAO,EAAEiB,MAAM,CAAC,CAAC;EAEnE,OAAO;IACLC,IAAI;IACJG,IAAI,EAAE,MAAe;IACrBpB;EACF,CAAC;AACH","ignoreList":[]}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { type GestureResponderEvent, Text, type TextProps } from 'react-native';
|
|
3
3
|
import { type LinkProps } from './useLinkProps.js';
|
|
4
|
+
type PressEvent = React.MouseEvent<HTMLAnchorElement, MouseEvent> | GestureResponderEvent;
|
|
4
5
|
type Props<ParamList extends ReactNavigation.RootParamList> = LinkProps<ParamList> & Omit<TextProps, 'disabled'> & {
|
|
5
6
|
target?: string;
|
|
6
|
-
onPress?: (e:
|
|
7
|
-
disabled?: boolean |
|
|
7
|
+
onPress?: (e: PressEvent) => void;
|
|
8
|
+
disabled?: boolean | undefined;
|
|
8
9
|
children: React.ReactNode;
|
|
9
10
|
};
|
|
10
11
|
/**
|
|
@@ -14,9 +15,9 @@ type Props<ParamList extends ReactNavigation.RootParamList> = LinkProps<ParamLis
|
|
|
14
15
|
* @param props.screen Name of the screen to navigate to (e.g. `'Feeds'`).
|
|
15
16
|
* @param props.params Params to pass to the screen to navigate to (e.g. `{ sort: 'hot' }`).
|
|
16
17
|
* @param props.href Optional absolute path to use for the href (e.g. `/feeds/hot`).
|
|
17
|
-
* @param props.action Optional action to
|
|
18
|
+
* @param props.action Optional action to override the in-page navigation. The `href` is still derived from `screen`, so this can be used to render a link while dispatching a different action (e.g. a `replace`).
|
|
18
19
|
* @param props.children Child elements to render the content.
|
|
19
20
|
*/
|
|
20
|
-
export declare function Link<ParamList extends ReactNavigation.RootParamList>({ screen, params, action, href, style, ...rest }: Props<ParamList>): React.CElement<TextProps, Text>;
|
|
21
|
+
export declare function Link<ParamList extends ReactNavigation.RootParamList>({ screen, params, action, href, style, target, ...rest }: Props<ParamList>): React.CElement<TextProps, Text>;
|
|
21
22
|
export {};
|
|
22
23
|
//# sourceMappingURL=Link.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Link.d.ts","sourceRoot":"","sources":["../../../src/Link.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EACL,KAAK,qBAAqB,EAE1B,IAAI,EACJ,KAAK,SAAS,EACf,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,KAAK,SAAS,EAAgB,MAAM,mBAAgB,CAAC;AAE9D,KAAK,KAAK,CAAC,SAAS,SAAS,eAAe,CAAC,aAAa,IACxD,SAAS,CAAC,SAAS,CAAC,GAClB,IAAI,CAAC,SAAS,EAAE,UAAU,CAAC,GAAG;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"Link.d.ts","sourceRoot":"","sources":["../../../src/Link.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EACL,KAAK,qBAAqB,EAE1B,IAAI,EACJ,KAAK,SAAS,EACf,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,KAAK,SAAS,EAAgB,MAAM,mBAAgB,CAAC;AAE9D,KAAK,UAAU,GACX,KAAK,CAAC,UAAU,CAAC,iBAAiB,EAAE,UAAU,CAAC,GAC/C,qBAAqB,CAAC;AAE1B,KAAK,KAAK,CAAC,SAAS,SAAS,eAAe,CAAC,aAAa,IACxD,SAAS,CAAC,SAAS,CAAC,GAClB,IAAI,CAAC,SAAS,EAAE,UAAU,CAAC,GAAG;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,UAAU,KAAK,IAAI,CAAC;IAClC,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC/B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B,CAAC;AAEN;;;;;;;;;GASG;AACH,wBAAgB,IAAI,CAAC,SAAS,SAAS,eAAe,CAAC,aAAa,EAAE,EACpE,MAAM,EACN,MAAM,EACN,MAAM,EACN,IAAI,EACJ,KAAK,EACL,MAAM,EACN,GAAG,IAAI,EACR,EAAE,KAAK,CAAC,SAAS,CAAC,mCA4ClB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type DefaultNavigatorOptions, type NavigationListBase, type ParamListBase, type StackNavigationState, type TypedNavigator } from '@react-navigation/core';
|
|
2
|
-
declare const StackNavigator: (props: DefaultNavigatorOptions<ParamListBase, string | undefined, StackNavigationState<ParamListBase>, {}, {}, unknown>) => import("react
|
|
2
|
+
declare const StackNavigator: (props: DefaultNavigatorOptions<ParamListBase, string | undefined, StackNavigationState<ParamListBase>, {}, {}, unknown>) => import("react").JSX.Element;
|
|
3
3
|
export declare function createStackNavigator<ParamList extends ParamListBase>(): TypedNavigator<{
|
|
4
4
|
ParamList: ParamList;
|
|
5
5
|
NavigatorID: string | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createStackNavigator.d.ts","sourceRoot":"","sources":["../../../../src/__stubs__/createStackNavigator.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,kBAAkB,EACvB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EAEzB,KAAK,cAAc,EAEpB,MAAM,wBAAwB,CAAC;AAEhC,QAAA,MAAM,cAAc,GAClB,OAAO,uBAAuB,CAC5B,aAAa,EACb,MAAM,GAAG,SAAS,EAClB,oBAAoB,CAAC,aAAa,CAAC,EACnC,EAAE,EACF,EAAE,EACF,OAAO,CACR,
|
|
1
|
+
{"version":3,"file":"createStackNavigator.d.ts","sourceRoot":"","sources":["../../../../src/__stubs__/createStackNavigator.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,kBAAkB,EACvB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EAEzB,KAAK,cAAc,EAEpB,MAAM,wBAAwB,CAAC;AAEhC,QAAA,MAAM,cAAc,GAClB,OAAO,uBAAuB,CAC5B,aAAa,EACb,MAAM,GAAG,SAAS,EAClB,oBAAoB,CAAC,aAAa,CAAC,EACnC,EAAE,EACF,EAAE,EACF,OAAO,CACR,gCAYF,CAAC;AAEF,wBAAgB,oBAAoB,CAClC,SAAS,SAAS,aAAa,KAC5B,cAAc,CAAC;IAClB,SAAS,EAAE,SAAS,CAAC;IACrB,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IAChC,KAAK,EAAE,oBAAoB,CAAC,SAAS,CAAC,CAAC;IACvC,aAAa,EAAE,EAAE,CAAC;IAClB,QAAQ,EAAE,EAAE,CAAC;IACb,cAAc,EAAE,kBAAkB,CAAC,SAAS,CAAC,CAAC;IAC9C,SAAS,EAAE,OAAO,cAAc,CAAC;CAClC,CAAC,CAED"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMemoryHistory.d.ts","sourceRoot":"","sources":["../../../src/createMemoryHistory.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAG9D,KAAK,aAAa,GAAG;IAEnB,EAAE,EAAE,MAAM,CAAC;IAEX,KAAK,EAAE,eAAe,CAAC;IAEvB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAIF,wBAAgB,mBAAmB;oBAmBlB,MAAM;eAcR,MAAM;wBAIG;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE;0BAmBd;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,eAAe,CAAA;KAAE;6BAoBrC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,eAAe,CAAA;KAAE;UAwC3D,MAAM;
|
|
1
|
+
{"version":3,"file":"createMemoryHistory.d.ts","sourceRoot":"","sources":["../../../src/createMemoryHistory.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAG9D,KAAK,aAAa,GAAG;IAEnB,EAAE,EAAE,MAAM,CAAC;IAEX,KAAK,EAAE,eAAe,CAAC;IAEvB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAIF,wBAAgB,mBAAmB;oBAmBlB,MAAM;eAcR,MAAM;wBAIG;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE;0BAmBd;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,eAAe,CAAA;KAAE;6BAoBrC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,eAAe,CAAA;KAAE;UAwC3D,MAAM;qBAkGK,MAAM,IAAI;EAqB9B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDeepStableValue.d.ts","sourceRoot":"","sources":["../../../src/useDeepStableValue.tsx"],"names":[],"mappings":"AAGA,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAQjD"}
|
|
@@ -22,10 +22,19 @@ export type LinkProps<ParamList extends ReactNavigation.RootParamList, RouteName
|
|
|
22
22
|
* @param props.screen Name of the screen to navigate to (e.g. `'Feeds'`).
|
|
23
23
|
* @param props.params Params to pass to the screen to navigate to (e.g. `{ sort: 'hot' }`).
|
|
24
24
|
* @param props.href Optional absolute path to use for the href (e.g. `/feeds/hot`).
|
|
25
|
-
* @param props.action Optional action to
|
|
25
|
+
* @param props.action Optional action to override the in-page navigation. The `href` is still derived from `screen`, so this can be used to render a link while dispatching a different action (e.g. a `replace`).
|
|
26
26
|
*/
|
|
27
|
-
export declare function useLinkProps<ParamList extends ReactNavigation.RootParamList>({ screen, params,
|
|
28
|
-
href: string |
|
|
27
|
+
export declare function useLinkProps<ParamList extends ReactNavigation.RootParamList>({ screen, params, action, ...rest }: LinkProps<ParamList>): {
|
|
28
|
+
href: string | ({
|
|
29
|
+
href?: string;
|
|
30
|
+
action?: NavigationAction;
|
|
31
|
+
} & (keyof ParamList extends infer T ? T extends keyof ParamList ? T extends unknown ? undefined extends ParamList[T] ? {
|
|
32
|
+
screen: T;
|
|
33
|
+
params?: ParamList[T] | undefined;
|
|
34
|
+
} : {
|
|
35
|
+
screen: T;
|
|
36
|
+
params: ParamList[T];
|
|
37
|
+
} : never : never : never))["href"] | undefined;
|
|
29
38
|
role: "link";
|
|
30
39
|
onPress: (e?: React.MouseEvent<HTMLAnchorElement, MouseEvent> | GestureResponderEvent) => void;
|
|
31
40
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLinkProps.d.ts","sourceRoot":"","sources":["../../../src/useLinkProps.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"useLinkProps.d.ts","sourceRoot":"","sources":["../../../src/useLinkProps.tsx"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,gBAAgB,EAGtB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,KAAK,qBAAqB,EAAY,MAAM,cAAc,CAAC;AAMpE,MAAM,MAAM,SAAS,CACnB,SAAS,SAAS,eAAe,CAAC,aAAa,EAC/C,SAAS,SAAS,MAAM,SAAS,GAAG,MAAM,SAAS,IAEjD,CAAC;IACC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,gBAAgB,CAAC;CAC3B,GAAG,CAAC,SAAS,SAAS,OAAO,GAC1B,SAAS,SAAS,SAAS,CAAC,SAAS,CAAC,GACpC;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAA;CAAE,GACpD;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,SAAS,CAAC,SAAS,CAAC,CAAA;CAAE,GACrD,KAAK,CAAC,CAAC,GACX;IACE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,gBAAgB,CAAC;IACzB,MAAM,CAAC,EAAE,SAAS,CAAC;IACnB,MAAM,CAAC,EAAE,SAAS,CAAC;CACpB,CAAC;AAkHN;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,SAAS,SAAS,eAAe,CAAC,aAAa,EAAE,EAC5E,MAAM,EACN,MAAM,EACN,MAAM,EACN,GAAG,IAAI,EACR,EAAE,SAAS,CAAC,SAAS,CAAC;;eA3IV,MAAM;iBACJ,gBAAgB;;;;;;;;;kBAyJrB,KAAK,CAAC,UAAU,CAAC,iBAAiB,EAAE,UAAU,CAAC,GAC/C,qBAAqB;EAkF9B"}
|
|
@@ -11,7 +11,7 @@ export declare function useLinking(ref: React.RefObject<NavigationContainerRef<P
|
|
|
11
11
|
routes: import("@react-navigation/routers").NavigationRoute<ParamListBase, string>[];
|
|
12
12
|
type: string;
|
|
13
13
|
stale: false;
|
|
14
|
-
}>, "
|
|
14
|
+
}>, "routes" | "stale">> & Readonly<{
|
|
15
15
|
stale?: true;
|
|
16
16
|
routes: import("@react-navigation/routers").PartialRoute<import("@react-navigation/routers").Route<string, object | undefined>>[];
|
|
17
17
|
}> & {
|
|
@@ -23,7 +23,7 @@ export declare function useLinking(ref: React.RefObject<NavigationContainerRef<P
|
|
|
23
23
|
routes: import("@react-navigation/routers").NavigationRoute<ParamListBase, string>[];
|
|
24
24
|
type: string;
|
|
25
25
|
stale: false;
|
|
26
|
-
}>, "
|
|
26
|
+
}>, "routes" | "stale">> & Readonly<{
|
|
27
27
|
stale?: true;
|
|
28
28
|
routes: import("@react-navigation/routers").PartialRoute<import("@react-navigation/routers").Route<string, object | undefined>>[];
|
|
29
29
|
}> & /*elided*/ any;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-navigation/native",
|
|
3
3
|
"description": "React Native integration for React Navigation",
|
|
4
|
-
"version": "7.3.
|
|
4
|
+
"version": "7.3.9",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-native",
|
|
7
7
|
"react-navigation",
|
|
@@ -42,25 +42,25 @@
|
|
|
42
42
|
"clean": "del lib"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@react-navigation/core": "^7.21.
|
|
45
|
+
"@react-navigation/core": "^7.21.6",
|
|
46
46
|
"escape-string-regexp": "^4.0.0",
|
|
47
47
|
"fast-deep-equal": "^3.1.3",
|
|
48
48
|
"nanoid": "^3.3.11",
|
|
49
|
-
"standard-navigation": "^0.0.
|
|
49
|
+
"standard-navigation": "^0.0.8",
|
|
50
50
|
"use-latest-callback": "^0.2.4"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@jest/globals": "^30.0.0",
|
|
54
54
|
"@testing-library/react-native": "^13.2.1",
|
|
55
|
-
"@types/react": "~19.
|
|
56
|
-
"@types/react-dom": "~19.
|
|
55
|
+
"@types/react": "~19.2.4",
|
|
56
|
+
"@types/react-dom": "~19.2.3",
|
|
57
57
|
"del-cli": "^6.0.0",
|
|
58
|
-
"react": "19.
|
|
59
|
-
"react-dom": "19.
|
|
60
|
-
"react-native": "0.
|
|
58
|
+
"react": "19.2.3",
|
|
59
|
+
"react-dom": "19.2.3",
|
|
60
|
+
"react-native": "0.85.3",
|
|
61
61
|
"react-native-builder-bob": "^0.42.1",
|
|
62
|
-
"react-test-renderer": "19.
|
|
63
|
-
"typescript": "
|
|
62
|
+
"react-test-renderer": "19.2.3",
|
|
63
|
+
"typescript": "~6.0.3"
|
|
64
64
|
},
|
|
65
65
|
"peerDependencies": {
|
|
66
66
|
"react": ">= 18.2.0",
|
|
@@ -84,5 +84,5 @@
|
|
|
84
84
|
]
|
|
85
85
|
]
|
|
86
86
|
},
|
|
87
|
-
"gitHead": "
|
|
87
|
+
"gitHead": "d9dafe23ab092e3e76fab1e35dbb1973f45107ca"
|
|
88
88
|
}
|
package/src/Link.tsx
CHANGED
|
@@ -9,16 +9,16 @@ import {
|
|
|
9
9
|
|
|
10
10
|
import { type LinkProps, useLinkProps } from './useLinkProps';
|
|
11
11
|
|
|
12
|
+
type PressEvent =
|
|
13
|
+
| React.MouseEvent<HTMLAnchorElement, MouseEvent>
|
|
14
|
+
| GestureResponderEvent;
|
|
15
|
+
|
|
12
16
|
type Props<ParamList extends ReactNavigation.RootParamList> =
|
|
13
17
|
LinkProps<ParamList> &
|
|
14
18
|
Omit<TextProps, 'disabled'> & {
|
|
15
19
|
target?: string;
|
|
16
|
-
onPress?: (
|
|
17
|
-
|
|
18
|
-
| React.MouseEvent<HTMLAnchorElement, MouseEvent>
|
|
19
|
-
| GestureResponderEvent
|
|
20
|
-
) => void;
|
|
21
|
-
disabled?: boolean | null;
|
|
20
|
+
onPress?: (e: PressEvent) => void;
|
|
21
|
+
disabled?: boolean | undefined;
|
|
22
22
|
children: React.ReactNode;
|
|
23
23
|
};
|
|
24
24
|
|
|
@@ -29,7 +29,7 @@ type Props<ParamList extends ReactNavigation.RootParamList> =
|
|
|
29
29
|
* @param props.screen Name of the screen to navigate to (e.g. `'Feeds'`).
|
|
30
30
|
* @param props.params Params to pass to the screen to navigate to (e.g. `{ sort: 'hot' }`).
|
|
31
31
|
* @param props.href Optional absolute path to use for the href (e.g. `/feeds/hot`).
|
|
32
|
-
* @param props.action Optional action to
|
|
32
|
+
* @param props.action Optional action to override the in-page navigation. The `href` is still derived from `screen`, so this can be used to render a link while dispatching a different action (e.g. a `replace`).
|
|
33
33
|
* @param props.children Child elements to render the content.
|
|
34
34
|
*/
|
|
35
35
|
export function Link<ParamList extends ReactNavigation.RootParamList>({
|
|
@@ -38,15 +38,23 @@ export function Link<ParamList extends ReactNavigation.RootParamList>({
|
|
|
38
38
|
action,
|
|
39
39
|
href,
|
|
40
40
|
style,
|
|
41
|
+
target,
|
|
41
42
|
...rest
|
|
42
43
|
}: Props<ParamList>) {
|
|
43
|
-
const { colors, fonts } = useTheme();
|
|
44
44
|
// @ts-expect-error: This is already type-checked by the prop types
|
|
45
45
|
const props = useLinkProps<ParamList>({ screen, params, action, href });
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
// Keep usage of `useTheme` after `useLinkProps`
|
|
48
|
+
// This ensures proper error when used outside of navigation container
|
|
49
|
+
const { colors, fonts } = useTheme();
|
|
50
|
+
|
|
51
|
+
const onPress = (e: PressEvent) => {
|
|
52
|
+
if (rest.disabled) {
|
|
53
|
+
e.preventDefault();
|
|
54
|
+
e.stopPropagation();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
50
58
|
if ('onPress' in rest) {
|
|
51
59
|
rest.onPress?.(e);
|
|
52
60
|
}
|
|
@@ -61,7 +69,17 @@ export function Link<ParamList extends ReactNavigation.RootParamList>({
|
|
|
61
69
|
...props,
|
|
62
70
|
...rest,
|
|
63
71
|
...Platform.select({
|
|
64
|
-
web: {
|
|
72
|
+
web: {
|
|
73
|
+
'aria-disabled': rest.disabled,
|
|
74
|
+
'onAuxClick': rest.disabled
|
|
75
|
+
? (e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
|
|
76
|
+
e.preventDefault();
|
|
77
|
+
e.stopPropagation();
|
|
78
|
+
}
|
|
79
|
+
: undefined,
|
|
80
|
+
'onClick': onPress,
|
|
81
|
+
'hrefAttrs': { target },
|
|
82
|
+
},
|
|
65
83
|
default: { onPress },
|
|
66
84
|
}),
|
|
67
85
|
style: [{ color: colors.primary }, fonts.regular, style],
|
|
@@ -195,6 +195,8 @@ export function createMemoryHistory() {
|
|
|
195
195
|
// But on Firefox, it seems to take much longer, around 50ms from our testing
|
|
196
196
|
// We're using a hacky timeout since there doesn't seem to be way to know for sure
|
|
197
197
|
const timer = setTimeout(() => {
|
|
198
|
+
window.removeEventListener('popstate', onPopState);
|
|
199
|
+
|
|
198
200
|
const foundIndex = pending.findIndex((it) => it.ref === done);
|
|
199
201
|
|
|
200
202
|
if (foundIndex > -1) {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import isEqual from 'fast-deep-equal';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
|
|
4
|
+
export function useDeepStableValue<T>(value: T): T {
|
|
5
|
+
const valueRef = React.useRef(value);
|
|
6
|
+
|
|
7
|
+
if (!isEqual(valueRef.current, value)) {
|
|
8
|
+
valueRef.current = value;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return valueRef.current;
|
|
12
|
+
}
|
package/src/useLinkProps.tsx
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
+
CommonActions,
|
|
2
3
|
getPathFromState,
|
|
3
4
|
type NavigationAction,
|
|
4
5
|
NavigationContainerRefContext,
|
|
5
6
|
NavigationHelpersContext,
|
|
6
|
-
type NavigatorScreenParams,
|
|
7
|
-
type ParamListBase,
|
|
8
7
|
} from '@react-navigation/core';
|
|
9
|
-
import type { NavigationState, PartialState } from '@react-navigation/routers';
|
|
10
8
|
import * as React from 'react';
|
|
11
9
|
import { type GestureResponderEvent, Platform } from 'react-native';
|
|
10
|
+
import useLatestCallback from 'use-latest-callback';
|
|
12
11
|
|
|
13
12
|
import { LinkingContext } from './LinkingContext';
|
|
13
|
+
import { useDeepStableValue } from './useDeepStableValue';
|
|
14
14
|
|
|
15
15
|
export type LinkProps<
|
|
16
16
|
ParamList extends ReactNavigation.RootParamList,
|
|
@@ -31,33 +31,116 @@ export type LinkProps<
|
|
|
31
31
|
params?: undefined;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
const NESTED_KEYS = ['payload', 'params', 'state', 'routes'] as const;
|
|
35
|
+
const NESTED_PARAMS_KEYS = ['params', 'state'] as const;
|
|
36
|
+
const NAVIGATE_ACTION_TYPES = [
|
|
37
|
+
'NAVIGATE',
|
|
38
|
+
'PUSH',
|
|
39
|
+
'REPLACE',
|
|
40
|
+
'POP_TO',
|
|
41
|
+
'JUMP_TO',
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Nested params are tracked as consumed after handling
|
|
46
|
+
* So navigating with same params again won't work
|
|
47
|
+
* This can happen if the action or params are memoized
|
|
48
|
+
* Or component hasn't re-renderd to re-create inline objects
|
|
49
|
+
* So we clone the action and params when necessary before dispatch
|
|
50
|
+
*/
|
|
51
|
+
function clone<T>(value: T): T;
|
|
52
|
+
function clone(
|
|
53
|
+
value: unknown,
|
|
54
|
+
keys: typeof NESTED_KEYS | typeof NESTED_PARAMS_KEYS,
|
|
55
|
+
clones: WeakMap<object, object>
|
|
56
|
+
): unknown;
|
|
57
|
+
function clone(
|
|
58
|
+
value: unknown,
|
|
59
|
+
keys: typeof NESTED_KEYS | typeof NESTED_PARAMS_KEYS = NESTED_KEYS,
|
|
60
|
+
clones?: WeakMap<object, object>
|
|
61
|
+
): unknown {
|
|
62
|
+
if (typeof value !== 'object' || value == null) {
|
|
63
|
+
return value;
|
|
39
64
|
}
|
|
40
65
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
const existing = clones?.get(value);
|
|
67
|
+
|
|
68
|
+
if (existing) {
|
|
69
|
+
return existing;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const isArray = Array.isArray(value);
|
|
73
|
+
|
|
74
|
+
if (
|
|
75
|
+
keys === NESTED_PARAMS_KEYS &&
|
|
76
|
+
'screen' in value &&
|
|
77
|
+
typeof value.screen !== 'string' &&
|
|
78
|
+
!(
|
|
79
|
+
'state' in value &&
|
|
80
|
+
typeof value.state === 'object' &&
|
|
81
|
+
value.state != null &&
|
|
82
|
+
'routes' in value.state &&
|
|
83
|
+
Array.isArray(value.state.routes)
|
|
84
|
+
)
|
|
85
|
+
) {
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let copy: object | undefined =
|
|
90
|
+
keys === NESTED_PARAMS_KEYS ? { ...value } : undefined;
|
|
91
|
+
|
|
92
|
+
clones ??= new WeakMap();
|
|
93
|
+
clones.set(value, copy ?? value);
|
|
94
|
+
|
|
95
|
+
for (const key of isArray ? value.keys() : keys) {
|
|
96
|
+
const nested: unknown = Reflect.get(value, key);
|
|
97
|
+
|
|
98
|
+
const cloned = clone(
|
|
99
|
+
nested,
|
|
100
|
+
key === 'params' ? NESTED_PARAMS_KEYS : NESTED_KEYS,
|
|
101
|
+
clones
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (cloned !== nested) {
|
|
105
|
+
copy ??= isArray ? [...value] : { ...value };
|
|
106
|
+
clones.set(value, copy);
|
|
107
|
+
|
|
108
|
+
Object.assign(copy, { [key]: cloned });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return copy ?? value;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const getTargetFromAction = (
|
|
116
|
+
action: NavigationAction | undefined,
|
|
117
|
+
screens: Record<string, unknown> | undefined
|
|
118
|
+
) => {
|
|
119
|
+
if (action === undefined || screens === undefined) {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const { payload } = action;
|
|
124
|
+
|
|
125
|
+
if (
|
|
126
|
+
!NAVIGATE_ACTION_TYPES.includes(action.type) ||
|
|
127
|
+
payload === undefined ||
|
|
128
|
+
!('name' in payload) ||
|
|
129
|
+
typeof payload.name !== 'string' ||
|
|
130
|
+
!(payload.name in screens)
|
|
131
|
+
) {
|
|
132
|
+
return undefined;
|
|
58
133
|
}
|
|
59
134
|
|
|
60
|
-
return
|
|
135
|
+
return {
|
|
136
|
+
screen: payload.name,
|
|
137
|
+
params:
|
|
138
|
+
'params' in payload &&
|
|
139
|
+
typeof payload.params === 'object' &&
|
|
140
|
+
payload.params != null
|
|
141
|
+
? payload.params
|
|
142
|
+
: undefined,
|
|
143
|
+
};
|
|
61
144
|
};
|
|
62
145
|
|
|
63
146
|
/**
|
|
@@ -66,90 +149,108 @@ const getStateFromParams = (
|
|
|
66
149
|
* @param props.screen Name of the screen to navigate to (e.g. `'Feeds'`).
|
|
67
150
|
* @param props.params Params to pass to the screen to navigate to (e.g. `{ sort: 'hot' }`).
|
|
68
151
|
* @param props.href Optional absolute path to use for the href (e.g. `/feeds/hot`).
|
|
69
|
-
* @param props.action Optional action to
|
|
152
|
+
* @param props.action Optional action to override the in-page navigation. The `href` is still derived from `screen`, so this can be used to render a link while dispatching a different action (e.g. a `replace`).
|
|
70
153
|
*/
|
|
71
154
|
export function useLinkProps<ParamList extends ReactNavigation.RootParamList>({
|
|
72
155
|
screen,
|
|
73
156
|
params,
|
|
74
|
-
href,
|
|
75
157
|
action,
|
|
158
|
+
...rest
|
|
76
159
|
}: LinkProps<ParamList>) {
|
|
77
160
|
const root = React.useContext(NavigationContainerRefContext);
|
|
78
|
-
const navigation = React.useContext(NavigationHelpersContext);
|
|
161
|
+
const navigation = React.useContext(NavigationHelpersContext) ?? root;
|
|
162
|
+
|
|
163
|
+
if (navigation == null) {
|
|
164
|
+
throw new Error(
|
|
165
|
+
"Couldn't find a navigation object. Is your component inside NavigationContainer?"
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
79
169
|
const { options } = React.useContext(LinkingContext);
|
|
80
170
|
|
|
81
|
-
const onPress = (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
e
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
: true;
|
|
106
|
-
|
|
107
|
-
if (!hasModifierKey && isLeftClick && isSelfTarget) {
|
|
108
|
-
e.preventDefault?.();
|
|
109
|
-
shouldHandle = true;
|
|
171
|
+
const onPress = useLatestCallback(
|
|
172
|
+
(
|
|
173
|
+
e?:
|
|
174
|
+
| React.MouseEvent<HTMLAnchorElement, MouseEvent>
|
|
175
|
+
| GestureResponderEvent
|
|
176
|
+
) => {
|
|
177
|
+
if (Platform.OS === 'web' && e) {
|
|
178
|
+
const hasModifierKey =
|
|
179
|
+
('metaKey' in e && e.metaKey) ||
|
|
180
|
+
('altKey' in e && e.altKey) ||
|
|
181
|
+
('ctrlKey' in e && e.ctrlKey) ||
|
|
182
|
+
('shiftKey' in e && e.shiftKey);
|
|
183
|
+
|
|
184
|
+
const isLeftClick =
|
|
185
|
+
'button' in e ? e.button == null || e.button === 0 : true;
|
|
186
|
+
|
|
187
|
+
const isSelfTarget =
|
|
188
|
+
e.currentTarget && 'target' in e.currentTarget
|
|
189
|
+
? [undefined, null, '', '_self'].includes(e.currentTarget.target)
|
|
190
|
+
: true;
|
|
191
|
+
|
|
192
|
+
if (hasModifierKey || !isLeftClick || !isSelfTarget) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
110
195
|
}
|
|
111
|
-
}
|
|
112
196
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
197
|
+
e?.preventDefault();
|
|
198
|
+
|
|
199
|
+
let cloned: NavigationAction;
|
|
200
|
+
|
|
201
|
+
if (action != null) {
|
|
202
|
+
cloned = clone(action);
|
|
203
|
+
} else {
|
|
204
|
+
if (screen == null) {
|
|
120
205
|
throw new Error(
|
|
121
|
-
"Couldn't find a
|
|
206
|
+
"Couldn't find a screen to navigate to. Make sure to provide a screen name."
|
|
122
207
|
);
|
|
123
208
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
209
|
+
|
|
210
|
+
cloned = clone(
|
|
211
|
+
// @ts-expect-error This is already type-checked by the prop types
|
|
212
|
+
CommonActions.navigate(screen, params)
|
|
213
|
+
);
|
|
127
214
|
}
|
|
215
|
+
|
|
216
|
+
navigation.dispatch(cloned);
|
|
128
217
|
}
|
|
129
|
-
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
const target = useDeepStableValue(
|
|
221
|
+
Platform.OS === 'web'
|
|
222
|
+
? typeof screen === 'string'
|
|
223
|
+
? {
|
|
224
|
+
screen,
|
|
225
|
+
params:
|
|
226
|
+
typeof params === 'object' && params != null ? params : undefined,
|
|
227
|
+
}
|
|
228
|
+
: getTargetFromAction(action, options?.config?.screens)
|
|
229
|
+
: undefined
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const href = React.useMemo(() => {
|
|
233
|
+
if (Platform.OS !== 'web' || rest.href != null || target == null) {
|
|
234
|
+
return rest.href;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const getPath = options?.getPathFromState ?? getPathFromState;
|
|
130
238
|
|
|
131
|
-
|
|
239
|
+
return getPath(
|
|
240
|
+
{
|
|
241
|
+
routes: [
|
|
242
|
+
{
|
|
243
|
+
name: target.screen,
|
|
244
|
+
params: target.params,
|
|
245
|
+
},
|
|
246
|
+
],
|
|
247
|
+
},
|
|
248
|
+
options?.config
|
|
249
|
+
);
|
|
250
|
+
}, [rest.href, target, options?.getPathFromState, options?.config]);
|
|
132
251
|
|
|
133
252
|
return {
|
|
134
|
-
href
|
|
135
|
-
href ??
|
|
136
|
-
(Platform.OS === 'web' && screen != null
|
|
137
|
-
? getPathFromStateHelper(
|
|
138
|
-
{
|
|
139
|
-
routes: [
|
|
140
|
-
{
|
|
141
|
-
// @ts-expect-error this is fine 🔥
|
|
142
|
-
name: screen,
|
|
143
|
-
// @ts-expect-error this is fine 🔥
|
|
144
|
-
params: params,
|
|
145
|
-
// @ts-expect-error this is fine 🔥
|
|
146
|
-
state: getStateFromParams(params),
|
|
147
|
-
},
|
|
148
|
-
],
|
|
149
|
-
},
|
|
150
|
-
options?.config
|
|
151
|
-
)
|
|
152
|
-
: undefined),
|
|
253
|
+
href,
|
|
153
254
|
role: 'link' as const,
|
|
154
255
|
onPress,
|
|
155
256
|
};
|