Skip to content

Commit 53df4a9

Browse files
authored
Merge pull request #8622 from AyushKumarMishra123/fix/service-mesh-books-hover
fix(books): enable hover effect, side-card sliding, and 1-click open
2 parents 5f27251 + 71ef1db commit 53df4a9

2 files changed

Lines changed: 104 additions & 11 deletions

File tree

‎src/sections/Learn/Books-grid/BooksGrid.style.js‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@ export const BooksPageWrapper = styled.div`
1414
margin: -1.25rem auto 2rem;
1515
}
1616
.slick-slide{
17+
position: relative;
1718
filter: drop-shadow(0px 0px 8px gray) brightness(0.75);
19+
transition: filter 0.3s ease-in-out;
1820
.book_image{
19-
cursor: grab;
21+
cursor: pointer;
2022
}
2123
.book_image:active{
2224
cursor: grabbing;
2325
}
26+
&:hover,
27+
&:focus-within{
28+
z-index: 2;
29+
filter: drop-shadow(0px 0px 12px rgba(0, 179, 159, 0.45)) brightness(1);
30+
.book_image{
31+
transform: scale(1.06);
32+
}
33+
}
2434
}
2535
2636
.slick-current{
@@ -35,20 +45,38 @@ export const BooksPageWrapper = styled.div`
3545
z-index: 1;
3646
transform: scale(1.1);
3747
}
48+
&:hover,
49+
&:focus-within{
50+
.book_image{
51+
transform: scale(1.13);
52+
}
53+
}
54+
}
55+
56+
.book_image{
57+
position: relative;
58+
width: 100% !important;
59+
max-width: 30rem;
60+
transition: transform 0.35s ease-in-out;
3861
a{
3962
cursor: pointer;
4063
position: absolute;
4164
top: 0px;
4265
left: 0;
4366
bottom: 0;
4467
right: 0;
68+
&:focus-visible{
69+
outline: 2px solid #00b39f;
70+
outline-offset: 4px;
71+
}
4572
}
4673
}
4774
48-
.book_image{
49-
width: 100% !important;
50-
max-width: 30rem;
51-
transition: all 0.5s ease-in-out;
75+
@media (prefers-reduced-motion: reduce){
76+
.slick-slide,
77+
.book_image{
78+
transition: none;
79+
}
5280
}
5381
5482
/* Same slide padding as the 1050px block, so cover spacing is consistent above 1050px. */

‎src/sections/Learn/Books-grid/index.js‎

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11

2-
import React from "react";
2+
import React, { useEffect, useRef } from "react";
33
import { graphql, useStaticQuery } from "gatsby";
44
import PageHeader from "../../../reusecore/PageHeader";
55
import { Link } from "gatsby";
66
import Slider from "react-slick";
77
import { BooksPageWrapper } from "./BooksGrid.style";
88

99
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+
1025
const data = useStaticQuery(
1126
graphql`query allBooks {
1227
allMdx(
@@ -34,13 +49,56 @@ const BooksPage = ({ hide_path }) => {
3449

3550
let path = hide_path ? "" : "Books";
3651

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+
3794
var settings = {
38-
focusOnSelect: true,
95+
focusOnSelect: false,
3996
lazyLoad: true,
4097
infinite: false,
4198
speed: 400,
4299
slidesToShow: 3.5,
43100
slidesToScroll: 1,
101+
afterChange: handleAfterChange,
44102
responsive: [
45103
{
46104
breakpoint: 1400,
@@ -72,13 +130,20 @@ const BooksPage = ({ hide_path }) => {
72130
<PageHeader title="Service mesh with the best" path={path}/>
73131
<h2 className="sub-heading">Learn to service mesh from the world’s authority </h2>
74132
<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+
>
78143
<div className="blog-slider_img">
79144
<img src={frontmatter.cover.publicURL} alt={frontmatter.title} />
80145
</div>
81-
<Link className="book-page_link" to={fields.slug}/>
146+
<Link className="book-page_link" to={fields.slug} aria-label={frontmatter.title}/>
82147
</div>
83148
))}
84149
</Slider>

0 commit comments

Comments
 (0)