Skip to content

Commit 94a4128

Browse files
authored
refactor/Post (#186)
* refactor/Post * fix: format and lint * fix: added dots for gallery * fix: added dots for gallery * fix: added dots for gallery * fix: plural name
1 parent ba96686 commit 94a4128

File tree

5 files changed

+84
-12
lines changed

5 files changed

+84
-12
lines changed

platforms/metagram/src/lib/dummyData.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ export const dummyPosts = Array.from({ length: 100 }, (_, i) => ({
44
id: i + 1,
55
avatar: 'https://www.gravatar.com/avatar/2c7d99fe281ecd3bcd65ab915bac6dd5?s=250',
66
username: `user${i + 1}`,
7-
imgUri: 'https://picsum.photos/800',
7+
imgUri: [
8+
'https://picsum.photos/800',
9+
'https://picsum.photos/200',
10+
'https://picsum.photos/800',
11+
'https://picsum.photos/200'
12+
],
813
postAlt: 'Sample',
914
text: `This is post number ${i + 1}. Loving how these shots came out! 📸`,
1015
time: `${i + 1} hours ago`,

platforms/metagram/src/lib/fragments/Post/Post.stories.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export const Primary = {
1515
args: {
1616
avatar: 'https://www.gravatar.com/avatar/2c7d99fe281ecd3bcd65ab915bac6dd5?s=250',
1717
username: 'blurryface',
18-
imgUri: 'https://graphicsfamily.com/wp-content/uploads/edd/2023/01/Free-Photographer-Social-Media-Post-Design-Template-870x870.jpg',
18+
imgUris: [
19+
'https://graphicsfamily.com/wp-content/uploads/edd/2023/01/Free-Photographer-Social-Media-Post-Design-Template-870x870.jpg',
20+
'https://picsum.photos/200'
21+
],
1922
postAlt: 'Sample',
2023
text: 'Took some pictures today! Really love how this one in particular turned out! ',
2124
time: '2 hours ago',

platforms/metagram/src/lib/fragments/Post/Post.svelte

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { Avatar } from '$lib/ui';
33
import { cn } from '$lib/utils';
44
import {
5+
ArrowLeftIcon,
6+
ArrowRightIcon,
57
Message02Icon,
68
MoreVerticalIcon,
79
RecordIcon,
@@ -13,7 +15,7 @@
1315
interface IPostProps extends HTMLAttributes<HTMLElement> {
1416
avatar: string;
1517
username: string;
16-
imgUri: string;
18+
imgUris: string[];
1719
postAlt?: string;
1820
text: string;
1921
count: {
@@ -31,14 +33,35 @@
3133
const {
3234
avatar,
3335
username,
34-
imgUri,
36+
imgUris,
3537
text,
3638
postAlt,
3739
count,
3840
callback,
3941
time,
4042
...restProps
4143
}: IPostProps = $props();
44+
45+
let galleryRef: HTMLDivElement;
46+
let currentIndex = $state(0);
47+
48+
function scrollLeft() {
49+
if (!galleryRef) return;
50+
galleryRef.scrollBy({ left: -galleryRef.clientWidth, behavior: 'smooth' });
51+
}
52+
53+
function scrollRight() {
54+
if (!galleryRef) return;
55+
galleryRef.scrollBy({ left: galleryRef.clientWidth, behavior: 'smooth' });
56+
}
57+
58+
function handleScroll() {
59+
if (!galleryRef) return;
60+
const scrollLeft = galleryRef.scrollLeft;
61+
const galleryWidth = galleryRef.clientWidth;
62+
const newIndex = Math.round(scrollLeft / galleryWidth);
63+
currentIndex = newIndex;
64+
}
4265
</script>
4366

4467
<article {...restProps} class={cn(['flex w-full flex-col gap-4', restProps.class])}>
@@ -52,12 +75,53 @@
5275
<HugeiconsIcon icon={MoreVerticalIcon} size={24} color="var(--color-black-500)" />
5376
</button>
5477
</div>
55-
<div class="overflow-hidden rounded-4xl">
56-
<img
57-
src={imgUri}
58-
alt={postAlt ?? text}
59-
class="aspect-[4/5] h-full w-full object-cover md:aspect-[16/9]"
60-
/>
78+
<div class="relative">
79+
{#if imgUris.length > 1}
80+
<button
81+
onclick={scrollLeft}
82+
class="absolute start-2 top-1/2 z-10 hidden -translate-y-1/2 rounded-full bg-white p-2 shadow hover:bg-gray-200 md:inline-block"
83+
>
84+
<HugeiconsIcon icon={ArrowLeftIcon} size={20} color="black" />
85+
</button>
86+
{/if}
87+
<div
88+
bind:this={galleryRef}
89+
onscroll={handleScroll}
90+
class="hide-scrollbar flex aspect-[4/5] snap-x snap-mandatory flex-nowrap gap-2 overflow-hidden overflow-x-scroll rounded-4xl md:aspect-[16/9]"
91+
>
92+
{#each imgUris as img}
93+
<div class="aspect-[4/5] h-full w-full snap-center md:aspect-[16/9]">
94+
<img
95+
src={img}
96+
alt={postAlt ?? text}
97+
class=" h-full w-full rounded-4xl object-cover"
98+
/>
99+
</div>
100+
{/each}
101+
</div>
102+
{#if imgUris.length > 1}
103+
<div
104+
class="absolute start-[50%] bottom-4 mt-2 flex translate-x-[-50%] items-center justify-center gap-1"
105+
>
106+
{#if imgUris.length > 1}
107+
<div class="mt-2 flex items-center justify-center gap-1">
108+
{#each imgUris as _, i}
109+
<div
110+
class={`h-1.5 w-1.5 rounded-full ${currentIndex === i ? 'bg-white' : 'bg-black-600'}`}
111+
></div>
112+
{/each}
113+
</div>
114+
{/if}
115+
</div>
116+
{/if}
117+
{#if imgUris.length > 1}
118+
<button
119+
onclick={scrollRight}
120+
class="absolute end-2 top-1/2 z-10 hidden -translate-y-1/2 rounded-full bg-white p-2 shadow hover:bg-gray-200 md:inline-block"
121+
>
122+
<HugeiconsIcon icon={ArrowRightIcon} size={20} color="black" />
123+
</button>
124+
{/if}
61125
</div>
62126
<p class="text-black/80">{text}</p>
63127
<p class="text-black/60">{time}</p>

platforms/metagram/src/lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type PostData = {
2121
id: number;
2222
avatar: string;
2323
username: string;
24-
imgUri: string;
24+
imgUri: string[];
2525
postAlt: string;
2626
text: string;
2727
time: string;

platforms/metagram/src/routes/(protected)/home/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<Post
9090
avatar={post.avatar}
9191
username={post.username}
92-
imgUri={post.imgUri}
92+
imgUris={post.imgUri}
9393
postAlt={post.postAlt}
9494
text={post.text}
9595
time={post.time}

0 commit comments

Comments
 (0)