UNPKG

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