Просмотр исходного кода

feat: fill the writing card with as many posts as fit

The card listed a fixed five posts, which left a gap at the bottom on
phones. It now shows as many whole rows as the card has room for (five
to eight on phones, three or four on wider screens), and the rows share
the spare height so the list ends at the card's inset. Hidden rows leave
the tab order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QEPoSHwHCYMNpm2iKxY9hL
Claude 1 месяц назад
Родитель
Сommit
9099e9794d
2 измененных файлов с 70 добавлено и 13 удалено
  1. 48 0
      components/cards/fit-list.tsx
  2. 22 13
      components/cards/writing.tsx

+ 48 - 0
components/cards/fit-list.tsx

@@ -0,0 +1,48 @@
+"use client";
+
+import { useEffect, useRef, type ReactNode } from "react";
+
+import { cn } from "@/lib/utils";
+
+/**
+ * A list that shows only the rows that fit its height, so a card never ends
+ * on half a row or on a gap. Rows that don't fit are hidden outright, which
+ * also takes their links out of the tab order.
+ */
+export function FitList({
+  className,
+  children,
+}: {
+  className?: string;
+  children: ReactNode;
+}) {
+  const ref = useRef<HTMLUListElement>(null);
+
+  useEffect(() => {
+    const list = ref.current;
+
+    if (!list) return;
+    const fit = () => {
+      const rows = [...list.children] as HTMLElement[];
+
+      rows.forEach((row) => (row.hidden = false));
+      const limit = list.clientHeight + 0.5;
+
+      rows.forEach((row) => {
+        row.hidden = row.offsetTop + row.offsetHeight > limit;
+      });
+    };
+    const observer = new ResizeObserver(fit);
+
+    fit();
+    observer.observe(list);
+
+    return () => observer.disconnect();
+  }, []);
+
+  return (
+    <ul ref={ref} className={cn("relative overflow-hidden", className)}>
+      {children}
+    </ul>
+  );
+}

+ 22 - 13
components/cards/writing.tsx

@@ -1,5 +1,6 @@
 import Link from "next/link";
 
+import { FitList } from "./fit-list";
 import { Chevron } from "./ui";
 
 export type PostSummary = {
@@ -33,24 +34,32 @@ export function WritingCard({ posts }: { posts: PostSummary[] }) {
           <Chevron />
         </Link>
       </div>
-      <ul className="mt-2 flex flex-1 flex-col justify-start divide-y divide-line sm:justify-center">
-        {posts.slice(0, 5).map((p, i) => (
-          <li key={p.id} className={i >= 3 ? "sm:hidden" : undefined}>
+      {/* As many posts as the card has room for; the rows that show share out
+          the spare height, so the list always ends at the card's inset. Rows
+          never shrink, so FitList can see which ones overflow. */}
+      <FitList className="mt-2 flex min-h-0 flex-1 flex-col">
+        {posts.slice(0, 10).map((p) => (
+          <li
+            key={p.id}
+            className="flex flex-[1_0_auto] flex-col border-t border-line first:border-t-0"
+          >
             <Link
-              className="group flex items-baseline justify-between gap-4 py-2.5"
+              className="group flex flex-1 items-center py-2.5"
               data-cursor="Read"
               href={`/blog/${p.id}`}
               prefetch={false}
             >
-              <span className="truncate text-[15px] font-medium transition-colors group-hover:text-glacier">
-                {p.title}
+              <span className="flex w-full min-w-0 items-baseline justify-between gap-4">
+                <span className="truncate text-[15px] font-medium transition-colors group-hover:text-glacier">
+                  {p.title}
+                </span>
+                <time
+                  className="shrink-0 text-[13px] text-muted tabular-nums"
+                  dateTime={p.createdAt ?? undefined}
+                >
+                  {fmt(p.createdAt)}
+                </time>
               </span>
-              <time
-                className="shrink-0 text-[13px] text-muted tabular-nums"
-                dateTime={p.createdAt ?? undefined}
-              >
-                {fmt(p.createdAt)}
-              </time>
             </Link>
           </li>
         ))}
@@ -61,7 +70,7 @@ export function WritingCard({ posts }: { posts: PostSummary[] }) {
             </Link>
           </li>
         )}
-      </ul>
+      </FitList>
     </div>
   );
 }