Skip to content

Commit 80fec55

Browse files
committed
fix: fixed sharebutton's behaviour during fullscreen
1 parent 9d5e0d3 commit 80fec55

3 files changed

Lines changed: 76 additions & 63 deletions

File tree

src/components/ShareButton.tsx

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,70 @@
11
"use client";
22

3-
import React, { useEffect, useState } from "react";
3+
import React, { useEffect, useRef, useState } from "react";
44
import {
55
Dialog,
6-
DialogContent,
76
DialogDescription,
87
DialogHeader,
98
DialogTitle,
109
DialogTrigger,
10+
DialogContent,
1111
} from "./ui/dialog";
12+
import * as DialogPrimitive from "@radix-ui/react-dialog";
1213
import { Copy } from "lucide-react";
1314
import toast from "react-hot-toast";
1415
import { FaShare } from "react-icons/fa";
1516
import QR from "./qr";
1617
import { Button } from "./ui/button";
1718
import { usePathname } from "next/navigation";
1819

19-
export default function ShareButton() {
20+
interface ShareButtonProps {
21+
isFullscreen: boolean;
22+
viewerRef: React.RefObject<HTMLDivElement>;
23+
}
24+
25+
export default function ShareButton({ isFullscreen, viewerRef }: ShareButtonProps) {
2026
const [origin, setOrigin] = useState("");
27+
const pathname = usePathname();
2128

2229
useEffect(() => {
23-
if (typeof window !== "undefined") {
24-
setOrigin(window.location.origin);
25-
}
30+
setOrigin(window.location.origin);
2631
}, []);
2732

28-
const pathname = usePathname();
2933
const paperPath = origin + pathname;
34+
console.log("isFullscreen:", isFullscreen, "container:", isFullscreen ? viewerRef.current : document.body);
3035
return (
3136
<Dialog>
3237
<DialogTrigger asChild>
33-
<Button className="aspect-square h-10 w-10 p-0 rounded text-white bg-[#6536c1] transition hover:bg-[#7d4fc7]" title="Share this paper">
38+
<Button
39+
className="aspect-square h-10 w-10 p-0 rounded text-white bg-[#6536c1] transition hover:bg-[#7d4fc7]"
40+
title="Share this paper"
41+
>
3442
<FaShare />
3543
</Button>
3644
</DialogTrigger>
37-
<DialogContent className="max-w-96">
45+
<DialogContent
46+
container={isFullscreen ? viewerRef.current : document.body}
47+
className="max-w-96"
48+
>
3849
<DialogHeader>
3950
<DialogTitle>Share Papers with your friends!</DialogTitle>
4051
<DialogDescription>
4152
Either scan the QR or copy the link and share
4253
</DialogDescription>
4354
</DialogHeader>
4455
<div className="flex flex-col items-center justify-center gap-5">
45-
<QR url={paperPath}></QR>
56+
<QR url={paperPath} />
4657
<Button
47-
type="submit"
58+
type="button"
4859
size="sm"
4960
className="flex w-fit items-center justify-between gap-5 px-3"
5061
title="Copy link to clipboard"
5162
onClick={async () => {
52-
await toast.promise(
53-
navigator.clipboard.writeText(paperPath), // This is a promise
54-
{
55-
success: "Link copied successfully",
56-
loading: "Copying link...",
57-
error: "Error copying link",
58-
},
59-
);
63+
await toast.promise(navigator.clipboard.writeText(paperPath), {
64+
success: "Link copied successfully",
65+
loading: "Copying link...",
66+
error: "Error copying link",
67+
});
6068
}}
6169
>
6270
<p>Copy Link To Clipboard</p>
@@ -66,4 +74,4 @@ export default function ShareButton() {
6674
</DialogContent>
6775
</Dialog>
6876
);
69-
}
77+
}

src/components/newPdfViewer.tsx

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface ControlProps {
2525
forceMobile?: boolean;
2626
isMobile: boolean;
2727
isSmall: boolean;
28+
viewerRef: React.RefObject<HTMLDivElement>;
2829
}
2930

3031
interface PdfViewerProps {
@@ -57,7 +58,7 @@ function useBreakpoint() {
5758
}
5859

5960
const Controls = memo(function Controls({documentId, toggleFullscreen, isFullscreen, onDownload,
60-
forceMobile, isMobile, isSmall}: ControlProps) {
61+
forceMobile, isMobile, isSmall, viewerRef}: ControlProps) {
6162

6263
const { provides: zoomProv, state: zoomState } = useZoom(documentId);
6364
const { provides: scrollProv, state: scrollState } = useScroll(documentId);
@@ -127,7 +128,7 @@ const Controls = memo(function Controls({documentId, toggleFullscreen, isFullscr
127128
<Download size={24} />
128129
</Button>
129130

130-
<ShareButton />
131+
<ShareButton isFullscreen={isFullscreen} viewerRef={viewerRef} />
131132

132133
<Button
133134
onClick={zoomOut}
@@ -492,47 +493,48 @@ if (isLoading || !engine) {
492493
forceMobile={true}
493494
isMobile={isMobile}
494495
isSmall={isSmall}
496+
viewerRef={viewerRef}
495497
/>}
496498
<DocumentContent documentId={activeDocumentId}>
497-
{({ isLoaded }) => (
498-
<>
499-
<div
500-
className="absolute inset-0 z-50 flex items-center justify-center bg-[#070114]"
501-
style={{
502-
opacity: isLoaded ? 0 : 1,
503-
pointerEvents: isLoaded ? "none" : "auto",
504-
transition: "opacity 0.3s",
505-
backgroundColor: effectiveBackgroundColor,
506-
}}
507-
>
508-
<Loader
509-
backgroundColor={effectiveBackgroundColor}
510-
textColor={loaderTextColor}
511-
/>
512-
</div>
513-
<Viewport
514-
documentId={activeDocumentId}
515-
style={{
516-
backgroundColor: effectiveBackgroundColor,
517-
visibility: isLoaded ? "visible" : "hidden",
518-
}}
519-
>
520-
<Scroller
521-
documentId={activeDocumentId}
522-
renderPage={({ width, height, pageIndex }) => (
523-
<div
524-
style={{ width, height }}
525-
onClick={(e) => e.stopPropagation()}
526-
>
527-
<RenderLayer documentId={activeDocumentId} pageIndex={pageIndex} />
528-
</div>
529-
)}
530-
/>
531-
</Viewport>
532-
</>
533-
)}
534-
</DocumentContent>
535-
499+
{({ isLoaded }) => (
500+
<>
501+
<div
502+
className="absolute inset-0 z-50 flex items-center justify-center bg-[#070114]"
503+
style={{
504+
opacity: isLoaded ? 0 : 1,
505+
pointerEvents: isLoaded ? "none" : "auto",
506+
transition: "opacity 0.3s",
507+
backgroundColor: effectiveBackgroundColor,
508+
}}
509+
>
510+
<Loader
511+
backgroundColor={effectiveBackgroundColor}
512+
textColor={loaderTextColor}
513+
/>
514+
</div>
515+
<Viewport
516+
documentId={activeDocumentId}
517+
style={{
518+
backgroundColor: effectiveBackgroundColor,
519+
visibility: isLoaded ? "visible" : "hidden",
520+
}}
521+
>
522+
<Scroller
523+
documentId={activeDocumentId}
524+
renderPage={({ width, height, pageIndex }) => (
525+
<div
526+
style={{ width, height }}
527+
onClick={(e) => e.stopPropagation()}
528+
>
529+
<RenderLayer documentId={activeDocumentId} pageIndex={pageIndex} />
530+
</div>
531+
)}
532+
/>
533+
</Viewport>
534+
</>
535+
)}
536+
</DocumentContent>
537+
536538
{!hideControls && (!isMobile || isFullscreen) && (
537539
<Controls
538540
documentId={activeDocumentId}
@@ -542,6 +544,7 @@ if (isLoading || !engine) {
542544
forceMobile={false}
543545
isMobile={isMobile}
544546
isSmall={isSmall}
547+
viewerRef={viewerRef}
545548
/>
546549
)}
547550
</>

src/components/ui/dialog.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ DialogOverlay.displayName = DialogPrimitive.Overlay.displayName
3030

3131
const DialogContent = React.forwardRef<
3232
React.ElementRef<typeof DialogPrimitive.Content>,
33-
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
33+
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
34+
container?: HTMLElement | null
35+
}
3436
>(({ className, children, ...props }, ref) => (
35-
<DialogPortal>
37+
<DialogPortal container={props.container}>
3638
<DialogOverlay />
3739
<DialogPrimitive.Content
3840
ref={ref}

0 commit comments

Comments
 (0)