sheet.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use client"
  2. import * as React from "react"
  3. import * as SheetPrimitive from "@radix-ui/react-dialog"
  4. import { cva, type VariantProps } from "class-variance-authority"
  5. import { X } from "lucide-react"
  6. import { cn } from "@/lib/utils"
  7. const Sheet = SheetPrimitive.Root
  8. const SheetTrigger = SheetPrimitive.Trigger
  9. const SheetClose = SheetPrimitive.Close
  10. const SheetPortal = SheetPrimitive.Portal
  11. const SheetOverlay = React.forwardRef<
  12. React.ElementRef<typeof SheetPrimitive.Overlay>,
  13. React.ComponentPropsWithoutRef<typeof SheetPrimitive.Overlay>
  14. >(({ className, ...props }, ref) => (
  15. <SheetPrimitive.Overlay
  16. className={cn(
  17. "fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
  18. className
  19. )}
  20. {...props}
  21. ref={ref}
  22. />
  23. ))
  24. SheetOverlay.displayName = SheetPrimitive.Overlay.displayName
  25. const sheetVariants = cva(
  26. "fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-300 data-[state=open]:duration-500",
  27. {
  28. variants: {
  29. side: {
  30. top: "inset-x-0 top-0 border-b data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top",
  31. bottom:
  32. "inset-x-0 bottom-0 border-t data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom",
  33. left: "inset-y-0 left-0 h-full w-3/4 border-r data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left sm:max-w-sm",
  34. right:
  35. "inset-y-0 right-0 h-full w-3/4 border-l data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right sm:max-w-sm",
  36. },
  37. },
  38. defaultVariants: {
  39. side: "right",
  40. },
  41. }
  42. )
  43. interface SheetContentProps
  44. extends React.ComponentPropsWithoutRef<typeof SheetPrimitive.Content>,
  45. VariantProps<typeof sheetVariants> {}
  46. const SheetContent = React.forwardRef<
  47. React.ElementRef<typeof SheetPrimitive.Content>,
  48. SheetContentProps
  49. >(({ side = "right", className, children, ...props }, ref) => (
  50. <SheetPortal>
  51. <SheetOverlay />
  52. <SheetPrimitive.Content
  53. ref={ref}
  54. className={cn(sheetVariants({ side }), className)}
  55. {...props}
  56. >
  57. {/* 隐藏的标题,用于可访问性 */}
  58. <SheetPrimitive.Title className="sr-only">
  59. 侧边栏
  60. </SheetPrimitive.Title>
  61. {children}
  62. <SheetPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-secondary">
  63. <X className="h-4 w-4" />
  64. <span className="sr-only">Close</span>
  65. </SheetPrimitive.Close>
  66. </SheetPrimitive.Content>
  67. </SheetPortal>
  68. ))
  69. SheetContent.displayName = SheetPrimitive.Content.displayName
  70. const SheetHeader = ({
  71. className,
  72. ...props
  73. }: React.HTMLAttributes<HTMLDivElement>) => (
  74. <div
  75. className={cn(
  76. "flex flex-col space-y-2 text-center sm:text-left",
  77. className
  78. )}
  79. {...props}
  80. />
  81. )
  82. SheetHeader.displayName = "SheetHeader"
  83. const SheetFooter = ({
  84. className,
  85. ...props
  86. }: React.HTMLAttributes<HTMLDivElement>) => (
  87. <div
  88. className={cn(
  89. "flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
  90. className
  91. )}
  92. {...props}
  93. />
  94. )
  95. SheetFooter.displayName = "SheetFooter"
  96. const SheetTitle = React.forwardRef<
  97. React.ElementRef<typeof SheetPrimitive.Title>,
  98. React.ComponentPropsWithoutRef<typeof SheetPrimitive.Title>
  99. >(({ className, ...props }, ref) => (
  100. <SheetPrimitive.Title
  101. ref={ref}
  102. className={cn("text-lg font-semibold text-foreground", className)}
  103. {...props}
  104. />
  105. ))
  106. SheetTitle.displayName = SheetPrimitive.Title.displayName
  107. const SheetDescription = React.forwardRef<
  108. React.ElementRef<typeof SheetPrimitive.Description>,
  109. React.ComponentPropsWithoutRef<typeof SheetPrimitive.Description>
  110. >(({ className, ...props }, ref) => (
  111. <SheetPrimitive.Description
  112. ref={ref}
  113. className={cn("text-sm text-muted-foreground", className)}
  114. {...props}
  115. />
  116. ))
  117. SheetDescription.displayName = SheetPrimitive.Description.displayName
  118. export {
  119. Sheet,
  120. SheetPortal,
  121. SheetOverlay,
  122. SheetTrigger,
  123. SheetClose,
  124. SheetContent,
  125. SheetHeader,
  126. SheetFooter,
  127. SheetTitle,
  128. SheetDescription,
  129. }