-
Notifications
You must be signed in to change notification settings - Fork 91
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
Intersection observer #285
Comments
I think you wanted const Thumb = () => div( should be const Thumb = div( |
Thanks, that worked for intersection Observer. In this case, I want the GalleryThumbs to react when their canvas.id === selectedCanvasID, but not sure how to go about it. Are these still reactive? let selectedCanvasID = vanX.reactive('123')
const GalleryThumbs = () => {
prnt('selected id', selectedCanvasID)
return vanX.list(() => div({ id: 'galleryGrid' }), canvases, ({ val: canvas }) =>
GalleryThumb(canvas)
)
}
const GalleryThumb = (canvas) => {
const selected = selectedCanvasID === canvas.id
prnt('GALLERY', selected)
const Thumb = div(
{
id: `galleryThumb${canvas.id}`,
class: `galleryThumb ${selectedCanvasID === canvas.id ? 'selected' : ''}`,
onclick: (e) => {
if (selectedCanvasID === canvas.id) { goPaint(); loadFromID(canvas.id); return }
selectedCanvasID = canvas.id
prnt('selected canvas id', canvas.id, e.target)
},
},
div({
class: 'image',
loading: 'lazy',
}),
div({ class: `hoverable deleteButton ${selectedCanvasID === canvas.id ? '' : 'hidden'}` }),
div({ class: 'hoverable playButton hidden' }),
div({ class: 'hoverable restoreButton hidden' })
)
const galleryObserver = createGalleryObserver(canvas)
galleryObserver.observe(Thumb)
return Thumb
} |
|
Thanks, I've tried switching to van.state, but the gallery thumbs are not rerendering when selectedCanvasID is changed. This fires when they are first generated, but never again. This fires when gallery thumb is clicked, but does not cause rerender (which I want) is there a way to force rerender thumbs when selectedCanvasID is changed? (only 1 should be selected at a time) let selectedCanvasID = van.state('123')
const GalleryThumbs = () => {
prnt('gallery thumbs', selectedCanvasID)
return vanX.list(() => div({ id: 'galleryGrid' }), canvases, ({ val: canvas }) =>
GalleryThumb(canvas)
)
}
const GalleryThumb = (canvas) => {
prnt('gallery thumb rendered', canvas.id, selectedCanvasID)
const Thumb = div(
{
id: `galleryThumb${canvas.id}`,
class: `galleryThumb ${selectedCanvasID === canvas.id ? 'selected' : ''}`,
onclick: (e) => {
if (selectedCanvasID === canvas.id) { goPaint(); loadFromID(canvas.id); return }
selectedCanvasID = canvas.id
prnt('selected canvas id', canvas.id, e.target)
},
},
div({
class: 'image',
loading: 'lazy',
}),
div({ class: `hoverable deleteButton ${selectedCanvasID === canvas.id ? '' : 'hidden'}` }),
div({ class: `hoverable playButton ${selectedCanvasID === canvas.id ? '' : 'hidden'}` }),
div({ class: `hoverable restoreButton ${selectedCanvasID === canvas.id ? '' : 'hidden'}` }),
)
const galleryObserver = createGalleryObserver(canvas)
galleryObserver.observe(Thumb)
return Thumb
} |
I think you should use |
Thanks, .val works! Now, when the selectedCanvasID changes, the thumbnail image flashes, is there a way to not rerender the image when selectedCanvasID is changed? (it should only be handled by galleryObserver and not affected by state) let selectedCanvasID = van.state('123')
const GalleryThumbs = () => {
prnt('gallery thumbs', selectedCanvasID)
return vanX.list(() => div({ id: 'galleryGrid' }), canvases, ({ val: canvas }) =>
GalleryThumb(canvas)
)
}
const GalleryThumb = (canvas) => {
const Thumb = div(
{
id: `galleryThumb${canvas.id}`,
class: `galleryThumb ${selectedCanvasID.val === canvas.id ? 'selected' : ''}`,
onclick: (e) => {
if (selectedCanvasID.val === canvas.id) { goPaint(); loadFromID(canvas.id); return }
selectedCanvasID.val = canvas.id
prnt('selected canvas id', canvas.id, e.target)
},
},
div({
class: 'image',
loading: 'lazy',
}),
div({ class: `hoverable deleteButton ${selectedCanvasID.val === canvas.id ? '' : 'hidden'}` }),
div({ class: `hoverable playButton ${selectedCanvasID.val === canvas.id ? '' : 'hidden'}` }),
)
const galleryObserver = createGalleryObserver(canvas)
galleryObserver.observe(Thumb)
return Thumb
}
van.add(galleryGridContainer, GalleryThumbs()) |
I'm confused. Do you want |
I want class galleryThumb, deleteButton and playButton to be reactive to selectedCanvasID |
My understanding is class image shouldn't be reactive to the |
Here's the intersection observer, I think this is everything related to the gallery thumb. is the observer being recreated on each change of selectedCanvasID? const createGalleryObserver = (canvas) => {
const options = {
root: null,
rootMargin: '0px',
threshold: 0.1,
}
const callback = (entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const imageElement = entry.target.querySelector('.image')
if (imageElement) {
getLatestAsset(canvas.id, 'image').then((imageArrayBuffer) => {
const imageBlob = arrayToBlob(imageArrayBuffer, 'image/webp').then(
(blob) => {
const imageUrl = URL.createObjectURL(blob)
imageElement.style.backgroundImage = `url(${imageUrl})`
}
)
})
}
}
})
}
return new IntersectionObserver(callback, options)
} |
Calling |
How would you set up an intersection observer with vanJS? I have gallery of image thumbnails and would like to dynamically load/unload them when they come into view with intersection observer
galleryObserver.observe(Thumb) throws TypeError: Failed to execute 'observe' on 'IntersectionObserver': parameter 1 is not of type 'Element'.
The text was updated successfully, but these errors were encountered: