Skip to content

Commit 83106c3

Browse files
committed
fix: pdf viewer dark mode compatibility
1 parent 6e179e7 commit 83106c3

1 file changed

Lines changed: 62 additions & 11 deletions

File tree

src/components/newPdfViewer.tsx

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { downloadFile } from "../lib/utils/download";
1515
import { Button } from "./ui/button";
1616
import ShareButton from "./ShareButton";
1717
import ReportButton from "./ReportButton";
18+
import { useTheme } from "next-themes";
1819

1920
interface ControlProps {
2021
documentId: string;
@@ -34,6 +35,7 @@ interface PdfViewerProps {
3435
hideControls?: boolean;
3536
backgroundColor?: string;
3637
hideScrollbar?: boolean;
38+
isolateFromTheme?: boolean;
3739
}
3840

3941
interface WheelZoomProps {
@@ -333,14 +335,23 @@ const LOADING_MESSAGES = [
333335
"Almost there",
334336
];
335337

336-
export function Loader() {
338+
export function Loader({
339+
backgroundColor = "#070114",
340+
textColor = "rgba(255,255,255,0.5)",
341+
}: {
342+
backgroundColor?: string;
343+
textColor?: string;
344+
}) {
337345
const { message, visible } = useLoadingMessage(LOADING_MESSAGES);
338346
return (
339-
<div className="flex flex-col items-center justify-center gap-3 h-dvh w-full bg-[#070114]">
347+
<div
348+
className="flex h-dvh w-full flex-col items-center justify-center gap-3"
349+
style={{ backgroundColor }}
350+
>
340351
<div className="w-7 h-7 rounded-full border-2 border-white/10 border-t-white animate-spin" />
341352
<span
342-
className="text-white/50 text-sm tracking-wide transition-opacity duration-400"
343-
style={{ opacity: visible ? 1 : 0 }}
353+
className="text-sm tracking-wide transition-opacity duration-400"
354+
style={{ opacity: visible ? 1 : 0, color: textColor }}
344355
>
345356
{message}
346357
</span>
@@ -353,13 +364,19 @@ export default function PDFViewer({
353364
className,
354365
height = "100dvh",
355366
hideControls = false,
356-
backgroundColor = "#070114",
367+
backgroundColor,
357368
hideScrollbar = false,
369+
isolateFromTheme = true,
358370
}: PdfViewerProps) {
359371
const { engine, isLoading } = usePdfiumEngine();
360-
const {isMobile, isSmall} = useBreakpoint();
372+
const { isMobile, isSmall } = useBreakpoint();
373+
const { resolvedTheme } = useTheme();
361374
const [isFullscreen, setIsFullscreen] = useState(false);
362375
const viewerRef = useRef<HTMLDivElement>(null);
376+
const effectiveBackgroundColor =
377+
backgroundColor ?? (resolvedTheme === "light" ? "#F3F5FF" : "#070114");
378+
const loaderTextColor =
379+
resolvedTheme === "light" ? "rgba(17,24,39,0.6)" : "rgba(255,255,255,0.5)";
363380

364381
const handleDownload = useCallback(async () => {
365382
window.dataLayer?.push({
@@ -402,7 +419,10 @@ export default function PDFViewer({
402419

403420
if (isLoading || !engine) {
404421
return (
405-
<Loader />
422+
<Loader
423+
backgroundColor={effectiveBackgroundColor}
424+
textColor={loaderTextColor}
425+
/>
406426
);
407427
}
408428

@@ -423,11 +443,36 @@ if (isLoading || !engine) {
423443
}
424444
`}</style>
425445
)}
446+
{isolateFromTheme && (
447+
<style jsx global>{`
448+
[data-pdf-viewer-theme="light"] {
449+
color-scheme: light;
450+
forced-color-adjust: none;
451+
}
452+
453+
[data-pdf-viewer-theme="light"] canvas,
454+
[data-pdf-viewer-theme="light"] img,
455+
[data-pdf-viewer-theme="light"] svg {
456+
filter: none !important;
457+
forced-color-adjust: none;
458+
}
459+
`}</style>
460+
)}
426461
<div
427462
ref={viewerRef}
463+
data-pdf-viewer-theme={isolateFromTheme ? "light" : "inherited"}
428464
data-pdf-viewer-scrollbars={hideScrollbar ? "hidden" : "visible"}
429465
className={className}
430-
style={{ height, width: "100%", position: "relative", backgroundColor, display: "flex", flexDirection: "column" }}
466+
style={{
467+
height,
468+
width: "100%",
469+
position: "relative",
470+
backgroundColor: effectiveBackgroundColor,
471+
display: "flex",
472+
flexDirection: "column",
473+
colorScheme: isolateFromTheme ? "light" : undefined,
474+
forcedColorAdjust: isolateFromTheme ? "none" : undefined,
475+
}}
431476
>
432477
<EmbedPDF engine={engine} plugins={plugins}>
433478
{({ activeDocumentId }) =>
@@ -453,14 +498,20 @@ if (isLoading || !engine) {
453498
opacity: isLoaded ? 0 : 1,
454499
pointerEvents: isLoaded ? "none" : "auto",
455500
transition: "opacity 0.3s",
456-
backgroundColor,
501+
backgroundColor: effectiveBackgroundColor,
457502
}}
458503
>
459-
<Loader />
504+
<Loader
505+
backgroundColor={effectiveBackgroundColor}
506+
textColor={loaderTextColor}
507+
/>
460508
</div>
461509
<Viewport
462510
documentId={activeDocumentId}
463-
style={{ backgroundColor, visibility: isLoaded ? "visible" : "hidden" }}
511+
style={{
512+
backgroundColor: effectiveBackgroundColor,
513+
visibility: isLoaded ? "visible" : "hidden",
514+
}}
464515
>
465516
<Scroller
466517
documentId={activeDocumentId}

0 commit comments

Comments
 (0)