@vvedantb/ui 0.81.0 → 0.82.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.
@@ -8,11 +8,12 @@ import {
8
8
  type ReactNode,
9
9
  } from "react";
10
10
  import { AnimatePresence, domMax, LazyMotion, m, type HTMLMotionProps, type PanInfo } from "motion/react";
11
- import { IconMenu2 } from "@tabler/icons-react";
11
+ import { IconMenu2, IconPin, IconPinFilled } from "@tabler/icons-react";
12
12
  import { Button, type ButtonProps } from "../../src/ui/button";
13
13
  import { motionEase, motionTiming } from "../../src/motion/presets";
14
14
  import { cn } from "../../src/utils/cn";
15
15
  import { mobileMenuButtonClass } from "./app-shell-classes";
16
+ import { useSidebarPeek, type SidebarPeek } from "./SidebarPeek";
16
17
  import { SidebarResizeHandle, useSidebarResizeContext, type SidebarResizeHandleProps } from "./SidebarResize";
17
18
 
18
19
  type SidebarContextValue = {
@@ -20,6 +21,9 @@ type SidebarContextValue = {
20
21
  mobileOpen: boolean;
21
22
  onMobileOpenChange: (open: boolean) => void;
22
23
  collapsed: boolean;
24
+ unpinned: boolean;
25
+ peek: SidebarPeek | null;
26
+ onCollapsedChange: ((collapsed: boolean) => void) | null;
23
27
  panelHidden: boolean;
24
28
  drawerId: string;
25
29
  };
@@ -41,7 +45,12 @@ export type SidebarRootProps = {
41
45
  isDesktop: boolean;
42
46
  mobileOpen: boolean;
43
47
  onMobileOpenChange: (open: boolean) => void;
48
+ /** The panel is unpinned: the rail shows alone and the panel opens on hover. */
44
49
  collapsed?: boolean;
50
+ /** Pins or unpins the panel. Adds `Sidebar.PinButton` to `SidebarPanel`. */
51
+ onCollapsedChange?: (collapsed: boolean) => void;
52
+ /** Hover state from `useSidebarPeek`, to read the hovered item. `false` turns hover-open off. */
53
+ peek?: SidebarPeek | false;
45
54
  panelHidden?: boolean;
46
55
  id?: string;
47
56
  children?: ReactNode;
@@ -52,11 +61,16 @@ function Root({
52
61
  mobileOpen,
53
62
  onMobileOpenChange,
54
63
  collapsed = false,
64
+ onCollapsedChange,
65
+ peek: peekProp,
55
66
  panelHidden = false,
56
67
  id,
57
68
  children,
58
69
  }: SidebarRootProps) {
59
70
  const autoId = useId();
71
+ const ownPeek = useSidebarPeek();
72
+ const peek = peekProp === false ? null : (peekProp ?? ownPeek);
73
+ const peeking = collapsed && isDesktop && (peek?.peeking ?? false);
60
74
 
61
75
  useEffect(() => {
62
76
  if (isDesktop && mobileOpen) onMobileOpenChange(false);
@@ -64,7 +78,17 @@ function Root({
64
78
 
65
79
  return (
66
80
  <SidebarContext.Provider
67
- value={{ isDesktop, mobileOpen, onMobileOpenChange, collapsed, panelHidden, drawerId: id ?? autoId }}
81
+ value={{
82
+ isDesktop,
83
+ mobileOpen,
84
+ onMobileOpenChange,
85
+ collapsed: collapsed && !peeking,
86
+ unpinned: collapsed,
87
+ peek,
88
+ onCollapsedChange: onCollapsedChange ?? null,
89
+ panelHidden,
90
+ drawerId: id ?? autoId,
91
+ }}
68
92
  >
69
93
  {children}
70
94
  </SidebarContext.Provider>
@@ -198,10 +222,22 @@ function Backdrop({ label = "Close navigation", className, onClick, ...props }:
198
222
 
199
223
  export type SidebarAsideProps = HTMLMotionProps<"aside">;
200
224
 
201
- function Aside({ id, className, onKeyDown, children, ...props }: SidebarAsideProps) {
225
+ function Aside({
226
+ id,
227
+ className,
228
+ onKeyDown,
229
+ onPointerOver,
230
+ onPointerLeave,
231
+ onFocus,
232
+ onBlur,
233
+ children,
234
+ ...props
235
+ }: SidebarAsideProps) {
202
236
  const sidebar = useSidebar("Aside");
203
237
  const resize = useSidebarResizeContext();
204
- const { isDesktop, mobileOpen, collapsed, panelHidden } = sidebar;
238
+ const { isDesktop, mobileOpen, collapsed, panelHidden, peek } = sidebar;
239
+ const hover = isDesktop && sidebar.unpinned && !panelHidden ? peek : null;
240
+ const peeking = hover !== null && !collapsed;
205
241
 
206
242
  const handleDrawerDragEnd = (_event: MouseEvent | TouchEvent | PointerEvent, info: PanInfo) => {
207
243
  if (isDesktop || !mobileOpen) return;
@@ -217,6 +253,23 @@ function Aside({ id, className, onKeyDown, children, ...props }: SidebarAsidePro
217
253
  data-sidebar-layout={isDesktop ? "desktop" : "drawer"}
218
254
  {...props}
219
255
  inert={!isDesktop && !mobileOpen}
256
+ data-sidebar-peek={peeking ? "" : undefined}
257
+ onPointerOver={(event) => {
258
+ onPointerOver?.(event);
259
+ hover?.handlers.onPointerOver(event);
260
+ }}
261
+ onPointerLeave={(event) => {
262
+ onPointerLeave?.(event);
263
+ hover?.handlers.onPointerLeave(event);
264
+ }}
265
+ onFocus={(event) => {
266
+ onFocus?.(event);
267
+ hover?.handlers.onFocus(event);
268
+ }}
269
+ onBlur={(event) => {
270
+ onBlur?.(event);
271
+ hover?.handlers.onBlur(event);
272
+ }}
220
273
  onKeyDown={(event) => {
221
274
  onKeyDown?.(event);
222
275
  closeOnEscape(sidebar, event);
@@ -229,7 +282,10 @@ function Aside({ id, className, onKeyDown, children, ...props }: SidebarAsidePro
229
282
  collapsed || panelHidden
230
283
  ? "md:w-[var(--app-shell-rail-width)]"
231
284
  : "md:w-[var(--app-shell-sidebar-width)]",
285
+ hover !== null && "md:transition-none",
286
+ peeking && "md:shadow-[8px_0_24px_-12px_rgb(0_0_0/0.18)]",
232
287
  !resize?.isResizing &&
288
+ hover === null &&
233
289
  "md:transition-[width] md:[transition-duration:280ms] md:[transition-timing-function:cubic-bezier(0.22,1,0.36,1)]",
234
290
  className,
235
291
  )}
@@ -255,11 +311,48 @@ function ResizeHandle(props: SidebarResizeHandlePartProps) {
255
311
  const resize = useSidebarResizeContext();
256
312
  const show =
257
313
  resize !== null &&
258
- (sidebar === null || (sidebar.isDesktop && !sidebar.collapsed && !sidebar.panelHidden));
314
+ (sidebar === null || (sidebar.isDesktop && !sidebar.unpinned && !sidebar.panelHidden));
259
315
  return show ? <SidebarResizeHandle {...props} /> : null;
260
316
  }
261
317
 
318
+ export type SidebarPinButtonProps = Omit<ButtonProps, "children" | "onClick">;
319
+
320
+ /**
321
+ * Pins or unpins the panel. Pinned keeps it docked beside the rail; unpinned
322
+ * shows the rail alone and opens the panel on hover. Renders nothing when
323
+ * `Sidebar.Root` has no `onCollapsedChange`, and is hidden below `md`.
324
+ */
325
+ function PinButton({ className, ...props }: SidebarPinButtonProps) {
326
+ const sidebar = useOptionalSidebar();
327
+ if (!sidebar?.onCollapsedChange) return null;
328
+ const { onCollapsedChange, peek } = sidebar;
329
+ const pinned = !sidebar.unpinned;
330
+ const label = pinned ? "Unpin sidebar" : "Pin sidebar";
331
+ const Icon = pinned ? IconPinFilled : IconPin;
332
+ return (
333
+ <Button
334
+ type="button"
335
+ variant="ghost"
336
+ size="icon-sm"
337
+ shape="rounded"
338
+ {...props}
339
+ onClick={() => {
340
+ if (pinned) peek?.hold();
341
+ else peek?.reset();
342
+ onCollapsedChange(pinned);
343
+ }}
344
+ aria-pressed={pinned}
345
+ aria-label={label}
346
+ title={label}
347
+ className={cn("size-8 shrink-0 text-muted-foreground max-md:hidden", className)}
348
+ >
349
+ <Icon size={16} />
350
+ </Button>
351
+ );
352
+ }
353
+
262
354
  export const SidebarChromeParts = {
355
+ PinButton,
263
356
  Root,
264
357
  MobileHeader,
265
358
  MobileMenuButton,
@@ -14,7 +14,7 @@ import {
14
14
  sidebarNavItemLeadingClass,
15
15
  sidebarNavItemTrailingClass,
16
16
  } from "./app-shell-classes";
17
- import { useOptionalSidebar } from "./SidebarChrome";
17
+ import { SidebarChromeParts, useOptionalSidebar } from "./SidebarChrome";
18
18
  import { useAppShellPanelDock } from "./shell-variant";
19
19
 
20
20
  export type SidebarPanelProps = {
@@ -39,13 +39,15 @@ export function SidebarPanel({
39
39
  className,
40
40
  children,
41
41
  }: SidebarPanelProps) {
42
+ const pin = Boolean(useOptionalSidebar()?.onCollapsedChange);
42
43
  return (
43
44
  <Panel collapsed={collapsed} panelHidden={false} className={className}>
44
45
  <PanelHeader>
45
46
  <PanelTitle>{title}</PanelTitle>
46
- {headerTrailing || onClose ? (
47
+ {headerTrailing || onClose || pin ? (
47
48
  <PanelActions>
48
49
  {headerTrailing}
50
+ <SidebarChromeParts.PinButton />
49
51
  {onClose ? (
50
52
  <PanelClose onClick={onClose} label={closeLabel} className={closeButtonClassName} />
51
53
  ) : null}
@@ -67,13 +69,15 @@ export function Panel({ collapsed, panelHidden, className, ...props }: SidebarPa
67
69
  const sidebar = useOptionalSidebar();
68
70
  const dock = useAppShellPanelDock();
69
71
  if (panelHidden ?? sidebar?.panelHidden ?? false) return null;
72
+ const peeking = sidebar !== null && sidebar.unpinned && !sidebar.collapsed;
70
73
  return (
71
74
  <div
72
75
  data-shell-dock-panel={dock ? "" : undefined}
76
+ data-sidebar-panel=""
73
77
  {...props}
74
78
  className={cn(
75
79
  "relative flex h-full min-w-0 flex-1 flex-col overflow-hidden bg-background",
76
- (collapsed ?? sidebar?.collapsed ?? false) && "md:hidden",
80
+ !peeking && (collapsed ?? sidebar?.collapsed ?? false) && "md:hidden",
77
81
  className,
78
82
  )}
79
83
  />
@@ -0,0 +1,127 @@
1
+ import { useRef, useState, type FocusEvent, type PointerEvent } from "react";
2
+ import { dockPopIn, dockPopOut } from "../../src/motion/dock-motion";
3
+
4
+ const OPEN_DELAY_MS = 120;
5
+ const CLOSE_DELAY_MS = 350;
6
+ const OPEN_MENU_TRIGGER = '[aria-haspopup][aria-expanded="true"]';
7
+ const PANEL_SELECTOR = "[data-sidebar-panel]";
8
+
9
+ function peekTile(target: EventTarget | null): HTMLElement | null {
10
+ if (!(target instanceof Element)) return null;
11
+ const tile = target.closest<HTMLElement>("[data-rail-peek]");
12
+ return tile && tile.closest(PANEL_SELECTOR) === null ? tile : null;
13
+ }
14
+
15
+ function inPanel(target: EventTarget | null): boolean {
16
+ return target instanceof Element && target.closest(PANEL_SELECTOR) !== null;
17
+ }
18
+
19
+ export type SidebarPeekHandlers = {
20
+ onPointerOver: (event: PointerEvent<HTMLElement>) => void;
21
+ onPointerLeave: (event: PointerEvent<HTMLElement>) => void;
22
+ onFocus: (event: FocusEvent<HTMLElement>) => void;
23
+ onBlur: (event: FocusEvent<HTMLElement>) => void;
24
+ };
25
+
26
+ export type SidebarPeek = {
27
+ /** The unpinned panel shows as an overlay. */
28
+ peeking: boolean;
29
+ /** Path of the hovered rail link, or null. */
30
+ previewPath: string | null;
31
+ /** `data-rail-peek` value of the hovered rail item (its `tabValue` or `peek` string), or null. */
32
+ previewValue: string | null;
33
+ /** The hovered rail item. */
34
+ tile: HTMLElement | null;
35
+ /** Handlers `Sidebar.Aside` spreads while the panel is unpinned. */
36
+ handlers: SidebarPeekHandlers;
37
+ /** Ends the peek at once, with no motion. */
38
+ reset: () => void;
39
+ /** Keeps the panel open as a peek until the pointer leaves it. */
40
+ hold: () => void;
41
+ };
42
+
43
+ /**
44
+ * Hover and focus state of an unpinned sidebar. The panel opens as an overlay
45
+ * when the pointer or focus is on a rail item that has `data-rail-peek` (rail
46
+ * links and tabs have it), and stays open while the pointer or focus is on a
47
+ * rail item or in the panel. It grows out of the hovered item with
48
+ * `dockPopIn` and shrinks back with `dockPopOut`. It stays open while a menu
49
+ * opened from the sidebar is open. `Sidebar.Root` makes one when none is
50
+ * passed; pass your own to read `previewPath` or `previewValue` and show the
51
+ * hovered item's panel without navigating.
52
+ */
53
+ export function useSidebarPeek(): SidebarPeek {
54
+ const [peeking, setPeeking] = useState(false);
55
+ const [tile, setTile] = useState<HTMLElement | null>(null);
56
+ const timer = useRef<number | undefined>(undefined);
57
+ const open = useRef(false);
58
+ const tileRef = useRef<HTMLElement | null>(null);
59
+
60
+ const cancelPending = () => window.clearTimeout(timer.current);
61
+
62
+ const reset = () => {
63
+ cancelPending();
64
+ open.current = false;
65
+ setPeeking(false);
66
+ setTile(null);
67
+ };
68
+
69
+ const hold = () => {
70
+ cancelPending();
71
+ open.current = true;
72
+ setPeeking(true);
73
+ };
74
+
75
+ const scheduleOpen = (aside: HTMLElement, delayMs: number) => {
76
+ cancelPending();
77
+ if (open.current) return;
78
+ timer.current = window.setTimeout(() => {
79
+ open.current = true;
80
+ setPeeking(true);
81
+ requestAnimationFrame(() => {
82
+ const panel = aside.querySelector<HTMLElement>(PANEL_SELECTOR);
83
+ if (panel) dockPopIn(panel, tileRef.current, "left");
84
+ });
85
+ }, delayMs);
86
+ };
87
+
88
+ const scheduleClose = (aside: HTMLElement) => {
89
+ cancelPending();
90
+ if (!open.current) return;
91
+ timer.current = window.setTimeout(() => {
92
+ if (aside.querySelector(OPEN_MENU_TRIGGER)) return;
93
+ dockPopOut(aside.querySelector<HTMLElement>(PANEL_SELECTOR), tileRef.current, "left", reset);
94
+ }, CLOSE_DELAY_MS);
95
+ };
96
+
97
+ const onTile = (aside: HTMLElement, next: HTMLElement, delayMs: number) => {
98
+ tileRef.current = next;
99
+ setTile(next);
100
+ scheduleOpen(aside, delayMs);
101
+ };
102
+
103
+ const handlers: SidebarPeekHandlers = {
104
+ onPointerOver: (event) => {
105
+ const next = peekTile(event.target);
106
+ if (next) onTile(event.currentTarget, next, OPEN_DELAY_MS);
107
+ else if (inPanel(event.target)) cancelPending();
108
+ else scheduleClose(event.currentTarget);
109
+ },
110
+ onPointerLeave: (event) => scheduleClose(event.currentTarget),
111
+ onFocus: (event) => {
112
+ const next = peekTile(event.target);
113
+ if (next) onTile(event.currentTarget, next, 0);
114
+ else if (inPanel(event.target)) cancelPending();
115
+ },
116
+ onBlur: (event) => {
117
+ if (peekTile(event.relatedTarget) === null && !inPanel(event.relatedTarget)) {
118
+ scheduleClose(event.currentTarget);
119
+ }
120
+ },
121
+ };
122
+
123
+ const previewPath = peeking && tile instanceof HTMLAnchorElement ? tile.pathname : null;
124
+ const previewValue = peeking && tile ? tile.dataset.railPeek || null : null;
125
+
126
+ return { peeking, previewPath, previewValue, tile, handlers, reset, hold };
127
+ }
@@ -178,6 +178,12 @@ type SidebarRailItemBaseProps = Omit<ComponentPropsWithoutRef<"button">, "childr
178
178
  badge?: ReactNode;
179
179
  asChild?: boolean;
180
180
  tabValue?: string;
181
+ /**
182
+ * Opens the unpinned panel on hover. On by default for links (`asChild`)
183
+ * and tabs (`tabValue`). A string is the `previewValue` `useSidebarPeek`
184
+ * reports for this item (default: `tabValue`).
185
+ */
186
+ peek?: boolean | string;
181
187
  };
182
188
 
183
189
  export type SidebarRailItemProps = SidebarRailItemBaseProps &
@@ -201,12 +207,15 @@ export function RailItem({
201
207
  badge,
202
208
  asChild = false,
203
209
  tabValue,
210
+ peek,
204
211
  className,
205
212
  app,
206
213
  children,
207
214
  ...props
208
215
  }: SidebarRailItemProps) {
209
216
  const grouped = use(RailGroupContext);
217
+ const peekValue =
218
+ typeof peek === "string" ? peek : (peek ?? (asChild || tabValue !== undefined)) ? (tabValue ?? "") : undefined;
210
219
  const stateClass = railTileStateClass(active, grouped);
211
220
  const groupMarks = grouped ? { "data-rail-tile": "", "data-active": active ? "" : undefined } : undefined;
212
221
  const AppIcon = app ? APP_ICONS[app] : null;
@@ -219,6 +228,7 @@ export function RailItem({
219
228
  {...props}
220
229
  value={tabValue}
221
230
  {...groupMarks}
231
+ data-rail-peek={peekValue}
222
232
  className={cn(railTileClass, "group relative", stateClass, className)}
223
233
  >
224
234
  {content}
@@ -231,6 +241,7 @@ export function RailItem({
231
241
  aria-label={label}
232
242
  aria-current={active ? "page" : undefined}
233
243
  {...groupMarks}
244
+ data-rail-peek={peekValue}
234
245
  className={cn(railTileClass, "group", stateClass, className)}
235
246
  >
236
247
  {appIcon}
@@ -243,6 +254,7 @@ export function RailItem({
243
254
  {...props}
244
255
  aria-label={label}
245
256
  {...groupMarks}
257
+ data-rail-peek={peekValue}
246
258
  className={cn(railTileClass, "group", stateClass, className)}
247
259
  >
248
260
  {content}
@@ -255,7 +267,10 @@ export function RailItem({
255
267
 
256
268
  export type RailTileProps = SidebarRailItemProps;
257
269
 
258
- /** Legacy name of `Sidebar.RailItem`. */
270
+ /**
271
+ * Legacy name of `Sidebar.RailItem`.
272
+ * @alias
273
+ */
259
274
  export const RailTile = RailItem;
260
275
 
261
276
  export type SidebarRailSeparatorProps = ComponentPropsWithoutRef<"div">;
package/src/client.ts CHANGED
@@ -236,6 +236,8 @@ export type {
236
236
  SidePanelTabsRailProps,
237
237
  SidePanelTabsTabProps,
238
238
  } from "../shell/src/SidePanelTabs";
239
+ export { useSidebarPeek } from "../shell/src/SidebarPeek";
240
+ export type { SidebarPeek, SidebarPeekHandlers } from "../shell/src/SidebarPeek";
239
241
  export { DockArrow, useDockArrow } from "../shell/src/DockArrow";
240
242
  export type { DockArrowProps, UseDockArrowOptions } from "../shell/src/DockArrow";
241
243
  export { AppSidebar } from "../shell/src/AppSidebar";
package/src/index.ts CHANGED
@@ -239,6 +239,8 @@ export type {
239
239
  SidePanelTabsRailProps,
240
240
  SidePanelTabsTabProps,
241
241
  } from "../shell/src/SidePanelTabs";
242
+ export { useSidebarPeek } from "../shell/src/SidebarPeek";
243
+ export type { SidebarPeek, SidebarPeekHandlers } from "../shell/src/SidebarPeek";
242
244
  export { DockArrow, useDockArrow } from "../shell/src/DockArrow";
243
245
  export type { DockArrowProps, UseDockArrowOptions } from "../shell/src/DockArrow";
244
246
  export { AppSidebar } from "../shell/src/AppSidebar";