From ab732429335ab58c8f65a4fdaedd7ced7440646a Mon Sep 17 00:00:00 2001 From: Taylor Young Date: Wed, 26 Apr 2023 12:51:13 -0700 Subject: [PATCH] docs: Carousel Component (#1096) --- www/components/gallery/Carousel.tsx | 199 ++++++++++++++++++++++++++++ www/islands/ComponentGallery.tsx | 5 + www/routes/components.tsx | 1 + 3 files changed, 205 insertions(+) create mode 100644 www/components/gallery/Carousel.tsx diff --git a/www/components/gallery/Carousel.tsx b/www/components/gallery/Carousel.tsx new file mode 100644 index 00000000..6a542477 --- /dev/null +++ b/www/components/gallery/Carousel.tsx @@ -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 ( +
+ {text} + +
+ ); +}; + +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(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 = () => ( +
+ {SLIDE_DATA.map((_item, idx) => { + return ( +
{ + goToSlide(idx); + }} + key={idx} + > + {idx === currentSlide.value ? <>● : <>○} +
+ ); + })} +
+ ); + + return ( +
+ chevronClick(previousSlide)} + /> + chevronClick(nextSlide)} + /> + {SLIDE_DATA.map((item, idx) => ( + + ))} + {SHOW_NAVIGATION && + } + +
+ ); +}; + +export default Carousel; diff --git a/www/islands/ComponentGallery.tsx b/www/islands/ComponentGallery.tsx index b14c96b6..6c2a6664 100644 --- a/www/islands/ComponentGallery.tsx +++ b/www/islands/ComponentGallery.tsx @@ -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) {
+ +
+ +
); } diff --git a/www/routes/components.tsx b/www/routes/components.tsx index 85c8bd95..1d990793 100644 --- a/www/routes/components.tsx +++ b/www/routes/components.tsx @@ -23,6 +23,7 @@ export const handler: Handlers = { "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);