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 | 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 |
|
33 | ele.focus({ preventScroll: preventScroll });
|
34 | });
|
35 | return function () {
|
36 | clearTimeout(timeout);
|
37 | };
|
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 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 |
|
74 | var 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 |
|
88 | React.createElement(Anchor, __assign({ onClick: handler, href: href, ref: ref }, forward), typeof children === "function"
|
89 | ? children(navigating)
|
90 | : children));
|
91 | });
|
92 |
|
93 | Object.keys(reactUniversal).forEach(function (key) { exports[key] = reactUniversal[key]; });
|
94 | exports.Link = HookLink;
|
95 | exports.useNavigationFocus = useNavigationFocus;
|