Skip to content

Commit

Permalink
Merge pull request #155 from Games-of-Switzerland/feature/150--popove…
Browse files Browse the repository at this point in the history
…r-games-teaser-genres-platforms

change Game teaser using Popover for multiple genres & platforms - #150
  • Loading branch information
WengerK authored Mar 4, 2024
2 parents eecdff4 + 0d6728b commit 18e9860
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- add missing platforms (acorn_archimedes, AppleIIGS, CDC1604 & playdate) translations - Games-of-Switzerland/swissgamesgarden#149

### Changed
- improve display of studioes & members on Game detail page - Games-of-Switzerland/swissgamesgarden#140
- improve display of studios & members on Game detail page - Games-of-Switzerland/swissgamesgarden#140
- change Game teaser using Popover for multiple genres & platforms - Games-of-Switzerland/swissgamesgarden#150

### Fixed
- fix placeholder image still visible when images are loaded from Browser cache
Expand Down
51 changes: 37 additions & 14 deletions src/components/Game/Teaser/GameTeaser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import Link from 'next/link';
import classNames from 'classnames';
import Image from 'components/Image';
import Popover from 'components/Popover';
import {useTranslation} from 'react-i18next';

const GameTeaser = ({game}) => {
Expand Down Expand Up @@ -46,25 +47,47 @@ const GameTeaser = ({game}) => {

{releases.length > 0 && (
<div className="flex flex-wrap gap-1">
{releases.map(({platform_slug}) => (
<Link href={`/?platforms[]=${platform_slug}`} key={platform_slug}>
<a className="inline-block leading-none p-1 border border-gray-850 text-gray-500 font-light hover:border-gray-550 hover:text-white relative z-10">
{t(`platforms.${platform_slug}`)}
</a>
</Link>
))}
<Link href={`/?platforms[]=${releases[0].platform_slug}`} key={releases[0].platform_slug}>
<a
className="inline-block leading-none p-1 border border-gray-850 text-gray-500 font-light hover:border-gray-550 hover:text-white relative z-10">
{t(`platforms.${releases[0].platform_slug}`)}
</a>
</Link>
{releases.length > 1 && (
<Popover
className="z-20"
classNamePopover="-mt-14"
trigger="hover"
content={
<p>{releases.slice(1).map(release => t(`platforms.${release.platform_slug}`)).join(', ', )}</p>
}
>
<span className="p-1 pl-0 text-sm text-gray-500 z-10">+{releases.length - 1}</span>
</Popover>
)}
</div>
)}

{genres.length > 0 && (
<div className="flex flex-wrap gap-1 mt-1">
{genres.map(({slug}) => (
<Link href={`/?genres[]=${slug}`} key={slug}>
<a className="font-light border-b border-dotted border-gray-700 text-gray-500 hover:text-white hover:border-gray-450 relative z-10 mb-1">
{t(`genres.${slug}`)}
</a>
</Link>
))}
<Link href={`/?genres[]=${genres[0].slug}`} key={genres[0].slug}>
<a
className="font-light border-b border-dotted border-gray-700 text-gray-500 hover:text-white hover:border-gray-450 relative z-10 mb-1">
{t(`genres.${genres[0].slug}`)}
</a>
</Link>
{genres.length > 1 && (
<Popover
className="z-20"
classNamePopover="-mt-14"
trigger="hover"
content={
<p>{genres.slice(1).map(genre => t(`genres.${genre.slug}`)).join(', ', )}</p>
}
>
<span className="p-1 pl-0 text-sm text-gray-500 z-10">+{genres.length - 1}</span>
</Popover>
)}
</div>
)}
</div>
Expand Down
58 changes: 58 additions & 0 deletions src/components/Popover.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useEffect, useRef, useState } from "react";

function Popover({children, content, trigger = "click", className = "", classNamePopover = ""}) {
const [show, setShow] = useState(false);
const wrapperRef = useRef(null);

const handleMouseOver = () => {
if (trigger === "hover") {
setShow(true);
};
};

const handleMouseLeft = () => {
if (trigger === "hover") {
setShow(false);
};
};

useEffect(() => {
function handleClickOutside(event) {
if (wrapperRef.current && !wrapperRef.current.contains(event.target)) {
setShow(false);
}
}

if (show) {
// Bind the event listener
document.addEventListener("mousedown", handleClickOutside);
return () => {
// Unbind the event listener on clean up
document.removeEventListener("mousedown", handleClickOutside);
};
}
}, [show, wrapperRef]);

return (
<div
ref={wrapperRef}
onMouseEnter={handleMouseOver}
onMouseLeave={handleMouseLeft}
className={`${className} w-fit h-fit relative flex justify-center`}>
<div
onClick={() => setShow(!show)}
>
{children}
</div>
<div
hidden={!show}
className={`${classNamePopover} min-w-max w-[200px] h-fit absolute bottom-[100%] z-50 transition-all`}>
<div className="rounded bg-white p-3 shadow-[10px_30px_150px_rgba(46,38,92,0.25)] mb-[10px]">
{content}
</div>
</div>
</div>
);
};

export default Popover;

0 comments on commit 18e9860

Please sign in to comment.