mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-03 18:22:57 +00:00
4.4 KiB
4.4 KiB
Admin Navigation Auto-Centering Implementation
Overview
The admin sidebar now automatically centers the current page navigation item in view, providing clear visual feedback about the user's current location in the admin panel.
How It Works
1. Hook Implementation
- File:
frontend/src/hooks/useAdminNavScrollRetention.ts - Purpose: Automatically centers the current page's nav item in the sidebar viewport
- Trigger: On route changes and component mount
2. Centering Logic
// Calculate scroll position to center the item
const targetScrollTop = itemTopRelativeToContainer - (containerHeight / 2) + (itemHeight / 2);
// Ensure we don't scroll beyond bounds
const finalScrollTop = Math.max(0, Math.min(targetScrollTop, maxScrollTop));
// Smooth scroll to position
container.scrollTo({
top: finalScrollTop,
behavior: 'smooth'
});
3. Navigation Matching
- Exact match:
/admin/sponzorimatcheshref="/admin/sponzori" - Partial match:
/admin/scoreboard/remotematcheshref="/admin/scoreboard" - Priority: Exact matches take precedence over partial matches
Features
✅ Automatic Centering
- Centers the current page item in the sidebar viewport
- Uses smooth scrolling for polished user experience
- Handles edge cases (top/bottom boundaries)
✅ Smart Matching
- Exact path matching for most pages
- Partial matching for nested routes (e.g., scoreboard remote)
- Fallback handling for missing nav items
✅ Performance Optimized
- Debounced scrolling to prevent conflicts
- Proper cleanup of event listeners
- Minimal DOM queries
✅ Debug Support
- Development mode logging
- Detailed calculation output
- Test script included
Testing
Manual Testing
- Navigate to different admin pages
- Observe sidebar auto-centering the current page item
- Check smooth scrolling behavior
- Test edge cases (first/last items)
Automated Testing
Run the test script in browser console:
// Copy and paste contents of test-scroll-retention.js
Expected Behavior
/admin→ Centers "Nástěnka" item/admin/sponzori→ Centers "Sponzoři" item/admin/analytika→ Centers "Analytika" item/admin/scoreboard/remote→ Centers "Scoreboard Remote" item
File Changes
Modified Files
-
frontend/src/hooks/useAdminNavScrollRetention.ts- Complete rewrite for auto-centering functionality
- Removed scroll position storage/restoration
- Added nav item detection and centering logic
-
frontend/src/components/admin/AdminSidebar.tsx- Updated to use new hook API
- Removed manual scroll save/restore logic
- Simplified NavItem interface
-
frontend/src/i18n/index.ts- Fixed duplicate
tablesproperty causing build errors
- Fixed duplicate
Added Files
test-scroll-retention.js- Comprehensive test script for validation
- Can be run in browser console
- Tests multiple scenarios and edge cases
Configuration Options
The hook accepts these options:
{
scrollContainerSelector?: string; // Default: '[data-sidebar="true"]'
navItemSelector?: string; // Default: 'a[href*="/admin/"]'
enableDebug?: boolean; // Default: false
}
Troubleshooting
Common Issues
-
Auto-centering not working
- Check if sidebar has
data-sidebar="true"attribute - Verify nav items have correct
hrefattributes - Check browser console for debug messages
- Check if sidebar has
-
Scrolling to wrong position
- Ensure sidebar has proper height/scroll dimensions
- Check for CSS conflicts affecting positioning
- Verify container boundaries calculation
-
Performance issues
- Check for excessive re-renders
- Verify event listener cleanup
- Monitor for memory leaks
Debug Mode
Enable debug logging by setting:
useAdminNavScrollRetention({
enableDebug: true
});
Or use development environment (automatically enables debug).
Future Enhancements
- Animation customization: Allow custom scroll behavior
- Position memory: Remember user's preferred scroll position
- Keyboard navigation: Support arrow key navigation with centering
- Mobile optimization: Different behavior for mobile viewports
- Accessibility: ARIA labels and screen reader support
Browser Compatibility
- ✅ Chrome 61+
- ✅ Firefox 36+
- ✅ Safari 14+
- ✅ Edge 79+
Uses standard scrollTo() API with behavior: 'smooth' option.