Newsletter: a handle between the parts and the preview sets their widths
Drag it, or focus it and use the arrow keys, Home and End; 320px (a phone's view of the email) up to all but 360px of the parts list. Remembered in the browser. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
00170ca912
commit
3b32e2114d
1 changed files with 97 additions and 5 deletions
|
|
@ -20,8 +20,8 @@
|
|||
email is read far from this admin.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { Article, Check, Copy, DownloadSimple, FloppyDisk, Trash } from '@phosphor-icons/react'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { Article, Check, Copy, DotsSixVertical, DownloadSimple, FloppyDisk, Trash } from '@phosphor-icons/react'
|
||||
import { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
|
||||
import { ApiError, del, get, patch, post } from '../../lib/api.ts'
|
||||
|
|
@ -496,6 +496,7 @@ export default function AdminNewsletter() {
|
|||
}
|
||||
|
||||
const empty = parts.length === 0
|
||||
const split = usePreviewWidth()
|
||||
|
||||
return (
|
||||
<div>
|
||||
|
|
@ -543,8 +544,13 @@ export default function AdminNewsletter() {
|
|||
)}
|
||||
</div>
|
||||
|
||||
{/* The editor takes what the preview (the email's 600px and its margin) leaves. */}
|
||||
<div className="mt-6 grid gap-8 lg:grid-cols-[minmax(0,1fr)_minmax(0,42rem)]">
|
||||
{/* The editor takes what the preview leaves; the handle between
|
||||
them sets the preview's width (from lg up). */}
|
||||
<div
|
||||
ref={split.ref}
|
||||
className="mt-6 grid gap-8 lg:grid-cols-[minmax(0,1fr)_auto_var(--preview-width)] lg:gap-3"
|
||||
style={{ '--preview-width': `${split.width}px` } as React.CSSProperties}
|
||||
>
|
||||
<section className="border-l-4 border-brand bg-raised p-5 shadow-sm">
|
||||
<Repeater spec={PARTS_SPEC} rows={parts} options={pickOptions} errors={{}} errorPrefix="parts." className="" onChange={setParts} />
|
||||
<p className="mt-3 text-xs" style={{ color: MUTED }}>
|
||||
|
|
@ -552,7 +558,25 @@ export default function AdminNewsletter() {
|
|||
</p>
|
||||
</section>
|
||||
|
||||
<div>
|
||||
<div
|
||||
role="separator"
|
||||
aria-orientation="vertical"
|
||||
aria-label="Resize the parts and the preview"
|
||||
aria-valuemin={PREVIEW_MIN}
|
||||
aria-valuemax={split.max}
|
||||
aria-valuenow={split.width}
|
||||
tabIndex={0}
|
||||
onPointerDown={split.start}
|
||||
onPointerMove={split.drag}
|
||||
onPointerUp={split.end}
|
||||
onPointerCancel={split.end}
|
||||
onKeyDown={split.key}
|
||||
className="hidden cursor-col-resize touch-none items-start justify-center self-stretch border-x border-brand/20 bg-tint-soft px-0.5 pt-6 text-muted hover:bg-tint hover:text-link focus-visible:bg-tint focus-visible:text-link lg:flex"
|
||||
>
|
||||
<DotsSixVertical size={16} weight="bold" aria-hidden="true" className="lg:sticky lg:top-1/2" />
|
||||
</div>
|
||||
|
||||
<div className="min-w-0">
|
||||
<div className="flex flex-wrap gap-3">
|
||||
<button type="button" onClick={() => copy('html')} className={button} disabled={empty}>
|
||||
{copied === 'html' ? <Check weight="bold" aria-hidden="true" /> : <Copy weight="bold" aria-hidden="true" />}
|
||||
|
|
@ -581,3 +605,71 @@ export default function AdminNewsletter() {
|
|||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/* ── The handle between the parts and the preview ─────────────
|
||||
Drag it, or focus it and use the arrow keys (Home and End for
|
||||
the narrowest and widest). Under the email's 600px the preview
|
||||
shows it as a phone would. The width is remembered in this
|
||||
browser, a convenience only. */
|
||||
|
||||
const PREVIEW_MIN = 320
|
||||
const PARTS_MIN = 360 // what the parts list keeps, at least
|
||||
const PREVIEW_DEFAULT = 672
|
||||
const PREVIEW_KEY = 'site-newsletter-preview-width'
|
||||
const STEP = 32
|
||||
|
||||
function usePreviewWidth() {
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
const [room, setRoom] = useState(0)
|
||||
const [wanted, setWanted] = useState(() => {
|
||||
try {
|
||||
const saved = Number(window.localStorage.getItem(PREVIEW_KEY))
|
||||
return saved >= PREVIEW_MIN ? saved : PREVIEW_DEFAULT
|
||||
} catch {
|
||||
return PREVIEW_DEFAULT
|
||||
}
|
||||
})
|
||||
|
||||
// The row's width, so the widest the preview can be follows the window.
|
||||
useEffect(() => {
|
||||
const row = ref.current
|
||||
if (!row) return undefined
|
||||
const observer = new ResizeObserver(() => setRoom(row.clientWidth))
|
||||
observer.observe(row)
|
||||
return () => observer.disconnect()
|
||||
}, [])
|
||||
|
||||
const max = Math.max(PREVIEW_MIN, room ? room - PARTS_MIN : PREVIEW_DEFAULT)
|
||||
const width = Math.min(max, Math.max(PREVIEW_MIN, wanted))
|
||||
|
||||
const set = (next: number) => {
|
||||
const clamped = Math.round(Math.min(max, Math.max(PREVIEW_MIN, next)))
|
||||
setWanted(clamped)
|
||||
try {
|
||||
window.localStorage.setItem(PREVIEW_KEY, String(clamped))
|
||||
} catch {
|
||||
// Storage off: the width lasts until the page is left.
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ref,
|
||||
width,
|
||||
max,
|
||||
start: (e: React.PointerEvent<HTMLDivElement>) => e.currentTarget.setPointerCapture(e.pointerId),
|
||||
drag: (e: React.PointerEvent<HTMLDivElement>) => {
|
||||
if (!e.currentTarget.hasPointerCapture(e.pointerId) || !ref.current) return
|
||||
set(ref.current.getBoundingClientRect().right - e.clientX - e.currentTarget.offsetWidth / 2)
|
||||
},
|
||||
end: (e: React.PointerEvent<HTMLDivElement>) => {
|
||||
if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId)
|
||||
},
|
||||
key: (e: React.KeyboardEvent<HTMLDivElement>) => {
|
||||
// Left widens the preview (the handle moves left), Right narrows it.
|
||||
const next = { ArrowLeft: width + STEP, ArrowRight: width - STEP, Home: PREVIEW_MIN, End: max }[e.key]
|
||||
if (next === undefined) return
|
||||
e.preventDefault()
|
||||
set(next)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue