SafeAreaMargin.vue 887 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <template>
  2. <view :style="{
  3. marginTop: props.top ? `${safeAreaInsets.top}px` : undefined,
  4. marginBottom: props.bottom ? `${safeAreaInsets.bottom}px` : undefined,
  5. marginLeft: props.left ? `${safeAreaInsets.left}px` : undefined,
  6. marginRight: props.right ? `${safeAreaInsets.right}px` : undefined,
  7. }">
  8. <slot />
  9. </view>
  10. </template>
  11. <script setup lang="ts">
  12. const systemInfo = uni.getSystemInfoSync();
  13. const safeAreaInsets = systemInfo?.safeAreaInsets || {
  14. top: 0,
  15. bottom: 0,
  16. left: 0,
  17. right: 0,
  18. };
  19. export interface SafeAreaMarginProps {
  20. top?: boolean;
  21. bottom?: boolean;
  22. left?: boolean;
  23. right?: boolean;
  24. }
  25. const props = withDefaults(defineProps<SafeAreaMarginProps>(), {
  26. top: true,
  27. bottom: true,
  28. left: true,
  29. right: true,
  30. });
  31. defineOptions({
  32. options: {
  33. virtualHost: true,
  34. styleIsolation: "shared",
  35. }
  36. })
  37. </script>
  38. <style>
  39. </style>