Skip to content

Experimental event API: Hover module eventdata #15347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions packages/react-events/src/Hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,37 @@ type HoverProps = {
onHoverStart: (e: HoverEvent) => void,
};

type PointType = {
x: number,
y: number,
};

type HoverEventType = 'hoverstart' | 'hoverend' | 'hoverchange';

type PointerType = 'mouse';

type HoverState = {
isActiveHovered: boolean,
isHovered: boolean,
isInHitSlop: boolean,
isTouched: boolean,
hoverStartTimeout: null | TimeoutID,
hoverEndTimeout: null | TimeoutID,
point: PointType,
pointerType: PointerType,
};

type HoverEventType = 'hoverstart' | 'hoverend' | 'hoverchange';
type EventData = {
pointerType: PointerType,
point: PointType,
};

type HoverEvent = {|
listener: HoverEvent => void,
target: Element | Document,
type: HoverEventType,
pointerType: PointerType,
point: PointType,
|};

const DEFAULT_HOVER_END_DELAY_MS = 0;
Expand All @@ -55,11 +71,13 @@ function createHoverEvent(
type: HoverEventType,
target: Element | Document,
listener: HoverEvent => void,
eventData?: EventData,
): HoverEvent {
return {
listener,
target,
type,
...eventData,
};
}

Expand All @@ -69,13 +87,19 @@ function dispatchHoverChangeEvent(
props: HoverProps,
state: HoverState,
): void {
const {nativeEvent} = event;
const listener = () => {
props.onHoverChange(state.isActiveHovered);
};
const eventData = {
point: state.point,
pointerType: (nativeEvent: any).pointerType,
};
const syntheticEvent = createHoverEvent(
'hoverchange',
event.target,
listener,
eventData,
);
context.dispatchEvent(syntheticEvent, {discrete: true});
}
Expand All @@ -92,6 +116,16 @@ function dispatchHoverStartEvents(
}

state.isHovered = true;
const x = (nativeEvent: any).screenX;
const y = (nativeEvent: any).screenY;
state.point = {
x,
y,
};
const eventData = {
point: state.point,
pointerType: (nativeEvent: any).pointerType,
};

if (state.hoverEndTimeout !== null) {
clearTimeout(state.hoverEndTimeout);
Expand All @@ -106,6 +140,7 @@ function dispatchHoverStartEvents(
'hoverstart',
target,
props.onHoverStart,
eventData,
);
context.dispatchEvent(syntheticEvent, {discrete: true});
}
Expand Down Expand Up @@ -143,6 +178,16 @@ function dispatchHoverEndEvents(
}

state.isHovered = false;
const x = (nativeEvent: any).screenX;
const y = (nativeEvent: any).screenY;
state.point = {
x,
y,
};
const eventData = {
point: state.point,
pointerType: (nativeEvent: any).pointerType,
};

if (state.hoverStartTimeout !== null) {
clearTimeout(state.hoverStartTimeout);
Expand All @@ -157,6 +202,7 @@ function dispatchHoverEndEvents(
'hoverend',
target,
props.onHoverEnd,
eventData,
);
context.dispatchEvent(syntheticEvent, {discrete: true});
}
Expand Down Expand Up @@ -188,14 +234,16 @@ function calculateDelayMS(delay: ?number, min = 0, fallback = 0) {

const HoverResponder = {
targetEventTypes,
createInitialState() {
createInitialState(): HoverState {
return {
isActiveHovered: false,
isHovered: false,
isInHitSlop: false,
isTouched: false,
hoverStartTimeout: null,
hoverEndTimeout: null,
point: {x: 0, y: 0},
pointerType: 'mouse',
};
},
onEvent(
Expand Down