Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

feat(ui): fill the library page and its thumbnails

Luis Ortiz65c0a999969f
diff --git a/packages/ui/src/guide/components/ScreenshotView.tsx b/packages/ui/src/guide/components/ScreenshotView.tsx
index e91bda9..8ab2e07 100644
--- a/packages/ui/src/guide/components/ScreenshotView.tsx
+++ b/packages/ui/src/guide/components/ScreenshotView.tsx
@@ -31,6 +31,7 @@ interface ScreenshotViewProps {
animate?: boolean;
crop?: boolean;
frameRatio?: number;
+ cover?: boolean;
readOnly?: boolean;
cache?: boolean;
onOpenEditor?: (tool: 'annotate' | 'redact' | 'crop' | 'target') => void;
@@ -63,6 +64,7 @@ export function ScreenshotView({
animate = false,
crop = false,
frameRatio,
+ cover = false,
readOnly = false,
cache = false,
onOpenEditor,
@@ -305,7 +307,10 @@ export function ScreenshotView({
if (!fullUrl) {
return (
- <div className={`rounded-lg bg-secondary p-4 flex flex-col gap-2.5 ${className}`} style={{ aspectRatio: ratio }}>
+ <div
+ className={`rounded-lg bg-secondary p-4 flex flex-col gap-2.5 ${cover ? 'h-full' : ''} ${className}`}
+ style={cover ? undefined : { aspectRatio: ratio }}
+ >
<div className="h-7 rounded-md bg-border/60 animate-pulse" />
<div className="flex-1 flex gap-3">
<div className="w-[30%] flex flex-col gap-2">
@@ -345,15 +350,18 @@ export function ScreenshotView({
const vpRatio = displayedViewport.width / displayedViewport.height;
const letterbox =
- frameRatio !== undefined && Math.abs(vpRatio - frameRatio) > FRAME_RATIO_EPSILON ? frameRatio : undefined;
+ !cover && frameRatio !== undefined && Math.abs(vpRatio - frameRatio) > FRAME_RATIO_EPSILON ? frameRatio : undefined;
+ const coverFit: React.CSSProperties | undefined = cover
+ ? { minWidth: '100%', minHeight: '100%', flex: 'none' }
+ : undefined;
const frameFit: React.CSSProperties | undefined =
letterbox === undefined ? undefined : vpRatio >= letterbox ? { width: '100%' } : { height: '100%' };
const frame = (
<div
data-screenshot-frame=""
- className={letterbox === undefined ? 'relative overflow-hidden w-full' : 'relative overflow-hidden'}
- style={{ aspectRatio: `${displayedViewport.width} / ${displayedViewport.height}`, ...frameFit }}
+ className={letterbox === undefined && !cover ? 'relative overflow-hidden w-full' : 'relative overflow-hidden'}
+ style={{ aspectRatio: `${displayedViewport.width} / ${displayedViewport.height}`, ...frameFit, ...coverFit }}
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerEnd}
@@ -370,7 +378,9 @@ export function ScreenshotView({
);
return (
- <div className={`relative overflow-hidden rounded-lg border border-border ${className}`}>
+ <div
+ className={`relative overflow-hidden rounded-lg border border-border ${cover ? 'h-full flex items-center justify-center' : ''} ${className}`}
+ >
{letterbox === undefined ? (
frame
) : (
diff --git a/packages/ui/src/library/components/GuideGridView.tsx b/packages/ui/src/library/components/GuideGridView.tsx
index 62b9ce6..41dfbcf 100644
--- a/packages/ui/src/library/components/GuideGridView.tsx
+++ b/packages/ui/src/library/components/GuideGridView.tsx
@@ -10,6 +10,7 @@ import { MimikEyes } from './MimikEyes';
interface GuideGridViewProps {
columns: number;
+ thumbHeight: number | null;
category: 'all' | 'starred' | 'trash';
onStar: (e: React.MouseEvent, id: string) => void;
onTrash: (e: React.MouseEvent, id: string) => void;
@@ -20,6 +21,7 @@ interface GuideGridViewProps {
export function GuideGridView({
columns,
+ thumbHeight,
category,
onStar,
onTrash,
@@ -44,15 +46,18 @@ export function GuideGridView({
onClick={() => navigate({ page: 'guide', guideId: guide.id })}
className="group rounded-xl bg-card cursor-pointer hover:shadow-md transition-shadow border border-border relative"
>
- <div className="relative aspect-video overflow-hidden rounded-t-xl border-b border-secondary">
+ <div
+ className={`relative overflow-hidden rounded-t-xl border-b border-secondary flex items-center ${thumbHeight === null ? 'aspect-video' : ''}`}
+ style={thumbHeight === null ? undefined : { height: thumbHeight }}
+ >
{thumb ? (
<ScreenshotView
screenshot={thumb}
alt={guide.title}
crop
cache
- frameRatio={16 / 9}
- className="!rounded-none !border-0"
+ cover
+ className="!rounded-none !border-0 w-full"
readOnly
/>
) : (
diff --git a/packages/ui/src/library/components/GuideListView.tsx b/packages/ui/src/library/components/GuideListView.tsx
index 564eec3..6631d81 100644
--- a/packages/ui/src/library/components/GuideListView.tsx
+++ b/packages/ui/src/library/components/GuideListView.tsx
@@ -50,8 +50,8 @@ export function GuideListView({
alt={guide.title}
crop
cache
- frameRatio={16 / 9}
- className="!rounded-none !border-0"
+ cover
+ className="!rounded-none !border-0 w-full"
readOnly
/>
) : (
diff --git a/packages/ui/src/library/components/LibraryContent.tsx b/packages/ui/src/library/components/LibraryContent.tsx
index 36f493f..f9896ac 100644
--- a/packages/ui/src/library/components/LibraryContent.tsx
+++ b/packages/ui/src/library/components/LibraryContent.tsx
@@ -77,7 +77,7 @@ export function LibraryContent({ category }: LibraryContentProps) {
const allGuidesRef = useRef<Guide[]>([]);
const rootRef = useRef<HTMLDivElement>(null);
- const { columns, pageSize } = usePageFit(rootRef, display);
+ const { columns, pageSize, thumbHeight } = usePageFit(rootRef, display);
const key = `${category}:${sort}`;
const shownRef = useRef({ page, pageKey });
shownRef.current = { page, pageKey };
@@ -243,6 +243,7 @@ export function LibraryContent({ category }: LibraryContentProps) {
) : (
<GuideGridView
columns={columns}
+ thumbHeight={thumbHeight}
category={category}
onStar={handleStar}
onTrash={handleTrash}
diff --git a/packages/ui/src/library/hooks/__tests__/use-page-fit.test.ts b/packages/ui/src/library/hooks/__tests__/use-page-fit.test.ts
index 9a5b647..e76315f 100644
--- a/packages/ui/src/library/hooks/__tests__/use-page-fit.test.ts
+++ b/packages/ui/src/library/hooks/__tests__/use-page-fit.test.ts
@@ -22,14 +22,18 @@ function libraryIn(width: number, height: number) {
}
describe('usePageFit', () => {
- it('fills a page with as many columns and rows as fit above the pager', () => {
- const grid = renderHook(() => usePageFit(libraryIn(1600, 1000), 'grid'));
- expect(grid.result.current).toEqual({ columns: 5, pageSize: 10 });
+ it('fills the height above the pager by flexing the thumbnail between 2.8:1 and 16:9', () => {
+ const room = 1000 - 96 - 32 - 64;
+ const grid = renderHook(() => usePageFit(libraryIn(1600, 1000), 'grid')).result.current;
+ const rows = grid.pageSize / grid.columns;
+ const cardWidth = (1600 - 18 * (grid.columns - 1)) / grid.columns;
+ const used = rows * ((grid.thumbHeight ?? 0) + 113) + 18 * (rows - 1);
+ expect(grid).toEqual({ columns: 6, pageSize: 18, thumbHeight: 141 });
+ expect(room - used).toBeLessThanOrEqual(room * 0.04);
+ expect(grid.thumbHeight).toBeGreaterThanOrEqual(Math.floor(cardWidth * 0.36));
+ expect(grid.thumbHeight).toBeLessThanOrEqual(cardWidth * (9 / 16));
const list = renderHook(() => usePageFit(libraryIn(1100, 1000), 'list'));
- expect(list.result.current).toEqual({ columns: 1, pageSize: 7 });
-
- const small = renderHook(() => usePageFit(libraryIn(952, 800), 'grid'));
- expect(small.result.current).toEqual({ columns: 3, pageSize: 6 });
+ expect(list.result.current).toEqual({ columns: 1, pageSize: 7, thumbHeight: null });
});
});
diff --git a/packages/ui/src/library/hooks/use-page-fit.ts b/packages/ui/src/library/hooks/use-page-fit.ts
index aa09e9d..de8ae7f 100644
--- a/packages/ui/src/library/hooks/use-page-fit.ts
+++ b/packages/ui/src/library/hooks/use-page-fit.ts
@@ -1,19 +1,20 @@
import { type RefObject, useEffect, useState } from 'react';
import type { LibraryDisplay } from '../types';
-const MIN_CARD_WIDTH = 300;
+const MIN_CARD_WIDTH = 240;
const MAX_COLUMNS = 6;
+const MIN_THUMB_RATIO = 0.36;
+const MAX_THUMB_RATIO = 9 / 16;
+const SNUG = 0.04;
const CARD_GAP = 18;
const CARD_BODY = 113;
const ROW_GAP = 10;
const ROW_HEIGHT = 94;
const PAGER = 64;
-const lastFit = { current: { columns: 3, pageSize: 6 } };
+type PageFit = { columns: number; pageSize: number; thumbHeight: number | null };
+const lastFit: { current: PageFit } = { current: { columns: 3, pageSize: 6, thumbHeight: null } };
-export function usePageFit(
- ref: RefObject<HTMLElement | null>,
- display: LibraryDisplay,
-): { columns: number; pageSize: number } {
+export function usePageFit(ref: RefObject<HTMLElement | null>, display: LibraryDisplay): PageFit {
const [fit, setFit] = useState(lastFit.current);
useEffect(() => {
@@ -21,18 +22,47 @@ export function usePageFit(
if (!node) return;
const measure = () => {
const width = node.clientWidth;
- const grid = display === 'grid';
- const columns = grid
- ? Math.min(MAX_COLUMNS, Math.max(1, Math.floor((width + CARD_GAP) / (MIN_CARD_WIDTH + CARD_GAP))))
- : 1;
- const gap = grid ? CARD_GAP : ROW_GAP;
- const itemHeight = grid ? ((width - CARD_GAP * (columns - 1)) / columns) * (9 / 16) + CARD_BODY : ROW_HEIGHT;
const top = node.getBoundingClientRect().top + window.scrollY;
const bottom = Number.parseFloat(getComputedStyle(node.parentElement ?? node).paddingBottom) || 0;
const room = window.innerHeight - top - bottom - PAGER;
- const rows = Math.max(1, Math.floor((room + gap) / (itemHeight + gap)));
- const next = { columns, pageSize: columns * rows };
- if (next.columns === lastFit.current.columns && next.pageSize === lastFit.current.pageSize) return;
+ let next: PageFit;
+ if (display !== 'grid') {
+ next = {
+ columns: 1,
+ pageSize: Math.max(1, Math.floor((room + ROW_GAP) / (ROW_HEIGHT + ROW_GAP))),
+ thumbHeight: null,
+ };
+ } else {
+ const maxColumns = Math.min(
+ MAX_COLUMNS,
+ Math.max(1, Math.floor((width + CARD_GAP) / (MIN_CARD_WIDTH + CARD_GAP))),
+ );
+ let best: (PageFit & { left: number }) | null = null;
+ for (let columns = Math.min(2, maxColumns); columns <= maxColumns; columns++) {
+ const cardWidth = (width - CARD_GAP * (columns - 1)) / columns;
+ const rows = Math.max(
+ 1,
+ Math.floor((room + CARD_GAP) / (cardWidth * MIN_THUMB_RATIO + CARD_BODY + CARD_GAP)),
+ );
+ const thumbHeight = Math.floor(
+ Math.min(cardWidth * MAX_THUMB_RATIO, (room - CARD_GAP * (rows - 1)) / rows - CARD_BODY),
+ );
+ const left = room - rows * (thumbHeight + CARD_BODY) - CARD_GAP * (rows - 1);
+ const fit = { columns, pageSize: columns * rows, thumbHeight, left };
+ const snug = left <= room * SNUG;
+ const bestSnug = best !== null && best.left <= room * SNUG;
+ if (
+ !best ||
+ (snug && (!bestSnug || fit.pageSize > best.pageSize)) ||
+ (!snug && !bestSnug && left < best.left)
+ )
+ best = fit;
+ }
+ next = { columns: best?.columns ?? 1, pageSize: best?.pageSize ?? 1, thumbHeight: best?.thumbHeight ?? null };
+ }
+ const last = lastFit.current;
+ if (next.columns === last.columns && next.pageSize === last.pageSize && next.thumbHeight === last.thumbHeight)
+ return;
lastFit.current = next;
setFit(next);
};
diff --git a/packages/ui/src/search/components/SearchResults.tsx b/packages/ui/src/search/components/SearchResults.tsx
index 5f64869..cf4d07f 100644
--- a/packages/ui/src/search/components/SearchResults.tsx
+++ b/packages/ui/src/search/components/SearchResults.tsx
@@ -41,8 +41,8 @@ export function SearchResults({ guides, thumbnails, places, query, selected, onS
alt=""
crop
cache
- frameRatio={16 / 9}
- className="!rounded-none !border-0"
+ cover
+ className="!rounded-none !border-0 w-full"
readOnly
/>
) : (