Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 2x 2x 2x 2x 52x 7x 7x 7x 7x 47x 47x 52x 52x 52x 52x 52x 52x 52x 52x 44x 44x 44x 15x 11x 15x 11x 15x 15x 44x 13x 13x 13x 3x 13x 44x 8x 8x 8x 8x 8x 8x 8x 44x 24x 13x 24x 12x 24x 10x 24x 44x 26x 26x 22x 4x 22x 22x 2x 2x 22x 5x 2x 22x 22x 44x | /**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import { getModality } from '../modality';
import useEvent from '../useEvent';
import useLayoutEffect from '../useLayoutEffect';
/**
* Types
*/
export type HoverEventsConfig = {
contain?: ?boolean,
disabled?: ?boolean,
onHoverStart?: ?(e: any) => void,
onHoverChange?: ?(bool: boolean) => void,
onHoverUpdate?: ?(e: any) => void,
onHoverEnd?: ?(e: any) => void
};
/**
* Implementation
*/
const emptyObject = {};
const opts = { passive: true };
const lockEventType = 'react-gui:hover:lock';
const unlockEventType = 'react-gui:hover:unlock';
const supportsPointerEvent = () => !!(typeof window !== 'undefined' && window.PointerEvent != null);
function dispatchCustomEvent(
target: EventTarget,
type: string,
payload?: {
bubbles?: boolean,
cancelable?: boolean,
detail?: { [key: string]: mixed }
}
) {
const event = document.createEvent('CustomEvent');
const { bubbles = true, cancelable = true, detail } = payload || emptyObject;
event.initCustomEvent(type, bubbles, cancelable, detail);
target.dispatchEvent(event);
}
// This accounts for the non-PointerEvent fallback events.
function getPointerType(event) {
const { pointerType } = event;
return pointerType != null ? pointerType : getModality();
}
export default function useHover(targetRef: any, config: HoverEventsConfig): void {
const { contain, disabled, onHoverStart, onHoverChange, onHoverUpdate, onHoverEnd } = config;
const canUsePE = supportsPointerEvent();
const addMoveListener = useEvent(canUsePE ? 'pointermove' : 'mousemove', opts);
const addEnterListener = useEvent(canUsePE ? 'pointerenter' : 'mouseenter', opts);
const addLeaveListener = useEvent(canUsePE ? 'pointerleave' : 'mouseleave', opts);
// These custom events are used to implement the "contain" prop.
const addLockListener = useEvent(lockEventType, opts);
const addUnlockListener = useEvent(unlockEventType, opts);
useLayoutEffect(() => {
const target = targetRef.current;
Eif (target !== null) {
/**
* End the hover gesture
*/
const hoverEnd = function (e) {
if (onHoverEnd != null) {
onHoverEnd(e);
}
if (onHoverChange != null) {
onHoverChange(false);
}
// Remove the listeners once finished.
addMoveListener(target, null);
addLeaveListener(target, null);
};
/**
* Leave element
*/
const leaveListener = function (e) {
const target = targetRef.current;
Eif (target != null && getPointerType(e) !== 'touch') {
if (contain) {
dispatchCustomEvent(target, unlockEventType);
}
hoverEnd(e);
}
};
/**
* Move within element
*/
const moveListener = function (e) {
Eif (getPointerType(e) !== 'touch') {
Eif (onHoverUpdate != null) {
// Not all browsers have these properties
Eif (e.x == null) {
e.x = e.clientX;
}
Eif (e.y == null) {
e.y = e.clientY;
}
onHoverUpdate(e);
}
}
};
/**
* Start the hover gesture
*/
const hoverStart = function (e) {
if (onHoverStart != null) {
onHoverStart(e);
}
if (onHoverChange != null) {
onHoverChange(true);
}
// Set the listeners needed for the rest of the hover gesture.
if (onHoverUpdate != null) {
addMoveListener(target, !disabled ? moveListener : null);
}
addLeaveListener(target, !disabled ? leaveListener : null);
};
/**
* Enter element
*/
const enterListener = function (e) {
const target = targetRef.current;
if (target != null && getPointerType(e) !== 'touch') {
if (contain) {
dispatchCustomEvent(target, lockEventType);
}
hoverStart(e);
const lockListener = function (lockEvent) {
Eif (lockEvent.target !== target) {
hoverEnd(e);
}
};
const unlockListener = function (lockEvent) {
if (lockEvent.target !== target) {
hoverStart(e);
}
};
addLockListener(target, !disabled ? lockListener : null);
addUnlockListener(target, !disabled ? unlockListener : null);
}
};
addEnterListener(target, !disabled ? enterListener : null);
}
}, [
addEnterListener,
addMoveListener,
addLeaveListener,
addLockListener,
addUnlockListener,
contain,
disabled,
onHoverStart,
onHoverChange,
onHoverUpdate,
onHoverEnd,
targetRef
]);
}
|