Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

6.8 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import { AppPortalGate } from "@/components/app-readiness";
import { Dialog as SheetPrimitive } from "radix-ui";
import type * as React from "react";
import { useState } from "react";

import { Button } from "@/components/ui/button";
import { DialogPortalContainerContext } from "@/components/ui/dialog";
import { MODAL_SURFACE, useMergedRefs, useSurfaceRef } from "@/lib/surface-registry";
import { cn } from "@/lib/utils";
import { Cancel01Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";

// The desktop titlebar paints at z-70, so a fixed sheet starts below it; 0px in browser/macOS.
const VIEWPORT_TOP_EDGE =
  "data-[side=left]:top-[var(--studio-custom-titlebar-height,0px)] data-[side=right]:top-[var(--studio-custom-titlebar-height,0px)] data-[side=top]:top-[var(--studio-custom-titlebar-height,0px)]";

const CONTAINED_TOP_EDGE =
  "data-[side=left]:top-0 data-[side=right]:top-0 data-[side=top]:top-0";

function Sheet({ ...props }: React.ComponentProps<typeof SheetPrimitive.Root>) {
  return <SheetPrimitive.Root data-slot="sheet" {...props} />;
}

function SheetTrigger({
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Trigger>) {
  return <SheetPrimitive.Trigger data-slot="sheet-trigger" {...props} />;
}

function SheetClose({
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Close>) {
  return <SheetPrimitive.Close data-slot="sheet-close" {...props} />;
}

function SheetCloseButton({ className }: { className?: string }) {
  return (
    <SheetPrimitive.Close data-slot="sheet-close" asChild={true}>
      <Button variant="ghost" className={className} size="icon-sm">
        <HugeiconsIcon icon={Cancel01Icon} strokeWidth={2} />
        <span className="sr-only">Close</span>
      </Button>
    </SheetPrimitive.Close>
  );
}

function SheetPortal({
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Portal>) {
  return (
    <AppPortalGate>
      <SheetPrimitive.Portal data-slot="sheet-portal" {...props} />
    </AppPortalGate>
  );
}

function SheetOverlay({
  className,
  position = "fixed",
  ref,
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Overlay> & {
  position?: "fixed" | "absolute";
}) {
  const surfaceRef = useSurfaceRef(MODAL_SURFACE, ref);
  return (
    <SheetPrimitive.Overlay
      ref={surfaceRef}
      data-slot="sheet-overlay"
      data-viewport-backdrop={position === "fixed" ? true : undefined}
      className={cn(
        "data-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 bg-black/30 duration-100 data-ending-style:opacity-0 data-starting-style:opacity-0 inset-0 z-50",
        position === "fixed" ? "fixed" : "absolute",
        className,
      )}
      {...props}
    />
  );
}

function SheetContent({
  className,
  children,
  side = "right",
  showCloseButton = true,
  container,
  position = "fixed",
  overlayClassName,
  overlayPosition,
  onInteractOutside,
  ref,
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Content> & {
  side?: "top" | "right" | "bottom" | "left";
  showCloseButton?: boolean;
  container?: HTMLElement | null;
  position?: "fixed" | "absolute";
  overlayClassName?: string;
  overlayPosition?: "fixed" | "absolute";
}) {
  const [contentEl, setContentEl] = useState<HTMLDivElement | null>(null);
  // A caller's ref (the mobile sidebar registers through one) reaches the element too.
  const contentRef = useMergedRefs(setContentEl, ref);
  return (
    <SheetPortal container={container ?? undefined}>
      <SheetOverlay
        className={overlayClassName}
        position={overlayPosition ?? position}
      />
      <SheetPrimitive.Content
        ref={contentRef}
        data-slot="sheet-content"
        data-side={side}
        className={cn(
          "bg-background data-open:animate-in data-closed:animate-out data-[side=right]:data-closed:slide-out-to-right-10 data-[side=right]:data-open:slide-in-from-right-10 data-[side=left]:data-closed:slide-out-to-left-10 data-[side=left]:data-open:slide-in-from-left-10 data-[side=top]:data-closed:slide-out-to-top-10 data-[side=top]:data-open:slide-in-from-top-10 data-closed:fade-out-0 data-open:fade-in-0 data-[side=bottom]:data-closed:slide-out-to-bottom-10 data-[side=bottom]:data-open:slide-in-from-bottom-10 z-50 flex flex-col bg-clip-padding text-sm shadow-lg transition duration-200 ease-in-out data-[side=bottom]:inset-x-0 data-[side=bottom]:bottom-0 data-[side=bottom]:h-auto data-[side=bottom]:border-t data-[side=left]:left-0 data-[side=left]:bottom-0 data-[side=left]:w-3/4 data-[side=left]:border-r data-[side=right]:right-0 data-[side=right]:bottom-0 data-[side=right]:w-3/4 data-[side=right]:border-l data-[side=top]:inset-x-0 data-[side=top]:h-auto data-[side=top]:border-b data-[side=left]:sm:max-w-sm data-[side=right]:sm:max-w-sm data-[side=bottom]:max-h-[85dvh] data-[side=top]:max-h-[85dvh] data-[side=bottom]:overflow-y-auto data-[side=top]:overflow-y-auto",
          position === "fixed" ? "fixed" : "absolute",
          position === "fixed" ? VIEWPORT_TOP_EDGE : CONTAINED_TOP_EDGE,
          className,
        )}
        onInteractOutside={(event) => {
          onInteractOutside?.(event);
          if ((event.target as Element | null)?.closest?.("[data-sonner-toaster]")) {
            event.preventDefault();
          }
        }}
        {...props}
      >
        <DialogPortalContainerContext.Provider value={contentEl}>
          {children}
        </DialogPortalContainerContext.Provider>
        {showCloseButton && (
          <SheetCloseButton className="absolute top-4 right-4" />
        )}
      </SheetPrimitive.Content>
    </SheetPortal>
  );
}

function SheetHeader({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="sheet-header"
      className={cn("gap-1.5 p-6 flex flex-col", className)}
      {...props}
    />
  );
}

function SheetFooter({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="sheet-footer"
      className={cn("gap-2 p-6 mt-auto flex flex-col", className)}
      {...props}
    />
  );
}

function SheetTitle({
  className,
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Title>) {
  return (
    <SheetPrimitive.Title
      data-slot="sheet-title"
      className={cn("text-foreground text-base font-medium", className)}
      {...props}
    />
  );
}

function SheetDescription({
  className,
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Description>) {
  return (
    <SheetPrimitive.Description
      data-slot="sheet-description"
      className={cn("text-muted-foreground text-sm", className)}
      {...props}
    />
  );
}

export {
  Sheet,
  SheetTrigger,
  SheetClose,
  SheetCloseButton,
  SheetContent,
  SheetHeader,
  SheetFooter,
  SheetTitle,
  SheetDescription,
};