Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy rendering #150

Merged
merged 7 commits into from
Nov 14, 2023
Merged

Lazy rendering #150

merged 7 commits into from
Nov 14, 2023

Conversation

MaxiLein
Copy link
Contributor

Summary

  • Added Callback to Scrollbar that's invoked at specified scroll-position
  • Added Hook for lazy rendering
  • Added useIntervall lock for custom debounce

Details

Scrollbar:

import ScrollBar from '@/components/scrollbar';

// ...

/* Parent that specifies the size of the scrollable element */
<div style={{height: '200px', width: '200px'}}
    <ScrollBar
        width={'10px'} /* Width of the scrollbar */
        reachedEndCallBack={() => {console.log('Reached the threshold')}} /* Function that should be invoked if the scroll reaches the specified threshold; optional */
        threshold={0.5} /* After what % of scroll the callback should be invoked (0.5 = 50%); defaults to 0.8 */
        maxCallInterval={2_000} /* Time in ms that should be at least between invoking the callback; defaults to 500  */
    >
        {largeArray.map((item, i, arr) => (
            <SomeOtherComponent key={i}/>
        ))}
    </ScrollBar>
</div>

Lazy rendering:

import { useLazyLoading } from '@/components/scrollbar';

// ...
const watchThisElementRef = useRef<HTMLDivElement | null>(null);
 /* The next Scrollbar ancestor will be used as the viewport or, if there is none, the viewport of the client */
const shouldBeRendered= useLazyLoading(
        watchThisElementRef  /* what element to check for visibility */, 
        '100%' /* top and bottom margin of viewport in px or %, defaults to 50% */,
        false /* whether the returned value should remain true once it has been set to true, defaults to true */
    );

// ...

{ shouldBeRendered && <SomeExpensiveComponent/> }

Intervall Lock:

import { useIntervalLock } from '@/lib/useIntervalLock';

// ...
const foo = () => { console.log('FOO') }
const lockedFoo = useIntervalLock(foo, 5_000)
while (true) {
    lockedFoo() /* Will only invoke 'foo' every 5 sec */
}

@@ -87,7 +87,7 @@
}

.reduceLogo .bjs-powered-by svg {
width: 30px;
width: 20px;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added after meeting

@@ -22,7 +22,7 @@
.Content {
overflow-x: initial;
overflow-y: auto;
padding: 40px 20px;
padding: 10px 10px;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added after meeting

@OhKai OhKai merged commit 206cd6a into main Nov 14, 2023
10 checks passed
@OhKai OhKai deleted the lazy-rendering branch November 14, 2023 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants