Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

8.5 KB
Raw Blame
'use client'

import { Dialog as SheetPrimitive } from '@base-ui/react/dialog'
import i18n from 'i18next'
import * as React from 'react'
import { useScrolled } from '../hooks/use-scrolled'
import { cn } from '../lib/utils'
import { XIcon } from '../spree/icons'
import { Button } from './button'

function Sheet({
  children,
  open,
  defaultOpen,
  onOpenChange,
  modal,
  ...props
}: {
  children?: React.ReactNode
  open?: boolean
  defaultOpen?: boolean
  onOpenChange?: (open: boolean, eventDetails: SheetPrimitive.Root.ChangeEventDetails) => void
  /**
   * `true` (default) — full modal: focus trap + scroll lock + outside pointer interactions disabled.
   * `'trap-focus'` — focus trap only; outside pointer interactions remain enabled.
   * `false` — no modal behavior.
   *
   * Use `'trap-focus'` when the sheet contains a contenteditable (e.g. tiptap)
   * — the full-modal pointer-down listeners on `document` with `capture: true`
   * race with ProseMirror's own pointer handlers and break click-to-focus.
   */
  modal?: boolean | 'trap-focus'
}) {
  return (
    <SheetPrimitive.Root
      data-slot="sheet"
      open={open}
      defaultOpen={defaultOpen}
      onOpenChange={onOpenChange}
      modal={modal}
      {...props}
    >
      {children}
    </SheetPrimitive.Root>
  )
}

function SheetTrigger({
  asChild,
  children,
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Trigger> & {
  asChild?: boolean
}) {
  if (asChild && React.isValidElement(children)) {
    return <SheetPrimitive.Trigger data-slot="sheet-trigger" render={children} {...props} />
  }
  return (
    <SheetPrimitive.Trigger data-slot="sheet-trigger" {...props}>
      {children}
    </SheetPrimitive.Trigger>
  )
}

function SheetClose({
  asChild,
  children,
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Close> & {
  asChild?: boolean
}) {
  if (asChild && React.isValidElement(children)) {
    return <SheetPrimitive.Close data-slot="sheet-close" render={children} {...props} />
  }
  return (
    <SheetPrimitive.Close data-slot="sheet-close" {...props}>
      {children}
    </SheetPrimitive.Close>
  )
}

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

function SheetOverlay({
  className,
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Backdrop>) {
  return (
    <SheetPrimitive.Backdrop
      data-slot="sheet-overlay"
      className={cn(
        'fixed inset-0 z-50 bg-background/60 dark:bg-black/60 duration-200 data-[starting-style]:opacity-0 data-[ending-style]:opacity-0 transition-opacity',
        className,
      )}
      {...props}
    />
  )
}

function SheetContent({
  className,
  children,
  side = 'right',
  showCloseButton = true,
  dir,
  ...props
}: React.ComponentProps<typeof SheetPrimitive.Popup> & {
  side?: 'top' | 'right' | 'bottom' | 'left'
  showCloseButton?: boolean
  dir?: string
}) {
  return (
    <SheetPortal>
      <SheetOverlay />
      <SheetPrimitive.Popup
        data-slot="sheet-content"
        data-side={side}
        dir={dir}
        className={cn(
          // Tailwind v4 compiles `translate-*`/`scale-*` to the standalone
          // `translate`/`scale` properties rather than folding them into
          // `transform`, so all four are listed. The slide below needs
          // `translate`; the rest are listed because `className` is merged, so
          // a caller can animate any of them — and a missing entry fails
          // silently, as a jump rather than an error.
          'fixed z-50 flex flex-col border border-border bg-card text-foreground text-sm shadow-lg transition-[opacity,transform,translate,scale] duration-250 ease-out',
          'data-[side=right]:inset-y-0 data-[side=right]:right-0 data-[side=right]:h-[calc(100dvh-1rem)] data-[side=right]:max-h-[calc(100dvh-1rem)] data-[side=right]:w-[calc(100vw-1rem)] data-[side=right]:max-w-[600px] data-[side=right]:min-w-[320px] data-[side=right]:m-2 data-[side=right]:rounded-xl',
          'data-[side=left]:inset-y-0 data-[side=left]:left-0 data-[side=left]:h-full data-[side=left]:w-3/4 data-[side=left]:border-r data-[side=left]:sm:max-w-sm',
          'data-[side=bottom]:inset-x-0 data-[side=bottom]:bottom-0 data-[side=bottom]:h-auto data-[side=bottom]:border-t',
          'data-[side=top]:inset-x-0 data-[side=top]:top-0 data-[side=top]:h-auto data-[side=top]:border-b',
          'data-[starting-style]:opacity-0 data-[side=right]:data-[starting-style]:translate-x-10 data-[side=left]:data-[starting-style]:-translate-x-10 data-[side=bottom]:data-[starting-style]:translate-y-10 data-[side=top]:data-[starting-style]:-translate-y-10',
          'data-[ending-style]:opacity-0 data-[side=right]:data-[ending-style]:translate-x-10 data-[side=left]:data-[ending-style]:-translate-x-10 data-[side=bottom]:data-[ending-style]:translate-y-10 data-[side=top]:data-[ending-style]:-translate-y-10',
          className,
        )}
        {...props}
      >
        {children}
        {showCloseButton && (
          <SheetPrimitive.Close
            data-slot="sheet-close"
            render={
              // `size-11` on touch: at `icon-sm`'s 28px this is well under the
              // 44px minimum, and on a nav sheet it is the primary way out.
              <Button
                variant="ghost"
                className="absolute top-2 right-2 size-11 md:top-3 md:right-3 md:size-7"
                size="icon-sm"
              >
                <XIcon />
                <span className="sr-only">{i18n.t('admin.actions.close')}</span>
              </Button>
            }
          />
        )}
      </SheetPrimitive.Popup>
    </SheetPortal>
  )
}

/**
 * A sheet's title bar. It keeps its separating rule at all times and gains a
 * soft lift once the body scrolls under it, on the same trigger and curve as
 * `PageHeader`.
 */
function SheetHeader({ className, ...props }: React.ComponentProps<'div'>) {
  // A caller passing `sr-only` wants the title announced but not drawn. Merging
  // it with the visible chrome does not achieve that: `cn` keeps `flex` (a
  // different utility group from `sr-only`'s `absolute`), so the element stays
  // a laid-out box — a 33px bar with a border, above the nav. Detect the intent
  // and render the bare utility instead.
  const srOnly = className?.split(' ').includes('sr-only')
  // Hooks cannot be called conditionally, so this reads the flag before the
  // early return even though the bare variant never uses it.
  const scrolled = useScrolled()
  if (srOnly) return <div data-slot="sheet-header" className={className} {...props} />

  return (
    <div
      data-slot="sheet-header"
      className={cn(
        // No `z-index` here: the close button is a sibling rendered BEFORE this,
        // so any stacking context on the header paints over it and swallows the
        // clicks. The header needs no lift anyway — it is a flex sibling of the
        // body rather than an overlay, so nothing scrolls under it.
        'flex shrink-0 flex-col gap-1.5 border-b border-border-subtle p-4 pr-12',
        // The rule stays: a sheet header is a fixed band over its own scroller,
        // so it is divided from the body whether or not anything has moved. The
        // lift is what scrolling adds, on the same trigger and curve as
        // `PageHeader`.
        'transition-shadow duration-200 ease-out motion-reduce:transition-none',
        scrolled && 'shadow-xs',
        className,
      )}
      {...props}
    />
  )
}

function SheetFooter({ className, ...props }: React.ComponentProps<'div'>) {
  return (
    <div
      data-slot="sheet-footer"
      className={cn(
        'flex flex-col-reverse gap-2 border-t border-border-subtle bg-muted/50 p-4 sm:flex-row sm:justify-end rounded-b-xl',
        className,
      )}
      {...props}
    />
  )
}

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

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

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