Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(www): add resizable control example #5911

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions apps/www/content/docs/components/resizable.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,55 @@ export default function Example() {
)
}
```

### Control

You can control the panel status by using refs.

<ComponentPreview
name="resizable-control"
description="A group of resizable panels with a button to control the panel."
/>

```tsx showLineNumbers {11,34,13-21}
import { useRef } from "react"
import { Button } from "@/components/ui/button"
import { ImperativePanelHandle } from "react-resizable-panels"
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable"

export default function Example() {
const leftPanelRef = useRef<ImperativePanelHandle>(null)

const toggleLeftPanel = () => {
if (leftPanelRef.current) {
if (leftPanelRef.current.isCollapsed()) {
leftPanelRef.current.expand()
} else {
leftPanelRef.current.collapse()
}
}
}

return (
<ResizablePanelGroup direction="horizontal">
<ResizablePanel>
<Button onClick={toggleLeftPanel}>
Toggle Left Panel
</Button>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel
defaultSize={50}
collapsible={true}
ref={leftPanelRef}
>
Left Panel
</ResizablePanel>
</ResizablePanelGroup>
)
}
```
47 changes: 47 additions & 0 deletions apps/www/registry/default/example/resizable-control.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useRef } from "react"
import { Button } from "@/registry/default/ui/button"
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/registry/default/ui/resizable"
import { ImperativePanelHandle } from "react-resizable-panels"

export default function ResizableDemo() {
const leftPanelRef = useRef<ImperativePanelHandle>(null)

const toggleLeftPanel = () => {
if (leftPanelRef.current) {
if (leftPanelRef.current.isCollapsed()) {
leftPanelRef.current.expand()
} else {
leftPanelRef.current.collapse()
}
}
}

return (
<ResizablePanelGroup
direction="horizontal"
className="max-w-md rounded-lg border md:min-w-[450px]"
>
<ResizablePanel>
<div className="flex h-[200px] items-center justify-center p-6">
<Button onClick={toggleLeftPanel}>
Toggle Left Panel
</Button>
</div>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel
defaultSize={50}
collapsible={true}
ref={leftPanelRef}
>
<div className="flex h-[200px] items-center justify-center p-6">
Left Resizable Panel
</div>
</ResizablePanel>
</ResizablePanelGroup>
)
}
47 changes: 47 additions & 0 deletions apps/www/registry/new-york/example/resizable-control.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/registry/new-york/ui/resizable"
import { useRef } from "react"
import { Button } from "@/registry/new-york/ui/button"
import { ImperativePanelHandle } from "react-resizable-panels"

export default function ResizableDemo() {
const leftPanelRef = useRef<ImperativePanelHandle>(null)

const toggleLeftPanel = () => {
if (leftPanelRef.current) {
if (leftPanelRef.current.isCollapsed()) {
leftPanelRef.current.expand()
} else {
leftPanelRef.current.collapse()
}
}
}

return (
<ResizablePanelGroup
direction="horizontal"
className="max-w-md rounded-lg border md:min-w-[450px]"
>
<ResizablePanel>
<div className="flex h-[200px] items-center justify-center p-6">
<Button onClick={toggleLeftPanel}>
Toggle Left Panel
</Button>
</div>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel
defaultSize={50}
collapsible={true}
ref={leftPanelRef}
>
<div className="flex h-[200px] items-center justify-center p-6">
Left Resizable Panel
</div>
</ResizablePanel>
</ResizablePanelGroup>
)
}
11 changes: 11 additions & 0 deletions apps/www/registry/registry-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,17 @@ export const examples: Registry = [
},
],
},
{
name: "resizable-control",
type: "registry:example",
registryDependencies: ["resizable"],
files: [
{
path: "example/resizable-control.tsx",
type: "registry:example",
},
],
},
{
name: "scroll-area-demo",
type: "registry:example",
Expand Down