UNPKG

5.62 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var React = _interopDefault(require('react'));
8var reactUniversal = require('@curi/react-universal');
9
10function useNavigationFocus(ref, props) {
11 if (props === void 0) { props = {}; }
12 // The response isn't actually used, but the app should only
13 // re-focus when the response changes. The preserve and preventScroll
14 // values are used, but not used in the comparison array because
15 // changing these values would steal the app's focus even though
16 // the location hasn't changed.
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 // @ts-ignore
37 ele.focus({ preventScroll: preventScroll });
38 }, [response]);
39}
40
41/*! *****************************************************************************
42Copyright (c) Microsoft Corporation. All rights reserved.
43Licensed under the Apache License, Version 2.0 (the "License"); you may not use
44this file except in compliance with the License. You may obtain a copy of the
45License at http://www.apache.org/licenses/LICENSE-2.0
46
47THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
48KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
49WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
50MERCHANTABLITY OR NON-INFRINGEMENT.
51
52See the Apache Version 2.0 License for specific language governing permissions
53and limitations under the License.
54***************************************************************************** */
55
56var __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
67function __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
77function canNavigate(event, target) {
78 return (!event.defaultPrevented &&
79 !target &&
80 event.button === 0 &&
81 !(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey));
82}
83
84var Link = React.forwardRef(function (props, ref) {
85 var
86 // url
87 name = props.name, params = props.params, query = props.query, hash = props.hash,
88 // navigation
89 state = props.state, onNav = props.onNav, method = props.method,
90 // props
91 children = props.children, _a = props.anchor, Anchor = _a === void 0 ? "a" : _a, rest = __rest(props, ["name", "params", "query", "hash", "state", "onNav", "method", "children", "anchor"]);
92 var url = reactUniversal.useURL({ name: name, params: params, query: query, hash: hash });
93 var eventHandler = reactUniversal.useNavigationHandler({ url: url, state: state, onNav: onNav, method: method, canNavigate: canNavigate, target: rest.target }).eventHandler;
94 return (React.createElement(Anchor, __assign({}, rest, { onClick: eventHandler, href: url, ref: ref }), children));
95});
96var AsyncLink = React.forwardRef(function (props, ref) {
97 var
98 // url
99 name = props.name, params = props.params, query = props.query, hash = props.hash,
100 // navigation
101 state = props.state, onNav = props.onNav, method = props.method,
102 // props
103 children = props.children, _a = props.anchor, Anchor = _a === void 0 ? "a" : _a, rest = __rest(props, ["name", "params", "query", "hash", "state", "onNav", "method", "children", "anchor"]);
104 var url = reactUniversal.useURL({ name: name, params: params, query: query, hash: hash });
105 var _b = reactUniversal.useStatefulNavigationHandler({ url: url, state: state, onNav: onNav, method: method, canNavigate: canNavigate, target: rest.target }), eventHandler = _b.eventHandler, navigating = _b.navigating;
106 return (React.createElement(Anchor, __assign({}, rest, { onClick: eventHandler, href: url, ref: ref }), children(navigating)));
107});
108
109Object.keys(reactUniversal).forEach(function (key) {
110 Object.defineProperty(exports, key, {
111 enumerable: true,
112 get: function () {
113 return reactUniversal[key];
114 }
115 });
116});
117exports.AsyncLink = AsyncLink;
118exports.Link = Link;
119exports.useNavigationFocus = useNavigationFocus;