mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 21:06:42 +07:00
made new hero component
This commit is contained in:
parent
3b7997b442
commit
330e233869
10 changed files with 321 additions and 15 deletions
131
src/components/flow/FlowSwiperHero/index.tsx
Normal file
131
src/components/flow/FlowSwiperHero/index.tsx
Normal file
|
@ -0,0 +1,131 @@
|
|||
import React, { FC, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { Swiper, SwiperSlide } from 'swiper/react';
|
||||
import 'swiper/swiper.scss';
|
||||
import 'swiper/components/effect-fade/effect-fade.scss';
|
||||
import 'swiper/components/lazy/lazy.scss';
|
||||
import styles from './styles.module.scss';
|
||||
|
||||
import SwiperCore, { EffectFade, Navigation, Lazy, Autoplay } from 'swiper';
|
||||
import { Icon } from '~/components/input/Icon';
|
||||
import { IFlowState } from '~/redux/flow/reducer';
|
||||
import { getURLFromString } from '~/utils/dom';
|
||||
import { PRESETS, URLS } from '~/constants/urls';
|
||||
import SwiperClass from 'swiper/types/swiper-class';
|
||||
import { LoaderCircle } from '~/components/input/LoaderCircle';
|
||||
import { useHistory } from 'react-router';
|
||||
|
||||
SwiperCore.use([EffectFade, Lazy, Autoplay, Navigation]);
|
||||
|
||||
interface Props {
|
||||
heroes: IFlowState['heroes'];
|
||||
}
|
||||
|
||||
export const FlowSwiperHero: FC<Props> = ({ heroes }) => {
|
||||
const [controlledSwiper, setControlledSwiper] = useState<SwiperClass | undefined>(undefined);
|
||||
const [currentIndex, setCurrentIndex] = useState(heroes.length);
|
||||
const preset = useMemo(() => (window.innerWidth <= 768 ? PRESETS.cover : PRESETS.small_hero), []);
|
||||
const history = useHistory();
|
||||
|
||||
const onNext = useCallback(() => {
|
||||
controlledSwiper?.slideNext(1);
|
||||
}, [controlledSwiper]);
|
||||
|
||||
const onPrev = useCallback(() => {
|
||||
controlledSwiper?.slidePrev(1);
|
||||
}, [controlledSwiper]);
|
||||
|
||||
const onIndexChange = useCallback((swiper: SwiperClass) => {
|
||||
let activeIndex = swiper.activeIndex;
|
||||
let slidesLen = swiper.slides.length;
|
||||
|
||||
if (slidesLen === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (swiper.params.loop) {
|
||||
switch (swiper.activeIndex) {
|
||||
case 0:
|
||||
activeIndex = slidesLen - 3;
|
||||
break;
|
||||
case slidesLen - 1:
|
||||
activeIndex = 0;
|
||||
break;
|
||||
default:
|
||||
--activeIndex;
|
||||
}
|
||||
}
|
||||
|
||||
setCurrentIndex(activeIndex);
|
||||
}, []);
|
||||
|
||||
const onClick = useCallback(
|
||||
(sw: SwiperClass) => {
|
||||
history.push(URLS.NODE_URL(heroes[sw.realIndex]?.id));
|
||||
},
|
||||
[history, heroes]
|
||||
);
|
||||
|
||||
if (!heroes.length) {
|
||||
return (
|
||||
<div className={styles.loader}>
|
||||
<LoaderCircle size={100} fill="currentColor" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const title = heroes[currentIndex]?.title;
|
||||
|
||||
return (
|
||||
<div className={styles.wrap}>
|
||||
<div className={styles.slide}>
|
||||
<div className={styles.info}>
|
||||
<div className={styles.title}>{title}</div>
|
||||
|
||||
<div className={styles.buttons}>
|
||||
<button className={styles.button} onClick={onPrev} type="button">
|
||||
<Icon icon="left" />
|
||||
</button>
|
||||
|
||||
<button className={styles.button} onClick={onNext} type="button">
|
||||
<Icon icon="right" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Swiper
|
||||
spaceBetween={300}
|
||||
effect="fade"
|
||||
speed={3000}
|
||||
className={styles.swiper}
|
||||
lazy
|
||||
loop
|
||||
slidesPerView={1}
|
||||
autoplay={{
|
||||
delay: 3000,
|
||||
pauseOnMouseEnter: false,
|
||||
stopOnLastSlide: false,
|
||||
disableOnInteraction: false,
|
||||
}}
|
||||
runCallbacksOnInit
|
||||
onSwiper={setControlledSwiper}
|
||||
onSlidesLengthChange={onIndexChange}
|
||||
onRealIndexChange={onIndexChange}
|
||||
onAfterInit={onIndexChange}
|
||||
onClick={onClick}
|
||||
>
|
||||
{heroes
|
||||
.filter(node => node.thumbnail)
|
||||
.map(node => (
|
||||
<SwiperSlide key={node.id}>
|
||||
<img
|
||||
src={getURLFromString(node.thumbnail!, preset)}
|
||||
alt=""
|
||||
className={styles.preview}
|
||||
/>
|
||||
</SwiperSlide>
|
||||
))}
|
||||
</Swiper>
|
||||
</div>
|
||||
);
|
||||
};
|
168
src/components/flow/FlowSwiperHero/styles.module.scss
Normal file
168
src/components/flow/FlowSwiperHero/styles.module.scss
Normal file
|
@ -0,0 +1,168 @@
|
|||
@import '~/styles/variables';
|
||||
|
||||
.wrap {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
position: relative;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.swiper {
|
||||
@include outer_shadow;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border-radius: $radius;
|
||||
|
||||
:global(.swiper-slide) {
|
||||
object-fit: cover;
|
||||
|
||||
img {
|
||||
min-width: 150%;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.slide {
|
||||
width: 100%;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
padding: $gap;
|
||||
z-index: 5;
|
||||
pointer-events: none;
|
||||
touch-action: none;
|
||||
box-sizing: border-box;
|
||||
border-radius: $radius;
|
||||
|
||||
&::after {
|
||||
content: ' ';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: url('../../../sprites/stripes.svg') rgba(0, 0, 0, 0.3);
|
||||
z-index: -1;
|
||||
pointer-events: none;
|
||||
box-shadow: inset transparentize($color: white, $amount: 0.85) 0 1px;
|
||||
touch-action: none;
|
||||
border-radius: $radius;
|
||||
}
|
||||
|
||||
&::before {
|
||||
content: ' ';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: linear-gradient(
|
||||
182deg,
|
||||
transparentize($cell_shade, 1) 50%,
|
||||
transparentize($cell_shade, 0) 95%
|
||||
);
|
||||
z-index: 4;
|
||||
pointer-events: none;
|
||||
touch-action: none;
|
||||
border-radius: $radius;
|
||||
}
|
||||
}
|
||||
|
||||
img.preview {
|
||||
transform: translate(-15%, 0);
|
||||
transition: transform 7s linear, opacity 1s linear;
|
||||
opacity: 0;
|
||||
|
||||
:global(.swiper-slide-active) &,
|
||||
:global(.swiper-slide-duplicate-active) &
|
||||
{
|
||||
transform: translate(-15%, -20%);
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.info {
|
||||
display: flex;
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
right: 0;
|
||||
width: 100%;
|
||||
padding: $gap;
|
||||
box-sizing: border-box;
|
||||
z-index: 5;
|
||||
flex-direction: row;
|
||||
align-items: flex-end;
|
||||
pointer-events: none;
|
||||
touch-action: none;
|
||||
}
|
||||
|
||||
.title {
|
||||
@include clamp(2, 1.2 * 40px * 2);
|
||||
flex: 1;
|
||||
display: flex;
|
||||
margin-right: $gap;
|
||||
overflow: hidden;
|
||||
font: $font_hero_title;
|
||||
text-transform: uppercase;
|
||||
text-overflow: ellipsis;
|
||||
line-height: 1.2em;
|
||||
word-break: break-word;
|
||||
|
||||
@include tablet {
|
||||
@include clamp(3, 1.2 * 32px);
|
||||
white-space: initial;
|
||||
word-wrap: break-word;
|
||||
font: $font_32_bold;
|
||||
max-height: 3.6em;
|
||||
}
|
||||
|
||||
@include phone {
|
||||
white-space: initial;
|
||||
word-wrap: break-word;
|
||||
font: $font_24_bold;
|
||||
max-height: 3.6em;
|
||||
}
|
||||
}
|
||||
|
||||
.buttons {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 58px;
|
||||
flex-direction: row;
|
||||
width: 128px;
|
||||
border-radius: $radius;
|
||||
pointer-events: all;
|
||||
touch-action: auto;
|
||||
margin: 0 -15px -10px 0;
|
||||
|
||||
.button {
|
||||
cursor: pointer;
|
||||
flex: 0 0 58px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
opacity: 0.5;
|
||||
transition: opacity 0.5s;
|
||||
|
||||
&:hover {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
svg {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.loader {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: transparentize(white, 0.95);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue