native.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <script setup lang="ts">
  2. import type { NativeSafeArea } from '~/types/native'
  3. const SAFE_AREA_COOKIE_KEY = 'GAMI-cookie_safe_area'
  4. const nativeSafeArea = useState<NativeSafeArea>('native-safe-area', () => ({
  5. top: 0,
  6. bottom: 0,
  7. }))
  8. const safeAreaCookie = useCookie<string | null>(SAFE_AREA_COOKIE_KEY, {
  9. default: () => null,
  10. })
  11. const parseSafeArea = (raw?: string | null): NativeSafeArea | null => {
  12. if (!raw || typeof raw !== 'string') {
  13. return null
  14. }
  15. const [topStr, bottomStr] = raw.split('|')
  16. const top = Number.parseFloat(topStr ?? '')
  17. const bottom = Number.parseFloat(bottomStr ?? '')
  18. if (!Number.isFinite(top) && !Number.isFinite(bottom)) {
  19. return null
  20. }
  21. return {
  22. top: Number.isFinite(top) ? top : 0,
  23. bottom: Number.isFinite(bottom) ? bottom : 0,
  24. }
  25. }
  26. const setSafeAreaIfValid = (raw?: string | null) => {
  27. const parsed = parseSafeArea(raw)
  28. if (parsed) {
  29. nativeSafeArea.value = parsed
  30. }
  31. }
  32. if (import.meta.server) {
  33. setSafeAreaIfValid(safeAreaCookie.value)
  34. }
  35. if (import.meta.client) {
  36. setSafeAreaIfValid(window.GAMI_BRIDGE?.safeArea ?? safeAreaCookie.value)
  37. onMounted(() => {
  38. setSafeAreaIfValid(window.GAMI_BRIDGE?.safeArea ?? safeAreaCookie.value)
  39. })
  40. }
  41. </script>
  42. <template>
  43. <div>
  44. <slot />
  45. </div>
  46. </template>