docs: Carousel Component (#1096)
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
import { tw } from "twind";
|
||||
import { asset } from "$fresh/runtime.ts";
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { useEffect, useRef } from "preact/hooks";
|
||||
import IconCircleChevronsRight from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/circle-chevrons-right.tsx";
|
||||
import IconCircleChevronsLeft from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/circle-chevrons-left.tsx";
|
||||
|
||||
const SLIDE_DATA = [
|
||||
{
|
||||
color: "bg-green-300",
|
||||
text: "slide one",
|
||||
url: asset("/illustration/deno-plush.svg"),
|
||||
},
|
||||
{
|
||||
color: "bg-yellow-300",
|
||||
text: "slide too",
|
||||
url: asset("/illustration/lemon-squash.svg"),
|
||||
},
|
||||
{
|
||||
color: "bg-blue-300",
|
||||
text: "slide three",
|
||||
url: asset("/illustration/deno-plush.svg"),
|
||||
},
|
||||
{
|
||||
color: "bg-yellow-300",
|
||||
text: "slide four",
|
||||
url: asset("/illustration/lemon-squash.svg"),
|
||||
},
|
||||
];
|
||||
|
||||
type SlideProps = {
|
||||
class?: string;
|
||||
key?: number;
|
||||
data: {
|
||||
color: string;
|
||||
text: string;
|
||||
url: string;
|
||||
};
|
||||
};
|
||||
|
||||
const Slide = (props: SlideProps) => {
|
||||
const { key, data } = props;
|
||||
const { color, text, url } = data;
|
||||
if (props.class === undefined) props.class = "";
|
||||
return (
|
||||
<div
|
||||
key={key}
|
||||
class={`${props.class} ${color} h-80 w-full text-center text-black p-5`}
|
||||
>
|
||||
{text}
|
||||
<img src={url} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
type CarouselProps = {
|
||||
showNavigation?: boolean;
|
||||
interval?: number;
|
||||
currentSlide?: number;
|
||||
automatic?: boolean;
|
||||
class?: string;
|
||||
};
|
||||
|
||||
const Carousel = (props: CarouselProps) => {
|
||||
const NAVIGATION_COLOR = `hover:text-gray-300 text-white`;
|
||||
const CHEVRON_STYLE =
|
||||
`absolute z-30 w-10 h-10 ${NAVIGATION_COLOR} cursor-pointer`;
|
||||
const SHOW_NAVIGATION = props.showNavigation === false ? false : true;
|
||||
const SLIDE_INTERVAL = props.interval ? props.interval : 3500;
|
||||
const currentSlide = useSignal(props.currentSlide ? props.currentSlide : 0);
|
||||
const automatic = useSignal(props.automatic === false ? false : true);
|
||||
const slideshowRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const slideClasses = (idx = 0) => {
|
||||
let outgoingSlide = currentSlide.value - 1;
|
||||
let incomingSlide = currentSlide.value + 1;
|
||||
if (outgoingSlide === -1) outgoingSlide = SLIDE_DATA.length - 1;
|
||||
if (incomingSlide === SLIDE_DATA.length) incomingSlide = 0;
|
||||
// console.log(outgoingSlide, currentSlide.value, incomingSlide)
|
||||
const TRANSITION_CLASS = () => {
|
||||
if (currentSlide.value === idx) return "translate-x-0 z-20";
|
||||
if (incomingSlide === idx) return "translate-x-full z-10";
|
||||
if (outgoingSlide === idx) return "-translate-x-full z-10";
|
||||
return "translate-x-full";
|
||||
};
|
||||
return tw`slide absolute top-0 left-0 transition-all ease-in-out duration-700 transform ${TRANSITION_CLASS}`;
|
||||
};
|
||||
|
||||
const nextSlide = () => {
|
||||
if (SLIDE_DATA.length === currentSlide.value + 1) {
|
||||
currentSlide.value = 0;
|
||||
} else {
|
||||
currentSlide.value++;
|
||||
}
|
||||
};
|
||||
|
||||
const previousSlide = () => {
|
||||
if (currentSlide.value === 0) {
|
||||
currentSlide.value = SLIDE_DATA.length - 1;
|
||||
} else {
|
||||
currentSlide.value--;
|
||||
}
|
||||
};
|
||||
|
||||
const chevronClick = (doCallback = () => {}) => {
|
||||
if (automatic.value) automatic.value = false;
|
||||
return doCallback();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const interval = setInterval(() => {
|
||||
if (automatic.value) nextSlide();
|
||||
}, SLIDE_INTERVAL);
|
||||
return () => clearInterval(interval);
|
||||
}, []);
|
||||
|
||||
const ArrowKeyNavigation = () => {
|
||||
const keydownHandler = (event: KeyboardEvent) => {
|
||||
if (automatic.value) automatic.value = false;
|
||||
switch (event.code) {
|
||||
case "ArrowLeft":
|
||||
event.preventDefault();
|
||||
previousSlide();
|
||||
break;
|
||||
case "ArrowRight":
|
||||
event.preventDefault();
|
||||
nextSlide();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
slideshowRef.current?.addEventListener("keydown", keydownHandler);
|
||||
return () =>
|
||||
slideshowRef.current?.removeEventListener("keydown", keydownHandler);
|
||||
};
|
||||
useEffect(ArrowKeyNavigation, []);
|
||||
|
||||
const goToSlide = (slide_index = 0) => {
|
||||
if (automatic.value) automatic.value = false;
|
||||
currentSlide.value = slide_index;
|
||||
};
|
||||
|
||||
const DotsNavigation = () => (
|
||||
<div
|
||||
class={"slide_nav z-30 w-full absolute bottom-0 flex justify-center cursor-pointer"}
|
||||
>
|
||||
{SLIDE_DATA.map((_item, idx) => {
|
||||
return (
|
||||
<div
|
||||
class={`px-1 ${NAVIGATION_COLOR}`}
|
||||
onClick={() => {
|
||||
goToSlide(idx);
|
||||
}}
|
||||
key={idx}
|
||||
>
|
||||
{idx === currentSlide.value ? <>●</> : <>○</>}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={slideshowRef}
|
||||
class={`slideshow relative flex-1 flex-end p-0 overflow-hidden ${
|
||||
props.class !== undefined ? props.class : ""
|
||||
}`}
|
||||
tabIndex={0}
|
||||
>
|
||||
<IconCircleChevronsLeft
|
||||
class={`left-0 ${CHEVRON_STYLE}`}
|
||||
style="top: calc(50% - 20px)"
|
||||
onClick={() => chevronClick(previousSlide)}
|
||||
/>
|
||||
<IconCircleChevronsRight
|
||||
class={`right-0 ${CHEVRON_STYLE}`}
|
||||
style="top: calc(50% - 20px)"
|
||||
onClick={() => chevronClick(nextSlide)}
|
||||
/>
|
||||
{SLIDE_DATA.map((item, idx) => (
|
||||
<Slide
|
||||
data={item}
|
||||
key={idx}
|
||||
class={slideClasses(idx)}
|
||||
/>
|
||||
))}
|
||||
{SHOW_NAVIGATION &&
|
||||
<DotsNavigation />}
|
||||
<Slide
|
||||
data={SLIDE_DATA[0]}
|
||||
class="opacity-0 pointer-events-none"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Carousel;
|
||||
@@ -6,6 +6,7 @@ import Header from "../components/gallery/Header.tsx";
|
||||
import Footer from "../components/gallery/Footer.tsx";
|
||||
import Hero from "../components/gallery/Hero.tsx";
|
||||
import Features from "../components/gallery/Features.tsx";
|
||||
import Carousel from "../components/gallery/Carousel.tsx";
|
||||
import { ComponentChildren } from "preact";
|
||||
import IconHappy from "https://deno.land/x/tabler_icons_tsx@0.0.2/tsx/mood-crazy-happy.tsx";
|
||||
import IconHeart from "https://deno.land/x/tabler_icons_tsx@0.0.2/tsx/heart.tsx";
|
||||
@@ -110,6 +111,10 @@ export default function ComponentGallery(props: ComponentGalleryProps) {
|
||||
<Section title="Features" source={props.sources.Features}>
|
||||
<Features />
|
||||
</Section>
|
||||
|
||||
<Section title="Carousel" source={props.sources.Carousel}>
|
||||
<Carousel />
|
||||
</Section>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ export const handler: Handlers<HomeProps> = {
|
||||
"Footer": await getSource("../components/gallery/Footer.tsx"),
|
||||
"Hero": await getSource("../components/gallery/Hero.tsx"),
|
||||
"Features": await getSource("../components/gallery/Features.tsx"),
|
||||
"Carousel": await getSource("../components/gallery/Carousel.tsx"),
|
||||
},
|
||||
};
|
||||
return ctx.render(props);
|
||||
|
||||
Reference in New Issue
Block a user