@neynar/ui 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,12 +6,23 @@ type ResizablePanelGroupProps = React.ComponentProps<typeof PanelGroup>;
6
6
  * Supports both horizontal and vertical layouts with optional persistence via `id` prop.
7
7
  */
8
8
  declare function ResizablePanelGroup({ className, ...props }: ResizablePanelGroupProps): import("react/jsx-runtime").JSX.Element;
9
- type ResizablePanelProps = React.ComponentProps<typeof Panel>;
9
+ type ResizablePanelProps = Omit<React.ComponentProps<typeof Panel>, "panelRef"> & {
10
+ /** Enable smooth CSS transition for collapse/expand. Drag resizing stays instant. @default false */
11
+ animated?: boolean;
12
+ /** Controlled collapsed state. When provided, the panel syncs to this value. */
13
+ collapsed?: boolean;
14
+ };
10
15
  /**
11
16
  * Individual resizable panel within a ResizablePanelGroup.
12
17
  * Supports size constraints (minSize, maxSize), default sizing, and collapse behavior.
18
+ *
19
+ * Control collapse state declaratively via the `collapsed` prop.
20
+ * When `animated` is true, collapse/expand transitions are smooth.
21
+ *
22
+ * @example
23
+ * <ResizablePanel collapsed={isCollapsed} animated collapsible>
13
24
  */
14
- declare function ResizablePanel({ ...props }: ResizablePanelProps): import("react/jsx-runtime").JSX.Element;
25
+ declare function ResizablePanel({ className, animated, collapsed, ...props }: ResizablePanelProps): import("react/jsx-runtime").JSX.Element;
15
26
  type ResizableHandleProps = React.ComponentProps<typeof PanelResizeHandle> & {
16
27
  /** Display a visible grip indicator on the resize handle. @default false */
17
28
  withHandle?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"resizable.d.ts","sourceRoot":"","sources":["../../../src/components/ui/resizable.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EACL,KAAK,IAAI,UAAU,EACnB,KAAK,EACL,SAAS,IAAI,iBAAiB,EAC/B,MAAM,wBAAwB,CAAC;AAIhC,KAAK,wBAAwB,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,UAAU,CAAC,CAAC;AAExE;;;GAGG;AACH,iBAAS,mBAAmB,CAAC,EAC3B,SAAS,EACT,GAAG,KAAK,EACT,EAAE,wBAAwB,2CAW1B;AAED,KAAK,mBAAmB,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,KAAK,CAAC,CAAC;AAE9D;;;GAGG;AACH,iBAAS,cAAc,CAAC,EAAE,GAAG,KAAK,EAAE,EAAE,mBAAmB,2CAExD;AAED,KAAK,oBAAoB,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,iBAAiB,CAAC,GAAG;IAC3E,4EAA4E;IAC5E,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF;;;GAGG;AACH,iBAAS,eAAe,CAAC,EACvB,UAAU,EACV,SAAS,EACT,GAAG,KAAK,EACT,EAAE,oBAAoB,2CAetB;AAED,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,eAAe,EACf,KAAK,wBAAwB,EAC7B,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,GAC1B,CAAC"}
1
+ {"version":3,"file":"resizable.d.ts","sourceRoot":"","sources":["../../../src/components/ui/resizable.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EACL,KAAK,IAAI,UAAU,EACnB,KAAK,EACL,SAAS,IAAI,iBAAiB,EAE/B,MAAM,wBAAwB,CAAC;AAYhC,KAAK,wBAAwB,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,UAAU,CAAC,CAAC;AAExE;;;GAGG;AACH,iBAAS,mBAAmB,CAAC,EAC3B,SAAS,EACT,GAAG,KAAK,EACT,EAAE,wBAAwB,2CAW1B;AAED,KAAK,mBAAmB,GAAG,IAAI,CAC7B,KAAK,CAAC,cAAc,CAAC,OAAO,KAAK,CAAC,EAClC,UAAU,CACX,GAAG;IACF,oGAAoG;IACpG,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,gFAAgF;IAChF,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF;;;;;;;;;GASG;AACH,iBAAS,cAAc,CAAC,EACtB,SAAS,EACT,QAAQ,EACR,SAAS,EACT,GAAG,KAAK,EACT,EAAE,mBAAmB,2CAmErB;AAED,KAAK,oBAAoB,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,iBAAiB,CAAC,GAAG;IAC3E,4EAA4E;IAC5E,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF;;;GAGG;AACH,iBAAS,eAAe,CAAC,EACvB,UAAU,EACV,SAAS,EACT,GAAG,KAAK,EACT,EAAE,oBAAoB,2CAetB;AAED,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,eAAe,EACf,KAAK,wBAAwB,EAC7B,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,GAC1B,CAAC"}
@@ -1,8 +1,10 @@
1
1
  "use client";
2
2
  import { jsx } from "react/jsx-runtime";
3
- import "react";
3
+ import * as React from "react";
4
4
  import { Group as Tt, Panel as $t, Separator as Ht } from "../../node_modules/react-resizable-panels/dist/react-resizable-panels.js";
5
5
  import { cn } from "../../lib/utils.js";
6
+ const TRANSITION_DURATION = 300;
7
+ const TRANSITION_STYLE = "flex-grow 0.3s cubic-bezier(0.16, 1, 0.3, 1), flex-basis 0.3s cubic-bezier(0.16, 1, 0.3, 1)";
6
8
  function ResizablePanelGroup({
7
9
  className,
8
10
  ...props
@@ -19,8 +21,65 @@ function ResizablePanelGroup({
19
21
  }
20
22
  );
21
23
  }
22
- function ResizablePanel({ ...props }) {
23
- return /* @__PURE__ */ jsx($t, { "data-slot": "resizable-panel", ...props });
24
+ function ResizablePanel({
25
+ className,
26
+ animated,
27
+ collapsed,
28
+ ...props
29
+ }) {
30
+ const panelRef = React.useRef(null);
31
+ const elementRef = React.useRef(null);
32
+ const animatedRef = React.useRef(animated);
33
+ const isFirstRender = React.useRef(true);
34
+ React.useEffect(() => {
35
+ animatedRef.current = animated;
36
+ }, [animated]);
37
+ const withTransition = React.useCallback(
38
+ (action, skipAnimation = false) => {
39
+ const panelEl = elementRef.current?.closest(
40
+ '[data-panel][data-slot="resizable-panel"]'
41
+ );
42
+ if (animatedRef.current && panelEl && !skipAnimation) {
43
+ panelEl.style.transition = TRANSITION_STYLE;
44
+ action();
45
+ setTimeout(() => {
46
+ panelEl.style.transition = "";
47
+ }, TRANSITION_DURATION);
48
+ } else {
49
+ action();
50
+ }
51
+ },
52
+ []
53
+ );
54
+ React.useEffect(() => {
55
+ if (collapsed === void 0) return;
56
+ if (isFirstRender.current) {
57
+ isFirstRender.current = false;
58
+ requestAnimationFrame(() => {
59
+ if (collapsed) {
60
+ panelRef.current?.collapse();
61
+ }
62
+ });
63
+ return;
64
+ }
65
+ const isCurrentlyCollapsed = panelRef.current?.isCollapsed() ?? false;
66
+ if (collapsed === isCurrentlyCollapsed) return;
67
+ if (collapsed) {
68
+ withTransition(() => panelRef.current?.collapse());
69
+ } else {
70
+ withTransition(() => panelRef.current?.expand());
71
+ }
72
+ }, [collapsed, withTransition]);
73
+ return /* @__PURE__ */ jsx(
74
+ $t,
75
+ {
76
+ "data-slot": "resizable-panel",
77
+ panelRef,
78
+ elementRef,
79
+ className: cn(className),
80
+ ...props
81
+ }
82
+ );
24
83
  }
25
84
  function ResizableHandle({
26
85
  withHandle,
@@ -1 +1 @@
1
- {"version":3,"file":"resizable.js","sources":["../../../src/components/ui/resizable.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport {\n Group as PanelGroup,\n Panel,\n Separator as PanelResizeHandle,\n} from \"react-resizable-panels\";\n\nimport { cn } from \"@/lib/utils\";\n\ntype ResizablePanelGroupProps = React.ComponentProps<typeof PanelGroup>;\n\n/**\n * Container for resizable panels. Manages layout orientation and panel resizing behavior.\n * Supports both horizontal and vertical layouts with optional persistence via `id` prop.\n */\nfunction ResizablePanelGroup({\n className,\n ...props\n}: ResizablePanelGroupProps) {\n return (\n <PanelGroup\n data-slot=\"resizable-panel-group\"\n className={cn(\n \"flex h-full w-full aria-[orientation=vertical]:flex-col\",\n className,\n )}\n {...props}\n />\n );\n}\n\ntype ResizablePanelProps = React.ComponentProps<typeof Panel>;\n\n/**\n * Individual resizable panel within a ResizablePanelGroup.\n * Supports size constraints (minSize, maxSize), default sizing, and collapse behavior.\n */\nfunction ResizablePanel({ ...props }: ResizablePanelProps) {\n return <Panel data-slot=\"resizable-panel\" {...props} />;\n}\n\ntype ResizableHandleProps = React.ComponentProps<typeof PanelResizeHandle> & {\n /** Display a visible grip indicator on the resize handle. @default false */\n withHandle?: boolean;\n};\n\n/**\n * Resize handle between panels. Appears as a thin line with optional visible grip indicator.\n * Supports keyboard navigation and focus states.\n */\nfunction ResizableHandle({\n withHandle,\n className,\n ...props\n}: ResizableHandleProps) {\n return (\n <PanelResizeHandle\n data-slot=\"resizable-handle\"\n className={cn(\n \"group bg-border focus-visible:ring-primary/50 focus-visible:bg-primary/50 relative flex w-px items-center justify-center 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-offset-1 focus-visible:ring-offset-background focus-visible:outline-hidden data-[resize-handle-state=hover]:bg-primary/50 data-[resize-handle-state=drag]:bg-primary/50 data-[resize-handle-state=active]:bg-primary/50 aria-[orientation=horizontal]:h-px aria-[orientation=horizontal]:w-full 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\",\n className,\n )}\n {...props}\n >\n {withHandle && (\n <div className=\"bg-border group-focus-visible:bg-primary/50 group-data-[resize-handle-state=hover]:bg-primary/50 group-data-[resize-handle-state=drag]:bg-primary/50 group-data-[resize-handle-state=active]:bg-primary/50 h-6 w-1 rounded-lg z-10 flex shrink-0\" />\n )}\n </PanelResizeHandle>\n );\n}\n\nexport {\n ResizablePanelGroup,\n ResizablePanel,\n ResizableHandle,\n type ResizablePanelGroupProps,\n type ResizablePanelProps,\n type ResizableHandleProps,\n};\n"],"names":["PanelGroup","PanelResizeHandle"],"mappings":";;;;;AAiBA;AAA6B;AAC3B;AAEF;AACE;AACE;AAACA;AAAA;AACW;AACC;AACT;AACA;AAAA;AAEE;AAAA;AAGV;AAQA;AACE;AACF;AAWA;AAAyB;AACvB;AACA;AAEF;AACE;AACE;AAACC;AAAA;AACW;AACC;AACT;AACA;AAAA;AAEE;AAGgQ;AAAA;AAI1Q;;;;;;"}
1
+ {"version":3,"file":"resizable.js","sources":["../../../src/components/ui/resizable.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport {\n Group as PanelGroup,\n Panel,\n Separator as PanelResizeHandle,\n type PanelImperativeHandle,\n} from \"react-resizable-panels\";\n\nimport { cn } from \"@/lib/utils\";\n\n/** Duration of the collapse/expand animation in milliseconds. */\nconst TRANSITION_DURATION = 300;\n\n/** CSS transition value for animated collapse/expand. */\nconst TRANSITION_STYLE =\n \"flex-grow 0.3s cubic-bezier(0.16, 1, 0.3, 1), flex-basis 0.3s cubic-bezier(0.16, 1, 0.3, 1)\";\n\n\ntype ResizablePanelGroupProps = React.ComponentProps<typeof PanelGroup>;\n\n/**\n * Container for resizable panels. Manages layout orientation and panel resizing behavior.\n * Supports both horizontal and vertical layouts with optional persistence via `id` prop.\n */\nfunction ResizablePanelGroup({\n className,\n ...props\n}: ResizablePanelGroupProps) {\n return (\n <PanelGroup\n data-slot=\"resizable-panel-group\"\n className={cn(\n \"flex h-full w-full aria-[orientation=vertical]:flex-col\",\n className,\n )}\n {...props}\n />\n );\n}\n\ntype ResizablePanelProps = Omit<\n React.ComponentProps<typeof Panel>,\n \"panelRef\"\n> & {\n /** Enable smooth CSS transition for collapse/expand. Drag resizing stays instant. @default false */\n animated?: boolean;\n /** Controlled collapsed state. When provided, the panel syncs to this value. */\n collapsed?: boolean;\n};\n\n/**\n * Individual resizable panel within a ResizablePanelGroup.\n * Supports size constraints (minSize, maxSize), default sizing, and collapse behavior.\n *\n * Control collapse state declaratively via the `collapsed` prop.\n * When `animated` is true, collapse/expand transitions are smooth.\n *\n * @example\n * <ResizablePanel collapsed={isCollapsed} animated collapsible>\n */\nfunction ResizablePanel({\n className,\n animated,\n collapsed,\n ...props\n}: ResizablePanelProps) {\n const panelRef = React.useRef<PanelImperativeHandle>(null);\n const elementRef = React.useRef<HTMLDivElement>(null);\n const animatedRef = React.useRef(animated);\n const isFirstRender = React.useRef(true);\n\n // Keep the ref in sync with prop changes\n React.useEffect(() => {\n animatedRef.current = animated;\n }, [animated]);\n\n /** Apply transition, call action, then remove transition after duration */\n const withTransition = React.useCallback(\n (action: () => void, skipAnimation = false) => {\n const panelEl = elementRef.current?.closest(\n '[data-panel][data-slot=\"resizable-panel\"]',\n ) as HTMLElement | null;\n\n if (animatedRef.current && panelEl && !skipAnimation) {\n panelEl.style.transition = TRANSITION_STYLE;\n action();\n setTimeout(() => {\n panelEl.style.transition = \"\";\n }, TRANSITION_DURATION);\n } else {\n action();\n }\n },\n [],\n );\n\n // Sync collapsed prop to panel state\n React.useEffect(() => {\n if (collapsed === undefined) return;\n\n // On first render, defer to allow panel registration\n if (isFirstRender.current) {\n isFirstRender.current = false;\n // Use requestAnimationFrame to ensure panel is registered\n requestAnimationFrame(() => {\n if (collapsed) {\n panelRef.current?.collapse();\n }\n });\n return;\n }\n\n // After first render, check current state before acting\n const isCurrentlyCollapsed = panelRef.current?.isCollapsed() ?? false;\n if (collapsed === isCurrentlyCollapsed) return;\n\n if (collapsed) {\n withTransition(() => panelRef.current?.collapse());\n } else {\n withTransition(() => panelRef.current?.expand());\n }\n }, [collapsed, withTransition]);\n\n return (\n <Panel\n data-slot=\"resizable-panel\"\n panelRef={panelRef}\n elementRef={elementRef}\n className={cn(className)}\n {...props}\n />\n );\n}\n\ntype ResizableHandleProps = React.ComponentProps<typeof PanelResizeHandle> & {\n /** Display a visible grip indicator on the resize handle. @default false */\n withHandle?: boolean;\n};\n\n/**\n * Resize handle between panels. Appears as a thin line with optional visible grip indicator.\n * Supports keyboard navigation and focus states.\n */\nfunction ResizableHandle({\n withHandle,\n className,\n ...props\n}: ResizableHandleProps) {\n return (\n <PanelResizeHandle\n data-slot=\"resizable-handle\"\n className={cn(\n \"group bg-border focus-visible:ring-primary/50 focus-visible:bg-primary/50 relative flex w-px items-center justify-center 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-offset-1 focus-visible:ring-offset-background focus-visible:outline-hidden data-[resize-handle-state=hover]:bg-primary/50 data-[resize-handle-state=drag]:bg-primary/50 data-[resize-handle-state=active]:bg-primary/50 aria-[orientation=horizontal]:h-px aria-[orientation=horizontal]:w-full 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\",\n className,\n )}\n {...props}\n >\n {withHandle && (\n <div className=\"bg-border group-focus-visible:bg-primary/50 group-data-[resize-handle-state=hover]:bg-primary/50 group-data-[resize-handle-state=drag]:bg-primary/50 group-data-[resize-handle-state=active]:bg-primary/50 h-6 w-1 rounded-lg z-10 flex shrink-0\" />\n )}\n </PanelResizeHandle>\n );\n}\n\nexport {\n ResizablePanelGroup,\n ResizablePanel,\n ResizableHandle,\n type ResizablePanelGroupProps,\n type ResizablePanelProps,\n type ResizableHandleProps,\n};\n"],"names":["PanelGroup","Panel","PanelResizeHandle"],"mappings":";;;;;AAaA;AAGA;AAUA;AAA6B;AAC3B;AAEF;AACE;AACE;AAACA;AAAA;AACW;AACC;AACT;AACA;AAAA;AAEE;AAAA;AAGV;AAsBA;AAAwB;AACtB;AACA;AACA;AAEF;AACE;AACA;AACA;AACA;AAGA;AACE;AAAsB;AAIxB;AAA6B;AAEzB;AAAoC;AAClC;AAGF;AACE;AACA;AACA;AACE;AAA2B;AACP;AAEtB;AAAA;AACF;AACF;AACA;AAIF;AACE;AAGA;AACE;AAEA;AACE;AACE;AAAkB;AACpB;AAEF;AAAA;AAIF;AACA;AAEA;AACE;AAAiD;AAEjD;AAA+C;AACjD;AAGF;AACE;AAACC;AAAA;AACW;AACV;AACA;AACuB;AACnB;AAAA;AAGV;AAWA;AAAyB;AACvB;AACA;AAEF;AACE;AACE;AAACC;AAAA;AACW;AACC;AACT;AACA;AAAA;AAEE;AAGgQ;AAAA;AAI1Q;;;;;;"}
@@ -70,7 +70,11 @@ All panels inherit props from react-resizable-panels Panel component.
70
70
  | maxSize | number \| string | 100 | Maximum size (number=pixels, string=percentage or CSS unit) |
71
71
  | collapsible | boolean | false | Allow panel to collapse below minSize |
72
72
  | collapsedSize | number \| string | 0 | Size when collapsed |
73
+ | collapsed | boolean | - | Controlled collapsed state. Panel syncs to this value. |
74
+ | animated | boolean | false | Enable smooth CSS transition for collapse/expand. Drag resizing stays instant. |
73
75
  | onResize | (size: { asPercentage: number; inPixels: number }) => void | - | Called when panel is resized |
76
+ | onCollapse | () => void | - | Called when panel collapses |
77
+ | onExpand | () => void | - | Called when panel expands |
74
78
  | className | string | - | Additional CSS classes |
75
79
 
76
80
  ### ResizableHandle
@@ -222,6 +226,48 @@ function MonitoredPanels() {
222
226
  }
223
227
  ```
224
228
 
229
+ ### Animated Collapse/Expand
230
+
231
+ Use `collapsed` prop for declarative control and `animated` for smooth transitions. Drag resizing remains instant (no animation lag).
232
+
233
+ ```tsx
234
+ import { useState } from "react"
235
+ import {
236
+ ResizablePanelGroup,
237
+ ResizablePanel,
238
+ ResizableHandle,
239
+ } from "@neynar/ui/resizable"
240
+ import { Button } from "@neynar/ui/button"
241
+
242
+ function CollapsibleSidebar() {
243
+ const [isCollapsed, setIsCollapsed] = useState(false)
244
+
245
+ return (
246
+ <div className="h-[400px]">
247
+ <Button onClick={() => setIsCollapsed(!isCollapsed)}>
248
+ {isCollapsed ? "Expand" : "Collapse"} Sidebar
249
+ </Button>
250
+ <ResizablePanelGroup orientation="horizontal">
251
+ <ResizablePanel
252
+ collapsed={isCollapsed}
253
+ animated
254
+ defaultSize="25%"
255
+ minSize="15%"
256
+ collapsible
257
+ collapsedSize="0%"
258
+ >
259
+ <div className="p-4">Sidebar content</div>
260
+ </ResizablePanel>
261
+ <ResizableHandle withHandle />
262
+ <ResizablePanel>
263
+ <div className="p-4">Main content</div>
264
+ </ResizablePanel>
265
+ </ResizablePanelGroup>
266
+ </div>
267
+ )
268
+ }
269
+ ```
270
+
225
271
  ## Keyboard
226
272
 
227
273
  | Key | Action |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neynar/ui",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "license": "MIT",
5
5
  "author": "Neynar Inc.",
6
6
  "description": "AI-first React component library for coding agents. LLM-optimized docs, sensible defaults, zero config. Built on shadcn patterns, Base UI, and Tailwind CSS v4.",
@@ -343,7 +343,9 @@ export const LargeMultiLine: Story = {
343
343
  <div className="w-full max-w-2xl space-y-8">
344
344
  <section className="space-y-4">
345
345
  <div>
346
- <h3 className="text-lg font-semibold">Large Sizes with Multi-line Text</h3>
346
+ <h3 className="text-lg font-semibold">
347
+ Large Sizes with Multi-line Text
348
+ </h3>
347
349
  <p className="text-muted-foreground text-sm">
348
350
  5xl and 6xl sizes with proper line-height to prevent text overlap.
349
351
  </p>
@@ -5,10 +5,19 @@ import {
5
5
  Group as PanelGroup,
6
6
  Panel,
7
7
  Separator as PanelResizeHandle,
8
+ type PanelImperativeHandle,
8
9
  } from "react-resizable-panels";
9
10
 
10
11
  import { cn } from "@/lib/utils";
11
12
 
13
+ /** Duration of the collapse/expand animation in milliseconds. */
14
+ const TRANSITION_DURATION = 300;
15
+
16
+ /** CSS transition value for animated collapse/expand. */
17
+ const TRANSITION_STYLE =
18
+ "flex-grow 0.3s cubic-bezier(0.16, 1, 0.3, 1), flex-basis 0.3s cubic-bezier(0.16, 1, 0.3, 1)";
19
+
20
+
12
21
  type ResizablePanelGroupProps = React.ComponentProps<typeof PanelGroup>;
13
22
 
14
23
  /**
@@ -31,14 +40,98 @@ function ResizablePanelGroup({
31
40
  );
32
41
  }
33
42
 
34
- type ResizablePanelProps = React.ComponentProps<typeof Panel>;
43
+ type ResizablePanelProps = Omit<
44
+ React.ComponentProps<typeof Panel>,
45
+ "panelRef"
46
+ > & {
47
+ /** Enable smooth CSS transition for collapse/expand. Drag resizing stays instant. @default false */
48
+ animated?: boolean;
49
+ /** Controlled collapsed state. When provided, the panel syncs to this value. */
50
+ collapsed?: boolean;
51
+ };
35
52
 
36
53
  /**
37
54
  * Individual resizable panel within a ResizablePanelGroup.
38
55
  * Supports size constraints (minSize, maxSize), default sizing, and collapse behavior.
56
+ *
57
+ * Control collapse state declaratively via the `collapsed` prop.
58
+ * When `animated` is true, collapse/expand transitions are smooth.
59
+ *
60
+ * @example
61
+ * <ResizablePanel collapsed={isCollapsed} animated collapsible>
39
62
  */
40
- function ResizablePanel({ ...props }: ResizablePanelProps) {
41
- return <Panel data-slot="resizable-panel" {...props} />;
63
+ function ResizablePanel({
64
+ className,
65
+ animated,
66
+ collapsed,
67
+ ...props
68
+ }: ResizablePanelProps) {
69
+ const panelRef = React.useRef<PanelImperativeHandle>(null);
70
+ const elementRef = React.useRef<HTMLDivElement>(null);
71
+ const animatedRef = React.useRef(animated);
72
+ const isFirstRender = React.useRef(true);
73
+
74
+ // Keep the ref in sync with prop changes
75
+ React.useEffect(() => {
76
+ animatedRef.current = animated;
77
+ }, [animated]);
78
+
79
+ /** Apply transition, call action, then remove transition after duration */
80
+ const withTransition = React.useCallback(
81
+ (action: () => void, skipAnimation = false) => {
82
+ const panelEl = elementRef.current?.closest(
83
+ '[data-panel][data-slot="resizable-panel"]',
84
+ ) as HTMLElement | null;
85
+
86
+ if (animatedRef.current && panelEl && !skipAnimation) {
87
+ panelEl.style.transition = TRANSITION_STYLE;
88
+ action();
89
+ setTimeout(() => {
90
+ panelEl.style.transition = "";
91
+ }, TRANSITION_DURATION);
92
+ } else {
93
+ action();
94
+ }
95
+ },
96
+ [],
97
+ );
98
+
99
+ // Sync collapsed prop to panel state
100
+ React.useEffect(() => {
101
+ if (collapsed === undefined) return;
102
+
103
+ // On first render, defer to allow panel registration
104
+ if (isFirstRender.current) {
105
+ isFirstRender.current = false;
106
+ // Use requestAnimationFrame to ensure panel is registered
107
+ requestAnimationFrame(() => {
108
+ if (collapsed) {
109
+ panelRef.current?.collapse();
110
+ }
111
+ });
112
+ return;
113
+ }
114
+
115
+ // After first render, check current state before acting
116
+ const isCurrentlyCollapsed = panelRef.current?.isCollapsed() ?? false;
117
+ if (collapsed === isCurrentlyCollapsed) return;
118
+
119
+ if (collapsed) {
120
+ withTransition(() => panelRef.current?.collapse());
121
+ } else {
122
+ withTransition(() => panelRef.current?.expand());
123
+ }
124
+ }, [collapsed, withTransition]);
125
+
126
+ return (
127
+ <Panel
128
+ data-slot="resizable-panel"
129
+ panelRef={panelRef}
130
+ elementRef={elementRef}
131
+ className={cn(className)}
132
+ {...props}
133
+ />
134
+ );
42
135
  }
43
136
 
44
137
  type ResizableHandleProps = React.ComponentProps<typeof PanelResizeHandle> & {
@@ -3,6 +3,8 @@ import {
3
3
  CodeIcon,
4
4
  FileIcon,
5
5
  FolderIcon,
6
+ PanelLeftCloseIcon,
7
+ PanelLeftOpenIcon,
6
8
  PlayIcon,
7
9
  TerminalIcon,
8
10
  } from "lucide-react";
@@ -167,6 +169,103 @@ export function Counter() {
167
169
  },
168
170
  };
169
171
 
172
+ /**
173
+ * Animated collapse/expand using the `collapsed` and `animated` props.
174
+ * Control collapse state declaratively - no ref needed.
175
+ * Toggle the animated prop to compare instant vs smooth transitions.
176
+ */
177
+ export const AnimatedCollapse: Story = {
178
+ parameters: {
179
+ layout: "padded",
180
+ },
181
+ render: () => {
182
+ function AnimatedCollapseDemo() {
183
+ const [isCollapsed, setIsCollapsed] = useState(false);
184
+ const [animated, setAnimated] = useState(true);
185
+
186
+ return (
187
+ <div className="space-y-4">
188
+ <div className="flex gap-2">
189
+ <Button
190
+ onClick={() => setIsCollapsed(!isCollapsed)}
191
+ variant="default"
192
+ >
193
+ {isCollapsed ? (
194
+ <PanelLeftOpenIcon data-icon="inline-start" />
195
+ ) : (
196
+ <PanelLeftCloseIcon data-icon="inline-start" />
197
+ )}
198
+ {isCollapsed ? "Expand" : "Collapse"}
199
+ </Button>
200
+ <Button
201
+ onClick={() => setAnimated(!animated)}
202
+ variant={animated ? "secondary" : "outline"}
203
+ >
204
+ Animated: {animated ? "On" : "Off"}
205
+ </Button>
206
+ </div>
207
+ <div className="border-border h-[400px] w-full rounded-lg border">
208
+ <ResizablePanelGroup orientation="horizontal">
209
+ <ResizablePanel
210
+ collapsed={isCollapsed}
211
+ animated={animated}
212
+ defaultSize="25%"
213
+ minSize="15%"
214
+ maxSize="40%"
215
+ collapsible
216
+ collapsedSize="0%"
217
+ >
218
+ <div className="flex h-full flex-col">
219
+ <div className="border-b p-3">
220
+ <h3 className="flex items-center gap-2 text-sm font-semibold">
221
+ <FolderIcon className="size-4" />
222
+ Sidebar
223
+ </h3>
224
+ </div>
225
+ <div className="flex-1 overflow-auto p-4">
226
+ <div className="space-y-2">
227
+ {["Dashboard", "Analytics", "Reports", "Settings"].map(
228
+ (item) => (
229
+ <div
230
+ key={item}
231
+ className="hover:bg-accent rounded px-2 py-1.5 text-sm transition-colors"
232
+ >
233
+ {item}
234
+ </div>
235
+ ),
236
+ )}
237
+ </div>
238
+ </div>
239
+ </div>
240
+ </ResizablePanel>
241
+ <ResizableHandle withHandle />
242
+ <ResizablePanel defaultSize="75%">
243
+ <div className="flex h-full flex-col">
244
+ <div className="border-b p-3">
245
+ <h3 className="text-sm font-semibold">Main Content</h3>
246
+ </div>
247
+ <div className="flex-1 p-4">
248
+ <p className="text-muted-foreground text-sm">
249
+ The sidebar uses{" "}
250
+ <code className="bg-muted rounded px-1 text-xs">
251
+ collapsed={"{isCollapsed}"}
252
+ </code>{" "}
253
+ for declarative control. Toggle the button above to
254
+ compare instant vs animated transitions.
255
+ </p>
256
+ </div>
257
+ </div>
258
+ </ResizablePanel>
259
+ </ResizablePanelGroup>
260
+ </div>
261
+ </div>
262
+ );
263
+ }
264
+
265
+ return <AnimatedCollapseDemo />;
266
+ },
267
+ };
268
+
170
269
  /**
171
270
  * Complete design system reference showing all resizable panel configurations,
172
271
  * directions, handle styles, and complex layouts.