1 | import React from 'react';
|
2 | import { useCuri, useHref, useNavigationHandler } from '@curi/react-universal';
|
3 | export * from '@curi/react-universal';
|
4 |
|
5 | function useNavigationFocus(ref, props) {
|
6 | if (props === void 0) { props = {}; }
|
7 | var response = useCuri().response;
|
8 | React.useEffect(function () {
|
9 | var ele = ref.current;
|
10 | if (ele === null) {
|
11 | if (process.env.NODE_ENV !== "production") {
|
12 | console.warn("There is no element to focus. Did you forget to add the ref to an element?");
|
13 | }
|
14 | return;
|
15 | }
|
16 | if (props.preserve && ele.contains(document.activeElement)) {
|
17 | return;
|
18 | }
|
19 | if (process.env.NODE_ENV !== "production") {
|
20 | if (!ele.hasAttribute("tabIndex") && ele.tabIndex === -1) {
|
21 | console.warn('The component that is passed the ref must have a "tabIndex" prop or be focusable by default in order to be focused. ' +
|
22 | "Otherwise, the document's <body> will be focused instead.");
|
23 | }
|
24 | }
|
25 | var _a = props.preventScroll, preventScroll = _a === void 0 ? false : _a;
|
26 | var timeout = setTimeout(function () {
|
27 |
|
28 | ele.focus({ preventScroll: preventScroll });
|
29 | });
|
30 | return function () {
|
31 | clearTimeout(timeout);
|
32 | };
|
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 canNavigate(event, forward) {
|
63 | return (!event.defaultPrevented &&
|
64 | event.button === 0 &&
|
65 | !(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) &&
|
66 | (!forward || !forward.target));
|
67 | }
|
68 |
|
69 | var HookLink = React.forwardRef(function (props, ref) {
|
70 | var _a = React.useState(false), navigating = _a[0], setNavigating = _a[1];
|
71 | var href = useHref(props);
|
72 | var _b = useNavigationHandler(props, setNavigating, canNavigate), handler = _b.handler, cancel = _b.cancel;
|
73 | React.useEffect(function () {
|
74 | return function () {
|
75 | if (cancel.current) {
|
76 | cancel.current();
|
77 | }
|
78 | };
|
79 | }, []);
|
80 | var _c = props.anchor, Anchor = _c === void 0 ? "a" : _c, children = props.children, forward = props.forward;
|
81 | return (
|
82 |
|
83 | React.createElement(Anchor, __assign({ onClick: handler, href: href, ref: ref }, forward), typeof children === "function"
|
84 | ? children(navigating)
|
85 | : children));
|
86 | });
|
87 |
|
88 | export { HookLink as Link, useNavigationFocus };
|