UNPKG

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