UNPKG

2.66 kBJavaScriptView Raw
1import {useSyntheticBlurEvent as $8a9cb279dc87e130$export$715c682d09d639cc} from "./utils.mjs";
2import {useCallback as $hf0lj$useCallback} from "react";
3import {getOwnerDocument as $hf0lj$getOwnerDocument} from "@react-aria/utils";
4
5/*
6 * Copyright 2020 Adobe. All rights reserved.
7 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License. You may obtain a copy
9 * of the License at http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software distributed under
12 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
13 * OF ANY KIND, either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
15 */ // Portions of the code in this file are based on code from react.
16// Original licensing for the following can be found in the
17// NOTICE file in the root directory of this source tree.
18// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
19
20
21
22function $a1ea59d68270f0dd$export$f8168d8dd8fd66e6(props) {
23 let { isDisabled: isDisabled, onFocus: onFocusProp, onBlur: onBlurProp, onFocusChange: onFocusChange } = props;
24 const onBlur = (0, $hf0lj$useCallback)((e)=>{
25 if (e.target === e.currentTarget) {
26 if (onBlurProp) onBlurProp(e);
27 if (onFocusChange) onFocusChange(false);
28 return true;
29 }
30 }, [
31 onBlurProp,
32 onFocusChange
33 ]);
34 const onSyntheticFocus = (0, $8a9cb279dc87e130$export$715c682d09d639cc)(onBlur);
35 const onFocus = (0, $hf0lj$useCallback)((e)=>{
36 // Double check that document.activeElement actually matches e.target in case a previously chained
37 // focus handler already moved focus somewhere else.
38 const ownerDocument = (0, $hf0lj$getOwnerDocument)(e.target);
39 if (e.target === e.currentTarget && ownerDocument.activeElement === e.target) {
40 if (onFocusProp) onFocusProp(e);
41 if (onFocusChange) onFocusChange(true);
42 onSyntheticFocus(e);
43 }
44 }, [
45 onFocusChange,
46 onFocusProp,
47 onSyntheticFocus
48 ]);
49 return {
50 focusProps: {
51 onFocus: !isDisabled && (onFocusProp || onFocusChange || onBlurProp) ? onFocus : undefined,
52 onBlur: !isDisabled && (onBlurProp || onFocusChange) ? onBlur : undefined
53 }
54 };
55}
56
57
58export {$a1ea59d68270f0dd$export$f8168d8dd8fd66e6 as useFocus};
59//# sourceMappingURL=useFocus.module.js.map