UNPKG

3.98 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 var response = reactUniversal.useCuri().response;
13 React.useEffect(function () {
14 var ele = ref.current;
15 if (ele === null) {
16 if (process.env.NODE_ENV !== "production") {
17 console.warn("There is no element to focus. Did you forget to add the ref to an element?");
18 }
19 return;
20 }
21 if (props.preserve && ele.contains(document.activeElement)) {
22 return;
23 }
24 if (process.env.NODE_ENV !== "production") {
25 if (!ele.hasAttribute("tabIndex") && ele.tabIndex === -1) {
26 console.warn('The component that is passed the ref must have a "tabIndex" prop or be focusable by default in order to be focused. ' +
27 "Otherwise, the document's <body> will be focused instead.");
28 }
29 }
30 var _a = props.preventScroll, preventScroll = _a === void 0 ? false : _a;
31 var timeout = setTimeout(function () {
32 // @ts-ignore
33 ele.focus({ preventScroll: preventScroll });
34 });
35 return function () {
36 clearTimeout(timeout);
37 };
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 canNavigate(event, forward) {
68 return (!event.defaultPrevented &&
69 event.button === 0 &&
70 !(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) &&
71 (!forward || !forward.target));
72}
73
74var HookLink = React.forwardRef(function (props, ref) {
75 var _a = React.useState(false), navigating = _a[0], setNavigating = _a[1];
76 var href = reactUniversal.useHref(props);
77 var _b = reactUniversal.useNavigationHandler(props, setNavigating, canNavigate), handler = _b.handler, cancel = _b.cancel;
78 React.useEffect(function () {
79 return function () {
80 if (cancel.current) {
81 cancel.current();
82 }
83 };
84 }, []);
85 var _c = props.anchor, Anchor = _c === void 0 ? "a" : _c, children = props.children, forward = props.forward;
86 return (
87 // @ts-ignore
88 React.createElement(Anchor, __assign({ onClick: handler, href: href, ref: ref }, forward), typeof children === "function"
89 ? children(navigating)
90 : children));
91});
92
93Object.keys(reactUniversal).forEach(function (key) { exports[key] = reactUniversal[key]; });
94exports.Link = HookLink;
95exports.useNavigationFocus = useNavigationFocus;