Skip to content

Commit c4d2545

Browse files
committed
feat: refactor placeholder
1 parent 7eab822 commit c4d2545

File tree

5 files changed

+39
-33
lines changed

5 files changed

+39
-33
lines changed

src/components/LazyLoadComponent.tsx

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { reactive, computed, watch, defineComponent } from 'vue'
1+
import { reactive, computed, watch, defineComponent, Fragment } from 'vue'
22
import PlaceholderWithoutTracking from './PlaceholderWithoutTracking.tsx'
33
import isIntersectionObserverAvailable from '../utils/intersection-observer';
44
import PlaceholderWithTracking from './PlaceholderWithTracking.tsx'
@@ -18,7 +18,9 @@ import PlaceholderWithTracking from './PlaceholderWithTracking.tsx'
1818
// threshold: number
1919
// }
2020
export default defineComponent({
21+
compatConfig: { MODE: 3 },
2122
name: 'LazyLoadComponent',
23+
inheritAttrs: false,
2224
props: {
2325
afterLoad: {
2426
type: Function,
@@ -72,9 +74,8 @@ export default defineComponent({
7274
type: Object,
7375
default: () => { }
7476
}
75-
7677
},
77-
setup(props, { slots, attrs }) {
78+
setup(props, { slots }) {
7879
const state = reactive({
7980
visible: props.visibleByDefault ?? false,
8081
loaded: false,
@@ -98,7 +99,6 @@ export default defineComponent({
9899
)
99100

100101
const onVisible = () => {
101-
if (state.loaded) return
102102
state.loaded = true
103103
state.visible = true
104104
props.afterLoad?.()
@@ -108,37 +108,31 @@ export default defineComponent({
108108
props.afterLoad?.()
109109
props.afterLoad?.()
110110
}
111-
112111
return () => {
112+
if (state.visible) {
113+
return slots.default?.()
114+
}
113115
return isScrollTracked ||
114116
(props.useIntersectionObserver && isIntersectionObserverAvailable()) ?
115-
<>
116-
<PlaceholderWithoutTracking
117-
height={props.height}
118-
onVisible={onVisible}
119-
// placeholder={props.placeholder}
120-
scrollPosition={props.scrollPosition}
121-
threshold={props.threshold}
122-
useIntersectionObserver={props.useIntersectionObserver}
123-
width={props.width}
124-
>
125-
{slots.default?.()}
126-
</PlaceholderWithoutTracking>
127-
</>
117+
<PlaceholderWithoutTracking
118+
height={props.height}
119+
onVisible={onVisible}
120+
placeholder={props.placeholder}
121+
scrollPosition={props.scrollPosition}
122+
threshold={props.threshold}
123+
useIntersectionObserver={props.useIntersectionObserver}
124+
width={props.width}
125+
/>
128126
:
129-
<>
130-
<PlaceholderWithTracking
131-
height={props.height}
132-
onVisible={onVisible}
133-
// placeholder={props.placeholder}
134-
scrollPosition={props.scrollPosition}
135-
threshold={props.threshold}
136-
useIntersectionObserver={props.useIntersectionObserver}
137-
width={props.width}
138-
>
139-
{slots.default?.()}
140-
</PlaceholderWithTracking>
141-
</>
127+
<PlaceholderWithTracking
128+
height={props.height}
129+
onVisible={onVisible}
130+
placeholder={props.placeholder}
131+
scrollPosition={props.scrollPosition}
132+
threshold={props.threshold}
133+
useIntersectionObserver={props.useIntersectionObserver}
134+
width={props.width}
135+
/>
142136
}
143137
}
144138
})

src/components/LazyLoadImage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import LazyLoadComponent from './LazyLoadComponent.jsx';
33

44
export default defineComponent({
55
name: 'LazyLoadImage',
6+
compatConfig: { MODE: 3 },
67
props: {
78
afterLoad: {
89
type: Function,

src/components/PlaceholderWithTracking.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const PlaceholderWithTracking = defineComponent({
2727
width: {
2828
type: Number,
2929
default: 0
30+
},
31+
placeholder:{
32+
type: Object,
33+
default: () => { }
3034
}
3135
},
3236
setup(props, { attrs }) {

src/components/PlaceholderWithoutTracking.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export default defineComponent({
4343
width: {
4444
type: Number,
4545
default: 0
46+
},
47+
placeholder: {
48+
type: Object,
49+
default: () => { }
4650
}
4751
},
4852
setup(props, { slots }) {
@@ -75,7 +79,6 @@ export default defineComponent({
7579
if (typeof window === 'undefined' || placeholder.value) {
7680
return false;
7781
}
78-
7982
const boundingBox = getPlaceholderBoundingBox(props.scrollPosition);
8083
const viewport = {
8184
bottom: props.scrollPosition.y + window.innerHeight,
@@ -121,7 +124,7 @@ export default defineComponent({
121124
ref={placeholder}
122125
style={styleProp.value}
123126
>
124-
{slots.default?.()}
127+
{props.placeholder}
125128
</span>
126129
)
127130
}

src/components/warpper.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export const WrappedComponent = (Component: any) => {
3939
delayTime: {
4040
type: Number,
4141
default: 300
42+
},
43+
placeholder: {
44+
type: Object,
45+
default: () => { }
4246
}
4347
},
4448
setup(props, { attrs }) {

0 commit comments

Comments
 (0)