Skip to content

Commit

Permalink
feat: add sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
nahoc committed Aug 22, 2024
1 parent 6e12944 commit ccfbaf9
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ https://www.npmjs.com/package/@risc0/ui

| Statements | Branches | Functions | Lines |
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
| ![Statements](https://img.shields.io/badge/statements-38.99%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-74.68%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-61.9%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-38.99%25-red.svg?style=flat) |
| ![Statements](https://img.shields.io/badge/statements-37.29%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-73.75%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-60.46%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-37.29%25-red.svg?style=flat) |
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@risc0/ui",
"version": "0.0.156",
"version": "0.0.157",
"private": false,
"sideEffects": false,
"type": "module",
Expand All @@ -15,28 +15,27 @@
"story": "ladle serve"
},
"dependencies": {
"@radix-ui/react-alert-dialog": "^1.1.1",
"@radix-ui/react-avatar": "1.1.0",
"@radix-ui/react-checkbox": "1.1.1",
"@radix-ui/react-dialog": "1.0.5",
"@radix-ui/react-dropdown-menu": "2.1.1",
"@radix-ui/react-label": "2.1.0",
"@radix-ui/react-navigation-menu": "^1.2.0",
"@radix-ui/react-navigation-menu": "1.2.0",
"@radix-ui/react-popover": "1.1.1",
"@radix-ui/react-progress": "1.1.0",
"@radix-ui/react-radio-group": "1.2.0",
"@radix-ui/react-select": "2.1.1",
"@radix-ui/react-separator": "1.1.0",
"@radix-ui/react-slider": "1.2.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-slot": "1.1.0",
"@radix-ui/react-switch": "1.1.0",
"@radix-ui/react-tabs": "1.1.0",
"@radix-ui/react-tooltip": "1.1.2",
"autoprefixer": "10.4.20",
"class-variance-authority": "0.7.1-canary.2",
"clsx": "2.1.1",
"cmdk": "1.0.0",
"lucide-react": "0.428.0",
"lucide-react": "0.429.0",
"next": "15.0.0-canary.120",
"next-themes": "0.3.0",
"radash": "12.1.0",
Expand All @@ -48,7 +47,7 @@
"tailwind-merge": "2.5.2",
"tailwindcss": "3.4.10",
"tailwindcss-animate": "1.0.7",
"typescript": "5.6.0-dev.20240819",
"typescript": "5.7.0-dev.20240822",
"vaul": "0.9.1"
},
"devDependencies": {
Expand All @@ -61,7 +60,7 @@
"@vitejs/plugin-react-swc": "3.7.0",
"@vitest/coverage-v8": "2.0.5",
"deepmerge": "4.3.1",
"happy-dom": "14.12.3",
"happy-dom": "15.0.0",
"istanbul-badges-readme": "1.9.0",
"npm-run-all": "4.1.5"
},
Expand Down
112 changes: 112 additions & 0 deletions sheet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"use client";

import * as SheetPrimitive from "@radix-ui/react-dialog";
import { type VariantProps, cva } from "class-variance-authority";
import { cn } from "cn";
import { XIcon } from "lucide-react";
import { type ComponentPropsWithoutRef, type ElementRef, type HTMLAttributes, forwardRef } from "react";

const Sheet = SheetPrimitive.Root;
const SheetTrigger = SheetPrimitive.Trigger;
const SheetClose = SheetPrimitive.Close;
const SheetPortal = SheetPrimitive.Portal;

const sheetVariants = cva(
"fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=closed]:animate-out data-[state=open]:animate-in data-[state=closed]:duration-300 data-[state=open]:duration-500",
{
variants: {
side: {
top: "data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top inset-x-0 top-0 border-b",
bottom:
"data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom inset-x-0 bottom-0 border-t",
left: "data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left inset-y-0 left-0 h-full w-3/4 border-r sm:max-w-sm",
right:
"data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right inset-y-0 right-0 h-full w-3/4 border-l sm:max-w-sm",
},
},
defaultVariants: {
side: "right",
},
},
);

const SheetOverlay = forwardRef<
ElementRef<typeof SheetPrimitive.Overlay>,
ComponentPropsWithoutRef<typeof SheetPrimitive.Overlay>
>(({ className, ...props }, ref) => {
return (
<SheetPrimitive.Overlay
className={cn(
"data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-50 bg-black/80 data-[state=closed]:animate-out data-[state=open]:animate-in",
className,
)}
{...props}
ref={ref}
/>
);
});

interface SheetContentProps
extends ComponentPropsWithoutRef<typeof SheetPrimitive.Content>,
VariantProps<typeof sheetVariants> {}

const SheetContent = forwardRef<ElementRef<typeof SheetPrimitive.Content>, SheetContentProps>(
({ side = "right", className, children, ...props }, ref) => {
return (
<SheetPortal>
<SheetOverlay />
<SheetPrimitive.Content ref={ref} className={cn(sheetVariants({ side }), className)} {...props}>
<SheetPrimitive.Close className="absolute top-4 right-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">
<XIcon className="size-4" />
<span className="sr-only">Close</span>
</SheetPrimitive.Close>
{children}
</SheetPrimitive.Content>
</SheetPortal>
);
},
);

const SheetHeader = ({ className, ...props }: HTMLAttributes<HTMLDivElement>) => {
return <div className={cn("flex flex-col space-y-2 text-center sm:text-left", className)} {...props} />;
};

const SheetFooter = ({ className, ...props }: HTMLAttributes<HTMLDivElement>) => {
return <div className={cn("flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2", className)} {...props} />;
};

const SheetTitle = forwardRef<
ElementRef<typeof SheetPrimitive.Title>,
ComponentPropsWithoutRef<typeof SheetPrimitive.Title>
>(({ className, ...props }, ref) => {
return (
<SheetPrimitive.Title ref={ref} className={cn("font-semibold text-foreground text-lg", className)} {...props} />
);
});

const SheetDescription = forwardRef<
ElementRef<typeof SheetPrimitive.Description>,
ComponentPropsWithoutRef<typeof SheetPrimitive.Description>
>(({ className, ...props }, ref) => {
return <SheetPrimitive.Description ref={ref} className={cn("text-muted-foreground text-sm", className)} {...props} />;
});

SheetOverlay.displayName = SheetPrimitive.Overlay.displayName;
SheetDescription.displayName = SheetPrimitive.Description.displayName;
SheetTitle.displayName = SheetPrimitive.Title.displayName;
SheetFooter.displayName = "SheetFooter";
SheetHeader.displayName = "SheetHeader";
SheetContent.displayName = SheetPrimitive.Content.displayName;

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

0 comments on commit ccfbaf9

Please sign in to comment.