safe-area.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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, statusBarHeight }) => {
  10. const deviceType = model.replace(/\s/g, '-');
  11. const iphoneNew = /iphone-x|iPhone11|iPhone12|iPhone13/i.test(deviceType);
  12. cache = {
  13. isIPhoneX: 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. });