Skip to content

Instantly share code, notes, and snippets.

@leemartin
Created March 30, 2024 14:00
Show Gist options
  • Save leemartin/6f9bab074f36e74a1a6034f14697bb2c to your computer and use it in GitHub Desktop.
Save leemartin/6f9bab074f36e74a1a6034f14697bb2c to your computer and use it in GitHub Desktop.
Device Orientation Composable for Nuxt
export const useOrientation = () => {
// Start orientation
// ----------
const start = async () => {
// Promise
return new Promise((resolve, revoke) => {
// If request permission exists
if (window.DeviceOrientationEvent && typeof window.DeviceOrientationEvent.requestPermission === 'function') {
// Request permission
window.DeviceOrientationEvent.requestPermission()
.then(response => {
// Log
console.log('orientation permission: ', response)
// If granted
if (response == "granted") {
// Resolve
resolve()
} else {
// Revoke
revoke()
}
})
.catch(e => {
// Log
console.log('orientation error: ', e)
// Revoke
revoke("This experience requires access to your device's motion.")
})
} else {
// Log
console.log('orientation does not exist')
// Resolve
resolve()
}
})
}
// Return
return {
start
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment