|
| 1 | +'use client' |
| 2 | +import { PDFViewer, PDFViewerRef } from '@embedpdf/react-pdf-viewer' |
| 3 | +import { useRef, useState, useEffect } from 'react' |
| 4 | + |
| 5 | +// Available brand colors to choose from |
| 6 | +const brandColors = [ |
| 7 | + { |
| 8 | + name: 'Purple', |
| 9 | + primary: '#9333ea', |
| 10 | + hover: '#7e22ce', |
| 11 | + active: '#6b21a8', |
| 12 | + light: '#f3e8ff', |
| 13 | + darkLight: '#3b0764', |
| 14 | + }, |
| 15 | + { |
| 16 | + name: 'Blue', |
| 17 | + primary: '#2563eb', |
| 18 | + hover: '#1d4ed8', |
| 19 | + active: '#1e40af', |
| 20 | + light: '#dbeafe', |
| 21 | + darkLight: '#1e3a8a', |
| 22 | + }, |
| 23 | + { |
| 24 | + name: 'Green', |
| 25 | + primary: '#16a34a', |
| 26 | + hover: '#15803d', |
| 27 | + active: '#166534', |
| 28 | + light: '#dcfce7', |
| 29 | + darkLight: '#14532d', |
| 30 | + }, |
| 31 | + { |
| 32 | + name: 'Orange', |
| 33 | + primary: '#ea580c', |
| 34 | + hover: '#c2410c', |
| 35 | + active: '#9a3412', |
| 36 | + light: '#ffedd5', |
| 37 | + darkLight: '#7c2d12', |
| 38 | + }, |
| 39 | + { |
| 40 | + name: 'Pink', |
| 41 | + primary: '#db2777', |
| 42 | + hover: '#be185d', |
| 43 | + active: '#9d174d', |
| 44 | + light: '#fce7f3', |
| 45 | + darkLight: '#831843', |
| 46 | + }, |
| 47 | +] |
| 48 | + |
| 49 | +interface ThemeExampleProps { |
| 50 | + /** Theme preference: 'light' or 'dark' - synced with the website theme */ |
| 51 | + themePreference?: 'light' | 'dark' |
| 52 | +} |
| 53 | + |
| 54 | +export default function ThemeExample({ |
| 55 | + themePreference = 'light', |
| 56 | +}: ThemeExampleProps) { |
| 57 | + const viewerRef = useRef<PDFViewerRef>(null) |
| 58 | + const [selectedColor, setSelectedColor] = useState(brandColors[0]) |
| 59 | + |
| 60 | + // Update viewer theme when themePreference prop changes |
| 61 | + useEffect(() => { |
| 62 | + viewerRef.current?.container?.setTheme({ |
| 63 | + preference: themePreference, |
| 64 | + }) |
| 65 | + }, [themePreference]) |
| 66 | + |
| 67 | + const changeColor = (color: (typeof brandColors)[0]) => { |
| 68 | + setSelectedColor(color) |
| 69 | + // Update the theme colors at runtime |
| 70 | + viewerRef.current?.container?.setTheme({ |
| 71 | + preference: themePreference, |
| 72 | + light: { |
| 73 | + accent: { |
| 74 | + primary: color.primary, |
| 75 | + primaryHover: color.hover, |
| 76 | + primaryActive: color.active, |
| 77 | + primaryLight: color.light, |
| 78 | + primaryForeground: '#ffffff', |
| 79 | + }, |
| 80 | + }, |
| 81 | + dark: { |
| 82 | + accent: { |
| 83 | + primary: color.primary, |
| 84 | + primaryHover: color.hover, |
| 85 | + primaryActive: color.active, |
| 86 | + primaryLight: color.darkLight, |
| 87 | + primaryForeground: '#ffffff', |
| 88 | + }, |
| 89 | + }, |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className="flex flex-col gap-4"> |
| 95 | + {/* Color Selection */} |
| 96 | + <div className="flex flex-wrap items-center gap-3 rounded-lg border border-gray-200 bg-gray-50 p-4 dark:border-gray-700 dark:bg-gray-800"> |
| 97 | + <span className="text-sm font-medium text-gray-700 dark:text-gray-300"> |
| 98 | + Choose brand color: |
| 99 | + </span> |
| 100 | + <div className="flex gap-2"> |
| 101 | + {brandColors.map((color) => ( |
| 102 | + <button |
| 103 | + key={color.name} |
| 104 | + onClick={() => changeColor(color)} |
| 105 | + className={`h-8 w-8 rounded-full border-2 transition-transform hover:scale-110 ${ |
| 106 | + selectedColor.name === color.name |
| 107 | + ? 'border-gray-900 ring-2 ring-offset-2 dark:border-white' |
| 108 | + : 'border-transparent' |
| 109 | + }`} |
| 110 | + style={{ backgroundColor: color.primary }} |
| 111 | + title={color.name} |
| 112 | + /> |
| 113 | + ))} |
| 114 | + </div> |
| 115 | + <span className="text-sm text-gray-500 dark:text-gray-400"> |
| 116 | + Selected: <strong>{selectedColor.name}</strong> |
| 117 | + </span> |
| 118 | + </div> |
| 119 | + |
| 120 | + {/* Viewer Container */} |
| 121 | + <div className="h-[600px] w-full overflow-hidden rounded-xl border border-gray-300 shadow-lg dark:border-gray-600"> |
| 122 | + <PDFViewer |
| 123 | + ref={viewerRef} |
| 124 | + config={{ |
| 125 | + src: 'https://snippet.embedpdf.com/ebook.pdf', |
| 126 | + theme: { |
| 127 | + preference: themePreference, |
| 128 | + light: { |
| 129 | + accent: { |
| 130 | + primary: selectedColor.primary, |
| 131 | + primaryHover: selectedColor.hover, |
| 132 | + primaryActive: selectedColor.active, |
| 133 | + primaryLight: selectedColor.light, |
| 134 | + primaryForeground: '#ffffff', |
| 135 | + }, |
| 136 | + }, |
| 137 | + dark: { |
| 138 | + accent: { |
| 139 | + primary: selectedColor.primary, |
| 140 | + primaryHover: selectedColor.hover, |
| 141 | + primaryActive: selectedColor.active, |
| 142 | + primaryLight: selectedColor.darkLight, |
| 143 | + primaryForeground: '#ffffff', |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }} |
| 148 | + style={{ width: '100%', height: '100%' }} |
| 149 | + /> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + ) |
| 153 | +} |
0 commit comments