@react-aria/interactions
Version:
Spectrum UI components in React
116 lines (111 loc) • 5.22 kB
JavaScript
import {useRef as $6dfIe$useRef, useCallback as $6dfIe$useCallback} from "react";
import {useLayoutEffect as $6dfIe$useLayoutEffect, useEffectEvent as $6dfIe$useEffectEvent} from "@react-aria/utils";
/*
* 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.
*/
class $8a9cb279dc87e130$export$905e7fc544a71f36 {
isDefaultPrevented() {
return this.nativeEvent.defaultPrevented;
}
preventDefault() {
this.defaultPrevented = true;
this.nativeEvent.preventDefault();
}
stopPropagation() {
this.nativeEvent.stopPropagation();
this.isPropagationStopped = ()=>true;
}
isPropagationStopped() {
return false;
}
persist() {}
constructor(type, nativeEvent){
this.nativeEvent = nativeEvent;
this.target = nativeEvent.target;
this.currentTarget = nativeEvent.currentTarget;
this.relatedTarget = nativeEvent.relatedTarget;
this.bubbles = nativeEvent.bubbles;
this.cancelable = nativeEvent.cancelable;
this.defaultPrevented = nativeEvent.defaultPrevented;
this.eventPhase = nativeEvent.eventPhase;
this.isTrusted = nativeEvent.isTrusted;
this.timeStamp = nativeEvent.timeStamp;
this.type = type;
}
}
function $8a9cb279dc87e130$export$715c682d09d639cc(onBlur) {
let stateRef = (0, $6dfIe$useRef)({
isFocused: false,
observer: null
});
// Clean up MutationObserver on unmount. See below.
// eslint-disable-next-line arrow-body-style
(0, $6dfIe$useLayoutEffect)(()=>{
const state = stateRef.current;
return ()=>{
if (state.observer) {
state.observer.disconnect();
state.observer = null;
}
};
}, []);
let dispatchBlur = (0, $6dfIe$useEffectEvent)((e)=>{
onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
});
// This function is called during a React onFocus event.
return (0, $6dfIe$useCallback)((e)=>{
// React does not fire onBlur when an element is disabled. https://github.com/facebook/react/issues/9142
// Most browsers fire a native focusout event in this case, except for Firefox. In that case, we use a
// MutationObserver to watch for the disabled attribute, and dispatch these events ourselves.
// For browsers that do, focusout fires before the MutationObserver, so onBlur should not fire twice.
if (e.target instanceof HTMLButtonElement || e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement || e.target instanceof HTMLSelectElement) {
stateRef.current.isFocused = true;
let target = e.target;
let onBlurHandler = (e)=>{
stateRef.current.isFocused = false;
if (target.disabled) // For backward compatibility, dispatch a (fake) React synthetic event.
dispatchBlur(new $8a9cb279dc87e130$export$905e7fc544a71f36('blur', e));
// We no longer need the MutationObserver once the target is blurred.
if (stateRef.current.observer) {
stateRef.current.observer.disconnect();
stateRef.current.observer = null;
}
};
target.addEventListener('focusout', onBlurHandler, {
once: true
});
stateRef.current.observer = new MutationObserver(()=>{
if (stateRef.current.isFocused && target.disabled) {
var _stateRef_current_observer;
(_stateRef_current_observer = stateRef.current.observer) === null || _stateRef_current_observer === void 0 ? void 0 : _stateRef_current_observer.disconnect();
let relatedTargetEl = target === document.activeElement ? null : document.activeElement;
target.dispatchEvent(new FocusEvent('blur', {
relatedTarget: relatedTargetEl
}));
target.dispatchEvent(new FocusEvent('focusout', {
bubbles: true,
relatedTarget: relatedTargetEl
}));
}
});
stateRef.current.observer.observe(target, {
attributes: true,
attributeFilter: [
'disabled'
]
});
}
}, [
dispatchBlur
]);
}
export {$8a9cb279dc87e130$export$905e7fc544a71f36 as SyntheticFocusEvent, $8a9cb279dc87e130$export$715c682d09d639cc as useSyntheticBlurEvent};
//# sourceMappingURL=utils.module.js.map