ScrollView makes long content scrollable.
👉 Please note that HTML is rendered even when no children are provided. This is needed to allow the auto-scroll feature to work.
To implement the ScrollView component, you need to import it first:
import { ScrollView } from '@react-ui-org/react-ui';
And use it:
See API for all available options.
ScrollView takes up all available horizontal space and expands vertically according to the length of its content. Depending on your layout, you may need to restrict the height of its parent to activate scrolling.
While arrow controls are optional in the vertical mode, you should always enable arrows in the horizontal mode when the scrollbar is disabled. Because if you don't, users without horizontal-scrolling-enabled devices (like an old-school mouse) might not be able to access your content.
For dynamic content such as chat window or console output, consider using the auto-scroll feature. This will ensure the newest content is always visible.
ScrollView enables scrolling on all screen sizes. If you need to make an area in your app scrollable based on viewport size, please use custom CSS with media queries instead.
ScrollView only supports scrolling in a single direction at a time. It
crops content that would possibly overflow in the other direction because
additional scrollbars would be unreachable under scrolling shadows. If you
need your content to be ready for bi-directional scrolling, either consider
using just overflow: auto
instead of ScrollView, or make your content
scrollable before putting it into ScrollView and make sure its scrollbars
don't collide with scrolling shadows.
You can suppress the system scrollbar and display arrow controls instead.
The horizontal mode is useful when you have (or expect to have) a lot of horizontal content and you need to make sure it remains accessible on all viewport sizes.
ScrollView can watch its content for changes and automatically scroll to the end when the content has changed.
⚠️ The auto-scroll functionality requires all children to have the key
property defined because it detects changes of these keys.
It's possible to entirely customize the arrow controls, including the scroll step, and the scrolling shadows.
You can pass any HTML element or even a custom React component to be used as the arrow control. Furthermore, you can change the scroll step if you need to scroll by smaller or bigger portions.
You can customize the start and end scrolling shadows using startShadow*
and
endShadow*
properties.
For easier CSS definition of linear gradients for both vertical and horizontal
directions at the same time, there are --rui-local-start-shadow-direction
and
--rui-local-end-shadow-direction
custom properties that can be used inside
startShadowBackground
and endShadowBackground
definitions. The value of the
custom properties then reacts to the direction
option:
Custom property | Vertical direction | Horizontal direction |
---|---|---|
--rui-local-start-shadow-direction | to bottom | to right |
--rui-local-end-shadow-direction | to top | to left |
This is useful if you want to create a single definition of linear gradients for scrolling shadows in both directions via global props.
In addition to the options below in the component's API section, you
can specify React synthetic events or any HTML attribute you like. All
attributes that don't interfere with the API are forwarded to the <div>
HTML
element. This enables making the component interactive and helps to improve its
accessibility.
👉 Refer to the MDN reference for the full list of supported attributes of the div element.
If you provide ref, it is forwarded to the scrolling viewport native HTML
<div>
element.
Prop name | Type | Default | Required | Description |
---|---|---|---|---|
arrows | bool | false | false | If |
arrowsScrollStep | number | 200 | false | Portion to scroll by when the arrows are clicked, in px. |
autoScroll | 'always' │ 'detectEnd' │ 'off' | 'off' | false | The auto-scroll mechanism requires having the Option |
children | node | null | false | Content to be scrollable. |
debounce | number | 50 | false | Delay in ms before the display of arrows and scrolling shadows is evaluated during interaction. |
direction | 'horizontal' │ 'vertical' | 'vertical' | false | Direction of scrolling. |
endShadowBackground | string | "linear-gradient(var(--rui-local-end-shadow-direction), rgba(255 255 255 / 1), rgba(255 255 255 / 0))" | false | Custom background of the end scrolling shadow. Can be a CSS gradient or an image |
endShadowInitialOffset | string | "-1rem" | false | Initial offset of the end scrolling shadow (transitioned). If set, the end scrolling shadow slides in by this distance. |
endShadowSize | string | "2em" | false | Size of the end scrolling shadow. Accepts any valid CSS length value. |
id | string | undefined | false | ID of the root HTML element. It also serves as base for nested elements:
|
nextArrowColor | string | undefined | false | Text color of the end arrow control. Accepts any valid CSS color value. |
nextArrowElement | node | null | false | Custom HTML or React Component to replace the default next-arrow control. |
nextArrowInitialOffset | string | "-0.5rem" | false | Initial offset of the end arrow control (transitioned). If set, the next arrow slides in by this distance. |
prevArrowColor | string | undefined | false | Text color of the start arrow control. Accepts any valid CSS color value. |
prevArrowElement | node | null | false | Custom HTML or React Component to replace the default prev-arrow control. |
prevArrowInitialOffset | string | "-0.5rem" | false | Initial offset of the start arrow control (transitioned). If set, the prev arrow slides in by this distance. |
scrollbar | bool | true | false | If |
shadows | bool | true | false | If |
startShadowBackground | string | "linear-gradient(var(--rui-local-start-shadow-direction), rgba(255 255 255 / 1), rgba(255 255 255 / 0))" | false | Custom background of the start scrolling shadow. Can be a CSS gradient or an image |
startShadowInitialOffset | string | "-1rem" | false | Initial offset of the start scrolling shadow (transitioned). If set, the start scrolling shadow slides in by this distance. |
startShadowSize | string | "2em" | false | Size of the start scrolling shadow. Accepts any valid CSS length value. |