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

feat: virtual scrollbar will hide after delay, add a alwaysShowScrollbar param to support always show scrollbar #158

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ open http://localhost:9001/
```js
import List from 'rc-virtual-list';

<List data={[0, 1, 2]} height={200} itemHeight={30} itemKey="id">
<List data={[0, 1, 2]} height={200} itemHeight={30} alwaysShowScrollbar={true} itemKey="id">
{index => <div>{index}</div>}
</List>;
```
Expand All @@ -60,6 +60,7 @@ import List from 'rc-virtual-list';
| height | List height | number | - |
| itemHeight | Item minium height | number | - |
| itemKey | Match key with item | string | - |
| alwaysShowScrollbar | Virtual scrollbar will show always | boolean | false |

`children` provides additional `props` argument to support IE 11 scroll shaking.
It will set `style` to `visibility: hidden` when measuring. You can ignore this if no requirement on IE.
3 changes: 2 additions & 1 deletion examples/height.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const Demo = () => {

<List
data={data}
alwaysShowScrollbar={true}
height={500}
itemHeight={30}
itemKey="id"
Expand All @@ -50,7 +51,7 @@ const Demo = () => {
boxSizing: 'border-box',
}}
>
{item => <ForwardMyItem {...item} />}
{(item) => <ForwardMyItem {...item} />}
</List>
</div>
</React.StrictMode>
Expand Down
13 changes: 8 additions & 5 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface ListProps<T> extends React.HTMLAttributes<any> {
children: RenderFunc<T>;
data: T[];
height?: number;
alwaysShowScrollbar?: boolean;
itemHeight?: number;
/** If not match virtual scroll condition, Set List still use height of container. */
fullHeight?: boolean;
Expand All @@ -56,6 +57,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
const {
prefixCls = 'rc-virtual-list',
className,
alwaysShowScrollbar,
height,
itemHeight,
fullHeight = true,
Expand Down Expand Up @@ -99,7 +101,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {

// ================================ Scroll ================================
function syncScrollTop(newTop: number | ((prev: number) => number)) {
setScrollTop(origin => {
setScrollTop((origin) => {
let value: number;
if (typeof newTop === 'function') {
value = newTop(origin);
Expand Down Expand Up @@ -242,8 +244,8 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
useVirtual,
isScrollAtTop,
isScrollAtBottom,
offsetY => {
syncScrollTop(top => {
(offsetY) => {
syncScrollTop((top) => {
const newTop = top + offsetY;
return newTop;
});
Expand Down Expand Up @@ -338,9 +340,10 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
{listChildren}
</Filler>
</Component>

{useVirtual && (
{/* TODO: 高度不够 没有出现滚动条的时候不需要渲染下面的 scrollBar */}
{useVirtual && inVirtual && (
<ScrollBar
alwaysShowScrollbar={alwaysShowScrollbar}
ref={scrollBarRef}
prefixCls={prefixCls}
scrollTop={scrollTop}
Expand Down
4 changes: 3 additions & 1 deletion src/ScrollBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ScrollBarProps {
scrollHeight: number;
height: number;
count: number;
alwaysShowScrollbar?: boolean;
onScroll: (scrollTop: number) => void;
onStartMove: () => void;
onStopMove: () => void;
Expand Down Expand Up @@ -39,7 +40,7 @@ export default class ScrollBar extends React.Component<ScrollBarProps, ScrollBar
dragging: false,
pageY: null,
startTop: null,
visible: false,
visible: !!this.props.alwaysShowScrollbar,
};

componentDidMount() {
Expand All @@ -59,6 +60,7 @@ export default class ScrollBar extends React.Component<ScrollBarProps, ScrollBar
}

delayHidden = () => {
if (this.props.alwaysShowScrollbar) return;
clearTimeout(this.visibleTimeout);

this.setState({ visible: true });
Expand Down