Explorar el Código

Finished horizontal scroller

pull/5/head
Djordje Mitrovic hace 3 años
padre
commit
52b21f4991

+ 51
- 5
src/components/Scroller/HorizontalScroller.js Ver fichero

import React, { useRef } from "react";
import React, { useRef, useState } from "react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { import {
HorizontalScrollerContainer, HorizontalScrollerContainer,


const HorizontalScroller = (props) => { const HorizontalScroller = (props) => {
const scrollRef = useRef(null); const scrollRef = useRef(null);
const [isDisabledLeftButton, setIsDisabledLeftButton] = useState(true);
const [isDisabledRightButton, setIsDisabledRightButton] = useState(false);

const handleScroll = (event) => {
if (!event.external) {
if (scrollRef.current.scrollLeft === 0) {
if (isDisabledLeftButton !== true) setIsDisabledLeftButton(true);
} else {
if (isDisabledLeftButton !== false) setIsDisabledLeftButton(false);
}
if (
scrollRef.current.scrollWidth - scrollRef.current.scrollLeft ===
scrollRef.current.clientWidth
) {
if (isDisabledRightButton !== true) setIsDisabledRightButton(true);
} else {
if (isDisabledRightButton !== false) setIsDisabledRightButton(false);
}
}
};


const handleRight = () => { const handleRight = () => {
if (scrollRef.current.scrollLeft + 50 !== 0) {
if (isDisabledLeftButton !== false) setIsDisabledLeftButton(false);
}
if (
scrollRef.current.scrollWidth - scrollRef.current.scrollLeft - 50 <=
scrollRef.current.clientWidth
) {
if (isDisabledRightButton !== true) setIsDisabledRightButton(true);
}
scrollRef.current.scrollBy({ left: 50, behaviour: "smooth" }); scrollRef.current.scrollBy({ left: 50, behaviour: "smooth" });
}; };
const handleLeft = () => { const handleLeft = () => {
if (scrollRef.current.scrollLeft - 50 <= 0) {
if (isDisabledLeftButton !== true) setIsDisabledLeftButton(true);
}
if (
scrollRef.current.scrollWidth - scrollRef.current.scrollLeft + 50 !==
scrollRef.current.clientWidth
) {
if (isDisabledRightButton !== false) setIsDisabledRightButton(false);
}
scrollRef.current.scrollBy({ left: -50, behaviour: "smooth" }); scrollRef.current.scrollBy({ left: -50, behaviour: "smooth" });
}; };
return ( return (
<HorizontalScrollerContainer>
<Arrow onClick={handleLeft}>
<HorizontalScrollerContainer style={props.containerStyle}>
<Arrow onClick={handleLeft} disabled={isDisabledLeftButton}>
<ArrowIcon side={"left"} /> <ArrowIcon side={"left"} />
</Arrow> </Arrow>
<ListContainer innerRef={scrollRef}>{props.children}</ListContainer>
<Arrow onClick={handleRight}>
<ListContainer
innerRef={scrollRef}
style={props.listStyle}
onScroll={handleScroll}
>
{props.children}
</ListContainer>
<Arrow onClick={handleRight} disabled={isDisabledRightButton}>
<ArrowIcon side={"right"} /> <ArrowIcon side={"right"} />
</Arrow> </Arrow>
</HorizontalScrollerContainer> </HorizontalScrollerContainer>
HorizontalScroller.propTypes = { HorizontalScroller.propTypes = {
children: PropTypes.node, children: PropTypes.node,
className: PropTypes.string, className: PropTypes.string,
containerStyle: PropTypes.any,
listStyle: PropTypes.any,
}; };


export default HorizontalScroller; export default HorizontalScroller;

+ 10
- 3
src/components/Scroller/HorizontalScroller.styled.js Ver fichero

flex-direction: row; flex-direction: row;
flex-wrap: nowrap; flex-wrap: nowrap;
overflow: hidden; overflow: hidden;
cursor: grab;
/* scroll-behavior: smooth; */
` `
export const Arrow = styled(Button)` export const Arrow = styled(Button)`
border: 1px solid ${selectedTheme.primaryPurple}; border: 1px solid ${selectedTheme.primaryPurple};
padding-top: 10px; padding-top: 10px;
margin-top: auto; margin-top: auto;
margin-bottom: auto; margin-bottom: auto;
transition: 0.2s all ease;
&:hover { &:hover {
background-color: ${selectedTheme.primaryPurple}; background-color: ${selectedTheme.primaryPurple};
& svg path { & svg path {
stroke: white; stroke: white;
} }
} }
${props => props.disabled && `
border 1px solid ${selectedTheme.iconStrokeDisabledColor};
& svg path {
stroke: ${selectedTheme.iconStrokeDisabledColor};
transition: 0.2s all ease;
}
`}
` `
export const ListContainer = styled(ScrollContainer)` export const ListContainer = styled(ScrollContainer)`
display: flex; display: flex;
flex-wrap: nowrap; flex-wrap: nowrap;
cursor: grab; cursor: grab;
scroll-behavior: smooth; scroll-behavior: smooth;
margin: 0 20px;
margin: 0 18px;
user-select: none; user-select: none;
` `
export const ArrowIcon = styled(DownArrow)` export const ArrowIcon = styled(DownArrow)`

Cargando…
Cancelar
Guardar