跳至正文
获取 Pro
基础原语U.23

滚动容器

FreeRegistry
components/linyi/scroll-area.tsx
@base-ui/reactclass-variance-authority
"use client";
 
import * as React from "react";
import { ScrollArea as BaseScrollArea } from "@base-ui/react/scroll-area";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/cn";
 
export const scrollAreaVariants = cva("relative overflow-hidden", {
variants: {
variant: {
plain: "",
surface: "rounded-well border border-line bg-surface",
well: "rounded-well border border-line-well bg-well",
},
},
defaultVariants: {
variant: "plain",
},
});
 
export const scrollBarVariants = cva(
[
"flex touch-none select-none p-0.5 transition-opacity duration-200 motion-reduce:transition-none",
"opacity-0 pointer-events-none data-[hovering]:opacity-100 data-[hovering]:pointer-events-auto data-[scrolling]:opacity-100 data-[scrolling]:pointer-events-auto",
].join(" "),
{
variants: {
orientation: {
vertical: "w-2 m-0.5",
horizontal: "h-2 m-0.5 flex-col",
},
},
defaultVariants: {
orientation: "vertical",
},
}
);
 
export interface ScrollBarProps
extends Omit<
React.ComponentPropsWithRef<typeof BaseScrollArea.Scrollbar>,
"className" | "orientation"
>,
VariantProps<typeof scrollBarVariants> {
className?: string;
}
 
export function ScrollBar({
className,
orientation = "vertical",
...props
}: ScrollBarProps) {
const resolvedOrientation = orientation ?? "vertical";
return (
<BaseScrollArea.Scrollbar
orientation={resolvedOrientation}
className={cn(
scrollBarVariants({ orientation: resolvedOrientation }),
className
)}
{...props}
>
<BaseScrollArea.Thumb className="relative flex-1 rounded-pill bg-fg-muted/40 hover:bg-fg-muted/65 transition-colors" />
</BaseScrollArea.Scrollbar>
);
}
 
export interface ScrollAreaProps
extends Omit<
React.ComponentPropsWithRef<typeof BaseScrollArea.Root>,
"className"
>,
VariantProps<typeof scrollAreaVariants> {
className?: string;
viewportClassName?: string;
contentClassName?: string;
orientation?: "vertical" | "horizontal" | "both";
}
 
export function ScrollArea({
className,
viewportClassName,
contentClassName,
variant = "plain",
orientation = "vertical",
children,
...props
}: ScrollAreaProps) {
const showVertical = orientation === "vertical" || orientation === "both";
const showHorizontal = orientation === "horizontal" || orientation === "both";
 
return (
<BaseScrollArea.Root
className={cn(scrollAreaVariants({ variant }), className)}
{...props}
>
<BaseScrollArea.Viewport
className={cn(
"h-full w-full overscroll-contain outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-accent/35",
viewportClassName
)}
>
<BaseScrollArea.Content className={cn("min-w-full", contentClassName)}>
{children}
</BaseScrollArea.Content>
</BaseScrollArea.Viewport>
{showVertical && <ScrollBar orientation="vertical" />}
{showHorizontal && <ScrollBar orientation="horizontal" />}
{showVertical && showHorizontal && <BaseScrollArea.Corner />}
</BaseScrollArea.Root>
);
}