UNPKG

3.49 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 _useLinkTo = _interopRequireDefault(require("./useLinkTo"));
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18function _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); }
19
20function _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; }
21
22/**
23 * Hook to get props for an anchor tag so it can work with in page navigation.
24 *
25 * @param props.to Absolute path to screen (e.g. `/feeds/hot`).
26 * @param props.action Optional action to use for in-page navigation. By default, the path is parsed to an action based on linking config.
27 */
28function useLinkProps({
29 to,
30 action
31}) {
32 const root = React.useContext(_core.NavigationContainerRefContext);
33 const navigation = React.useContext(_core.NavigationHelpersContext);
34 const linkTo = (0, _useLinkTo.default)();
35
36 const onPress = e => {
37 var _e$currentTarget;
38
39 let shouldHandle = false;
40
41 if (_reactNative.Platform.OS !== 'web' || !e) {
42 shouldHandle = e ? !e.defaultPrevented : true;
43 } else if (!e.defaultPrevented && // onPress prevented default
44 // @ts-expect-error: these properties exist on web, but not in React Native
45 !(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) && ( // ignore clicks with modifier keys
46 // @ts-expect-error: these properties exist on web, but not in React Native
47 e.button == null || e.button === 0) && // ignore everything but left clicks
48 // @ts-expect-error: these properties exist on web, but not in React Native
49 [undefined, null, '', 'self'].includes((_e$currentTarget = e.currentTarget) === null || _e$currentTarget === void 0 ? void 0 : _e$currentTarget.target) // let browser handle "target=_blank" etc.
50 ) {
51 e.preventDefault();
52 shouldHandle = true;
53 }
54
55 if (shouldHandle) {
56 if (action) {
57 if (navigation) {
58 navigation.dispatch(action);
59 } else if (root) {
60 root.dispatch(action);
61 } else {
62 throw new Error("Couldn't find a navigation object. Is your component inside NavigationContainer?");
63 }
64 } else {
65 linkTo(to);
66 }
67 }
68 };
69
70 return {
71 href: to,
72 accessibilityRole: 'link',
73 onPress
74 };
75}
76//# sourceMappingURL=useLinkProps.js.map
\No newline at end of file