@react-aria/interactions
Version:
Spectrum UI components in React
77 lines (69 loc) • 3.25 kB
JavaScript
var $625cf83917e112ad$exports = require("./utils.main.js");
var $kDAhS$react = require("react");
function $parcel$export(e, n, v, s) {
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
}
$parcel$export(module.exports, "useFocusWithin", () => $d16842bbd0359d1b$export$420e68273165f4ec);
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you 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
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/ // Portions of the code in this file are based on code from react.
// Original licensing for the following can be found in the
// NOTICE file in the root directory of this source tree.
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
function $d16842bbd0359d1b$export$420e68273165f4ec(props) {
let { isDisabled: isDisabled, onBlurWithin: onBlurWithin, onFocusWithin: onFocusWithin, onFocusWithinChange: onFocusWithinChange } = props;
let state = (0, $kDAhS$react.useRef)({
isFocusWithin: false
});
let onBlur = (0, $kDAhS$react.useCallback)((e)=>{
// We don't want to trigger onBlurWithin and then immediately onFocusWithin again
// when moving focus inside the element. Only trigger if the currentTarget doesn't
// include the relatedTarget (where focus is moving).
if (state.current.isFocusWithin && !e.currentTarget.contains(e.relatedTarget)) {
state.current.isFocusWithin = false;
if (onBlurWithin) onBlurWithin(e);
if (onFocusWithinChange) onFocusWithinChange(false);
}
}, [
onBlurWithin,
onFocusWithinChange,
state
]);
let onSyntheticFocus = (0, $625cf83917e112ad$exports.useSyntheticBlurEvent)(onBlur);
let onFocus = (0, $kDAhS$react.useCallback)((e)=>{
// Double check that document.activeElement actually matches e.target in case a previously chained
// focus handler already moved focus somewhere else.
if (!state.current.isFocusWithin && document.activeElement === e.target) {
if (onFocusWithin) onFocusWithin(e);
if (onFocusWithinChange) onFocusWithinChange(true);
state.current.isFocusWithin = true;
onSyntheticFocus(e);
}
}, [
onFocusWithin,
onFocusWithinChange,
onSyntheticFocus
]);
if (isDisabled) return {
focusWithinProps: {
// These should not have been null, that would conflict in mergeProps
onFocus: undefined,
onBlur: undefined
}
};
return {
focusWithinProps: {
onFocus: onFocus,
onBlur: onBlur
}
};
}
//# sourceMappingURL=useFocusWithin.main.js.map