Skip to content

Commit

Permalink
docs(scroll-area): add example for horizontal scroll area (shadcn-ui#…
Browse files Browse the repository at this point in the history
…1515)

Adds documentation to the `<ScrollArea>` component for creating a scroll area with a horizontal scrollbar. There is also a commit to fix the improper sizing of a horizontal scroll bars.

Before:
https://github.com/shadcn-ui/ui/assets/9381099/a8b512f1-37f7-4107-a9fa-42a26e124696

After:
https://github.com/shadcn-ui/ui/assets/9381099/480f881c-b0fe-4b1b-9472-c533135e6769
  • Loading branch information
gregermendle authored Oct 21, 2023
1 parent 701a0f9 commit 89a3834
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 2 deletions.
14 changes: 14 additions & 0 deletions __registry__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,13 @@ export const Index: Record<string, any> = {
component: React.lazy(() => import("@/registry/default/example/scroll-area-demo")),
files: ["registry/default/example/scroll-area-demo.tsx"],
},
"scroll-area-horizontal-demo": {
name: "scroll-area-horizontal-demo",
type: "components:example",
registryDependencies: ["scroll-area"],
component: React.lazy(() => import("@/registry/default/example/scroll-area-horizontal-demo")),
files: ["registry/default/example/scroll-area-horizontal-demo.tsx"],
},
"select-demo": {
name: "select-demo",
type: "components:example",
Expand Down Expand Up @@ -1721,6 +1728,13 @@ export const Index: Record<string, any> = {
component: React.lazy(() => import("@/registry/new-york/example/scroll-area-demo")),
files: ["registry/new-york/example/scroll-area-demo.tsx"],
},
"scroll-area-horizontal-demo": {
name: "scroll-area-horizontal-demo",
type: "components:example",
registryDependencies: ["scroll-area"],
component: React.lazy(() => import("@/registry/new-york/example/scroll-area-horizontal-demo")),
files: ["registry/new-york/example/scroll-area-horizontal-demo.tsx"],
},
"select-demo": {
name: "select-demo",
type: "components:example",
Expand Down
6 changes: 6 additions & 0 deletions content/docs/components/scroll-area.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ import { ScrollArea } from "@/components/ui/scroll-area"
started laughing, they couldn't stop.
</ScrollArea>
```

## Examples

### Horizontal Scrolling

<ComponentPreview name="scroll-area-horizontal-demo" />
53 changes: 53 additions & 0 deletions registry/default/example/scroll-area-horizontal-demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as React from "react"
import Image from "next/image"

import { ScrollArea, ScrollBar } from "@/registry/default/ui/scroll-area"

export interface Artwork {
artist: string
art: string
}

export const works: Artwork[] = [
{
artist: "Ornella Binni",
art: "https://images.unsplash.com/photo-1465869185982-5a1a7522cbcb?auto=format&fit=crop&w=300&q=80",
},
{
artist: "Tom Byrom",
art: "https://images.unsplash.com/photo-1548516173-3cabfa4607e9?auto=format&fit=crop&w=300&q=80",
},
{
artist: "Vladimir Malyavko",
art: "https://images.unsplash.com/photo-1494337480532-3725c85fd2ab?auto=format&fit=crop&w=300&q=80",
},
]

export default function ScrollAreaHorizontalDemo() {
return (
<ScrollArea className="w-96 whitespace-nowrap rounded-md border">
<div className="flex w-max space-x-4 p-4">
{works.map((artwork) => (
<figure key={artwork.artist} className="shrink-0">
<div className="overflow-hidden rounded-md">
<Image
src={artwork.art}
alt={`Photo by ${artwork.artist}`}
className="aspect-[3/4] h-fit w-fit object-cover"
width={300}
height={400}
/>
</div>
<figcaption className="pt-2 text-xs text-muted-foreground">
Photo by{" "}
<span className="font-semibold text-foreground">
{artwork.artist}
</span>
</figcaption>
</figure>
))}
</div>
<ScrollBar orientation="horizontal" />
</ScrollArea>
)
}
2 changes: 1 addition & 1 deletion registry/default/ui/scroll-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const ScrollBar = React.forwardRef<
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]",
"h-2.5 flex-col border-t border-t-transparent p-[1px]",
className
)}
{...props}
Expand Down
53 changes: 53 additions & 0 deletions registry/new-york/example/scroll-area-horizontal-demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as React from "react"
import Image from "next/image"

import { ScrollArea, ScrollBar } from "@/registry/new-york/ui/scroll-area"

export interface Artwork {
artist: string
art: string
}

export const works: Artwork[] = [
{
artist: "Ornella Binni",
art: "https://images.unsplash.com/photo-1465869185982-5a1a7522cbcb?auto=format&fit=crop&w=300&q=80",
},
{
artist: "Tom Byrom",
art: "https://images.unsplash.com/photo-1548516173-3cabfa4607e9?auto=format&fit=crop&w=300&q=80",
},
{
artist: "Vladimir Malyavko",
art: "https://images.unsplash.com/photo-1494337480532-3725c85fd2ab?auto=format&fit=crop&w=300&q=80",
},
]

export default function ScrollAreaHorizontalDemo() {
return (
<ScrollArea className="w-96 whitespace-nowrap rounded-md border">
<div className="flex w-max space-x-4 p-4">
{works.map((artwork) => (
<figure key={artwork.artist} className="shrink-0">
<div className="overflow-hidden rounded-md">
<Image
src={artwork.art}
alt={`Photo by ${artwork.artist}`}
className="aspect-[3/4] h-fit w-fit object-cover"
width={300}
height={400}
/>
</div>
<figcaption className="pt-2 text-xs text-muted-foreground">
Photo by{" "}
<span className="font-semibold text-foreground">
{artwork.artist}
</span>
</figcaption>
</figure>
))}
</div>
<ScrollBar orientation="horizontal" />
</ScrollArea>
)
}
2 changes: 1 addition & 1 deletion registry/new-york/ui/scroll-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const ScrollBar = React.forwardRef<
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]",
"h-2.5 flex-col border-t border-t-transparent p-[1px]",
className
)}
{...props}
Expand Down
6 changes: 6 additions & 0 deletions registry/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,12 @@ const example: Registry = [
registryDependencies: ["scroll-area"],
files: ["example/scroll-area-demo.tsx"],
},
{
name: "scroll-area-horizontal-demo",
type: "components:example",
registryDependencies: ["scroll-area"],
files: ["example/scroll-area-horizontal-demo.tsx"],
},
{
name: "select-demo",
type: "components:example",
Expand Down

0 comments on commit 89a3834

Please sign in to comment.