covid19india-react icon indicating copy to clipboard operation
covid19india-react copied to clipboard

Stick the table headings to the top while scrolling down

Open Sayan-Nandy opened this issue 3 years ago • 11 comments

I can't remember which column stands for what when I scroll down, so it might be helpful to stick the table headers to the top when scrolling down. This is especially helpful in mobiles. Screenshot_20210424_123957

Sayan-Nandy avatar Apr 24 '21 07:04 Sayan-Nandy

/claim

KANE-99 avatar May 18 '21 07:05 KANE-99

/available

shuklaayush avatar Jun 05 '21 06:06 shuklaayush

This issue is available for contribution.

Please reply "/claim" (without quotes) if you wish to work on this issue.

github-actions[bot] avatar Jun 05 '21 06:06 github-actions[bot]

/claim

singharyan1007 avatar Jun 05 '21 11:06 singharyan1007

Thank you @singharyan1007 for claiming this issue! 🎉

Please reference this issue when you submit your Pull Request and make sure you follow the contributing guidelines.

github-actions[bot] avatar Jun 05 '21 11:06 github-actions[bot]

/claim

nandirishav avatar Jul 11 '21 15:07 nandirishav

Because of left-column being sticky we have to use JS magic for this. I don't have time to integrate in project at moment, but here's some example working code (not fully optimised, mind you):

(function(){
    // Get table and header refs
    // TODO: Make headers dynamic as table changes
    let table = document.querySelector('.table-container>.table');
    let headers = document.querySelectorAll('.table-container>.table>.row.heading:first-child>.cell');
    
    // Set config values, set the special z indices for the header divs
    const stickBottomLimit = 150;
    const firstHeaderZ = 140;
    const otherHeaderZ = 120;
    headers.forEach((el, index)=>{
        el.style.zIndex = index==0 ? firstHeaderZ : otherHeaderZ;
    });

    // Build the handler that makes headers stick
    const handleScroll = ()=>{
        const rect = table.getBoundingClientRect();
        let offset = 0;
        if (rect.top < 0) {
            offset = Math.min(-rect.top, rect.height-stickBottomLimit);
        }
        headers.forEach((el)=>{
            el.style.transform = 'translateY('+offset+'px)';
        });
    };

    // Build debounced scroll listener
    let ticking = false;
    document.addEventListener('scroll', (e)=>{
        if (!ticking) {
            window.requestAnimationFrame(()=>{
                handleScroll();
                ticking = false;
            });
            ticking = true;
        }
    });
})();

Hopefully one of the claimants can integrate in React and add a pull request :D

n5r avatar Aug 17 '21 10:08 n5r

What is the status to this? Is this done? I would like to contribute to it.

sayna3311 avatar Aug 28 '21 10:08 sayna3311

@sayna3311 None of the original claimants has responded, so if you can integrate the code into React it would be great!

n5r avatar Aug 28 '21 11:08 n5r

Sure thing, you can assign it to me. Thank you.

sayna3311 avatar Aug 28 '21 13:08 sayna3311

@n5r @shuklaayush I have implemented your method in react using useEffect. Here is the demo

https://user-images.githubusercontent.com/67572440/131243014-5016847c-9ddd-4e6c-8ca3-6cccae71218b.mp4

I'm sending the PR, please review.

sayna3311 avatar Aug 29 '21 07:08 sayna3311