ScrollArea
Area with custom scrollbars
Import
Source
Docs
Package
Usage
ScrollArea
component supports the following props:
type
defines scrollbars behavior:hover
β scrollbars are visible on hoverscroll
β scrollbars are visible on scrollauto
β similar tooverflow: auto
β scrollbars are always visible when the content is overflowingalways
β same asauto
, but scrollbars are always visible regardless of whether the content is overflowingnever
β scrollbars are always hidden
offsetScrollbars
β offset scrollbars with paddingscrollbarSize
β scrollbar size, controls scrollbar and thumb width/heightscrollHideDelay
β delay in ms to hide scrollbars, applicable only when type ishover
orscroll
Charizard (PokΓ©mon)
Charizard description from Bulbapedia
Charizard is a draconic, bipedal PokΓ©mon. It is primarily orange with a cream underside from the chest to the tip of its tail. It has a long neck, small blue eyes, slightly raised nostrils, and two horn-like structures protruding from the back of its rectangular head. There are two fangs visible in the upper jaw when its mouth is closed. Two large wings with blue-green undersides sprout from its back, and a horn-like appendage juts out from the top of the third joint of each wing. A single wing-finger is visible through the center of each wing membrane. Charizard's arms are short and skinny compared to its robust belly, and each limb has three white claws. It has stocky legs with cream-colored soles on each of its plantigrade feet. The tip of its long, tapering tail burns with a sizable flame.
As Mega Charizard X, its body and legs are more physically fit, though its arms remain thin. Its skin turns black with a sky-blue underside and soles. Two spikes with blue tips curve upward from the front and back of each shoulder, while the tips of its horns sharpen, turn blue, and curve slightly upward. Its brow and claws are larger, and its eyes are now red. It has two small, fin-like spikes under each horn and two more down its lower neck. The finger disappears from the wing membrane, and the lower edges are divided into large, rounded points. The third joint of each wing-arm is adorned with a claw-like spike. Mega Charizard X breathes blue flames out the sides of its mouth, and the flame on its tail now burns blue. It is said that its new power turns it black and creates more intense flames.
import { ScrollArea } from '@mantine/core';
function Demo() {
return (
<ScrollArea h={250}>
{/* ... content */}
</ScrollArea>
);
}
Horizontal scrollbars
Charizard (PokΓ©mon)
Charizard description from Bulbapedia
Charizard is a draconic, bipedal PokΓ©mon. It is primarily orange with a cream underside from the chest to the tip of its tail. It has a long neck, small blue eyes, slightly raised nostrils, and two horn-like structures protruding from the back of its rectangular head. There are two fangs visible in the upper jaw when its mouth is closed. Two large wings with blue-green undersides sprout from its back, and a horn-like appendage juts out from the top of the third joint of each wing. A single wing-finger is visible through the center of each wing membrane. Charizard's arms are short and skinny compared to its robust belly, and each limb has three white claws. It has stocky legs with cream-colored soles on each of its plantigrade feet. The tip of its long, tapering tail burns with a sizable flame.
As Mega Charizard X, its body and legs are more physically fit, though its arms remain thin. Its skin turns black with a sky-blue underside and soles. Two spikes with blue tips curve upward from the front and back of each shoulder, while the tips of its horns sharpen, turn blue, and curve slightly upward. Its brow and claws are larger, and its eyes are now red. It has two small, fin-like spikes under each horn and two more down its lower neck. The finger disappears from the wing membrane, and the lower edges are divided into large, rounded points. The third joint of each wing-arm is adorned with a claw-like spike. Mega Charizard X breathes blue flames out the sides of its mouth, and the flame on its tail now burns blue. It is said that its new power turns it black and creates more intense flames.
import { ScrollArea, Box } from '@mantine/core';
function Demo() {
return (
<ScrollArea w={300} h={200}>
<Box w={600}>
{/* ... content */}
</Box>
</ScrollArea>
);
}
Disable horizontal scrollbars
To disable horizontal scrollbars set scrollbars="y"
prop:
Charizard (PokΓ©mon)
Charizard description from Bulbapedia
Charizard is a draconic, bipedal PokΓ©mon. It is primarily orange with a cream underside from the chest to the tip of its tail. It has a long neck, small blue eyes, slightly raised nostrils, and two horn-like structures protruding from the back of its rectangular head. There are two fangs visible in the upper jaw when its mouth is closed. Two large wings with blue-green undersides sprout from its back, and a horn-like appendage juts out from the top of the third joint of each wing. A single wing-finger is visible through the center of each wing membrane. Charizard's arms are short and skinny compared to its robust belly, and each limb has three white claws. It has stocky legs with cream-colored soles on each of its plantigrade feet. The tip of its long, tapering tail burns with a sizable flame.
As Mega Charizard X, its body and legs are more physically fit, though its arms remain thin. Its skin turns black with a sky-blue underside and soles. Two spikes with blue tips curve upward from the front and back of each shoulder, while the tips of its horns sharpen, turn blue, and curve slightly upward. Its brow and claws are larger, and its eyes are now red. It has two small, fin-like spikes under each horn and two more down its lower neck. The finger disappears from the wing membrane, and the lower edges are divided into large, rounded points. The third joint of each wing-arm is adorned with a claw-like spike. Mega Charizard X breathes blue flames out the sides of its mouth, and the flame on its tail now burns blue. It is said that its new power turns it black and creates more intense flames.
import { ScrollArea, Box } from '@mantine/core';
function Demo() {
return (
<ScrollArea w={300} h={200} scrollbars="y">
<Box w={600}>
{/* ... content */}
</Box>
</ScrollArea>
);
}
Subscribe to scroll position changes
Set onScrollPositionChange
function to subscribe to scroll position changes,
it will be called each time user scrolls with x and y coordinates:
Charizard (PokΓ©mon)
Charizard description from Bulbapedia
Charizard is a draconic, bipedal PokΓ©mon. It is primarily orange with a cream underside from the chest to the tip of its tail. It has a long neck, small blue eyes, slightly raised nostrils, and two horn-like structures protruding from the back of its rectangular head. There are two fangs visible in the upper jaw when its mouth is closed. Two large wings with blue-green undersides sprout from its back, and a horn-like appendage juts out from the top of the third joint of each wing. A single wing-finger is visible through the center of each wing membrane. Charizard's arms are short and skinny compared to its robust belly, and each limb has three white claws. It has stocky legs with cream-colored soles on each of its plantigrade feet. The tip of its long, tapering tail burns with a sizable flame.
As Mega Charizard X, its body and legs are more physically fit, though its arms remain thin. Its skin turns black with a sky-blue underside and soles. Two spikes with blue tips curve upward from the front and back of each shoulder, while the tips of its horns sharpen, turn blue, and curve slightly upward. Its brow and claws are larger, and its eyes are now red. It has two small, fin-like spikes under each horn and two more down its lower neck. The finger disappears from the wing membrane, and the lower edges are divided into large, rounded points. The third joint of each wing-arm is adorned with a claw-like spike. Mega Charizard X breathes blue flames out the sides of its mouth, and the flame on its tail now burns blue. It is said that its new power turns it black and creates more intense flames.
Scroll position: { x: 0, y: 0 }
import { useState } from 'react';
import { Text, ScrollArea, Code, Box } from '@mantine/core';
function Demo() {
const [scrollPosition, onScrollPositionChange] = useState({ x: 0, y: 0 });
return (
<>
<ScrollArea
w={300}
h={200}
onScrollPositionChange={onScrollPositionChange}
>
<Box w={600}>
{/* ... content */}
</Box>
</ScrollArea>
<Text>
Scroll position: <Code>{`{ x: ${scrollPosition.x}, y: ${scrollPosition.y} }`}</Code>
</Text>
</>
);
}
Scroll to position
To programmatically scroll to any position,
get viewport element ref with viewportRef
prop and call scrollTo
method:
Charizard (PokΓ©mon)
Charizard description from Bulbapedia
Charizard is a draconic, bipedal PokΓ©mon. It is primarily orange with a cream underside from the chest to the tip of its tail. It has a long neck, small blue eyes, slightly raised nostrils, and two horn-like structures protruding from the back of its rectangular head. There are two fangs visible in the upper jaw when its mouth is closed. Two large wings with blue-green undersides sprout from its back, and a horn-like appendage juts out from the top of the third joint of each wing. A single wing-finger is visible through the center of each wing membrane. Charizard's arms are short and skinny compared to its robust belly, and each limb has three white claws. It has stocky legs with cream-colored soles on each of its plantigrade feet. The tip of its long, tapering tail burns with a sizable flame.
As Mega Charizard X, its body and legs are more physically fit, though its arms remain thin. Its skin turns black with a sky-blue underside and soles. Two spikes with blue tips curve upward from the front and back of each shoulder, while the tips of its horns sharpen, turn blue, and curve slightly upward. Its brow and claws are larger, and its eyes are now red. It has two small, fin-like spikes under each horn and two more down its lower neck. The finger disappears from the wing membrane, and the lower edges are divided into large, rounded points. The third joint of each wing-arm is adorned with a claw-like spike. Mega Charizard X breathes blue flames out the sides of its mouth, and the flame on its tail now burns blue. It is said that its new power turns it black and creates more intense flames.
import { useRef } from 'react';
import { ScrollArea, Button, Stack, Group } from '@mantine/core';
function Demo() {
const viewport = useRef<HTMLDivElement>(null);
const scrollToBottom = () =>
viewport.current!.scrollTo({ top: viewport.current!.scrollHeight, behavior: 'smooth' });
const scrollToCenter = () =>
viewport.current!.scrollTo({ top: viewport.current!.scrollHeight / 2, behavior: 'smooth' });
const scrollToTop = () => viewport.current!.scrollTo({ top: 0, behavior: 'smooth' });
return (
<Stack align="center">
<ScrollArea w={300} h={200} viewportRef={viewport}>
{/* ... content */}
</ScrollArea>
<Group justify="center">
<Button onClick={scrollToBottom}>Scroll to bottom</Button>
<Button onClick={scrollToCenter}>Scroll to center</Button>
<Button onClick={scrollToTop}>Scroll to top</Button>
</Group>
</Stack>
);
}
Styles API
Charizard (PokΓ©mon)
Charizard description from Bulbapedia
Charizard is a draconic, bipedal PokΓ©mon. It is primarily orange with a cream underside from the chest to the tip of its tail. It has a long neck, small blue eyes, slightly raised nostrils, and two horn-like structures protruding from the back of its rectangular head. There are two fangs visible in the upper jaw when its mouth is closed. Two large wings with blue-green undersides sprout from its back, and a horn-like appendage juts out from the top of the third joint of each wing. A single wing-finger is visible through the center of each wing membrane. Charizard's arms are short and skinny compared to its robust belly, and each limb has three white claws. It has stocky legs with cream-colored soles on each of its plantigrade feet. The tip of its long, tapering tail burns with a sizable flame.
As Mega Charizard X, its body and legs are more physically fit, though its arms remain thin. Its skin turns black with a sky-blue underside and soles. Two spikes with blue tips curve upward from the front and back of each shoulder, while the tips of its horns sharpen, turn blue, and curve slightly upward. Its brow and claws are larger, and its eyes are now red. It has two small, fin-like spikes under each horn and two more down its lower neck. The finger disappears from the wing membrane, and the lower edges are divided into large, rounded points. The third joint of each wing-arm is adorned with a claw-like spike. Mega Charizard X breathes blue flames out the sides of its mouth, and the flame on its tail now burns blue. It is said that its new power turns it black and creates more intense flames.
import { ScrollArea, Box } from '@mantine/core';
import classes from './Demo.module.css';
function Demo() {
return (
<ScrollArea w={300} h={200} type="always" offsetScrollbars classNames={classes}>
<Box w={600}>
{/* ... content */}
</Box>
</ScrollArea>
);
}
Scroll element into view
import { useState, useRef } from 'react';
import { ScrollArea, UnstyledButton, TextInput } from '@mantine/core';
const groceries: string[] = [
'π Apples',
'π Bananas',
'π Oranges',
'π₯ Milk',
'π Bread',
'π₯ Eggs',
'π Chicken',
'π₯© Beef',
'π Pasta',
'π Rice',
'π₯ Potatoes',
'π§
Onions',
'π
Tomatoes',
'π₯ Cucumbers',
'π₯ Carrots',
'π₯¬ Lettuce',
'π Spinach',
'π₯¦ Broccoli',
'π§ Cheese',
'π¦ Yogurt',
'π§ Butter',
'π Sugar',
'π§ Salt',
'πΆοΈ Pepper',
'β Coffee',
'π΅ Tea',
'π₯€ Juice',
'π§ Water',
'πͺ Cookies',
'π« Chocolate',
];
function Demo() {
const viewportRef = useRef<HTMLDivElement>(null);
const [query, setQuery] = useState('');
const [hovered, setHovered] = useState(-1);
const filtered = groceries.filter((item) => item.toLowerCase().includes(query.toLowerCase()));
const items = filtered.map((item, index) => (
<UnstyledButton
data-list-item
key={item}
display="block"
bg={index === hovered ? 'var(--mantine-color-blue-light)' : undefined}
w="100%"
p={5}
>
{item}
</UnstyledButton>
));
return (
<>
<TextInput
value={query}
onChange={(event) => {
setQuery(event.currentTarget.value);
setHovered(-1);
}}
onKeyDown={(event) => {
if (event.key === 'ArrowDown') {
event.preventDefault();
setHovered((current) => {
const nextIndex = current + 1 >= filtered.length ? current : current + 1;
viewportRef.current
?.querySelectorAll('[data-list-item]')
?.[nextIndex]?.scrollIntoView({ block: 'nearest' });
return nextIndex;
});
}
if (event.key === 'ArrowUp') {
event.preventDefault();
setHovered((current) => {
const nextIndex = current - 1 < 0 ? current : current - 1;
viewportRef.current
?.querySelectorAll('[data-list-item]')
?.[nextIndex]?.scrollIntoView({ block: 'nearest' });
return nextIndex;
});
}
}}
placeholder="Search groceries"
/>
<ScrollArea h={150} type="always" mt="md" viewportRef={viewportRef}>
{items}
</ScrollArea>
</>
);
}
ScrollArea.Autosize
ScrollArea.Autosize
component allows to create scrollable containers when given max-height is reached:
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dicta perspiciatis reiciendis voluptate eaque itaque quos. Natus iure tenetur libero, reprehenderit ad, sequi, in aliquam eos necessitatibus expedita delectus veniam culpa!
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dicta perspiciatis reiciendis voluptate eaque itaque quos. Natus iure tenetur libero, reprehenderit ad, sequi, in aliquam eos necessitatibus expedita delectus veniam culpa!
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dicta perspiciatis reiciendis voluptate eaque itaque quos. Natus iure tenetur libero, reprehenderit ad, sequi, in aliquam eos necessitatibus expedita delectus veniam culpa!
import { useCounter } from '@mantine/hooks';
import { ScrollArea, Button, Group } from '@mantine/core';
const lorem =
'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dicta perspiciatis reiciendis voluptate eaque itaque quos. Natus iure tenetur libero, reprehenderit ad, sequi, in aliquam eos necessitatibus expedita delectus veniam culpa!';
function Demo() {
const [count, handlers] = useCounter(3, { min: 0, max: 10 });
const content = Array(count)
.fill(0)
.map((_, index) => <p key={index}>{lorem}</p>);
return (
<>
<ScrollArea.Autosize mah={300} maw={400} mx="auto">
{content}
</ScrollArea.Autosize>
<Group justify="center" mt="md">
<Button color="red" onClick={handlers.decrement}>
Remove paragraph
</Button>
<Button onClick={handlers.increment}>
Add paragraph
</Button>
</Group>
</>
);
}
ScrollArea.Autosize with Popover
import { useState, useRef } from 'react';
import { ScrollArea, Popover, TextInput, UnstyledButton, Text, Box } from '@mantine/core';
const groceries = [
'Apples',
'Bananas',
'Oranges',
'Milk',
'Bread',
'Eggs',
'Chicken',
'Beef',
'Pasta',
'Rice',
'Potatoes',
'Onions',
'Tomatoes',
'Cucumbers',
'Carrots',
'Lettuce',
'Spinach',
'Broccoli',
'Cheese',
'Yogurt',
'Butter',
'Sugar',
'Salt',
'Pepper',
'Coffee',
'Tea',
'Juice',
'Water',
'Cookies',
'Chocolate',
];
function Demo() {
const viewportRef = useRef<HTMLDivElement>(null);
const [query, setQuery] = useState('');
const [opened, setOpened] = useState(false);
const [hovered, setHovered] = useState(-1);
const filtered = groceries.filter((item) => item.toLowerCase().includes(query.toLowerCase()));
const items = filtered.map((item, index) => (
<UnstyledButton
data-list-item
key={item}
display="block"
bg={index === hovered ? 'var(--mantine-color-blue-light)' : undefined}
w="100%"
p={5}
>
{item}
</UnstyledButton>
));
return (
<Popover width="target" opened={opened}>
<Popover.Target>
<TextInput
value={query}
onFocus={() => setOpened(true)}
onBlur={() => setOpened(false)}
onChange={(event) => {
setQuery(event.currentTarget.value);
setHovered(-1);
}}
onKeyDown={(event) => {
if (event.key === 'ArrowDown') {
event.preventDefault();
setHovered((current) => {
const nextIndex = current + 1 >= filtered.length ? current : current + 1;
viewportRef.current
?.querySelectorAll('[data-list-item]')
?.[nextIndex]?.scrollIntoView({ block: 'nearest' });
return nextIndex;
});
}
if (event.key === 'ArrowUp') {
event.preventDefault();
setHovered((current) => {
const nextIndex = current - 1 < 0 ? current : current - 1;
viewportRef.current
?.querySelectorAll('[data-list-item]')
?.[nextIndex]?.scrollIntoView({ block: 'nearest' });
return nextIndex;
});
}
}}
placeholder="Search groceries"
/>
</Popover.Target>
<Popover.Dropdown p={0}>
<ScrollArea.Autosize viewportRef={viewportRef} mah={200} type="always" scrollbars="y">
<Box px="xs" py={5}>
{items.length > 0 ? items : <Text c="dimmed">Nothing found</Text>}
</Box>
</ScrollArea.Autosize>
</Popover.Dropdown>
</Popover>
);
}
Build fully functional accessible web applications faster than ever