Skip to content

Commit 481173e

Browse files
authored
feat: support scrollbar style (#235)
* feat: support scrollbar style * docs: update md * test: add case
1 parent 496be5c commit 481173e

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@ import List from 'rc-virtual-list';
5151

5252
## List
5353

54-
| Prop | Description | Type | Default |
55-
| ---------- | ------------------------------------------------------- | ------------------------------------ | ------- |
56-
| children | Render props of item | (item, index, props) => ReactElement | - |
57-
| component | Customize List dom element | string \| Component | div |
58-
| data | Data list | Array | - |
59-
| disabled | Disable scroll check. Usually used on animation control | boolean | false |
60-
| height | List height | number | - |
61-
| itemHeight | Item minium height | number | - |
62-
| itemKey | Match key with item | string | - |
54+
| Prop | Description | Type | Default |
55+
| ---------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
56+
| children | Render props of item | (item, index, props) => ReactElement | - |
57+
| component | Customize List dom element | string \| Component | div |
58+
| data | Data list | Array | - |
59+
| disabled | Disable scroll check. Usually used on animation control | boolean | false |
60+
| height | List height | number | - |
61+
| itemHeight | Item minium height | number | - |
62+
| itemKey | Match key with item | string | - |
63+
| styles | style | { horizontalScrollBar?: React.CSSProperties; horizontalScrollBarThumb?: React.CSSProperties; verticalScrollBar?: React.CSSProperties; verticalScrollBarThumb?: React.CSSProperties; } | - |
6364

6465
`children` provides additional `props` argument to support IE 11 scroll shaking.
6566
It will set `style` to `visibility: hidden` when measuring. You can ignore this if no requirement on IE.

src/List.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ export interface ListProps<T> extends Omit<React.HTMLAttributes<any>, 'children'
6464
*/
6565
scrollWidth?: number;
6666

67+
styles?: {
68+
horizontalScrollBar?: React.CSSProperties;
69+
horizontalScrollBarThumb?: React.CSSProperties;
70+
verticalScrollBar?: React.CSSProperties;
71+
verticalScrollBarThumb?: React.CSSProperties;
72+
};
73+
6774
onScroll?: React.UIEventHandler<HTMLElement>;
6875

6976
/**
@@ -102,6 +109,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
102109
onVisibleChange,
103110
innerProps,
104111
extraRender,
112+
styles,
105113
...restProps
106114
} = props;
107115

@@ -560,6 +568,8 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
560568
onStopMove={onScrollbarStopMove}
561569
spinSize={verticalScrollBarSpinSize}
562570
containerSize={size.height}
571+
style={styles?.verticalScrollBar}
572+
thumbStyle={styles?.verticalScrollBarThumb}
563573
/>
564574
)}
565575

@@ -576,6 +586,8 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
576586
spinSize={horizontalScrollBarSpinSize}
577587
containerSize={size.width}
578588
horizontal
589+
style={styles?.horizontalScrollBar}
590+
thumbStyle={styles?.horizontalScrollBarThumb}
579591
/>
580592
)}
581593
</div>

src/ScrollBar.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export interface ScrollBarProps {
1313
onStartMove: () => void;
1414
onStopMove: () => void;
1515
horizontal?: boolean;
16-
16+
style?: React.CSSProperties;
17+
thumbStyle?: React.CSSProperties;
1718
spinSize: number;
1819
containerSize: number;
1920
}
@@ -42,6 +43,8 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
4243
horizontal,
4344
spinSize,
4445
containerSize,
46+
style,
47+
thumbStyle: propsThumbStyle,
4548
} = props;
4649

4750
const [dragging, setDragging] = React.useState(false);
@@ -204,6 +207,7 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
204207
const containerStyle: React.CSSProperties = {
205208
position: 'absolute',
206209
visibility: visible && canScroll ? null : 'hidden',
210+
...style,
207211
};
208212

209213
const thumbStyle: React.CSSProperties = {
@@ -212,6 +216,7 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
212216
borderRadius: 99,
213217
cursor: 'pointer',
214218
userSelect: 'none',
219+
...propsThumbStyle,
215220
};
216221

217222
if (horizontal) {

tests/scroll.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,39 @@ describe('List.Scroll', () => {
401401

402402
expect(extraRender).toHaveBeenCalledWith(expect.objectContaining({ end: 99 }));
403403
});
404+
405+
it('scrollbar styles should work', () => {
406+
const { container } = genList(
407+
{
408+
itemHeight: 20,
409+
height: 100,
410+
data: genData(100),
411+
scrollWidth: 1000,
412+
styles: {
413+
horizontalScrollBar: { background: 'red' },
414+
horizontalScrollBarThumb: { background: 'green' },
415+
verticalScrollBar: { background: 'orange' },
416+
verticalScrollBarThumb: { background: 'blue' },
417+
},
418+
},
419+
render,
420+
);
421+
422+
expect(
423+
container.querySelector('.rc-virtual-list-scrollbar-horizontal').style.background,
424+
).toEqual('red');
425+
expect(
426+
container.querySelector(
427+
'.rc-virtual-list-scrollbar-horizontal .rc-virtual-list-scrollbar-thumb',
428+
).style.background,
429+
).toEqual('green');
430+
expect(container.querySelector('.rc-virtual-list-scrollbar-vertical').style.background).toEqual(
431+
'orange',
432+
);
433+
expect(
434+
container.querySelector(
435+
'.rc-virtual-list-scrollbar-vertical .rc-virtual-list-scrollbar-thumb',
436+
).style.background,
437+
).toEqual('blue');
438+
});
404439
});

0 commit comments

Comments
 (0)