Skip to content

Commit

Permalink
refactor: rename InvisibleComponent to fallback
Browse files Browse the repository at this point in the history
BREAKING CHANGE: You should rename `InvisibleComponent` to `fallback` .
  • Loading branch information
hiroki0525 committed Oct 19, 2022
1 parent 3dc2ee4 commit 0b25cfe
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const App = () => (
{/* This will optimize Core Web Vitals */}
<LazyLoad
as='footer'
InvisibleComponent={<Loading />}
fallback={<Loading />}
rootMargin='200px 0px'
suspense
>
Expand All @@ -51,7 +51,7 @@ ReactDOM.render(<App />, document.body);
| Name | Required | Type | Default | Description |
|--------------------|----------|------------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| children | Yes | ReactNode | - | Component is rendered when it is in the viewport. Automatically enable `React.Suspense` if you use `React.lazy` . |
| InvisibleComponent | No | ReactNode | null | Component is rendered when it is not in the viewport. |
| fallback | No | ReactNode | null | Component is rendered when it is not in the viewport. |
| rootId | No | string | - | The id of element which is `IntersectionObserver`'s target. If `rootId` is not specified, then the bounds of the actual document viewport are used. This prop wraps [IntersectionObserver.root](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/root) because of performance. |
| rootMargin | No | string | - | Please see [IntersectionObserver.rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin). |
| threshold | No | number &#124; number[] | - | Please see [IntersectionObserver.thresholds](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/thresholds). |
Expand Down
2 changes: 1 addition & 1 deletion examples/src/site/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function Main() {
<LazyLoad
key={index}
rootId='cat-list'
InvisibleComponent={<Loading />}
fallback={<Loading />}
as='li'
>
<CatItem name={name} />
Expand Down
2 changes: 1 addition & 1 deletion examples/src/site/Site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function Site() {
<h1>Cats Lovers</h1>
</header>
<Main />
<LazyLoad InvisibleComponent={<Loading />} as='footer'>
<LazyLoad fallback={<Loading />} as='footer'>
<Footer />
</LazyLoad>
</>
Expand Down
2 changes: 1 addition & 1 deletion examples/src/usage/ForceVisibleExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function ForceVisibleExample() {
{[...Array(5)].map((_, index) => (
<LazyLoad
key={`demo3-${index}`}
InvisibleComponent='Invisible'
fallback='Invisible'
rootId='forceVisible'
as='li'
className='row'
Expand Down
6 changes: 3 additions & 3 deletions examples/src/usage/Usage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function Usage() {
{[...Array(10)].map((_, index) => (
<LazyLoad
key={`demo1-${index}`}
InvisibleComponent='Invisible'
fallback='Invisible'
rootId='onceIsTrue'
as='li'
className='row'
Expand All @@ -30,7 +30,7 @@ export default function Usage() {
{[...Array(10)].map((_, index) => (
<LazyLoad
key={`demo2-${index}`}
InvisibleComponent='Invisible'
fallback='Invisible'
rootId='onceIsFalse'
as='li'
className='row'
Expand All @@ -45,7 +45,7 @@ export default function Usage() {
<h2>Suspense</h2>
<LazyLoad
id='suspense'
InvisibleComponent='Invisible'
fallback='Invisible'
rootMargin='200px 0px'
suspense
>
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('LazyLoad', () => {
const defaultProps: ExcludeChildrenLazyLoadProps = {
rootId,
'data-testid': testId,
InvisibleComponent: invisibleText,
fallback: invisibleText,
};

afterEach(() => {
Expand Down
8 changes: 4 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'react';

export type LazyLoadProps = {
InvisibleComponent?: ReactNode;
fallback?: ReactNode;
children: ReactNode;
as?: ElementType;
forceVisible?: boolean;
Expand All @@ -22,7 +22,7 @@ export type LazyLoadProps = {
} & Omit<IntersectionObserverInit, 'root'>;

export default function LazyLoad({
InvisibleComponent = null,
fallback,
children,
forceVisible = false,
rootId,
Expand Down Expand Up @@ -88,12 +88,12 @@ export default function LazyLoad({
};
}, [once, rootMargin, threshold]);

const displayComponent = isVisible ? children : InvisibleComponent;
const displayComponent = isVisible ? children : fallback;

return (
<Tag ref={targetRef} {...props}>
{suspense ? (
<Suspense fallback={InvisibleComponent}>{displayComponent}</Suspense>
<Suspense fallback={fallback}>{displayComponent}</Suspense>
) : (
displayComponent
)}
Expand Down

0 comments on commit 0b25cfe

Please sign in to comment.