refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
77 lines (64 loc) • 2.17 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.mapKeyboardFocused = void 0;
var _react = require("react");
var _tsfn = require("tsfn");
const mapKeyboardFocused = props => {
const [isKeyboardFocused, setIsKeyboardFocused] = (0, _react.useState)(false);
const origOnFocusRef = (0, _react.useRef)();
const origOnBlurRef = (0, _react.useRef)();
const origOnPressInRef = (0, _react.useRef)();
const origOnPressOutRef = (0, _react.useRef)();
const isPressed = (0, _react.useRef)(false);
const onPressInRef = (0, _react.useRef)(_tsfn.NOOP);
const onPressOutRef = (0, _react.useRef)(_tsfn.NOOP);
const onFocusRef = (0, _react.useRef)(_tsfn.NOOP);
const onBlurRef = (0, _react.useRef)(_tsfn.NOOP);
origOnFocusRef.current = props.onFocus;
origOnBlurRef.current = props.onBlur;
origOnPressInRef.current = props.onPressIn;
origOnPressOutRef.current = props.onPressOut;
if (onPressInRef.current === _tsfn.NOOP) {
onPressInRef.current = () => {
isPressed.current = true;
if ((0, _tsfn.isFunction)(origOnPressInRef.current)) {
origOnPressInRef.current();
}
};
}
if (onPressOutRef.current === _tsfn.NOOP) {
onPressOutRef.current = () => {
isPressed.current = false;
if ((0, _tsfn.isFunction)(origOnPressOutRef.current)) {
origOnPressOutRef.current();
}
};
}
if (onFocusRef.current === _tsfn.NOOP) {
onFocusRef.current = () => {
if (isPressed.current === false) {
setIsKeyboardFocused(true);
}
if ((0, _tsfn.isFunction)(origOnFocusRef.current)) {
origOnFocusRef.current();
}
};
}
if (onBlurRef.current === _tsfn.NOOP) {
onBlurRef.current = () => {
setIsKeyboardFocused(false);
if ((0, _tsfn.isFunction)(origOnBlurRef.current)) {
origOnBlurRef.current();
}
};
}
return { ...props,
isKeyboardFocused: isKeyboardFocused || Boolean(props.isKeyboardFocused),
onFocus: onFocusRef.current,
onBlur: onBlurRef.current,
onPressIn: onPressInRef.current,
onPressOut: onPressOutRef.current
};
};
exports.mapKeyboardFocused = mapKeyboardFocused;