Skip to content

Commit

Permalink
feat(react/scrollbar): add scrollViewProps to enable passing cust…
Browse files Browse the repository at this point in the history
…om props to the `ScrollView` component (#939)

* docs(scrollbar): add React Virtuoso integration example to the FAQ section

* docs: update scrollbar page

* feat: add `scrollViewProps` to enable passing custom props to the `ScrollView` component

* Create tonic-ui-938.md

* docs: update scrollbar page

* docs: update scrollbar page
  • Loading branch information
cheton authored Oct 17, 2024
1 parent 7b994c5 commit 43d3615
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tonic-ui-938.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tonic-ui/react": minor
---

feat(`react/scrollbar`): add `scrollViewProps` to enable passing custom props to the `ScrollView` component
1 change: 1 addition & 0 deletions packages/react-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"react-movable": "^3.0.4",
"react-table": "^7.8.0",
"react-virtualized-auto-sizer": "^1.0.15",
"react-virtuoso": "^4.12.0",
"rehype-autolink-headings": "^6.1.1",
"rehype-slug": "^5.1.0",
"remark-emoji": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Box, Scrollbar } from '@tonic-ui/react';
import { Virtuoso } from 'react-virtuoso';
import React, { forwardRef } from 'react';

const CustomScrollbar = forwardRef((inProps, ref) => {
const { children, ...props } = inProps;

return (
<Scrollbar
overflowY="scroll"
scrollViewProps={props}
scrollViewRef={ref}
>
{children}
</Scrollbar>
);
});

CustomScrollbar.displayName = 'CustomScrollbar';

const App = () => {
return (
<Virtuoso
components={{
Scroller: CustomScrollbar,
}}
style={{
height: 400,
}}
totalCount={10000}
itemContent={(index) => (
<Box>Item {index}</Box>
)}
/>
);
};

export default App;
42 changes: 42 additions & 0 deletions packages/react-docs/pages/components/scrollbar/index.page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,47 @@ To see this in action, simply scroll through the content and click the toggle bu

{render('./faq-scroll-position')}

### How to integrate React Virtuoso with a custom scrollbar?

Check out the demo below to see how you can integrate React Virtuoso with a custom scrollbar:

{render('./faq-react-virtuoso')}

To achieve this, you'll need to create a `CustomScrollbar` component and pass it as the `Scroller` for the `Virtuoso` component.

```jsx disabled
<Virtuoso
components={{
Scroller: CustomScrollbar,
}}
style={{
height: 400,
}}
totalCount={10000}
itemContent={(index) => (
<Box>Item {index}</Box>
)}
/>
```

In the `CustomScrollbar` component, pass `props` and `ref` to `scrollViewProps` and `scrollViewRef`, respectively. This ensures that the props coming from React Virtuoso are passed down correctly, allowing its scrolling behavior to integrate seamlessly into the `Scrollbar`.

```jsx disabled
const CustomScrollbar = forwardRef((inProps, ref) => {
const { children, ...props } = inProps;

return (
<Scrollbar
overflowY="scroll"
scrollViewProps={props}
scrollViewRef={ref}
>
{children}
</Scrollbar>
);
});
```

## Props

### Scrollbar
Expand All @@ -102,4 +143,5 @@ To see this in action, simply scroll through the content and click the toggle bu
| overflowY | string | | The vertical overflow of the scrollable content. One of: 'auto', 'scroll', 'hidden'. |
| scrollLeft | number | 0 | The horizontal scroll position of the scrollable content. |
| scrollTop | number | 0 | The vertical scroll position of the scrollable content. |
| scrollViewProps | object | | Additional props to be applied to the `ScrollView` component. |
| scrollViewRef | RefObject | | A `ref` to the `ScrollView` component. |
11 changes: 7 additions & 4 deletions packages/react/src/scrollbar/Scrollbar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useHydrated, useMergeRefs } from '@tonic-ui/react-hooks';
import { callEventHandlers } from '@tonic-ui/utils';
import { ensurePositiveFiniteNumber } from 'ensure-type';
import React, { forwardRef, useCallback, useEffect, useState, useRef } from 'react';
import { Box } from '../box';
Expand Down Expand Up @@ -37,6 +38,7 @@ const Scrollbar = forwardRef((inProps, ref) => {
overflowY: overflowYProp,
scrollLeft: scrollLeftProp,
scrollTop: scrollTopProp,
scrollViewProps: scrollViewPropsProp,
scrollViewRef: scrollViewRefProp,
...rest
} = useDefaultProps({ props: inProps, name: 'Scrollbar' });
Expand Down Expand Up @@ -546,12 +548,13 @@ const Scrollbar = forwardRef((inProps, ref) => {

const getScrollViewProps = () => {
return {
children,
...scrollViewStyle,
...scrollViewPropsProp,
ref: combinedScrollViewRef,
onScroll: handleScrollViewScroll,
onMouseEnter: handleScrollViewMouseEnter,
onMouseLeave: handleScrollViewMouseLeave,
children,
onScroll: callEventHandlers(scrollViewPropsProp?.onScroll, handleScrollViewScroll),
onMouseEnter: callEventHandlers(scrollViewPropsProp?.onMouseEnter, handleScrollViewMouseEnter),
onMouseLeave: callEventHandlers(scrollViewPropsProp?.onMouseLeave, handleScrollViewMouseLeave),
};
};

Expand Down
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4395,6 +4395,7 @@ __metadata:
react-movable: ^3.0.4
react-table: ^7.8.0
react-virtualized-auto-sizer: ^1.0.15
react-virtuoso: ^4.12.0
rehype-autolink-headings: ^6.1.1
rehype-slug: ^5.1.0
remark-emoji: ^4.0.0
Expand Down Expand Up @@ -14872,6 +14873,16 @@ __metadata:
languageName: node
linkType: hard

"react-virtuoso@npm:^4.12.0":
version: 4.12.0
resolution: "react-virtuoso@npm:4.12.0"
peerDependencies:
react: ">=16 || >=17 || >= 18"
react-dom: ">=16 || >=17 || >= 18"
checksum: 23e802f3e4c58b94f70179fd6f3768ebc2e28c46bbf27f8d48f94627f27ae84111e6291ac817bab91302cabc376db67b8aa99f9cd674941448cc7752d6c9ae76
languageName: node
linkType: hard

"react@npm:^16.14.0 || ^17 || ^18, react@npm:latest":
version: 18.2.0
resolution: "react@npm:18.2.0"
Expand Down

0 comments on commit 43d3615

Please sign in to comment.