Skip to content
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

[react-events] Support screen reader virtual clicks #16584

Merged
merged 3 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions packages/react-events/src/dom/Press.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,17 @@ function handleStopPropagation(
}
}

// After some investigation work, screen reader virtual
// clicks (NVDA, Jaws, VoiceOver) do not have co-ords associated with the click
// event and "detail" is always 0 (where normal clicks are > 0)
function isScreenReaderVirtualClick(nativeEvent): boolean {
return (
nativeEvent.detail === 0 &&
nativeEvent.screenX === 0 &&
nativeEvent.screenY === 0
);
}

function targetIsDocument(target: null | Node): boolean {
// When target is null, it is the root
return target === null || target.nodeType === 9;
Expand Down Expand Up @@ -617,6 +628,13 @@ const pressResponderImpl = {
if (state.shouldPreventClick) {
nativeEvent.preventDefault();
}
const onPress = props.onPress;

if (isFunction(onPress) && isScreenReaderVirtualClick(nativeEvent)) {
state.pointerType = 'keyboard';
state.pressTarget = event.responderTarget;
dispatchEvent(event, onPress, context, state, 'press', DiscreteEvent);
}
break;
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/react-events/src/dom/__tests__/Press-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,18 @@ describe.each(environmentTable)('Press responder', hasPointerEvents => {
innerTarget.pointerup({pointerType: 'mouse'});
expect(onPress).toBeCalled();
});

it('is called once after virtual screen reader "click" event', () => {
const target = createEventTarget(ref.current);
target.screenReaderClick();
expect(onPress).toHaveBeenCalledTimes(1);
expect(onPress).toHaveBeenCalledWith(
expect.objectContaining({
pointerType: 'keyboard',
type: 'press',
}),
);
});
});

describe('onPressMove', () => {
Expand Down
41 changes: 41 additions & 0 deletions packages/react-events/src/dom/testing-library/domEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,43 @@ function createMouseEvent(
});
}

function createScreenReaderMouseEvent(
type,
{
altKey = false,
buttons = buttonsType.none,
ctrlKey = false,
metaKey = false,
preventDefault = emptyFunction,
shiftKey = false,
} = {},
) {
const modifierState = {altKey, ctrlKey, metaKey, shiftKey};

return createEvent(type, {
altKey,
buttons,
clientX: 0,
clientY: 0,
ctrlKey,
detail: 0,
trueadm marked this conversation as resolved.
Show resolved Hide resolved
getModifierState(keyArg) {
createGetModifierState(keyArg, modifierState);
},
metaKey,
movementX: 0,
movementY: 0,
offsetX: 0,
offsetY: 0,
pageX: 0,
pageY: 0,
preventDefault,
screenX: 0,
screenY: 0,
shiftKey,
});
}

function createTouchEvent(
type,
{
Expand Down Expand Up @@ -254,6 +291,10 @@ export function click(payload) {
return createMouseEvent('click', payload);
}

export function screenReaderClick(payload) {
trueadm marked this conversation as resolved.
Show resolved Hide resolved
return createScreenReaderMouseEvent('click', payload);
trueadm marked this conversation as resolved.
Show resolved Hide resolved
}

export function contextmenu(payload) {
return createMouseEvent('contextmenu', payload);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/react-events/src/dom/testing-library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const createEventTarget = node => ({
keyup(payload) {
node.dispatchEvent(domEvents.keyup(payload));
},
screenReaderClick(payload) {
trueadm marked this conversation as resolved.
Show resolved Hide resolved
node.dispatchEvent(domEvents.screenReaderClick(payload));
},
scroll(payload) {
node.dispatchEvent(domEvents.scroll(payload));
},
Expand Down