Skip to content

Commit

Permalink
Fix issue with horizontal slider next button. (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenDufresne authored Oct 18, 2022
1 parent 43252c2 commit 2e08cd1
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions mu-plugins/blocks/horizontal-slider/src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ const SET_WIDTH = CARD_WIDTH * 3;
*/
function Block( { items, title } ) {
const outerRef = useRef();
const [ scrollLeftPos, setScrollLeftPos ] = useState( 0 );
const [ canPrevious, setCanPrevious ] = useState( false );
const [ canNext, setCanNext ] = useState( true );

// Calculate to total width of the content
const totalContainerWidth = items.length * ( CARD_WIDTH + CARD_GAP ) - CARD_GAP;
const innerContainerWidth = items.length * ( CARD_WIDTH + CARD_GAP ) - CARD_GAP;

const scrollContainer = ( pos ) => {
outerRef.current.scrollTo( {
Expand All @@ -54,28 +53,29 @@ function Block( { items, title } ) {
if ( ! canPrevious ) {
return;
}
setScrollLeftPos( outerRef.current.scrollLeft - SET_WIDTH );
scrollContainer( outerRef.current.scrollLeft - SET_WIDTH );
};

const handleNext = () => {
if ( ! canNext ) {
return;
}
setScrollLeftPos( outerRef.current.scrollLeft + SET_WIDTH );
};

useEffect( () => {
scrollContainer( scrollLeftPos );
}, [ scrollLeftPos ] );
scrollContainer( outerRef.current.scrollLeft + SET_WIDTH );
};

useEffect( () => {
if ( ! outerRef.current ) {
return;
}

const { paddingLeft, paddingRight } = window.getComputedStyle( outerRef.current );
const outerContainerWidth =
outerRef.current.clientWidth - parseFloat( paddingLeft ) - parseFloat( paddingRight );

const handleScrollEvent = () => {
setCanPrevious( outerRef.current.scrollLeft > 0 );
setCanNext( totalContainerWidth - outerRef.current.scrollLeft > outerRef.current.offsetWidth );
setCanNext( innerContainerWidth - outerRef.current.scrollLeft > outerContainerWidth );
};

handleScrollEvent();
Expand Down

0 comments on commit 2e08cd1

Please sign in to comment.