Open1
shadcn/uiで横スクロールエリアを実装する
$ npx create-next-app@latest --experimental-app
Need to install the following packages:
create-next-app@13.3.2
Ok to proceed? (y) y
✔ What is your project named? … my-app
✔ Would you like to use TypeScript with this project? … No / Yes
✔ Would you like to use ESLint with this project? … No / Yes
✔ Would you like to use Tailwind CSS with this project? … No / Yes
✔ Would you like to use `src/` directory with this project? … No / Yes
✔ What import alias would you like configured? … @/*
Creating a new Next.js app in /workspace/my-app.
すべてYesを選択する。
npx shadcn-ui init
npx shadcn-ui add scroll-area
Scroll-areaをインストールする。
const ScrollBar = React.forwardRef<
React.ElementRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>,
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>
>(({ className, orientation = "horizontal", ...props }, ref) => (
<ScrollAreaPrimitive.ScrollAreaScrollbar
ref={ref}
orientation={orientation}
className={cn(
"flex touch-none select-none transition-colors",
orientation === "vertical" &&
"h-full w-2.5 border-l border-l-transparent p-[1px]",
orientation === "horizontal" &&
"h-2.5 border-t border-t-transparent p-[1px]",
className
)}
{...props}
>
<ScrollAreaPrimitive.ScrollAreaThumb className="relative flex-1 rounded-full bg-border" />
</ScrollAreaPrimitive.ScrollAreaScrollbar>
));
ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName;
orientation = "horizontal"
に書き換える。
export default function Home() {
return (
<main>
<ScrollArea>
<div className="flex gap-3">
{[...Array(10)].map((_, index) => {
return <div className="bg-blue-500 w-[200px] h-[200px]" />;
})}
</div>
</ScrollArea>
</main>
);
}
子要素が縮小される場合は子要素にshrink-0
をつける。
もっといいやり方があれば更新する。