UNPKG

@curi/react-dom

Version:

React DOM components to use with Curi

114 lines (104 loc) 5.28 kB
import React from 'react'; import { useResponse, useURL, useNavigationHandler, useStatefulNavigationHandler } from '@curi/react-universal'; export * from '@curi/react-universal'; function useNavigationFocus(ref, props) { if (props === void 0) { props = {}; } // The response isn't actually used, but the app should only // re-focus when the response changes. The preserve and preventScroll // values are used, but not used in the comparison array because // changing these values would steal the app's focus even though // the location hasn't changed. var response = useResponse().response; var preserve = props.preserve, _a = props.preventScroll, preventScroll = _a === void 0 ? false : _a; React.useEffect(function () { var ele = ref.current; if (ele === null) { if (process.env.NODE_ENV !== "production") { console.warn("There is no element to focus. Did you forget to add the ref to an element?"); } return; } if (preserve && ele.contains(document.activeElement)) { return; } if (process.env.NODE_ENV !== "production") { if (!ele.hasAttribute("tabIndex") && ele.tabIndex === -1) { console.warn('The component that is passed the ref must have a "tabIndex" prop or be focusable by default in order to be focused. ' + "Otherwise, the document's <body> will be focused instead."); } } // @ts-ignore ele.focus({ preventScroll: preventScroll }); }, [response]); } /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]]; return t; } function canNavigate(event, target) { return (!event.defaultPrevented && !target && event.button === 0 && !(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey)); } var Link = React.forwardRef(function (props, ref) { var _a; var // url name = props.name, params = props.params, query = props.query, hash = props.hash, // navigation state = props.state, onNav = props.onNav, method = props.method, // props children = props.children, Anchor = (_a = props.anchor, _a === void 0 ? "a" : _a), rest = __rest(props, ["name", "params", "query", "hash", "state", "onNav", "method", "children", "anchor"]); var url = useURL({ name: name, params: params, query: query, hash: hash }); var eventHandler = useNavigationHandler({ url: url, state: state, onNav: onNav, method: method, canNavigate: canNavigate, target: rest.target }).eventHandler; return (React.createElement(Anchor, __assign({}, rest, { onClick: eventHandler, href: url, ref: ref }), children)); }); var AsyncLink = React.forwardRef(function (props, ref) { var _a, _b; var // url name = props.name, params = props.params, query = props.query, hash = props.hash, // navigation state = props.state, onNav = props.onNav, method = props.method, // props children = props.children, Anchor = (_a = props.anchor, _a === void 0 ? "a" : _a), rest = __rest(props, ["name", "params", "query", "hash", "state", "onNav", "method", "children", "anchor"]); var url = useURL({ name: name, params: params, query: query, hash: hash }); var eventHandler = (_b = useStatefulNavigationHandler({ url: url, state: state, onNav: onNav, method: method, canNavigate: canNavigate, target: rest.target }), _b.eventHandler), navigating = _b.navigating; return (React.createElement(Anchor, __assign({}, rest, { onClick: eventHandler, href: url, ref: ref }), children(navigating))); }); export { AsyncLink, Link, useNavigationFocus };