device.js 708 B

123456789101112131415161718192021222324252627282930313233343536
  1. let deviceState = {
  2. isMobile: false,
  3. isTablet: false,
  4. isLaptop: false
  5. }
  6. function detectDeviceState () {
  7. if (window.innerWidth <= 425) {
  8. deviceState = {
  9. isMobile: true,
  10. isTablet: false,
  11. isLaptop: false
  12. }
  13. } else if (window.innerWidth <= 768) {
  14. deviceState = {
  15. isMobile: false,
  16. isTablet: true,
  17. isLaptop: false
  18. }
  19. } else {
  20. deviceState = {
  21. isMobile: false,
  22. isTablet: false,
  23. isLaptop: true
  24. }
  25. }
  26. }
  27. detectDeviceState()
  28. window.addEventListener('resize', detectDeviceState)
  29. // returns a copy of the device state
  30. // so other parts of code can't override this.
  31. export function getDeviceState () {
  32. return { ...deviceState }
  33. }