Scroll Area
A native scroll container with custom scrollbars.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{ScrollArea, ScrollAreaContent, ScrollAreaScrollbar, ScrollAreaThumb, ScrollAreaViewport};
#[Component]
function ScrollAreaHeroDemo(): Node
{
return (
<ScrollArea>
<ScrollAreaViewport>
<ScrollAreaContent>
<p>Vernacular architecture is building done outside any academic tradition, and without professional guidance. It is not a particular architectural movement or style, but rather a broad category, encompassing a wide range and variety of building types, with differing methods of construction, from around the world, both historical and extant and classical and modern. Vernacular architecture constitutes 95% of the world's built environment, as estimated in 1995 by Amos Rapoport, as measured against the small percentage of new buildings every year designed by architects and built by engineers.</p>
<p>This type of architecture usually serves immediate, local needs, is constrained by the materials available in its particular region and reflects local traditions and cultural practices. The study of vernacular architecture does not examine formally schooled architects, but instead that of the design skills and tradition of local builders, who were rarely given any attribution for the work. More recently, vernacular architecture has been examined by designers and the building industry in an effort to be more energy conscious with contemporary design and construction—part of a broader interest in sustainable design.</p>
</ScrollAreaContent>
</ScrollAreaViewport>
<ScrollAreaScrollbar>
<ScrollAreaThumb />
</ScrollAreaScrollbar>
</ScrollArea>
);
}
Examples#
Both scrollbars#
Use ScrollAreaCorner to prevent the scrollbars from intersecting.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Orientation;
use function Phem\Ui\{ScrollArea, ScrollAreaContent, ScrollAreaCorner, ScrollAreaScrollbar, ScrollAreaThumb, ScrollAreaViewport};
#[Component]
function ScrollAreaBothDemo(): Node
{
return (
<ScrollArea className="h-80 w-80">
<ScrollAreaViewport>
<ScrollAreaContent className="pt-3 pr-6 pb-6">
<ul className="m-0 grid list-none grid-cols-[repeat(10,6.25rem)] grid-rows-[repeat(10,6.25rem)] gap-3 p-0">
{% for i in 1..100 %}
<li key={{ i }} className="flex items-center justify-center bg-neutral-200 text-sm font-bold text-neutral-600 dark:bg-neutral-800 dark:text-neutral-400">{{ i }}</li>
{% endfor %}
</ul>
</ScrollAreaContent>
</ScrollAreaViewport>
<ScrollAreaScrollbar>
<ScrollAreaThumb />
</ScrollAreaScrollbar>
<ScrollAreaScrollbar orientation={{ Orientation.Horizontal }}>
<ScrollAreaThumb />
</ScrollAreaScrollbar>
<ScrollAreaCorner />
</ScrollArea>
);
}
Gradient scroll fade#
Use the viewport overflow CSS variables to drive a CSS mask, which gradually increases the fade as the user scrolls away from the edges.
The server render has no overflow variables yet, so give a fallback to the end-side var() call: the mask shows before the controller connects.
When the fade is applied to ScrollAreaViewport itself, the variables can be used directly. However, inheritance to children is disabled, so they must explicitly opt-in using the inherit keyword.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{ScrollArea, ScrollAreaContent, ScrollAreaScrollbar, ScrollAreaThumb, ScrollAreaViewport};
#[Component]
function ScrollAreaFadeDemo(): Node
{
return (
<ScrollArea className="h-48 w-96 max-w-[calc(100vw-8rem)] bg-neutral-100 dark:bg-neutral-800 has-[>_:first-child:focus-visible]:outline-2 has-[>_:first-child:focus-visible]:outline-offset-0 has-[>_:first-child:focus-visible]:outline-neutral-950 dark:has-[>_:first-child:focus-visible]:outline-white">
<ScrollAreaViewport className="border-0 bg-neutral-100 outline-none dark:bg-neutral-800 mask-linear-[to_bottom,transparent_0,black_min(40px,var(--scroll-area-overflow-y-start)),black_calc(100%_-_min(40px,var(--scroll-area-overflow-y-end,40px))),transparent_100%] mask-no-repeat">
<ScrollAreaContent>
<p>Vernacular architecture is building done outside any academic tradition, and without professional guidance. It is not a particular architectural movement or style, but rather a broad category, encompassing a wide range and variety of building types, with differing methods of construction, from around the world, both historical and extant and classical and modern. Vernacular architecture constitutes 95% of the world’s built environment, as estimated in 1995 by Amos Rapoport, as measured against the small percentage of new buildings every year designed by architects and built by engineers.</p>
<p>This type of architecture usually serves immediate, local needs, is constrained by the materials available in its particular region and reflects local traditions and cultural practices. The study of vernacular architecture does not examine formally schooled architects, but instead that of the design skills and tradition of local builders, who were rarely given any attribution for the work. More recently, vernacular architecture has been examined by designers and the building industry in an effort to be more energy conscious with contemporary design and construction—part of a broader interest in sustainable design.</p>
</ScrollAreaContent>
</ScrollAreaViewport>
<ScrollAreaScrollbar className="bg-black/8 dark:bg-white/12">
<ScrollAreaThumb />
</ScrollAreaScrollbar>
</ScrollArea>
);
}
Combining with Tabs#
Put the TabsList in a ScrollAreaViewport when the tab list needs the viewport overflow values for a mask fade.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Tabs;
use function Phem\Ui\{ScrollArea, ScrollAreaViewport, TabsIndicator, TabsList, TabsPanel, TabsTab};
#[Component]
function ScrollAreaTabsDemo(): Node
{
$tabs = [
'overview' => 'Overview',
'projects' => 'Projects',
'account' => 'Account',
'billing' => 'Billing',
'integrations' => 'Integrations',
'security' => 'Security',
'notifications' => 'Notifications',
];
return (
<Tabs id="scroll-tabs" defaultValue="overview" className="w-72">
<ScrollArea className="relative z-1 -mb-px h-auto w-full bg-transparent dark:bg-transparent">
<ScrollAreaViewport className="border-0 mask-linear-[to_right,transparent_0,black_min(40px,var(--scroll-area-overflow-x-start)),black_calc(100%_-_min(40px,var(--scroll-area-overflow-x-end,40px))),transparent_100%] mask-no-repeat">
<TabsList className="mb-0 w-max">
{% for value, name in tabs %}
<TabsTab key={{ value }} value={{ value }}>{{ name }}</TabsTab>
{% endfor %}
<TabsIndicator />
</TabsList>
</ScrollAreaViewport>
</ScrollArea>
<div className="grid min-h-24 w-full grid-cols-1 border border-neutral-950 dark:border-white">
{% for value, name in tabs %}
<TabsPanel key={{ value }} value={{ value }}>
<p>{{ name }}</p>
</TabsPanel>
{% endfor %}
</div>
</Tabs>
);
}
API reference#
Import the components of the family:
use function Phem\Ui\{ScrollArea, ScrollAreaViewport, ScrollAreaContent, ScrollAreaScrollbar, ScrollAreaThumb, ScrollAreaCorner};ScrollArea#
Custom scrollbars over a native scroll container: Base UI ScrollArea.Root, on the phem-scroll-area controller. The scrollbars show while the pointer is over the area, or while the user scrolls.
| Prop | Type | Default |
|---|---|---|
overflowEdgeThreshold | int|array | 0 |
children | ?Html | null |
...rest | mixed | — |
ScrollAreaViewport#
The element that scrolls.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
ScrollAreaContent#
The content of the scroll area, in the ScrollAreaViewport.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
ScrollAreaScrollbar#
A scrollbar on one axis. It gets hidden when its axis has no overflow.
| Prop | Type | Default |
|---|---|---|
orientation | 'horizontal' | 'vertical' | Orientation::Vertical |
children | ?Html | null |
...rest | mixed | — |
ScrollAreaThumb#
The thumb of a ScrollAreaScrollbar: its size and its position show the part of the content in view.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
ScrollAreaCorner#
The square between the two scrollbars.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
The source of the examples: examples/ui/ScrollArea.phem.