✨ add menu pdf & pdf lib
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import type { PDFDocumentProxy, RenderTask } from 'pdfjs-dist'
|
||||
|
||||
type Props = {
|
||||
src: string
|
||||
}
|
||||
|
||||
// pdfjs-dist touches the DOM/Canvas at import time, so it's loaded lazily inside
|
||||
// the effect below instead of at module scope — this file also runs during SSR.
|
||||
export function PdfMenu({ src }: Props) {
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const canvasRef = useRef<HTMLCanvasElement>(null)
|
||||
const pdfRef = useRef<PDFDocumentProxy | null>(null)
|
||||
const directionRef = useRef(1)
|
||||
const hasRenderedOnceRef = useRef(false)
|
||||
const [error, setError] = useState(false)
|
||||
const [numPages, setNumPages] = useState(0)
|
||||
const [pageNumber, setPageNumber] = useState(1)
|
||||
const [isBusy, setIsBusy] = useState(false)
|
||||
|
||||
function goToPage(next: number) {
|
||||
directionRef.current = next > pageNumber ? 1 : -1
|
||||
setPageNumber(next)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
setError(false)
|
||||
setNumPages(0)
|
||||
setPageNumber(1)
|
||||
pdfRef.current = null
|
||||
hasRenderedOnceRef.current = false
|
||||
|
||||
async function load() {
|
||||
const pdfjsLib = await import('pdfjs-dist')
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL(
|
||||
'pdfjs-dist/build/pdf.worker.min.mjs',
|
||||
import.meta.url,
|
||||
).href
|
||||
|
||||
try {
|
||||
const pdf = await pdfjsLib.getDocument({ url: src }).promise
|
||||
if (cancelled) return
|
||||
pdfRef.current = pdf
|
||||
setNumPages(pdf.numPages)
|
||||
} catch (err) {
|
||||
console.error('PdfMenu failed to load', err)
|
||||
if (!cancelled) setError(true)
|
||||
}
|
||||
}
|
||||
|
||||
load()
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [src])
|
||||
|
||||
useEffect(() => {
|
||||
const pdf = pdfRef.current
|
||||
const canvas = canvasRef.current
|
||||
const container = containerRef.current
|
||||
if (!pdf || !canvas || !container || numPages === 0) return
|
||||
|
||||
let cancelled = false
|
||||
let renderTask: RenderTask | undefined
|
||||
let exitTimeout: ReturnType<typeof setTimeout> | undefined
|
||||
|
||||
function wait(ms: number) {
|
||||
return new Promise<void>((resolve) => {
|
||||
exitTimeout = setTimeout(resolve, ms)
|
||||
})
|
||||
}
|
||||
|
||||
async function render(pdf: PDFDocumentProxy, canvas: HTMLCanvasElement, container: HTMLDivElement) {
|
||||
const direction = directionRef.current
|
||||
setIsBusy(true)
|
||||
|
||||
if (hasRenderedOnceRef.current) {
|
||||
canvas.style.transition = 'opacity 0.18s ease, transform 0.18s ease'
|
||||
canvas.style.opacity = '0'
|
||||
canvas.style.transform = `translateX(${direction * -2}rem)`
|
||||
await wait(180)
|
||||
if (cancelled) return
|
||||
}
|
||||
|
||||
try {
|
||||
const page = await pdf.getPage(pageNumber)
|
||||
if (cancelled) return
|
||||
const unscaledViewport = page.getViewport({ scale: 1 })
|
||||
const scale = container.clientWidth / unscaledViewport.width
|
||||
const viewport = page.getViewport({ scale })
|
||||
|
||||
const pixelRatio = window.devicePixelRatio || 1
|
||||
canvas.width = viewport.width * pixelRatio
|
||||
canvas.height = viewport.height * pixelRatio
|
||||
canvas.style.width = `${viewport.width}px`
|
||||
canvas.style.height = `${viewport.height}px`
|
||||
|
||||
const context = canvas.getContext('2d')
|
||||
if (!context) return
|
||||
context.scale(pixelRatio, pixelRatio)
|
||||
|
||||
renderTask = page.render({ canvasContext: context, viewport, canvas })
|
||||
await renderTask.promise
|
||||
if (cancelled) return
|
||||
|
||||
// Jump to the entry offset with transitions off, then force the browser to
|
||||
// commit that position before re-enabling them — otherwise the two style
|
||||
// writes land in the same frame and the entry animation never paints.
|
||||
canvas.style.transition = 'none'
|
||||
canvas.style.transform = `translateX(${direction * 2}rem)`
|
||||
canvas.offsetHeight
|
||||
canvas.style.transition = 'opacity 0.22s ease, transform 0.22s ease'
|
||||
canvas.style.opacity = '1'
|
||||
canvas.style.transform = 'translateX(0)'
|
||||
hasRenderedOnceRef.current = true
|
||||
} catch (err) {
|
||||
if (!cancelled) {
|
||||
console.error('PdfMenu failed to render page', err)
|
||||
setError(true)
|
||||
}
|
||||
} finally {
|
||||
if (!cancelled) setIsBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
render(pdf, canvas, container)
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
clearTimeout(exitTimeout)
|
||||
renderTask?.cancel()
|
||||
}
|
||||
}, [numPages, pageNumber])
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<p className="field-hint">
|
||||
Die Speisekarte konnte nicht geladen werden.{' '}
|
||||
<a href={src} target="_blank" rel="noopener noreferrer">
|
||||
Als PDF öffnen
|
||||
</a>
|
||||
.
|
||||
</p>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="pdf-menu" ref={containerRef}>
|
||||
<div className="pdf-menu-viewport">
|
||||
<canvas className="pdf-menu-page" ref={canvasRef} />
|
||||
{numPages === 0 && (
|
||||
<div className="pdf-menu-loading" role="status">
|
||||
<div className="pdf-menu-spinner" aria-hidden="true" />
|
||||
<p>Die Speisekarte wird aufgetischt …</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{numPages > 1 && (
|
||||
<div className="pdf-menu-pagination">
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-secondary"
|
||||
onClick={() => goToPage(pageNumber - 1)}
|
||||
disabled={isBusy || pageNumber <= 1}
|
||||
>
|
||||
Zurück
|
||||
</button>
|
||||
<span>
|
||||
Seite {pageNumber} von {numPages}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-secondary"
|
||||
onClick={() => goToPage(pageNumber + 1)}
|
||||
disabled={isBusy || pageNumber >= numPages}
|
||||
>
|
||||
Weiter
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
<p className="field-hint">
|
||||
Speisekarte nicht geladen?{' '}
|
||||
<a href={src} target="_blank" rel="noopener noreferrer">
|
||||
Klick hier
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState } from 'react'
|
||||
import { createFileRoute, useRouter } from '@tanstack/react-router'
|
||||
|
||||
import { PdfMenu } from '#/components/pdf-menu'
|
||||
import { ReservationConfirmedModal } from '#/components/reservation-confirmed-modal'
|
||||
import { ReservationDateTimeField } from '#/components/reservation-datetime-field'
|
||||
import { SiteFooter } from '#/components/site-footer'
|
||||
@@ -227,6 +228,16 @@ function Home() {
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="section" id="speisekarte">
|
||||
<div className="section-grid">
|
||||
<div>
|
||||
<span className="section-kicker">Was uns schmeckt</span>
|
||||
<h2 className="section-heading">Speisekarte</h2>
|
||||
</div>
|
||||
<PdfMenu src="/menu.pdf" />
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="section" id="reservieren">
|
||||
<div className="section-grid">
|
||||
<div>
|
||||
|
||||
@@ -1508,6 +1508,83 @@ a {
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.pdf-menu {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.pdf-menu-viewport {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
border-radius: 0.4rem;
|
||||
border: 1px solid var(--line);
|
||||
background: #fff;
|
||||
box-shadow: 0 0.5rem 1.5rem rgb(0 0 0 / 25%);
|
||||
}
|
||||
|
||||
.pdf-menu-loading {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
width: 90%;
|
||||
max-width: 20rem;
|
||||
transform: translate(-50%, -50%);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
opacity: 0;
|
||||
animation: pdf-menu-spinner-fade-in 0.15s ease 0.15s forwards;
|
||||
}
|
||||
|
||||
.pdf-menu-loading p {
|
||||
margin: 0;
|
||||
text-align: center;
|
||||
font-family: 'Fraunces', Georgia, serif;
|
||||
font-style: italic;
|
||||
color: #6b5f4d;
|
||||
}
|
||||
|
||||
.pdf-menu-spinner {
|
||||
width: 2.5rem;
|
||||
height: 2.5rem;
|
||||
border-radius: 50%;
|
||||
border: 3px solid var(--line);
|
||||
border-top-color: var(--gold);
|
||||
animation: pdf-menu-spin 0.8s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes pdf-menu-spin {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes pdf-menu-spinner-fade-in {
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.pdf-menu-page {
|
||||
display: block;
|
||||
width: 100%;
|
||||
max-width: 100%;
|
||||
aspect-ratio: 210 / 297;
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.pdf-menu-pagination {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
/* A classic map-pin: a square with three rounded corners (50% 50% 50% 0 leaves
|
||||
the bottom-left one sharp), rotated -45deg around that exact sharp corner so
|
||||
the corner itself — not the box center — lands on the `left`/`top` coordinate
|
||||
|
||||
Reference in New Issue
Block a user