UNPKG

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