|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import Image from 'next/image'; |
| 3 | +import makeStyles from '@mui/styles/makeStyles'; |
| 4 | +import { IconButton, Typography, useMediaQuery } from '@mui/material'; |
| 5 | +import ArrowLeft from '@mui/icons-material/ArrowCircleLeftOutlined'; |
| 6 | +import ArrowRight from '@mui/icons-material/ArrowCircleRightOutlined'; |
| 7 | + |
| 8 | +// Utils |
| 9 | +import theme from '../../config/themes/light'; |
| 10 | + |
| 11 | +const Carousel = ({ content }) => { |
| 12 | + const classes = useStyles(); |
| 13 | + const length = content.length; |
| 14 | + |
| 15 | + const [current, setCurrent] = useState(0); |
| 16 | + |
| 17 | + const nextSlide = () => { |
| 18 | + setCurrent(current === length - 1 ? 0 : current + 1); |
| 19 | + }; |
| 20 | + const prevSlide = () => { |
| 21 | + setCurrent(current === 0 ? length - 1 : current - 1); |
| 22 | + }; |
| 23 | + |
| 24 | + if (!Array.isArray(content) || content.length <= 0) { |
| 25 | + return null; |
| 26 | + } |
| 27 | + |
| 28 | + const Desktop = useMediaQuery(theme.breakpoints.up('sm')); |
| 29 | + |
| 30 | + return ( |
| 31 | + <section className={classes.wrapper}> |
| 32 | + <div className={classes.imgContainer}> |
| 33 | + {Desktop && ( |
| 34 | + <Image |
| 35 | + className={classes.sideImage} |
| 36 | + src={ |
| 37 | + current === 0 ? content[length - 1].img : content[current - 1].img |
| 38 | + } |
| 39 | + alt='image' |
| 40 | + width='400px' |
| 41 | + height='300px' |
| 42 | + objectFit='contain' |
| 43 | + /> |
| 44 | + )} |
| 45 | + <div className={classes.midSlide}> |
| 46 | + <a |
| 47 | + target='_blank' |
| 48 | + href={content[current].link} |
| 49 | + rel='noopener noreferrer' |
| 50 | + > |
| 51 | + <Image |
| 52 | + className={classes.centreImage} |
| 53 | + src={content[current].img} |
| 54 | + alt='image' |
| 55 | + width='600px' |
| 56 | + height='400px' |
| 57 | + /> |
| 58 | + </a> |
| 59 | + {content[current].desc && ( |
| 60 | + <div className={classes.textWrapper}> |
| 61 | + <Typography |
| 62 | + variant='body2' |
| 63 | + textAlign='center' |
| 64 | + className={classes.text} |
| 65 | + > |
| 66 | + {content[current].desc} |
| 67 | + </Typography> |
| 68 | + </div> |
| 69 | + )} |
| 70 | + </div> |
| 71 | + {Desktop && ( |
| 72 | + <Image |
| 73 | + className={classes.sideImage} |
| 74 | + src={ |
| 75 | + current === length - 1 ? content[0].img : content[current + 1].img |
| 76 | + } |
| 77 | + alt='image' |
| 78 | + width='400px' |
| 79 | + height='300px' |
| 80 | + objectFit='contain' |
| 81 | + /> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + |
| 85 | + <span className={classes.navIconWrapper}> |
| 86 | + <IconButton |
| 87 | + className={classes.navIcon} |
| 88 | + onClick={prevSlide} |
| 89 | + size='large' |
| 90 | + > |
| 91 | + <ArrowLeft /> |
| 92 | + </IconButton> |
| 93 | + <IconButton |
| 94 | + className={classes.navIcon} |
| 95 | + onClick={nextSlide} |
| 96 | + size='large' |
| 97 | + > |
| 98 | + <ArrowRight /> |
| 99 | + </IconButton> |
| 100 | + </span> |
| 101 | + </section> |
| 102 | + ); |
| 103 | +}; |
| 104 | + |
| 105 | +export default Carousel; |
| 106 | + |
| 107 | +const useStyles = makeStyles((theme) => ({ |
| 108 | + wrapper: { |
| 109 | + maxWidth: '100%', |
| 110 | + position: 'relative', |
| 111 | + backgroundColor: theme.palette.secondary.main, |
| 112 | + }, |
| 113 | + |
| 114 | + carousel: { |
| 115 | + display: 'flex', |
| 116 | + overflowX: 'scroll', |
| 117 | + overflowY: 'none', |
| 118 | + scrollBehavior: 'smooth', |
| 119 | + }, |
| 120 | + |
| 121 | + imgContainer: { |
| 122 | + display: 'flex', |
| 123 | + justifyContent: 'center', |
| 124 | + marginTop: '1em', |
| 125 | + gap: 20, |
| 126 | + }, |
| 127 | + |
| 128 | + centreImage: { |
| 129 | + borderRadius: '16px', |
| 130 | + }, |
| 131 | + |
| 132 | + sideImage: { |
| 133 | + borderRadius: '16px', |
| 134 | + opacity: '0.5', |
| 135 | + }, |
| 136 | + |
| 137 | + midSlide: { |
| 138 | + display: 'flex', |
| 139 | + flexDirection: 'column', |
| 140 | + alignItems: 'center', |
| 141 | + borderRadius: '16px', |
| 142 | + }, |
| 143 | + |
| 144 | + bottomContainer: { |
| 145 | + display: 'flex', |
| 146 | + flexDirection: 'column', |
| 147 | + alignItems: 'center', |
| 148 | + justifyContent: 'center', |
| 149 | + bottom: '3%', |
| 150 | + position: 'absolute', |
| 151 | + }, |
| 152 | + |
| 153 | + textWrapper: { |
| 154 | + justifyContent: 'justify', |
| 155 | + alignItems: 'justify', |
| 156 | + marginTop: '15px', |
| 157 | + color: theme.palette.common.white, |
| 158 | + [theme.breakpoints.down('sm')]: { |
| 159 | + paddingLeft: '15px', |
| 160 | + paddingRight: '15px', |
| 161 | + }, |
| 162 | + }, |
| 163 | + |
| 164 | + navIconWrapper: { |
| 165 | + display: 'flex', |
| 166 | + justifyContent: 'center', |
| 167 | + alignItems: 'center', |
| 168 | + color: theme.palette.secondary.neutral70, |
| 169 | + }, |
| 170 | + |
| 171 | + navIcon: { |
| 172 | + color: theme.palette.secondary.neutral70, |
| 173 | + }, |
| 174 | +})); |
0 commit comments