-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f4b95e
commit a99e0f4
Showing
7 changed files
with
188 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import '../../embla.css'; | ||
|
||
import AutoHeight from 'embla-carousel-auto-height'; | ||
import useEmblaCarousel from 'embla-carousel-react'; | ||
import { Dispatch, FC, SetStateAction, useEffect } from 'react'; | ||
import { FaArrowLeft, FaArrowRight } from 'react-icons/fa6'; | ||
|
||
import { MediaMetaData } from './MediaMetaData'; | ||
import { BareMediaPreview } from './MediaPreview'; | ||
|
||
export const MediaCarousel: FC<{ | ||
ids: number[]; | ||
mediaId: number; | ||
setMediaId: Dispatch<SetStateAction<number | undefined>>; | ||
}> = ({ ids, mediaId, setMediaId }) => { | ||
const [emblaReference, emblaApi] = useEmblaCarousel( | ||
{ | ||
loop: true, | ||
}, | ||
[AutoHeight()] | ||
); | ||
|
||
// update mediaId based on embla state | ||
emblaApi?.on('select', () => { | ||
const selected = emblaApi.selectedScrollSnap(); | ||
|
||
if (selected !== undefined) { | ||
setMediaId(ids[selected]); | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
if (mediaId !== undefined) { | ||
const index = ids.indexOf(mediaId); | ||
|
||
// TODO: If this useEffect wasn't triggered by emblaApi.on('select'), then we should set the scrollTo(index, true) boolean to true to not animate the scroll | ||
emblaApi?.scrollTo(index); | ||
} | ||
}); | ||
|
||
return ( | ||
<div className="flex flex-col md:flex-row"> | ||
<div ref={emblaReference} className="embla overflow-hidden"> | ||
<div className="embla__container"> | ||
{ids.map((id) => ( | ||
<div | ||
key={id} | ||
className="embla__slide rounded-lg select-none" | ||
draggable={false} | ||
> | ||
<BareMediaPreview media_id={id} /> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
<div className="p-4 min-w-64"> | ||
<code> | ||
<MediaMetaData mediaId={mediaId} /> | ||
</code> | ||
<div> | ||
<button | ||
className="bg-gray-200 rounded-lg p-2 disabled:opacity-50 text-black px-2 gap-x-2" | ||
onClick={() => { | ||
emblaApi?.scrollPrev(); | ||
}} | ||
> | ||
<FaArrowLeft /> | ||
</button> | ||
<button | ||
className="bg-gray-200 rounded-lg p-2 disabled:opacity-50 text-black px-2 gap-x-2" | ||
onClick={() => { | ||
emblaApi?.scrollNext(); | ||
}} | ||
> | ||
<FaArrowRight /> | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { FC } from 'react'; | ||
|
||
import { useMedia } from '@/api/media'; | ||
|
||
export const MediaMetaData: FC<{ | ||
mediaId: number; | ||
}> = ({ mediaId }) => { | ||
const media = useMedia(mediaId).data; | ||
|
||
return ( | ||
<> | ||
<p>Type: {media?.kind}</p> | ||
<p>Created: {media?.created_at}</p> | ||
<p>Updated: {media?.updated_at}</p> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.embla { | ||
max-width: 48rem; | ||
margin: auto; | ||
--slide-height: 19rem; | ||
--slide-spacing: 1rem; | ||
--slide-size: 100%; | ||
} | ||
.embla__viewport { | ||
overflow: hidden; | ||
} | ||
.embla__container { | ||
display: flex; | ||
touch-action: pan-y pinch-zoom; | ||
margin-left: calc(var(--slide-spacing) * -1); | ||
} | ||
.embla__slide { | ||
transform: translate3d(0, 0, 0); | ||
flex: 0 0 var(--slide-size); | ||
min-width: 0; | ||
padding-left: var(--slide-spacing); | ||
} |