Resizable for Astro
Astro panels with pointer and keyboard size controls.
Installation
bunx --bun shadcn@latest add @gnostic/resizablenpx shadcn@latest add @gnostic/resizablepnpm dlx shadcn@latest add @gnostic/resizableComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
Install component packages
bun add @data-slot/resizableCopy the files
src/components/ui/resizable/index.ts
export { default as ResizableHandle } from './resizable-handle.astro'
export { default as ResizablePanel } from './resizable-panel.astro'
export { default as ResizablePanelGroup } from './resizable-panel-group.astro'
export type { ResizableProps } from './types'
src/components/ui/resizable/resizable-handle.astro
---
import type { HTMLAttributes } from "astro/types"
import type { ResizableHandleProps } from "./types.ts"
import { cn } from "cn"
/**
* Separator between two resizable panels.
*
* `@data-slot/resizable` sets `aria-orientation` on the handle after startup.
* The parent `data-direction` classes paint the correct axis before that runs.
* A horizontal group uses a vertical separator.
* A vertical group uses a horizontal separator.
*/
type Props = HTMLAttributes<"div"> & ResizableHandleProps
const {
class: className,
withHandle = false,
"aria-label": ariaLabel = "Resize panels",
...props
} = Astro.props
---
{/* The handle is really a panel separator */}
<div
data-slot="resizable-handle"
role="separator"
tabindex="0"
aria-label={ariaLabel}
class={cn(
"bg-border ring-offset-background relative flex w-px shrink-0 cursor-col-resize touch-none items-center justify-center select-none after:absolute after:inset-y-0 after:left-1/2 after:w-1 after:-translate-x-1/2 focus-visible:ring-1 focus-visible:ring-ring focus-visible:outline-hidden",
"aria-[orientation=horizontal]:h-px aria-[orientation=horizontal]:w-full aria-[orientation=horizontal]:cursor-row-resize aria-[orientation=horizontal]:after:left-0 aria-[orientation=horizontal]:after:h-1 aria-[orientation=horizontal]:after:w-full aria-[orientation=horizontal]:after:translate-x-0 aria-[orientation=horizontal]:after:-translate-y-1/2 [&[aria-orientation=horizontal]>div]:rotate-90",
"[[data-direction=vertical]>&]:h-px [[data-direction=vertical]>&]:w-full [[data-direction=vertical]>&]:cursor-row-resize [[data-direction=vertical]>&]:after:left-0 [[data-direction=vertical]>&]:after:h-1 [[data-direction=vertical]>&]:after:w-full [[data-direction=vertical]>&]:after:translate-x-0 [[data-direction=vertical]>&]:after:-translate-y-1/2 [[data-direction=vertical]>&]:[&>div]:rotate-90",
className,
)}
{...props}
>
{/* Small handle in the center of the separator */}
{withHandle && (
<div class="bg-border z-10 flex h-6 w-1 shrink-0 rounded-lg" data-slot="resizable-handle-handle" />
)}
<slot />
</div>
src/components/ui/resizable/resizable-panel-group.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import type { ResizableProps } from "./types.ts"
/**
* Group of panels that share space.
*
* `@data-slot/resizable` binds `data-slot="resizable"`.
* The public component name stays `ResizablePanelGroup`.
*/
type Props = HTMLAttributes<"div"> & ResizableProps
const {
class: className,
direction = "horizontal",
keyboardResizeBy,
...props
} = Astro.props
/**
* Keep a numeric 0 in the attribute.
* Astro can omit a bare 0.
*/
const dataNumber = (value: number | undefined) =>
value === undefined ? undefined : String(value)
---
<div
data-slot="resizable"
data-direction={direction}
data-keyboard-resize-by={dataNumber(keyboardResizeBy)}
class={cn(
"flex h-full w-full min-h-0 min-w-0 overflow-hidden data-[direction=vertical]:flex-col",
className,
)}
{...props}
>
<slot />
</div>
<script>
import { create } from "@data-slot/resizable"
import type { ResizableController } from "@data-slot/resizable"
// Destroy bindings before a view transition so the next page-load binds once.
const controllers: ResizableController[] = []
const initialize = () => controllers.push(...create())
initialize()
document.addEventListener("astro:page-load", initialize)
document.addEventListener("astro:before-swap", () => {
for (const controller of controllers) controller.destroy()
controllers.length = 0
})
</script>
src/components/ui/resizable/resizable-panel.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import type { ResizablePanelProps } from "./types.ts"
type Props = HTMLAttributes<"div"> & ResizablePanelProps
const {
class: className,
defaultSize,
minSize,
maxSize,
collapsible,
collapsedSize,
style,
...props
} = Astro.props
/**
* Keep a numeric 0 in the attribute.
* Astro can omit a bare 0.
*/
const dataNumber = (value: number | undefined) =>
value === undefined ? undefined : String(value)
const layoutStyle = defaultSize === undefined ? "" : `flex-grow:${defaultSize}`
const userStyle = typeof style === "string" ? style : ""
const mergedStyle =
[layoutStyle, userStyle].filter(Boolean).join(";") || undefined
---
<div
data-slot="resizable-panel"
data-default-size={dataNumber(defaultSize)}
data-min-size={dataNumber(minSize)}
data-max-size={dataNumber(maxSize)}
data-collapsible={collapsible ? "" : undefined}
data-collapsed-size={dataNumber(collapsedSize)}
class={cn("min-h-0 min-w-0 flex-1 overflow-hidden", className)}
style={mergedStyle}
{...props}
>
<slot />
</div>
src/components/ui/resizable/types.ts
import type { ResizableDirection } from '@data-slot/resizable'
/** Configuration for a group of resizable panels. */
export type ResizableProps = {
/**
* Layout axis for the panels.
* `horizontal` places panels side by side.
* `vertical` stacks the panels.
* @default "horizontal"
*/
direction?: ResizableDirection
/**
* Percent added or removed for each arrow key press.
* Shift with an arrow key moves to the limit.
* @default 10
*/
keyboardResizeBy?: number
}
export type ResizablePanelProps = {
/**
* Initial size as a percentage of the group.
* Panels without a size share the remaining space.
*/
defaultSize?: number
/**
* Minimum size as a percentage.
* @default 0
*/
minSize?: number
/**
* Maximum size as a percentage.
* @default 100
*/
maxSize?: number
/**
* Allow this panel to collapse past `minSize`.
* Press Enter on the preceding handle to collapse and expand it.
* @default false
*/
collapsible?: boolean
/**
* Size as a percentage when the panel is collapsed.
* This value must not exceed `minSize`.
* @default 0
*/
collapsedSize?: number
}
export type ResizableHandleProps = {
/**
* Show a grip in the center of the handle.
* @default false
*/
withHandle?: boolean
}
Usage
---
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable"
---
<ResizablePanelGroup class="h-48 w-full rounded-lg border">
<ResizablePanel defaultSize={50} minSize={20}>
<div class="flex h-full items-center justify-center p-6">One</div>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel defaultSize={50} minSize={20}>
<div class="flex h-full items-center justify-center p-6">Two</div>
</ResizablePanel>
</ResizablePanelGroup>
Composition
Place one ResizableHandle between each pair of panels. Sizes are percentages of the group. Give the group a height.
ResizablePanelGroup
├── ResizablePanel
├── ResizableHandle
└── ResizablePanel
Examples
Vertical
Set direction="vertical" to stack the panels. The handle moves up and down.
Custom handle
Add a custom handle by passing a direct child to ResizableHandle.
Nested groups
A panel can contain another group. Use withHandle to show a grip on the separator.
Collapsible panel
Set collapsible on a panel to let it collapse past minSize. Focus the handle and press Enter to collapse and expand that panel. Arrow keys resize by 10 percent. Home and End move to the limits.
API Reference
ResizablePanelGroup, ResizablePanel, and ResizableHandle accept native div attributes.
ResizablePanelProps
defaultSize?number
Initial size as a percentage of the group. Panels without a size share the remaining space.
numberminSize?number
Minimum size as a percentage.
number0maxSize?number
Maximum size as a percentage.
number100collapsible?boolean
Allow this panel to collapse past `minSize`. Press Enter on the preceding handle to collapse and expand it.
booleanfalsecollapsedSize?number
Size as a percentage when the panel is collapsed. This value must not exceed `minSize`.
number0ResizableProps
direction?ResizableDirection
Layout axis for the panels. `horizontal` places panels side by side. `vertical` stacks the panels.
ResizableDirection"horizontal"keyboardResizeBy?number
Percent added or removed for each arrow key press. Shift with an arrow key moves to the limit.
number10ResizableHandleProps
withHandle?boolean
Show a grip in the center of the handle.
booleanfalseEvents
Dispatch these events on the resizable group root. The root emits the outbound events. The component does not store the layout.
Outbound events
| Event | Detail |
|---|---|
resizable:change |
{ layout: number[] } |
resizable:dragging |
{ dragging: boolean } |
Inbound events
| Event | Detail |
|---|---|
resizable:set |
{ layout: number[] } |
See the data-slot docs for keyboard behavior and the controller API.
Read the installed source for the component source.
When to use Resizable
Compose panels and resize handles inside a ResizablePanelGroup. Choose an orientation and useful size bounds so neither panel becomes unusable. Keep the panel content independent of its starting size.
Keyboard and behavior
The existing data-slot controller handles pointer and keyboard resizing. Give each handle a useful label and test its arrow-key behavior. A panel that contains a form must still expose all controls at its minimum size.
Theme and layout
The handle uses shared border and focus colors. Check content overflow after a resize and at narrow viewport widths. Some layouts are clearer when panels stack instead of remaining resizable on a phone.