Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: posts grid row image skeleton size #189

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/components/feature/lazy-image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,39 @@ export function LazyImage({
skeletonClassName,
].join(" ")}
style={{
...(width && height && { width, height }),
...(height && { height }),
...(width && { width }),
...(skeletonBgColor && { backgroundColor: skeletonBgColor }),
}}
/>
)}
{error && <ErrorComponent containerClassName={skeletonClassName} />}
{error && (
<ErrorComponent
height={height}
width={width}
containerClassName={skeletonClassName}
/>
)}
</>
);
}

function ErrorComponent({
containerClassName,
width,
height,
}: {
containerClassName: string;
width?: number;
height?: number;
}) {
return (
<div
className={`flex justify-center items-center bg-neutral-300 dark:bg-neutral-800 ${containerClassName}`}
style={{
...(height && { height }),
...(width && { width }),
}}
>
<ImageOff className="h-5 w-5 text-neutral-400 dark:text-neutral-500" />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ClientRouting } from "@/models/routing/client";
import { Database } from "@/supabase/types";
import { LazyImage } from "@/components/feature/lazy-image";
import { PostRowFooter } from "../post-row-footer";
import { useCallback, useState } from "react";

export function PostsGridRow({
post,
Expand All @@ -15,15 +16,25 @@ export function PostsGridRow({
collection?: Database["public"]["Tables"]["collection"]["Row"];
userId: Database["public"]["Tables"]["users"]["Row"]["id"];
}) {
const [columnWidth, setColumnWidth] = useState(0);

const column = useCallback((node: HTMLLIElement) => {
if (node) {
setColumnWidth(node.offsetWidth);
}
}, []);

return (
<li className="w-full mb-6 rounded-md flex flex-col gap-1">
<li className="w-full mb-6 rounded-md flex flex-col gap-1" ref={column}>
<Link
href={ClientRouting.post().page(post.id)}
className="w-full rounded-md"
>
<LazyImage
src={post.asset_url}
alt={post.title}
width={columnWidth}
height={(post.asset_height / post.asset_width) * columnWidth}
className="flex w-full h-full rounded-md object-cover object-center"
skeletonClassName="w-full h-full rounded-md"
skeletonBgColor={post.asset_color || undefined}
Expand Down
Loading