safe-area.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. let cache = null;
  2. function getSafeArea() {
  3. return new Promise((resolve, reject) => {
  4. if (cache != null) {
  5. resolve(cache);
  6. }
  7. else {
  8. wx.getSystemInfo({
  9. success: ({ model, screenHeight, statusBarHeight }) => {
  10. const iphoneX = /iphone x/i.test(model);
  11. const iphoneNew = /iPhone11/i.test(model) && screenHeight === 812;
  12. cache = {
  13. isIPhoneX: iphoneX || iphoneNew,
  14. statusBarHeight
  15. };
  16. resolve(cache);
  17. },
  18. fail: reject
  19. });
  20. }
  21. });
  22. }
  23. export const safeArea = ({ safeAreaInsetBottom = true, safeAreaInsetTop = false } = {}) => Behavior({
  24. properties: {
  25. safeAreaInsetTop: {
  26. type: Boolean,
  27. value: safeAreaInsetTop
  28. },
  29. safeAreaInsetBottom: {
  30. type: Boolean,
  31. value: safeAreaInsetBottom
  32. }
  33. },
  34. created() {
  35. getSafeArea().then(({ isIPhoneX, statusBarHeight }) => {
  36. this.set({ isIPhoneX, statusBarHeight });
  37. });
  38. }
  39. });