1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var React = _interopDefault(require('react'));
|
8 | var reactUniversal = require('@curi/react-universal');
|
9 |
|
10 | function useNavigationFocus(ref, props) {
|
11 | if (props === void 0) { props = {}; }
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var response = reactUniversal.useResponse().response;
|
18 | var preserve = props.preserve, _a = props.preventScroll, preventScroll = _a === void 0 ? false : _a;
|
19 | React.useEffect(function () {
|
20 | var ele = ref.current;
|
21 | if (ele === null) {
|
22 | if (process.env.NODE_ENV !== "production") {
|
23 | console.warn("There is no element to focus. Did you forget to add the ref to an element?");
|
24 | }
|
25 | return;
|
26 | }
|
27 | if (preserve && ele.contains(document.activeElement)) {
|
28 | return;
|
29 | }
|
30 | if (process.env.NODE_ENV !== "production") {
|
31 | if (!ele.hasAttribute("tabIndex") && ele.tabIndex === -1) {
|
32 | console.warn('The component that is passed the ref must have a "tabIndex" prop or be focusable by default in order to be focused. ' +
|
33 | "Otherwise, the document's <body> will be focused instead.");
|
34 | }
|
35 | }
|
36 |
|
37 | ele.focus({ preventScroll: preventScroll });
|
38 | }, [response]);
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | var __assign = function() {
|
57 | __assign = Object.assign || function __assign(t) {
|
58 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
59 | s = arguments[i];
|
60 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
61 | }
|
62 | return t;
|
63 | };
|
64 | return __assign.apply(this, arguments);
|
65 | };
|
66 |
|
67 | function __rest(s, e) {
|
68 | var t = {};
|
69 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
70 | t[p] = s[p];
|
71 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
72 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
|
73 | t[p[i]] = s[p[i]];
|
74 | return t;
|
75 | }
|
76 |
|
77 | function canNavigate(event, target) {
|
78 | return (!event.defaultPrevented &&
|
79 | !target &&
|
80 | event.button === 0 &&
|
81 | !(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey));
|
82 | }
|
83 |
|
84 | var Link = React.forwardRef(function (props, ref) {
|
85 | var _a;
|
86 | var
|
87 |
|
88 | name = props.name, params = props.params, query = props.query, hash = props.hash,
|
89 |
|
90 | state = props.state, onNav = props.onNav, method = props.method,
|
91 |
|
92 | children = props.children, Anchor = (_a = props.anchor, _a === void 0 ? "a" : _a), rest = __rest(props, ["name", "params", "query", "hash", "state", "onNav", "method", "children", "anchor"]);
|
93 | var url = reactUniversal.useURL({ name: name, params: params, query: query, hash: hash });
|
94 | var eventHandler = reactUniversal.useNavigationHandler({
|
95 | url: url,
|
96 | state: state,
|
97 | onNav: onNav,
|
98 | method: method,
|
99 | canNavigate: canNavigate,
|
100 | target: rest.target
|
101 | }).eventHandler;
|
102 | return (React.createElement(Anchor, __assign({}, rest, { onClick: eventHandler, href: url, ref: ref }), children));
|
103 | });
|
104 | var AsyncLink = React.forwardRef(function (props, ref) {
|
105 | var _a, _b;
|
106 | var
|
107 |
|
108 | name = props.name, params = props.params, query = props.query, hash = props.hash,
|
109 |
|
110 | state = props.state, onNav = props.onNav, method = props.method,
|
111 |
|
112 | children = props.children, Anchor = (_a = props.anchor, _a === void 0 ? "a" : _a), rest = __rest(props, ["name", "params", "query", "hash", "state", "onNav", "method", "children", "anchor"]);
|
113 | var url = reactUniversal.useURL({ name: name, params: params, query: query, hash: hash });
|
114 | var eventHandler = (_b = reactUniversal.useStatefulNavigationHandler({ url: url, state: state, onNav: onNav, method: method, canNavigate: canNavigate, target: rest.target }), _b.eventHandler), navigating = _b.navigating;
|
115 | return (React.createElement(Anchor, __assign({}, rest, { onClick: eventHandler, href: url, ref: ref }), children(navigating)));
|
116 | });
|
117 |
|
118 | Object.keys(reactUniversal).forEach(function (key) {
|
119 | Object.defineProperty(exports, key, {
|
120 | enumerable: true,
|
121 | get: function () {
|
122 | return reactUniversal[key];
|
123 | }
|
124 | });
|
125 | });
|
126 | exports.AsyncLink = AsyncLink;
|
127 | exports.Link = Link;
|
128 | exports.useNavigationFocus = useNavigationFocus;
|