Skip to content

Commit

Permalink
fix(model-viewer): handle resize
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Jan 28, 2024
1 parent 118f100 commit 878b724
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions resources/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,33 @@ createInertiaApp({
},
})

window.requestIdleCallback(() => {
if (!window.matchMedia('(min-width: 768px)').matches) {
let modelViewerScriptLoaded = false

function loadModelViewerScript() {
if (!window.matchMedia('(min-width: 768px)').matches || modelViewerScriptLoaded) {
return
}

const scriptEl = document.createElement('script')
scriptEl.src = 'https://ajax.googleapis.com/ajax/libs/model-viewer/3.3.0/model-viewer.min.js'
scriptEl.async = true
scriptEl.type = 'module'
scriptEl.onload = () => {
const modelViewerEls = document.querySelectorAll('model-viewer[data-not-loaded]')

for (const modelViewerEl of modelViewerEls) {
modelViewerEl.removeAttribute('data-not-loaded')
}
}

document.body.appendChild(scriptEl)
modelViewerScriptLoaded = true
}

window.requestIdleCallback(() => {
loadModelViewerScript()
})

window.addEventListener('resize', () => {
loadModelViewerScript()
})

0 comments on commit 878b724

Please sign in to comment.