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