UNPKG

3.13 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = useLinkBuilder;
7
8var _core = require("@react-navigation/core");
9
10var React = _interopRequireWildcard(require("react"));
11
12var _LinkingContext = _interopRequireDefault(require("./LinkingContext"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function _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); }
17
18function _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; }
19
20const getRootStateForNavigate = (navigation, state) => {
21 const parent = navigation.getParent();
22
23 if (parent) {
24 const parentState = parent.getState();
25 return getRootStateForNavigate(parent, {
26 index: 0,
27 routes: [{ ...parentState.routes[parentState.index],
28 state: state
29 }]
30 });
31 }
32
33 return state;
34};
35/**
36 * Build destination link for a navigate action.
37 * Useful for showing anchor tags on the web for buttons that perform navigation.
38 */
39
40
41function useLinkBuilder() {
42 const navigation = React.useContext(_core.NavigationHelpersContext);
43 const linking = React.useContext(_LinkingContext.default);
44 const buildLink = React.useCallback((name, params) => {
45 const {
46 options
47 } = linking;
48
49 if ((options === null || options === void 0 ? void 0 : options.enabled) === false) {
50 return undefined;
51 }
52
53 const state = navigation ? getRootStateForNavigate(navigation, {
54 index: 0,
55 routes: [{
56 name,
57 params
58 }]
59 }) : // If we couldn't find a navigation object in context, we're at root
60 // So we'll construct a basic state object to use
61 {
62 index: 0,
63 routes: [{
64 name,
65 params
66 }]
67 };
68 const path = options !== null && options !== void 0 && options.getPathFromState ? options.getPathFromState(state, options === null || options === void 0 ? void 0 : options.config) : (0, _core.getPathFromState)(state, options === null || options === void 0 ? void 0 : options.config);
69 return path;
70 }, [linking, navigation]);
71 return buildLink;
72}
73//# sourceMappingURL=useLinkBuilder.js.map
\No newline at end of file