feat: 쇼츠를 모달 대신 페이지(/shorts/영상ID)로 — 넘기면 주소도 바뀌어 공유·뒤로 가기 가능, 헤더·탭 바 사이를 채우고 배경은 지금 영상 표지를 흐리게 깔기

This commit is contained in:
minaChoi committed 2026-10-26 15:08:18 +09:00
1 parent 9adee6762e
commit fa99ea4f08
2 files changed
+77 -23

No files matched your search

+2
View File
@@ -3,6 +3,7 @@ import { BLayout } from './b/BLayout'
import { FoliagePage } from './b/FoliagePage'
import { GakPage } from './b/GakPage'
import { SeasonPage } from './b/SeasonPage'
import { ShortsPage } from './b/Shorts'
import { SkyPage } from './b/SkyPage'
import { TalkPage } from './b/TalkPage'
import { TodayPage } from './b/TodayPage'
@@ -22,6 +23,7 @@ export default function App() {
<Route path="season" element={<SeasonPage />} />
<Route path="foliage" element={<FoliagePage />} />
<Route path="talk" element={<TalkPage />} />
<Route path="shorts/:id?" element={<ShortsPage />} />
<Route path="*" element={<TodayPage />} />
</Route>
</Routes>
+75 -23
View File
@@ -1,5 +1,6 @@
import { useQuery } from '@tanstack/react-query'
import { useEffect, useRef, useState } from 'react'
import { type CSSProperties, useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from 'react'
import { Link, useNavigate, useParams } from 'react-router'
import { listShortsOptions } from '../api/client/@tanstack/react-query.gen'
import type { ShortItem } from '../api/client'
import { cx } from '../lib/utils'
@@ -11,8 +12,21 @@ const CARD_W = 120
const PLAYER_W = 200
const SCALE = (CARD_W / PLAYER_W) * 1.4
// 전체 화면 창의 영상 칸: 닫기 줄 아래 남은 높이와 화면 너비 안에 9:16으로 맞춘다
const FIT = { width: 'min(100vw, calc((100dvh - env(safe-area-inset-top, 0px) - 52px) * 9 / 16))', aspectRatio: '9 / 16' }
// 쇼츠 페이지의 영상 칸: 위 헤더(54px)·버튼 줄(48px)과 아래 탭 바(--shorts-tab) 사이 높이와 화면 너비 안에 9:16으로 맞춘다
const FIT = { width: 'min(100vw, calc((100dvh - env(safe-area-inset-top, 0px) - 54px - 48px - var(--shorts-tab, 0px)) * 9 / 16))', aspectRatio: '9 / 16' }
/** 아래 탭 바 높이 (기기마다 안전 영역이 달라 실제로 잰다) */
function useTabBarHeight() {
const [h, setH] = useState(0)
useLayoutEffect(() => {
const nav = document.querySelector<HTMLElement>('nav.b-tabbar')
if (!nav) return
const ro = new ResizeObserver(() => setH(nav.offsetHeight))
ro.observe(nav)
return () => ro.disconnect()
}, [])
return h
}
const YT_ORIGIN = 'https://www.youtube-nocookie.com'
@@ -46,12 +60,11 @@ function useVisible<T extends Element>(threshold = 0.5) {
return [ref, visible] as const
}
/** 오늘 화면 히어로 아래 쇼츠 줄: 첫 카드만 소리 없이 재생, 누르면 전체 화면에서 위아래로 넘겨 본다 */
/** 오늘 화면 히어로 아래 쇼츠 줄: 첫 카드만 소리 없이 재생, 누르면 쇼츠 페이지(/shorts/영상ID)에서 위아래로 넘겨 본다 */
export function ShortsRail() {
const q = useQuery(listShortsOptions())
const items = q.data?.items ?? []
const [ref, visible] = useVisible<HTMLElement>()
const [openAt, setOpenAt] = useState<number | null>(null)
if (q.isPending) {
return (
@@ -72,17 +85,16 @@ export function ShortsRail() {
<Sec title="날씨각 쇼츠" />
<Rail label="쇼츠 목록">
{items.map((s, i) => (
<ShortCard key={s.id} s={s} playing={i === 0 && visible && openAt == null} onOpen={() => setOpenAt(i)} />
<ShortCard key={s.id} s={s} playing={i === 0 && visible} />
))}
</Rail>
{openAt != null && <ShortsViewer items={items} start={openAt} onClose={() => setOpenAt(null)} />}
</section>
)
}
function ShortCard({ s, playing, onOpen }: { s: ShortItem; playing: boolean; onOpen: () => void }) {
function ShortCard({ s, playing }: { s: ShortItem; playing: boolean }) {
return (
<button type="button" onClick={onOpen} aria-label={`${s.title} 보기`} className="relative aspect-[9/16] w-[120px] cursor-pointer overflow-hidden rounded-xl bg-[#1d2233] p-0 text-left text-white">
<Link to={`/shorts/${s.id}`} aria-label={`${s.title} 보기`} className="relative block aspect-[9/16] w-[120px] cursor-pointer overflow-hidden rounded-xl bg-[#1d2233] p-0 text-left text-white">
<img src={`https://i.ytimg.com/vi/${s.id}/hqdefault.jpg`} alt="" loading="lazy" className="absolute inset-0 size-full object-cover" />
{playing && (
<iframe
@@ -96,37 +108,60 @@ function ShortCard({ s, playing, onOpen }: { s: ShortItem; playing: boolean; onO
)}
<span className="absolute inset-0 bg-[linear-gradient(180deg,transparent_55%,rgba(0,0,0,.65))]" aria-hidden="true" />
<span className="absolute inset-x-2 bottom-2 line-clamp-3 text-[12px] leading-[1.3] font-bold">{s.title}</span>
</button>
</Link>
)
}
/** 쇼츠 페이지(/shorts/영상ID): 그 영상부터 위아래로 넘겨 보고, 넘기면 주소도 그 영상으로 바뀌어 그대로 공유할 수 있다 */
export function ShortsPage() {
const { id } = useParams()
const navigate = useNavigate()
const q = useQuery(listShortsOptions())
// 처음 들어온 영상만 시작점으로 쓴다 (넘기며 주소가 바뀌어도 다시 맞추지 않는다)
const [firstId] = useState(id)
const listed = q.data?.items ?? []
const items = firstId && listed.length && !listed.some((s) => s.id === firstId) ? [{ id: firstId, title: '' }, ...listed] : listed
const start = Math.max(0, items.findIndex((s) => s.id === firstId))
// 앱 안에서 들어왔으면 뒤로, 링크로 바로 들어왔으면 오늘 화면으로
const close = () => ((window.history.state as { idx?: number } | null)?.idx ? navigate(-1) : navigate('/', { replace: true }))
if (!items.length) return <div className="fixed inset-0 z-[70] bg-[#1d2233]" aria-busy={q.isPending} />
return <ShortsViewer items={items} start={start} onClose={close} onAt={(s) => navigate(`/shorts/${s.id}`, { replace: true })} />
}
/**
* 전체 화면 재생 창: 네이티브 <dialog>라 ESC·포커스 가두기가 기본.
* 처음 열 때·넘길 때 검은 화면이 보이지 않게, 첫 장면을 받을 때까지는 표지를 보이고 앞뒤 영상도 소리 없이 틀어 첫 장면을 받아 둔 뒤 멈춰 두고, 보이는 것만 처음부터 재생한다
*/
function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: number; onClose: () => void }) {
const dialog = useRef<HTMLDialogElement>(null)
function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; start: number; onClose: () => void; onAt: (s: ShortItem) => void }) {
const list = useRef<HTMLDivElement>(null)
const [at, setAt] = useState(start)
const [sound, setSound] = useState(false)
const tabH = useTabBarHeight()
// 첫 장면을 받은 플레이어. 그 전엔 플레이어의 검은 화면 대신 표지를 보인다
const [ready, setReady] = useState<ReadonlySet<number>>(new Set())
const frames = useRef(new Map<number, HTMLIFrameElement>())
const atRef = useRef(start)
const soundRef = useRef(false)
// 처음 한 번: 시작 영상으로 맞추고, 뒤 페이지 스크롤을 잠근다
const startRef = useRef(start)
useEffect(() => {
const el = dialog.current
if (!el) return
el.showModal()
list.current?.children[start]?.scrollIntoView({ block: 'start' })
// scrollIntoView 는 바깥 칸까지 굴려 버튼 줄이 헤더 밑으로 숨으므로 목록만 옮긴다
const box = list.current
if (box) box.scrollTop = box.clientHeight * startRef.current
const root = document.documentElement
const before = root.style.overflow
root.style.overflow = 'hidden'
return () => {
root.style.overflow = before
}
}, [start])
}, [])
const onEscape = useEffectEvent((e: KeyboardEvent) => e.key === 'Escape' && onClose())
useEffect(() => {
window.addEventListener('keydown', onEscape)
return () => window.removeEventListener('keydown', onEscape)
}, [])
useEffect(() => {
const box = list.current
@@ -164,7 +199,9 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu
return () => window.removeEventListener('message', onMessage)
}, [])
const moved = useEffectEvent((s: ShortItem) => onAt(s))
useEffect(() => {
if (atRef.current !== at) moved(items[at])
atRef.current = at
for (const [i, f] of frames.current) {
if (i === at) {
@@ -175,7 +212,7 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu
command(f, 'mute')
}
}
}, [at])
}, [at, items])
const toggleSound = () => {
const on = !sound
@@ -185,8 +222,23 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu
}
return (
<dialog ref={dialog} aria-label="날씨각 쇼츠" onClose={onClose} className="fixed inset-0 m-0 size-full max-h-none max-w-none bg-black p-0 text-white">
<div className="flex h-[calc(env(safe-area-inset-top,0px)+52px)] items-end justify-between px-3 pb-2">
// 헤더·탭 바는 그대로 두고 그 사이를 채운다. 흐린 배경은 반투명 헤더·탭 바 뒤까지 깐다
<div
role="region"
aria-label="날씨각 쇼츠"
className="fixed inset-0 flex flex-col overflow-hidden bg-[#1d2233] pt-[calc(54px+env(safe-area-inset-top,0px))] pb-(--shorts-tab) text-white"
style={{ '--shorts-tab': `${tabH}px` } as CSSProperties}
>
{/* 배경: 지금 영상 표지를 크게 흐려 깔아 영상 색이 번져 보이게 */}
<img
key={items[at].id}
src={`https://i.ytimg.com/vi/${items[at].id}/hqdefault.jpg`}
alt=""
aria-hidden="true"
className="absolute inset-0 size-full scale-125 object-cover opacity-70 blur-3xl"
/>
<div className="absolute inset-0 bg-black/35" aria-hidden="true" />
<div className="relative mx-auto flex h-12 w-full max-w-[440px] shrink-0 items-center justify-between px-3">
<button type="button" onClick={onClose} aria-label="닫기" className={cx('relative grid size-9 cursor-pointer place-items-center rounded-full bg-white/15 text-xl text-white', TAP44)}>
×
</button>
@@ -194,7 +246,7 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu
{sound ? '소리 끄기' : '소리 켜기'}
</button>
</div>
<div ref={list} className="h-[calc(100dvh-env(safe-area-inset-top,0px)-52px)] snap-y snap-mandatory overflow-y-auto [scrollbar-width:none]">
<div ref={list} className="relative min-h-0 flex-1 snap-y snap-mandatory overflow-y-auto [scrollbar-width:none]">
{items.map((s, i) => (
<div key={s.id} data-i={i} className="relative grid h-full snap-start place-items-center">
<div className="relative" style={FIT}>
@@ -226,6 +278,6 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu
</div>
))}
</div>
</dialog>
</div>
)
}