Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### Bug Fixes

- `InputControl`, `NumberControl`, `UnitControl`: Ignore the drag gesture for touch input, where it has no cursor affordance and is easy to trigger by accident ([#38865](https://github.com/WordPress/gutenberg/issues/38865), [#81519](https://github.com/WordPress/gutenberg/pull/81519)).
- `Text`: Make single-line truncation and `isBlock` overrides consistent across the main document and iframes ([#82573](https://github.com/WordPress/gutenberg/pull/82573)).
- `CardDivider`: Apply the Card's width, display, and border color consistently inside iframes ([#82577](https://github.com/WordPress/gutenberg/pull/82577)).

Expand Down
10 changes: 10 additions & 0 deletions packages/components/src/input-control/input-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ function InputField(
( dragProps ) => {
const { distance, dragging, event, target } = dragProps;

// The drag gesture is not offered for touch input, where there is
// no cursor affordance for it, and where it's easy to accidentally
// change the value when only meaning to tap or move the caret.
if (
event.pointerType === 'touch' ||
event.type.startsWith( 'touch' )
) {
return;
}

// The `target` prop always references the `input` element while, by
// default, the `dragProps.event.target` property would reference the real
// event target (i.e. any DOM element that the pointer is hovering while
Expand Down
38 changes: 37 additions & 1 deletion packages/components/src/number-control/test/index.jsdom.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, test, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { useState } from '@wordpress/element';
import _NumberControl from '..';
Expand Down Expand Up @@ -557,6 +557,42 @@ describe( 'NumberControl', () => {
} );
} );

describe( 'drag to change value', () => {
// jsdom implements neither `PointerEvent` nor `Touch`, so the drag
// gesture falls back to touch events that have to be built by hand.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like this would be better as a vitest browser mode test, since we have that infrastructure now.

const fireTouchEvent = (
type: string,
input: HTMLElement,
clientY: number
) => {
const event = new Event( type, {
bubbles: true,
cancelable: true,
} );
const touches = [
{ identifier: 1, target: input, clientX: 0, clientY },
];
Object.assign( event, {
touches,
targetTouches: touches,
changedTouches: touches,
} );
fireEvent( type === 'touchstart' ? input : window, event );
};

it( 'should not change the value when dragging with touch input', () => {
render( <StatefulNumberControl value="5" /> );

const input = screen.getByRole( 'spinbutton' );
fireTouchEvent( 'touchstart', input, 0 );
fireTouchEvent( 'touchmove', input, -20 );
fireTouchEvent( 'touchmove', input, -60 );
fireTouchEvent( 'touchend', input, -60 );

expect( input ).toHaveValue( 5 );
} );
} );

describe( 'custom spin buttons', () => {
test.each( [
undefined,
Expand Down
Loading