Skip to content

Commit e0a2337

Browse files
authored
Add an example for smooth scrolling (#3779)
* Update dependencies * Add an example for smooth scrolling * Add name
1 parent ae91bca commit e0a2337

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/ScrollToCell.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export default function ScrollToCell({
2121
useLayoutEffect(() => {
2222
// scroll until the cell is completely visible
2323
// this is needed if the grid has auto-sized columns
24-
scrollIntoView(ref.current);
24+
// setting the behavior to auto so it can be overridden
25+
scrollIntoView(ref.current, 'auto');
2526
});
2627

2728
useLayoutEffect(() => {

src/utils/domUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export function stopPropagation(event: React.SyntheticEvent) {
44
event.stopPropagation();
55
}
66

7-
export function scrollIntoView(element: Maybe<Element>) {
8-
element?.scrollIntoView({ inline: 'nearest', block: 'nearest' });
7+
export function scrollIntoView(element: Maybe<Element>, behavior: ScrollBehavior = 'instant') {
8+
element?.scrollIntoView({ inline: 'nearest', block: 'nearest', behavior });
99
}

website/routes/ScrollToCell.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useRef, useState } from 'react';
22
import { createFileRoute } from '@tanstack/react-router';
33
import { css } from '@linaria/core';
4+
import clsx from 'clsx';
45

56
import { DataGrid } from '../../src';
67
import type { Column, DataGridHandle } from '../../src';
@@ -29,13 +30,23 @@ for (let i = 0; i < 200; i++) {
2930
const flexClassname = css`
3031
display: flex;
3132
gap: 5px;
33+
align-items: center;
3234
margin-block-end: 5px;
35+
36+
> fieldset {
37+
display: contents;
38+
}
39+
`;
40+
41+
const smoothClassname = css`
42+
scroll-behavior: smooth;
3343
`;
3444

3545
function ScrollToCell() {
3646
const direction = useDirection();
3747
const [idx, setIdx] = useState<number | undefined>(10);
3848
const [rowIdx, setRowIdx] = useState<number | undefined>(10);
49+
const [scrollBehavior, setScrollBehavior] = useState<ScrollBehavior>('auto');
3950
const gridRef = useRef<DataGridHandle>(null);
4051

4152
function scrollToColumn() {
@@ -77,6 +88,31 @@ function ScrollToCell() {
7788
}}
7889
/>
7990
</label>
91+
<fieldset>
92+
<legend>Scroll behavior</legend>
93+
<label>
94+
Auto
95+
<input
96+
type="radio"
97+
name="scroll-behavior"
98+
checked={scrollBehavior === 'auto'}
99+
onChange={() => {
100+
setScrollBehavior('auto');
101+
}}
102+
/>
103+
</label>
104+
<label>
105+
Smooth
106+
<input
107+
type="radio"
108+
name="scroll-behavior"
109+
checked={scrollBehavior === 'smooth'}
110+
onChange={() => {
111+
setScrollBehavior('smooth');
112+
}}
113+
/>
114+
</label>
115+
</fieldset>
80116
</div>
81117
<div className={flexClassname}>
82118
<button type="button" onClick={scrollToCell}>
@@ -93,7 +129,7 @@ function ScrollToCell() {
93129
ref={gridRef}
94130
columns={columns}
95131
rows={rows}
96-
className="fill-grid"
132+
className={clsx('fill-grid', { [smoothClassname]: scrollBehavior === 'smooth' })}
97133
direction={direction}
98134
/>
99135
</>

0 commit comments

Comments
 (0)