fix(tools): top category slider self-sliding sync, topbar blur removal, web grid filling & mobile section horizontal scroller

This commit is contained in:
2026-11-05 16:38:59 +05:30
parent 42bddad6b3
commit 8137c05e53
2 changed files with 151 additions and 58 deletions
+147 -54
View File
@@ -1,6 +1,6 @@
'use client';
import React, { useState, useMemo, useRef } from 'react';
import React, { useState, useMemo, useRef, useEffect, useCallback } from 'react';
import Link from 'next/link';
import {
Search,
@@ -298,6 +298,8 @@ export function ToolGridSection({
const [selectedCategory, setSelectedCategory] = useState<string>('all');
const [searchQuery, setSearchQuery] = useState<string>('');
const railRef = useRef<HTMLDivElement>(null);
const buttonRefs = useRef<Record<string, HTMLButtonElement | null>>({});
const isClickScrollingRef = useRef(false);
// Group all tools by category
const categorizedTools = useMemo(() => {
@@ -335,15 +337,22 @@ export function ToolGridSection({
});
}, [tools, searchQuery, locale]);
// Categories to display based on selectedCategory
const displayedCategories = useMemo(() => {
if (selectedCategory === 'all') {
return categorizedTools;
// Center a pill smoothly in the horizontal top rail
const centerPillInRail = useCallback((catId: string) => {
const pill = buttonRefs.current[catId];
const rail = railRef.current;
if (pill && rail) {
const pillLeft = pill.offsetLeft;
const pillWidth = pill.offsetWidth;
const railWidth = rail.offsetWidth;
rail.scrollTo({
left: Math.max(0, pillLeft - railWidth / 2 + pillWidth / 2),
behavior: 'smooth',
});
}
return categorizedTools.filter((group) => group.id === selectedCategory);
}, [categorizedTools, selectedCategory]);
}, []);
// Smooth scroll rail for categories
// Smooth scroll rail for manual chevron arrows
const scrollRail = (direction: 'left' | 'right') => {
if (railRef.current) {
railRef.current.scrollBy({
@@ -353,24 +362,79 @@ export function ToolGridSection({
}
};
// Fluid category tab selection with smooth pill centering
const handleCategoryClick = (
catId: string,
e?: React.MouseEvent<HTMLButtonElement>
) => {
// Fluid click handler: scrolls to category section on page and centers pill in rail
const handleCategoryClick = (catId: string) => {
setSelectedCategory(catId);
if (searchQuery) setSearchQuery('');
centerPillInRail(catId);
// Smoothly center the clicked pill in the scrolling rail
if (e?.currentTarget) {
e.currentTarget.scrollIntoView({
behavior: 'smooth',
inline: 'center',
block: 'nearest',
});
isClickScrollingRef.current = true;
if (catId === 'all') {
const el = document.getElementById('tools-catalog');
if (el) {
const y = el.getBoundingClientRect().top + window.pageYOffset - 90;
window.scrollTo({ top: Math.max(0, y), behavior: 'smooth' });
}
} else {
const el = document.getElementById(`category-${catId}`);
if (el) {
const headerOffset = 145; // Space for navbar + sticky category rail
const y = el.getBoundingClientRect().top + window.pageYOffset - headerOffset;
window.scrollTo({ top: Math.max(0, y), behavior: 'smooth' });
}
}
setTimeout(() => {
isClickScrollingRef.current = false;
}, 850);
};
// Synchronized Scroll-Spy: Top slider slides itself as categories slide into view
useEffect(() => {
if (searchQuery) return;
const handleScroll = () => {
if (isClickScrollingRef.current) return;
const headerOffset = 180;
const categoryIds = categorizedTools.map((g) => g.id);
// Check if at the top before the categories start
const catalogEl = document.getElementById('tools-catalog');
if (catalogEl) {
const catalogTop = catalogEl.getBoundingClientRect().top;
if (catalogTop > 100) {
if (selectedCategory !== 'all') {
setSelectedCategory('all');
centerPillInRail('all');
}
return;
}
}
// Check each category section in chronological order
let matchedCategory = 'all';
for (const catId of categoryIds) {
const el = document.getElementById(`category-${catId}`);
if (el) {
const rect = el.getBoundingClientRect();
if (rect.top <= headerOffset + 50 && rect.bottom >= 100) {
matchedCategory = catId;
}
}
}
if (matchedCategory !== selectedCategory) {
setSelectedCategory(matchedCategory);
centerPillInRail(matchedCategory);
}
};
window.addEventListener('scroll', handleScroll, { passive: true });
return () => window.removeEventListener('scroll', handleScroll);
}, [categorizedTools, searchQuery, selectedCategory, centerPillInRail]);
return (
<section id="tools-catalog" className="py-10 sm:py-16 bg-[#FAF8F5]">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
@@ -390,11 +454,11 @@ export function ToolGridSection({
</div>
)}
{/* Sticky Fluid Horizontal Scrolling Category Bar & Search */}
{/* Sticky Solid Category Bar & Search (Immune to topbar blur, solid bg-[#FAF8F5], z-40) */}
{(showCategoryFilter || showSearch) && (
<div className="sticky top-[64px] sm:top-[72px] z-30 mb-10 -mx-4 sm:-mx-6 lg:-mx-8 px-4 sm:px-6 lg:px-8 py-3 bg-[#FAF8F5]/95 backdrop-blur-md border-y border-slate-200/80 shadow-[0_4px_20px_-10px_rgba(0,0,0,0.06)]">
<div className="sticky top-[64px] sm:top-[72px] z-40 mb-10 -mx-4 sm:-mx-6 lg:-mx-8 px-4 sm:px-6 lg:px-8 py-3 bg-[#FAF8F5] border-y border-slate-200/90 shadow-sm">
<div className="max-w-7xl mx-auto flex flex-col md:flex-row items-stretch md:items-center justify-between gap-3 sm:gap-4">
{/* Category Rail with Fluid Scrolling */}
{/* Category Rail that slides itself smoothly */}
{showCategoryFilter && (
<div className="relative flex-1 flex items-center min-w-0">
{/* Left Scroll Arrow */}
@@ -414,7 +478,10 @@ export function ToolGridSection({
>
{/* All Categories Pill */}
<button
onClick={(e) => handleCategoryClick('all', e)}
ref={(el) => {
buttonRefs.current['all'] = el;
}}
onClick={() => handleCategoryClick('all')}
className={`shrink-0 flex items-center gap-2 px-4 py-2 rounded-full text-xs font-bold transition-all duration-200 shadow-sm cursor-pointer ${
selectedCategory === 'all' && !searchQuery
? 'bg-blue-600 text-white shadow-blue-500/25 ring-2 ring-blue-500/30 scale-[1.02]'
@@ -446,7 +513,10 @@ export function ToolGridSection({
return (
<button
key={group.id}
onClick={(e) => handleCategoryClick(group.id, e)}
ref={(el) => {
buttonRefs.current[group.id] = el;
}}
onClick={() => handleCategoryClick(group.id)}
className={`shrink-0 flex items-center gap-2 px-3.5 py-2 rounded-full text-xs font-bold transition-all duration-200 shadow-sm cursor-pointer ${
isSelected
? 'bg-blue-600 text-white shadow-blue-500/25 ring-2 ring-blue-500/30 scale-[1.02]'
@@ -507,7 +577,7 @@ export function ToolGridSection({
</div>
)}
{/* Content: Categorized Sections or Search Results */}
{/* Content: Search Results or Categorized Sections */}
{searchResults ? (
/* Search Results View */
<div className="space-y-6">
@@ -526,11 +596,26 @@ export function ToolGridSection({
</div>
{searchResults.length > 0 ? (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3 gap-6 sm:gap-7">
{searchResults.map((tool) => (
<ToolCard key={tool.id} tool={tool} locale={locale} />
))}
</div>
<>
{/* Mobile Scroller */}
<div className="sm:hidden -mx-4 px-4 overflow-x-auto no-scrollbar scroll-smooth snap-x snap-mandatory flex gap-4 pb-4 pt-1 touch-pan-x">
{searchResults.map((tool) => (
<div
key={tool.id}
className="w-[84vw] max-w-[320px] shrink-0 snap-start"
>
<ToolCard tool={tool} locale={locale} />
</div>
))}
</div>
{/* Web Grid View: Keeps filling naturally */}
<div className="hidden sm:grid sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3 gap-6 sm:gap-7">
{searchResults.map((tool) => (
<ToolCard key={tool.id} tool={tool} locale={locale} />
))}
</div>
</>
) : (
<div className="text-center py-16 text-slate-500 space-y-3 font-mono">
<p className="font-bold text-slate-800 text-base">
@@ -550,24 +635,11 @@ export function ToolGridSection({
)}
</div>
) : (
/* Categorized Tools Sections (Fluid, All 12 Tools Included) */
/* Categorized Sections:
- On phones (< sm): Each section has a smooth horizontal scroller
- On web (>= sm): Grid that keeps filling naturally as tools are added */
<div className="space-y-12 sm:space-y-16">
{/* Quick reset button if filtered to single category */}
{selectedCategory !== 'all' && (
<div className="flex items-center justify-between pb-2">
<button
onClick={() => setSelectedCategory('all')}
className="inline-flex items-center gap-1.5 text-xs font-mono font-bold text-blue-600 hover:text-blue-700 bg-blue-50 border border-blue-200 px-3 py-1.5 rounded-full transition-colors cursor-pointer shadow-sm"
>
<span>← Show All {tools.length} Tools</span>
</button>
<span className="text-xs text-slate-400 font-mono">
Filtered Category
</span>
</div>
)}
{displayedCategories.map((group) => {
{categorizedTools.map((group) => {
const theme = CATEGORY_THEMES[group.id];
const catName = group.category
? resolveLocalised(group.category.name, locale)
@@ -580,19 +652,19 @@ export function ToolGridSection({
<div
key={group.id}
id={`category-${group.id}`}
className="space-y-6"
className="scroll-mt-36 space-y-5 sm:space-y-6"
>
{/* Category Header */}
<div className="flex flex-col sm:flex-row sm:items-end justify-between gap-3 pb-4 border-b border-slate-200/90">
<div className="flex flex-col sm:flex-row sm:items-end justify-between gap-3 pb-3 sm:pb-4 border-b border-slate-200/90">
<div className="flex items-center gap-3">
<div
className={`w-11 h-11 rounded-2xl flex items-center justify-center bg-white shadow-sm border border-slate-200/80 shrink-0`}
className={`w-10 h-10 sm:w-11 sm:h-11 rounded-2xl flex items-center justify-center bg-white shadow-sm border border-slate-200/80 shrink-0`}
>
{theme?.icon || <Wand2 className="w-5 h-5 text-blue-600" />}
</div>
<div>
<div className="flex items-center gap-2.5">
<h3 className="text-xl sm:text-2xl font-display font-black text-slate-900 tracking-tight">
<h3 className="text-lg sm:text-2xl font-display font-black text-slate-900 tracking-tight">
{catName}
</h3>
<span
@@ -614,8 +686,29 @@ export function ToolGridSection({
</div>
</div>
{/* Category Tools Grid with Image-Dominant Cards */}
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3 gap-6 sm:gap-7">
{/* 1. Phone View (< sm): Horizontal Scroller for this particular section */}
<div className="sm:hidden relative">
<div className="-mx-4 px-4 overflow-x-auto no-scrollbar scroll-smooth snap-x snap-mandatory flex gap-4 pb-4 pt-1 touch-pan-x">
{group.tools.map((tool) => (
<div
key={tool.id}
className="w-[84vw] max-w-[320px] shrink-0 snap-start"
>
<ToolCard tool={tool} locale={locale} />
</div>
))}
</div>
{/* Swipe hint */}
{group.tools.length > 1 && (
<div className="flex items-center justify-between text-[11px] font-mono text-slate-600 px-1 mt-1">
<span>Swipe to see all {group.tools.length} tools</span>
<span className="font-bold">→</span>
</div>
)}
</div>
{/* 2. Web View (>= sm): Grid that keeps filling naturally as tools are added */}
<div className="hidden sm:grid sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3 gap-6 sm:gap-7">
{group.tools.map((tool) => (
<ToolCard key={tool.id} tool={tool} locale={locale} />
))}