UNPKG

5.79 kBJavaScriptView Raw
1function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
3function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
5function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
6
7import "core-js/modules/es.string.starts-with.js";
8import "core-js/modules/es.array.concat.js";
9import "core-js/modules/es.string.search.js";
10import "core-js/modules/es.regexp.exec.js";
11import "core-js/modules/es.object.assign.js";
12import "core-js/modules/es.string.match.js";
13import "core-js/modules/es.object.keys.js";
14import "core-js/modules/es.symbol.js";
15import global from 'global';
16import React, { useCallback } from 'react';
17import { Link, BrowserRouter, useNavigate, useLocation, Router } from 'react-router-dom';
18import { ToggleVisibility } from './visibility';
19import { queryFromString, parsePath, getMatch } from './utils';
20var document = global.document;
21
22var getBase = function getBase() {
23 return "".concat(document.location.pathname, "?");
24};
25
26// const queryNavigate: NavigateFn = (to: string | number, options?: NavigateOptions<{}>) =>
27// typeof to === 'number' ? navigate(to) : navigate(`${getBase()}path=${to}`, options);
28var useQueryNavigate = function useQueryNavigate() {
29 var navigate = useNavigate();
30 return useCallback(function (to, options) {
31 if (typeof to === 'string' && to.startsWith('#')) {
32 document.location.hash = to;
33 return undefined;
34 }
35
36 if (typeof to === 'string') {
37 var target = options !== null && options !== void 0 && options.plain ? to : "?path=".concat(to);
38 return navigate(target, options);
39 }
40
41 if (typeof to === 'number') {
42 return navigate(to);
43 }
44
45 return undefined;
46 }, []);
47}; // A component that will navigate to a new location/path when clicked
48
49
50var QueryLink = function QueryLink(_ref) {
51 var to = _ref.to,
52 children = _ref.children,
53 rest = _objectWithoutProperties(_ref, ["to", "children"]);
54
55 return /*#__PURE__*/React.createElement(Link, _extends({
56 to: "".concat(getBase(), "path=").concat(to)
57 }, rest), children);
58};
59
60QueryLink.displayName = "QueryLink";
61QueryLink.displayName = 'QueryLink'; // A render-prop component where children is called with a location
62// and will be called whenever it changes when it changes
63
64var QueryLocation = function QueryLocation(_ref2) {
65 var children = _ref2.children;
66 var location = useLocation();
67
68 var _queryFromString = queryFromString(location.search),
69 path = _queryFromString.path,
70 singleStory = _queryFromString.singleStory;
71
72 var _parsePath = parsePath(path),
73 viewMode = _parsePath.viewMode,
74 storyId = _parsePath.storyId,
75 refId = _parsePath.refId;
76
77 return /*#__PURE__*/React.createElement(React.Fragment, null, children({
78 path: path,
79 location: location,
80 viewMode: viewMode,
81 storyId: storyId,
82 refId: refId,
83 singleStory: singleStory === 'true'
84 }));
85};
86
87QueryLocation.displayName = 'QueryLocation'; // A render-prop component for rendering when a certain path is hit.
88// It's immensely similar to `Location` but it receives an addition data property: `match`.
89// match has a truthy value when the path is hit.
90
91var QueryMatch = function QueryMatch(_ref3) {
92 var children = _ref3.children,
93 targetPath = _ref3.path,
94 _ref3$startsWith = _ref3.startsWith,
95 startsWith = _ref3$startsWith === void 0 ? false : _ref3$startsWith;
96 return /*#__PURE__*/React.createElement(QueryLocation, null, function (_ref4) {
97 var urlPath = _ref4.path,
98 rest = _objectWithoutProperties(_ref4, ["path"]);
99
100 return children(Object.assign({
101 match: getMatch(urlPath, targetPath, startsWith)
102 }, rest));
103 });
104};
105
106QueryMatch.displayName = "QueryMatch";
107QueryMatch.displayName = 'QueryMatch'; // A component to conditionally render children based on matching a target path
108
109var Route = function Route(_ref5) {
110 var path = _ref5.path,
111 children = _ref5.children,
112 _ref5$startsWith = _ref5.startsWith,
113 startsWith = _ref5$startsWith === void 0 ? false : _ref5$startsWith,
114 _ref5$hideOnly = _ref5.hideOnly,
115 hideOnly = _ref5$hideOnly === void 0 ? false : _ref5$hideOnly;
116 return /*#__PURE__*/React.createElement(QueryMatch, {
117 path: path,
118 startsWith: startsWith
119 }, function (_ref6) {
120 var match = _ref6.match;
121
122 if (hideOnly) {
123 return /*#__PURE__*/React.createElement(ToggleVisibility, {
124 hidden: !match
125 }, children);
126 }
127
128 return match ? children : null;
129 });
130};
131
132Route.displayName = "Route";
133Route.displayName = 'Route';
134export { QueryLink as Link };
135export { QueryMatch as Match };
136export { QueryLocation as Location };
137export { Route };
138export { useQueryNavigate as useNavigate };
139export { BrowserRouter as LocationProvider };
140export { Router as BaseLocationProvider };
141export { useNavigate as usePlainNavigate }; // eslint-disable-next-line no-undef
\No newline at end of file