Skip to content

Commit

Permalink
[add] Pressable support for hover state
Browse files Browse the repository at this point in the history
This patch ports the 'useHover' hook to React Native for Web, providing hover
state that is scoped to a pressable and does not bubble to ancestor pressables.
This behavior aligns with the behavior of the focus and press states.

Fix necolas#1708
  • Loading branch information
necolas committed Oct 9, 2020
1 parent da6a6ae commit de78bcc
Show file tree
Hide file tree
Showing 13 changed files with 2,072 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/exports/Button/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe('components/Button', () => {
});
const target = createEventTarget(ref.current);
act(() => {
target.pointerdown();
target.pointerup();
target.pointerdown({ button: 0 });
target.pointerup({ button: 0 });
});
expect(onPress).toBeCalled();
});
Expand Down
7 changes: 6 additions & 1 deletion src/exports/Pressable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import type { ViewProps } from '../View';
import * as React from 'react';
import { forwardRef, memo, useMemo, useState, useRef } from 'react';
import useMergeRefs from '../../modules/useMergeRefs';
import useHover from '../../modules/useHover';
import usePressEvents from '../../modules/usePressEvents';
import View from '../View';

export type StateCallbackType = $ReadOnly<{|
focused: boolean,
hovered: boolean,
pressed: boolean
|}>;

Expand Down Expand Up @@ -93,6 +95,7 @@ function Pressable(props: Props, forwardedRef): React.Node {
...rest
} = props;

const [hovered, setHovered] = useForceableState(false);
const [focused, setFocused] = useForceableState(false);
const [pressed, setPressed] = useForceableState(testOnly_pressed === true);

Expand Down Expand Up @@ -128,8 +131,10 @@ function Pressable(props: Props, forwardedRef): React.Node {

const pressEventHandlers = usePressEvents(hostRef, pressConfig);

useHover(hostRef, { contain: true, disabled, onHoverChange: setHovered });

const accessibilityState = { disabled, ...props.accessibilityState };
const interactionState = { focused, pressed };
const interactionState = { hovered, focused, pressed };

function createFocusHandler(callback, value) {
return function(event) {
Expand Down
4 changes: 2 additions & 2 deletions src/exports/Text/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ describe('components/Text', () => {
});
const target = createEventTarget(ref.current);
act(() => {
target.pointerdown();
target.pointerup();
target.pointerdown({ button: 0 });
target.pointerup({ button: 0 });
});
expect(onPress).toBeCalled();
});
Expand Down
Loading

0 comments on commit de78bcc

Please sign in to comment.