3syisgbyq: Add a reusable bottom sheet with the comments and composer sheets on the phone feed
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
"use client";
|
||||
|
||||
// Bottom sheet (spec "Sheets"): slides up over a dimmed, blurred backdrop with a
|
||||
// grab handle, a serif title and a close button. Reused by the comments,
|
||||
// composer and Discover sheets. Styles live in src/styles/sheet.css.
|
||||
import { useEffect, useId, useRef, type ReactNode } from "react";
|
||||
|
||||
const FOCUSABLE = 'button:not(:disabled), a[href], input:not(:disabled), textarea:not(:disabled), select:not(:disabled), [tabindex]:not([tabindex="-1"])';
|
||||
|
||||
export interface BottomSheetProps {
|
||||
title: string;
|
||||
onClose: () => void;
|
||||
children: ReactNode;
|
||||
/** Extra class on the sheet, for per-sheet sizing. */
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function BottomSheet({ title, onClose, children, className }: BottomSheetProps) {
|
||||
const sheet = useRef<HTMLElement | null>(null);
|
||||
const titleId = useId();
|
||||
const closeRef = useRef(onClose);
|
||||
closeRef.current = onClose;
|
||||
|
||||
useEffect(() => {
|
||||
const el = sheet.current;
|
||||
if (!el) return;
|
||||
const opener = document.activeElement as HTMLElement | null;
|
||||
// Focus moves into the sheet: its first field, else the sheet itself.
|
||||
const first = el.querySelector<HTMLElement>("input, textarea") ?? el.querySelector<HTMLElement>(FOCUSABLE);
|
||||
(first ?? el).focus({ preventScroll: true });
|
||||
|
||||
const prevOverflow = document.body.style.overflow;
|
||||
document.body.style.overflow = "hidden";
|
||||
|
||||
function onKey(e: KeyboardEvent) {
|
||||
if (e.key === "Escape") {
|
||||
// An open emoji popover closes first; it handles Escape itself.
|
||||
if (el?.querySelector('[aria-haspopup="dialog"][aria-expanded="true"]')) return;
|
||||
e.preventDefault();
|
||||
closeRef.current();
|
||||
return;
|
||||
}
|
||||
if (e.key !== "Tab" || !el) return;
|
||||
const items = [...el.querySelectorAll<HTMLElement>(FOCUSABLE)].filter((n) => n.offsetParent !== null || n === document.activeElement);
|
||||
if (!items.length) return;
|
||||
const a = items[0];
|
||||
const z = items[items.length - 1];
|
||||
if (e.shiftKey && (document.activeElement === a || document.activeElement === el)) {
|
||||
e.preventDefault();
|
||||
z.focus();
|
||||
} else if (!e.shiftKey && document.activeElement === z) {
|
||||
e.preventDefault();
|
||||
a.focus();
|
||||
}
|
||||
}
|
||||
document.addEventListener("keydown", onKey);
|
||||
return () => {
|
||||
document.removeEventListener("keydown", onKey);
|
||||
document.body.style.overflow = prevOverflow;
|
||||
// Focus returns to the opener (when it is still on the page).
|
||||
if (opener && opener.isConnected) opener.focus({ preventScroll: true });
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
<button type="button" className="sheet-bg" aria-label="Close" tabIndex={-1} onClick={onClose} data-testid="sheet-backdrop" />
|
||||
<section
|
||||
ref={sheet}
|
||||
className={`sheet${className ? ` ${className}` : ""}`}
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby={titleId}
|
||||
tabIndex={-1}
|
||||
data-testid="bottom-sheet"
|
||||
>
|
||||
<span className="sheet-grab" aria-hidden="true" />
|
||||
<div className="sheet-h">
|
||||
<h2 id={titleId} className="type-title type-title-strong">{title}</h2>
|
||||
<button type="button" className="sheet-x" aria-label="Close" onClick={onClose}>
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
|
||||
<path d="M6 6l12 12M18 6 6 18" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
<div className="sheet-body">{children}</div>
|
||||
</section>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState, type MouseEvent, type Ref } from "react";
|
||||
import { useCallback, useEffect, useRef, useState, type MouseEvent, type Ref } from "react";
|
||||
import { canDelete, formatDuration, isPlayableVideo, relativeTime, type CardMedia, type CardPost } from "@/lib/feed-model";
|
||||
import { GrowButton } from "./Buttons";
|
||||
import { HexAvatar } from "./HexAvatar";
|
||||
@@ -8,6 +8,9 @@ import { PlayIcon, PostMenu } from "./PostCard";
|
||||
import { VideoPlayer } from "./VideoPlayer";
|
||||
import { FeaturedCarousel } from "./FeaturedCarousel";
|
||||
import { MobileDock } from "./MobileDock";
|
||||
import { BottomSheet } from "./BottomSheet";
|
||||
import { CommentsSection } from "./CommentsSection";
|
||||
import { Composer } from "./Composer";
|
||||
import type { DiscoverExpert } from "@/lib/discover";
|
||||
import type { FeedViewProps } from "./FeedView";
|
||||
|
||||
@@ -278,6 +281,17 @@ function LoadSlide({ index, current, sentinelRef }: { index: number; current: bo
|
||||
export function MobileFeed(p: FeedViewProps) {
|
||||
const reel = useRef<HTMLDivElement | null>(null);
|
||||
const [current, setCurrent] = useState(0);
|
||||
// The open sheet: the comments of one post, the composer, or none.
|
||||
const [sheet, setSheet] = useState<{ kind: "comments"; postId: string } | { kind: "composer" } | null>(null);
|
||||
const closeSheet = useCallback(() => setSheet(null), []);
|
||||
// A successful post closes the composer: publishing ends with no error and an emptied draft.
|
||||
const wasBusy = useRef(false);
|
||||
const composerBusy = !!p.composer?.busy;
|
||||
const composerClear = !!p.composer && !p.composer.error && !p.composer.value && !p.composer.file;
|
||||
useEffect(() => {
|
||||
if (wasBusy.current && !composerBusy && composerClear) setSheet((s) => (s?.kind === "composer" ? null : s));
|
||||
wasBusy.current = composerBusy;
|
||||
}, [composerBusy, composerClear]);
|
||||
const onCurrentChange = p.onCurrentChange;
|
||||
const showLoad = p.status === "loading" || (p.status === "ready" && !p.loadMoreFailed && (p.loadingMore || !!p.hasMore));
|
||||
const experts = p.featuredExperts ?? [];
|
||||
@@ -421,11 +435,27 @@ export function MobileFeed(p: FeedViewProps) {
|
||||
slide={{ index, kind, post: currentPost }}
|
||||
onToggleLike={p.onToggleLike}
|
||||
likeBusy={currentPost ? p.likeBusy?.has(currentPost.key) : false}
|
||||
onOpenComments={p.onOpenComments}
|
||||
onOpenComposer={p.onOpenComposer}
|
||||
onOpenComments={(postId) => {
|
||||
setSheet({ kind: "comments", postId });
|
||||
p.onOpenComments?.(postId);
|
||||
}}
|
||||
onOpenComposer={() => {
|
||||
setSheet({ kind: "composer" });
|
||||
p.onOpenComposer?.();
|
||||
}}
|
||||
onOpenMore={p.onOpenMore}
|
||||
/>
|
||||
) : null}
|
||||
{sheet?.kind === "comments" ? (
|
||||
<BottomSheet title="Comments" onClose={closeSheet} className="sheet-comments">
|
||||
<CommentsSection postId={sheet.postId} onSignedOut={p.onSignedOut} />
|
||||
</BottomSheet>
|
||||
) : null}
|
||||
{sheet?.kind === "composer" && p.composer ? (
|
||||
<BottomSheet title="New post" onClose={closeSheet} className="sheet-composer">
|
||||
<Composer {...p.composer} />
|
||||
</BottomSheet>
|
||||
) : null}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user