UNPKG

4.65 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = useLinkProps;
7
8var _core = require("@react-navigation/core");
9
10var React = _interopRequireWildcard(require("react"));
11
12var _reactNative = require("react-native");
13
14var _LinkingContext = _interopRequireDefault(require("./LinkingContext"));
15
16var _useLinkTo = _interopRequireDefault(require("./useLinkTo"));
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
21
22function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
23
24const getStateFromParams = params => {
25 if (params !== null && params !== void 0 && params.state) {
26 return params.state;
27 }
28
29 if (params !== null && params !== void 0 && params.screen) {
30 return {
31 routes: [{
32 name: params.screen,
33 params: params.params,
34 // @ts-expect-error
35 state: params.screen ? getStateFromParams(params.params) : undefined
36 }]
37 };
38 }
39
40 return undefined;
41};
42/**
43 * Hook to get props for an anchor tag so it can work with in page navigation.
44 *
45 * @param props.to Absolute path to screen (e.g. `/feeds/hot`).
46 * @param props.action Optional action to use for in-page navigation. By default, the path is parsed to an action based on linking config.
47 */
48
49
50function useLinkProps(_ref) {
51 var _options$getPathFromS;
52
53 let {
54 to,
55 action
56 } = _ref;
57 const root = React.useContext(_core.NavigationContainerRefContext);
58 const navigation = React.useContext(_core.NavigationHelpersContext);
59 const {
60 options
61 } = React.useContext(_LinkingContext.default);
62 const linkTo = (0, _useLinkTo.default)();
63
64 const onPress = e => {
65 var _e$currentTarget;
66
67 let shouldHandle = false;
68
69 if (_reactNative.Platform.OS !== 'web' || !e) {
70 shouldHandle = e ? !e.defaultPrevented : true;
71 } else if (!e.defaultPrevented && // onPress prevented default
72 // @ts-expect-error: these properties exist on web, but not in React Native
73 !(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) && ( // ignore clicks with modifier keys
74 // @ts-expect-error: these properties exist on web, but not in React Native
75 e.button == null || e.button === 0) && // ignore everything but left clicks
76 // @ts-expect-error: these properties exist on web, but not in React Native
77 [undefined, null, '', 'self'].includes((_e$currentTarget = e.currentTarget) === null || _e$currentTarget === void 0 ? void 0 : _e$currentTarget.target) // let browser handle "target=_blank" etc.
78 ) {
79 e.preventDefault();
80 shouldHandle = true;
81 }
82
83 if (shouldHandle) {
84 if (action) {
85 if (navigation) {
86 navigation.dispatch(action);
87 } else if (root) {
88 root.dispatch(action);
89 } else {
90 throw new Error("Couldn't find a navigation object. Is your component inside NavigationContainer?");
91 }
92 } else {
93 linkTo(to);
94 }
95 }
96 };
97
98 const getPathFromStateHelper = (_options$getPathFromS = options === null || options === void 0 ? void 0 : options.getPathFromState) !== null && _options$getPathFromS !== void 0 ? _options$getPathFromS : _core.getPathFromState;
99 const href = typeof to === 'string' ? to : getPathFromStateHelper({
100 routes: [{
101 name: to.screen,
102 // @ts-expect-error
103 params: to.params,
104 // @ts-expect-error
105 state: getStateFromParams(to.params)
106 }]
107 }, options === null || options === void 0 ? void 0 : options.config);
108 return {
109 href,
110 accessibilityRole: 'link',
111 onPress
112 };
113}
114//# sourceMappingURL=useLinkProps.js.map
\No newline at end of file