Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Commit

Permalink
Props for slide sizing (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
brodcode-stale authored Oct 4, 2020
1 parent 96d87d0 commit 8fc71fd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
10 changes: 8 additions & 2 deletions src/Slide/index.module.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
.slide {
position: absolute;
width: 100%;
height: auto;
padding: 0 8px;
display: flex;
justify-content: center;

& > img {
height: 100%;
width: 100%;
object-fit: cover;
}
}
19 changes: 12 additions & 7 deletions src/Slide/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ const Slide: React.FC<{
offset: number;
move: number;
len: number;
imgsize: number;
}> = ({ src, animation: { timing, speed }, offset, move, len, imgsize }) => {
size: {
width: number;
height: number;
};
}> = ({ src, animation: { timing, speed }, offset, move, len, size }) => {
const [current, setCurrent] = useState(offset);
const [anim, setAnim] = useState<{ interrupt: () => void }>();

Expand All @@ -67,14 +70,16 @@ const Slide: React.FC<{

const pos = (((current % len) + len) % len) - half;
return (
<img
src={src}
alt={src}
<div
className={styles.slide}
style={{
left: `${pos * imgsize}px`,
width: `${size.width}px`,
height: `${size.height}px`,
left: `${pos * size.width}px`,
}}
/>
>
<img src={src} alt={src} />
</div>
);
};

Expand Down
18 changes: 0 additions & 18 deletions src/index.module.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
@value fadetime: 0.65s;
@value imgsize: 1138px;

:export {
transition: fadetime;
}

.wrapper {
width: 100%;
Expand Down Expand Up @@ -35,19 +30,6 @@
display: flex;
width: 100%;
height: 100%;

& > div {
display: contents;
width: 100%;
visibility: hidden;

& > img {
position: inherit;
width: 100%;
height: auto;
padding: 0 8px;
}
}
}

& > span {
Expand Down
29 changes: 21 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import styles from './index.module.scss';
import './index.scss';
import Slide from './Slide';

const fadetime = parseFloat(styles.fadetime);
const imgsize = parseFloat(styles.imgsize);

const cx = classNames.bind(styles);

const Carousel: React.FC<{
slides: string[];
size: { width: number; height: number };
size: {
width: number;
height: number;
};
animation: {
timing: (x: number) => number;
speed: number;
Expand All @@ -24,6 +24,7 @@ const Carousel: React.FC<{
const [move, setMove] = useState(0);
const [slides, setSlides] = useState(data);
const [swipe, setSwipe] = useState<number | undefined>();
// const [maxW, setMaxW] = useState<number | undefined>(0);

useEffect(() => {
if (data.length === 2) setSlides([...data, ...data]);
Expand Down Expand Up @@ -64,13 +65,25 @@ const Carousel: React.FC<{
dot: (selected: boolean) => cx({ dot: true, selected }),
};

// for (const s of slides) {
// const img = new Image();
// img.onload = function () {
// if (this.width > maxW) setMaxW(this.width);
// };
// img.src = s;
// }

return (
<div className={styles.wrapper}>
<div className={styles.carousel}>
<div>
<div>
<img src={slides[0]} alt={slides[0]} />
</div>
<span
style={{
visibility: 'hidden',
width: `${size.width}px`,
height: `${size.height}px`,
}}
></span>
{slides.map((s, key) => (
<Slide
key={key}
Expand All @@ -79,7 +92,7 @@ const Carousel: React.FC<{
offset={key}
move={move}
len={slides.length}
imgsize={imgsize}
size={size}
/>
))}
</div>
Expand Down

0 comments on commit 8fc71fd

Please sign in to comment.