# All files index.ts

0% Statements 0/69
0% Branches 0/65
0% Functions 0/9
0% Lines 0/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212

     import { defineComponentHOC, Doraemon, Component, Prop } from '@doraemon-ui/miniprogram.core-js'
    import { getSystemInfoSync, getMenuButtonBoundingClientRectSync } from '@doraemon-ui/miniprogram.shared'

    const { classNames, styleToCssString } = Doraemon.util

    export enum ESafeAreaStyle {
      DEFAULT = 'default',
      NAVBAR = 'navBar',
      STATUSBAR = 'statusBar',
    }

    export function getSafeAreaInset(safeAreaStyle: ESafeAreaStyle = ESafeAreaStyle.DEFAULT) {
      // StatusBar & NavBar
      const isDefault = [ESafeAreaStyle.DEFAULT, ESafeAreaStyle.NAVBAR].includes(safeAreaStyle)

      // iPhoneX 竖屏安全区域
      const safeAreaInset = {
        top: isDefault ? 88 : 44, // StatusBar & NavBar
        left: 0,
        right: 0,
        bottom: 34, // Home Indicator
      }

      try {
        const menuRect = getMenuButtonBoundingClientRectSync()
        const windowInfo = getSystemInfoSync(['window', 'device'])
        const { safeArea, screenHeight, windowHeight } = windowInfo
        const isIOS = !!(windowInfo.system.toLowerCase().search('ios') + 1)

        // 状态栏高度
        const statusBarHeight = !windowInfo.statusBarHeight
            ? screenHeight - windowHeight - 20
            : windowInfo.statusBarHeight

        // 胶囊高度
        const navBarHeight = (menuRect.top - statusBarHeight) * 2 + menuRect.height

        // 下方扩展 4 像素高度, 防止下方边距太小
        const navBarExtendHeight = windowInfo.statusBarHeight && isIOS ? 4 : 0

        safeAreaInset.top = isDefault
          ? statusBarHeight + navBarHeight + navBarExtendHeight
          : Math.max(statusBarHeight, safeAreaInset.top)

        safeAreaInset.bottom = screenHeight - safeArea.bottom
      } catch (e) {
        /** Ignore */
      }
      return safeAreaInset
    }

    export const checkIPhoneX = ({ model, windowHeight, windowWidth }) => {
      return /iphone (x|12|13|14)/.test(model.toLowerCase()) || (windowHeight >= 812 && windowHeight / windowWidth > 2)
    }

    export interface ISafeArea {
      top: boolean
      bottom: boolean
    }

    export const defaultSafeArea = {
      top: false,
      bottom: false,
    }

    export const getSafeArea = (params: boolean | string | ISafeArea): ISafeArea => {
      if (typeof params === 'boolean') {
        return Object.assign({}, defaultSafeArea, {
          top: params,
          bottom: params,
        })
      } else if (params !== null && typeof params === 'object') {
        return Object.assign({}, defaultSafeArea, params)
      } else if (typeof params === 'string') {
        return Object.assign({}, defaultSafeArea, {
          [params]: true,
        })
      }
      return Object.assign({}, defaultSafeArea)
    }

    @Component({
      props: {
        prefixCls: {
          type: String,
          default: 'dora-safe-area',
        },
      },
    })
    class SafeArea extends Doraemon {
      /**
       * 自定义类名前缀
       *
       * @type {string}
       * @memberof SafeArea
       */
      prefixCls!: string

      /**
       * 是否适配刘海屏
       *
       * @type {(boolean | string | ISafeArea)}
       * @memberof SafeArea
       */
      @Prop({
        type: [Boolean, String, Object],
        default: () => ({ ...defaultSafeArea }),
      })
      safeArea: boolean | string | ISafeArea

      /**
       * 安全区的范围，可选值为 default、navBar、statusBar，当其值为 default 或 navBar，顶部计算的安全区包含 StatusBar & NavBar
       *
       * @type {ESafeAreaStyle}
       * @memberof SafeArea
       */
      @Prop({
        type: String,
        default: ESafeAreaStyle.DEFAULT,
      })
      safeAreaStyle: ESafeAreaStyle

      /**
       * 当其值为 false 时，组件内部会判断是否刘海屏，进而计算出安全区的距离
       *
       * @type {boolean}
       * @memberof SafeArea
       */
      @Prop({
        type: Boolean,
        default: false,
      })
      forceRender: boolean

      /**
       * 使用 css 的 @supports 属性适配安全区
       *
       * @type {boolean}
       * @memberof SafeArea
       */
      @Prop({
        type: Boolean,
        default: false,
      })
      supports: boolean

      /**
       * 自定义样式
       *
       * @type {object}
       * @memberof SafeArea
       */
      @Prop({
        type: Object,
        default: null,
      })
      wrapStyle: object

      /**
       * 组件样式
       *
       * @readonly
       * @memberof SafeArea
       */
      get extStyle () {
        const { safeArea, safeAreaStyle, forceRender, supports, wrapStyle, isIPhoneX } = this
        const safeAreaConfig = getSafeArea(safeArea)
        const position = safeAreaConfig.bottom ? 'bottom' : safeAreaConfig.top ? 'top' : 'none'
        let varStyle = ''

        if (
          (forceRender || isIPhoneX) &&
          !supports &&
          ['bottom', 'top'].includes(position)
        ) {
          const safeAreaInset = getSafeAreaInset(safeAreaStyle)
          varStyle = `--safe-area-inset-${position}: ${safeAreaInset[position]}PX;`
        }

        return styleToCssString(
          varStyle + styleToCssString(wrapStyle)
        )
      }

      get classes () {
        const { prefixCls, forceRender, supports, safeArea, isIPhoneX } = this
        const safeAreaConfig = getSafeArea(safeArea)
        const wrap = classNames(prefixCls, {
          [`${prefixCls}--position-bottom`]: (forceRender || isIPhoneX) && safeAreaConfig.bottom,
          [`${prefixCls}--position-top`]: (forceRender || isIPhoneX) && !safeAreaConfig.bottom,
          [`${prefixCls}--supports`]: (forceRender || isIPhoneX) && supports,
        })

        return {
          wrap,
        }
      }

      /**
       * 内部判断是否刘海屏
       *
       * @readonly
       * @memberof SafeArea
       */
      get isIPhoneX() {
        const windowInfo = getSystemInfoSync(['window', 'device'])
        return checkIPhoneX(windowInfo)
      }
    }

    export default defineComponentHOC()(SafeArea)