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

Components: Use Element.scrollIntoView() instead of dom-scroll-into-view #59085

Merged
merged 6 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 0 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"colord": "^2.7.0",
"deepmerge": "^4.3.0",
"diff": "^4.0.2",
"dom-scroll-into-view": "^1.2.1",
"fast-deep-equal": "^3.1.3",
"memize": "^2.1.0",
"postcss": "^8.4.21",
Expand Down
21 changes: 6 additions & 15 deletions packages/block-editor/src/components/url-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import scrollIntoView from 'dom-scroll-into-view';

/**
* WordPress dependencies
Expand Down Expand Up @@ -83,21 +82,13 @@ class URLInput extends Component {
if (
showSuggestions &&
selectedSuggestion !== null &&
this.suggestionNodes[ selectedSuggestion ] &&
! this.scrollingIntoView
this.suggestionNodes[ selectedSuggestion ]
) {
this.scrollingIntoView = true;
scrollIntoView(
this.suggestionNodes[ selectedSuggestion ],
this.autocompleteRef.current,
{
onlyScrollIfNeeded: true,
}
);

this.props.setTimeout( () => {
this.scrollingIntoView = false;
}, 100 );
this.suggestionNodes[ selectedSuggestion ].scrollIntoView( {
behavior: 'instant',
block: 'nearest',
inline: 'nearest',
} );
}

// Update suggestions when the value changes.
Expand Down
2 changes: 2 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

- `ColorPicker`: Style without accessing internal `InputControl` classes ([#59069](https://github.com/WordPress/gutenberg/pull/59069)).
- Add lint rules for theme color CSS var usage ([#59022](https://github.com/WordPress/gutenberg/pull/59022)).
- `FormTokenField`: Use `Element.scrollIntoView()` instead of `dom-scroll-into-view` ([#59085](https://github.com/WordPress/gutenberg/pull/59085)).
- Removing `dom-scroll-into-view` as a dependency of the components package ([#59085](https://github.com/WordPress/gutenberg/pull/59085)).

## 26.0.1 (2024-02-13)

Expand Down
1 change: 0 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"colord": "^2.7.0",
"date-fns": "^2.28.0",
"deepmerge": "^4.3.0",
"dom-scroll-into-view": "^1.2.1",
"downshift": "^6.0.15",
"fast-deep-equal": "^3.1.3",
"framer-motion": "^10.13.0",
Expand Down
22 changes: 5 additions & 17 deletions packages/components/src/form-token-field/suggestions-list.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/**
* External dependencies
*/
import scrollView from 'dom-scroll-into-view';
import classnames from 'classnames';
import type { MouseEventHandler, ReactNode } from 'react';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { useRefEffect } from '@wordpress/compose';

/**
Expand All @@ -32,8 +30,6 @@ export function SuggestionsList< T extends string | { value: string } >( {
instanceId,
__experimentalRenderItem,
}: SuggestionsListProps< T > ) {
const [ scrollingIntoView, setScrollingIntoView ] = useState( false );

const listRef = useRefEffect< HTMLUListElement >(
( listNode ) => {
// only have to worry about scrolling selected suggestion into view
Expand All @@ -44,16 +40,10 @@ export function SuggestionsList< T extends string | { value: string } >( {
scrollIntoView &&
listNode.children[ selectedIndex ]
) {
setScrollingIntoView( true );
scrollView(
listNode.children[ selectedIndex ] as HTMLLIElement,
listNode,
{
onlyScrollIfNeeded: true,
}
);
rafId = requestAnimationFrame( () => {
setScrollingIntoView( false );
listNode.children[ selectedIndex ].scrollIntoView( {
behavior: 'instant',
block: 'nearest',
inline: 'nearest',
} );
}

Expand All @@ -68,9 +58,7 @@ export function SuggestionsList< T extends string | { value: string } >( {

const handleHover = ( suggestion: T ) => {
return () => {
if ( ! scrollingIntoView ) {
onHover?.( suggestion );
}
onHover?.( suggestion );
};
};

Expand Down
1 change: 0 additions & 1 deletion packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"types": [
"gutenberg-env",
"gutenberg-test-env",
"dom-scroll-into-view",
"jest",
"@testing-library/jest-dom"
],
Expand Down
25 changes: 0 additions & 25 deletions patches/dom-scroll-into-view+1.2.1.patch

This file was deleted.

8 changes: 8 additions & 0 deletions test/unit/config/global-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ global.ResizeObserver = require( 'resize-observer-polyfill' );
if ( ! window.DOMRect ) {
window.DOMRect = class DOMRect {};
}

/**
* Polyfill for Element.scrollIntoView().
* Necessary because it's not implemented in jsdom, and likely will never be.
*
* @see https://github.com/jsdom/jsdom/issues/1695
*/
global.Element.prototype.scrollIntoView = jest.fn();
18 changes: 0 additions & 18 deletions typings/dom-scroll-into-view/index.d.ts

This file was deleted.

Loading