|
1 | 1 |
|
2 | | -import React from "react"; |
| 2 | +import React, { useEffect, useRef } from "react"; |
3 | 3 | import { graphql, useStaticQuery } from "gatsby"; |
4 | 4 | import PageHeader from "../../../reusecore/PageHeader"; |
5 | 5 | import { Link } from "gatsby"; |
6 | 6 | import Slider from "react-slick"; |
7 | 7 | import { BooksPageWrapper } from "./BooksGrid.style"; |
8 | 8 |
|
9 | 9 | const BooksPage = ({ hide_path }) => { |
| 10 | + const sliderRef = useRef(null); |
| 11 | + const isSlidingRef = useRef(false); |
| 12 | + const hoveredCardRef = useRef(null); |
| 13 | + const lastSlidIndexRef = useRef(null); |
| 14 | + const slideTimerRef = useRef(null); |
| 15 | + |
| 16 | + const clearSlideTimer = () => { |
| 17 | + if (slideTimerRef.current) { |
| 18 | + clearTimeout(slideTimerRef.current); |
| 19 | + slideTimerRef.current = null; |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + useEffect(() => clearSlideTimer, []); |
| 24 | + |
10 | 25 | const data = useStaticQuery( |
11 | 26 | graphql`query allBooks { |
12 | 27 | allMdx( |
@@ -34,13 +49,56 @@ const BooksPage = ({ hide_path }) => { |
34 | 49 |
|
35 | 50 | let path = hide_path ? "" : "Books"; |
36 | 51 |
|
| 52 | + const slideIfClipped = (index, card) => { |
| 53 | + if (isSlidingRef.current || !sliderRef.current || !card) return; |
| 54 | + const list = card.closest(".slick-list"); |
| 55 | + if (!list) return; |
| 56 | + |
| 57 | + const cardRect = card.getBoundingClientRect(); |
| 58 | + const listRect = list.getBoundingClientRect(); |
| 59 | + const isClippedOnSide = |
| 60 | + cardRect.right > listRect.right - 2 || cardRect.left < listRect.left + 2; |
| 61 | + |
| 62 | + if (isClippedOnSide) { |
| 63 | + isSlidingRef.current = true; |
| 64 | + lastSlidIndexRef.current = index; |
| 65 | + sliderRef.current.slickGoTo(index); |
| 66 | + clearSlideTimer(); |
| 67 | + slideTimerRef.current = setTimeout(() => { |
| 68 | + slideTimerRef.current = null; |
| 69 | + isSlidingRef.current = false; |
| 70 | + }, 450); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + const handleBookHover = (index, event) => { |
| 75 | + hoveredCardRef.current = { index, element: event.currentTarget }; |
| 76 | + slideIfClipped(index, event.currentTarget); |
| 77 | + }; |
| 78 | + |
| 79 | + const handleBookLeave = (event) => { |
| 80 | + if (hoveredCardRef.current?.element === event.currentTarget) { |
| 81 | + hoveredCardRef.current = null; |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + const handleAfterChange = () => { |
| 86 | + clearSlideTimer(); |
| 87 | + isSlidingRef.current = false; |
| 88 | + const hovered = hoveredCardRef.current; |
| 89 | + if (hovered && hovered.index !== lastSlidIndexRef.current) { |
| 90 | + slideIfClipped(hovered.index, hovered.element); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
37 | 94 | var settings = { |
38 | | - focusOnSelect: true, |
| 95 | + focusOnSelect: false, |
39 | 96 | lazyLoad: true, |
40 | 97 | infinite: false, |
41 | 98 | speed: 400, |
42 | 99 | slidesToShow: 3.5, |
43 | 100 | slidesToScroll: 1, |
| 101 | + afterChange: handleAfterChange, |
44 | 102 | responsive: [ |
45 | 103 | { |
46 | 104 | breakpoint: 1400, |
@@ -72,13 +130,20 @@ const BooksPage = ({ hide_path }) => { |
72 | 130 | <PageHeader title="Service mesh with the best" path={path}/> |
73 | 131 | <h2 className="sub-heading">Learn to service mesh from the world’s authority </h2> |
74 | 132 | <div className="books_caraousel"> |
75 | | - <Slider {...settings}> |
76 | | - {data.allMdx.nodes.map(({ id, frontmatter, fields }) => ( |
77 | | - <div className="book_image" key={id}> |
| 133 | + <Slider ref={sliderRef} {...settings}> |
| 134 | + {data.allMdx.nodes.map(({ id, frontmatter, fields }, index) => ( |
| 135 | + <div |
| 136 | + className="book_image" |
| 137 | + key={id} |
| 138 | + onMouseEnter={(event) => handleBookHover(index, event)} |
| 139 | + onMouseLeave={handleBookLeave} |
| 140 | + onFocus={(event) => handleBookHover(index, event)} |
| 141 | + onBlur={handleBookLeave} |
| 142 | + > |
78 | 143 | <div className="blog-slider_img"> |
79 | 144 | <img src={frontmatter.cover.publicURL} alt={frontmatter.title} /> |
80 | 145 | </div> |
81 | | - <Link className="book-page_link" to={fields.slug}/> |
| 146 | + <Link className="book-page_link" to={fields.slug} aria-label={frontmatter.title}/> |
82 | 147 | </div> |
83 | 148 | ))} |
84 | 149 | </Slider> |
|
0 commit comments