Skip to content

Commit 36cd803

Browse files
committed
feat: add screenshot functionality to Toolbar and integrate modern-screenshot library
1 parent 2013939 commit 36cd803

5 files changed

Lines changed: 104 additions & 3 deletions

File tree

package-lock.json

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/responsive-preview-react/lib/PreviewWrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ export function PreviewWrapper({
114114
breakpointTitle={currentBreakpoint?.title}
115115
availableBreakpoints={availableBreakpoints}
116116
onBreakpointChange={(value) => {
117-
console.log("value", value);
118117
if (resizablePanelRef?.current) {
119118
resizablePanelRef.current.resize(parseFloat(value));
120119
}
121120
}}
121+
panelRef={panelContentRef} // Add this line
122122
/>
123123
)}
124124
</div>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as React from "react";
2+
import { Slot } from "@radix-ui/react-slot";
3+
import { cva, type VariantProps } from "class-variance-authority";
4+
5+
import { cn } from "@/base/lib/utils";
6+
7+
const buttonVariants = cva(
8+
"rpr-inline-flex rpr-items-center rpr-justify-center rpr-gap-2 rpr-whitespace-nowrap rpr-rounded-md rpr-text-sm rpr-font-medium rpr-transition-colors focus-visible:rpr-outline-none focus-visible:rpr-ring-1 focus-visible:rpr-ring-ring disabled:rpr-pointer-events-none disabled:rpr-opacity-50 [&_svg]:rpr-pointer-events-none [&_svg]:rpr-size-4 [&_svg]:rpr-shrink-0",
9+
{
10+
variants: {
11+
variant: {
12+
default:
13+
"rpr-bg-primary rpr-text-primary-foreground rpr-shadow hover:rpr-bg-primary/90",
14+
destructive:
15+
"rpr-bg-destructive rpr-text-destructive-foreground rpr-shadow-sm hover:rpr-bg-destructive/90",
16+
outline:
17+
"rpr-border rpr-border-input rpr-bg-background rpr-shadow-sm hover:rpr-bg-accent hover:rpr-text-accent-foreground",
18+
secondary:
19+
"rpr-bg-secondary rpr-text-secondary-foreground rpr-shadow-sm hover:rpr-bg-secondary/80",
20+
ghost: "hover:rpr-bg-accent hover:rpr-text-accent-foreground",
21+
link: "rpr-text-primary rpr-underline-offset-4 hover:rpr-underline",
22+
},
23+
size: {
24+
default: "rpr-h-9 rpr-px-4 rpr-py-2",
25+
sm: "rpr-h-8 rpr-rounded-md rpr-px-3 rpr-text-xs",
26+
lg: "rpr-h-10 rpr-rounded-md rpr-px-8",
27+
icon: "rpr-h-9 rpr-w-9",
28+
},
29+
},
30+
defaultVariants: {
31+
variant: "default",
32+
size: "default",
33+
},
34+
}
35+
);
36+
37+
export interface ButtonProps
38+
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
39+
VariantProps<typeof buttonVariants> {
40+
asChild?: boolean;
41+
}
42+
43+
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
44+
({ className, variant, size, asChild = false, ...props }, ref) => {
45+
const Comp = asChild ? Slot : "button";
46+
return (
47+
<Comp
48+
className={cn(buttonVariants({ variant, size, className }))}
49+
ref={ref}
50+
{...props}
51+
/>
52+
);
53+
}
54+
);
55+
Button.displayName = "Button";
56+
57+
export { Button, buttonVariants };

packages/responsive-preview-react/lib/components/Toolbar.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ToggleGroupItem,
1010
} from "@/base/components/ui/toggle-group";
1111
import {
12+
Camera,
1213
ChevronLeft,
1314
ChevronRight,
1415
MaximizeIcon,
@@ -17,13 +18,15 @@ import {
1718
} from "lucide-react";
1819
import type { Breakpoint } from "../breakpoints";
1920
import { useCallback, useEffect, useRef, useState } from "react";
21+
import { domToPng } from "modern-screenshot"; // Add this import
2022

2123
interface ToolbarProps {
2224
width: number;
2325
maxWidth: number;
2426
breakpointTitle?: string;
2527
availableBreakpoints: Breakpoint[];
2628
onBreakpointChange: (value: string) => void;
29+
panelRef: React.RefObject<HTMLDivElement>;
2730
}
2831

2932
export function Toolbar({
@@ -32,13 +35,28 @@ export function Toolbar({
3235
breakpointTitle,
3336
availableBreakpoints,
3437
onBreakpointChange,
38+
panelRef,
3539
}: ToolbarProps) {
3640
const [isPlaying, setIsPlaying] = useState(false);
3741
const intervalRef = useRef<NodeJS.Timeout>();
3842
const currentIndex = availableBreakpoints.findIndex(
3943
(bp) => bp.title === breakpointTitle
4044
);
4145

46+
const handleScreenshot = async () => {
47+
if (panelRef.current) {
48+
try {
49+
const dataUrl = await domToPng(panelRef.current);
50+
const link = document.createElement("a");
51+
link.download = `preview-${width}px.png`;
52+
link.href = dataUrl;
53+
link.click();
54+
} catch (err) {
55+
console.error("Screenshot failed:", err);
56+
}
57+
}
58+
};
59+
4260
const handlePrevBreakpoint = () => {
4361
if (currentIndex > 0) {
4462
const prevBp = availableBreakpoints[currentIndex - 1];
@@ -158,6 +176,22 @@ export function Toolbar({
158176
</TooltipContent>
159177
</Tooltip>
160178
</ToggleGroupItem>
179+
180+
<ToggleGroupItem
181+
value="screenshot"
182+
onClick={handleScreenshot}
183+
data-state="off"
184+
className="rpr-h-[22px] rpr-w-[22px] rpr-min-w-0 rpr-rounded-sm rpr-p-0"
185+
>
186+
<Tooltip>
187+
<TooltipTrigger asChild>
188+
<Camera className="rpr-h-3.5 rpr-w-3.5" />
189+
</TooltipTrigger>
190+
<TooltipContent>
191+
<p>Take Screenshot</p>
192+
</TooltipContent>
193+
</Tooltip>
194+
</ToggleGroupItem>
161195
</ToggleGroup>
162196
</TooltipProvider>
163197

packages/responsive-preview-react/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@
7373
"@radix-ui/react-hover-card": "^1.1.5",
7474
"@radix-ui/react-label": "^2.1.1",
7575
"@radix-ui/react-popover": "^1.1.5",
76+
"@radix-ui/react-slot": "^1.1.1",
7677
"@radix-ui/react-switch": "^1.1.2",
7778
"@radix-ui/react-tabs": "^1.1.2",
7879
"@radix-ui/react-toggle": "^1.1.1",
7980
"@radix-ui/react-toggle-group": "^1.1.1",
80-
"@radix-ui/react-tooltip": "^1.1.6"
81+
"@radix-ui/react-tooltip": "^1.1.6",
82+
"modern-screenshot": "^4.5.5"
8183
}
8284
}

0 commit comments

Comments
 (0)