UNPKG

3.15 kBJavaScriptView Raw
1import {useSyntheticBlurEvent as $8a9cb279dc87e130$export$715c682d09d639cc} from "./utils.mjs";
2import {useRef as $3b9Q0$useRef, useCallback as $3b9Q0$useCallback} from "react";
3
4/*
5 * Copyright 2020 Adobe. All rights reserved.
6 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License. You may obtain a copy
8 * of the License at http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software distributed under
11 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12 * OF ANY KIND, either express or implied. See the License for the specific language
13 * governing permissions and limitations under the License.
14 */ // Portions of the code in this file are based on code from react.
15// Original licensing for the following can be found in the
16// NOTICE file in the root directory of this source tree.
17// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
18
19
20function $9ab94262bd0047c7$export$420e68273165f4ec(props) {
21 let { isDisabled: isDisabled, onBlurWithin: onBlurWithin, onFocusWithin: onFocusWithin, onFocusWithinChange: onFocusWithinChange } = props;
22 let state = (0, $3b9Q0$useRef)({
23 isFocusWithin: false
24 });
25 let onBlur = (0, $3b9Q0$useCallback)((e)=>{
26 // We don't want to trigger onBlurWithin and then immediately onFocusWithin again
27 // when moving focus inside the element. Only trigger if the currentTarget doesn't
28 // include the relatedTarget (where focus is moving).
29 if (state.current.isFocusWithin && !e.currentTarget.contains(e.relatedTarget)) {
30 state.current.isFocusWithin = false;
31 if (onBlurWithin) onBlurWithin(e);
32 if (onFocusWithinChange) onFocusWithinChange(false);
33 }
34 }, [
35 onBlurWithin,
36 onFocusWithinChange,
37 state
38 ]);
39 let onSyntheticFocus = (0, $8a9cb279dc87e130$export$715c682d09d639cc)(onBlur);
40 let onFocus = (0, $3b9Q0$useCallback)((e)=>{
41 // Double check that document.activeElement actually matches e.target in case a previously chained
42 // focus handler already moved focus somewhere else.
43 if (!state.current.isFocusWithin && document.activeElement === e.target) {
44 if (onFocusWithin) onFocusWithin(e);
45 if (onFocusWithinChange) onFocusWithinChange(true);
46 state.current.isFocusWithin = true;
47 onSyntheticFocus(e);
48 }
49 }, [
50 onFocusWithin,
51 onFocusWithinChange,
52 onSyntheticFocus
53 ]);
54 if (isDisabled) return {
55 focusWithinProps: {
56 // These should not have been null, that would conflict in mergeProps
57 onFocus: undefined,
58 onBlur: undefined
59 }
60 };
61 return {
62 focusWithinProps: {
63 onFocus: onFocus,
64 onBlur: onBlur
65 }
66 };
67}
68
69
70export {$9ab94262bd0047c7$export$420e68273165f4ec as useFocusWithin};
71//# sourceMappingURL=useFocusWithin.module.js.map