UNPKG

5.28 kBJavaScriptView Raw
1import React from 'react';
2import { useResponse, useURL, useNavigationHandler, useStatefulNavigationHandler } from '@curi/react-universal';
3export * from '@curi/react-universal';
4
5function useNavigationFocus(ref, props) {
6 if (props === void 0) { props = {}; }
7 // The response isn't actually used, but the app should only
8 // re-focus when the response changes. The preserve and preventScroll
9 // values are used, but not used in the comparison array because
10 // changing these values would steal the app's focus even though
11 // the location hasn't changed.
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 // @ts-ignore
32 ele.focus({ preventScroll: preventScroll });
33 }, [response]);
34}
35
36/*! *****************************************************************************
37Copyright (c) Microsoft Corporation. All rights reserved.
38Licensed under the Apache License, Version 2.0 (the "License"); you may not use
39this file except in compliance with the License. You may obtain a copy of the
40License at http://www.apache.org/licenses/LICENSE-2.0
41
42THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
43KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
44WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
45MERCHANTABLITY OR NON-INFRINGEMENT.
46
47See the Apache Version 2.0 License for specific language governing permissions
48and limitations under the License.
49***************************************************************************** */
50
51var __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
62function __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
72function canNavigate(event, target) {
73 return (!event.defaultPrevented &&
74 !target &&
75 event.button === 0 &&
76 !(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey));
77}
78
79var Link = React.forwardRef(function (props, ref) {
80 var _a;
81 var
82 // url
83 name = props.name, params = props.params, query = props.query, hash = props.hash,
84 // navigation
85 state = props.state, onNav = props.onNav, method = props.method,
86 // props
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});
99var AsyncLink = React.forwardRef(function (props, ref) {
100 var _a, _b;
101 var
102 // url
103 name = props.name, params = props.params, query = props.query, hash = props.hash,
104 // navigation
105 state = props.state, onNav = props.onNav, method = props.method,
106 // props
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
113export { AsyncLink, Link, useNavigationFocus };