-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(taro): 新增 getTopBarInfo, useTopBarInfo
- Loading branch information
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getTopBarInfo 表现正常 1`] = ` | ||
Object { | ||
"menuButtonHeight": 32, | ||
"menuButtonHorizontalMargin": 10, | ||
"menuButtonVerticalMargin": 4, | ||
"menuButtonWidth": 87, | ||
"navigationBarHeight": 40, | ||
"statusBarHeight": 20, | ||
"topBarHeight": 60, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as Taro from '@tarojs/taro' | ||
|
||
describe('getTopBarInfo', () => { | ||
const getMenuButtonBoundingClientRect: any = jest | ||
.fn() | ||
.mockImplementation(() => ({ | ||
width: 87, | ||
height: 32, | ||
left: 223, | ||
top: 24, | ||
right: 310, | ||
bottom: 56, | ||
})) | ||
const getSystemInfoSync: any = jest.fn().mockImplementation(() => ({ | ||
model: 'iPhone 5', | ||
pixelRatio: 2, | ||
windowWidth: 320, | ||
windowHeight: 520, | ||
system: 'iOS 10.0.1', | ||
language: 'zh_CN', | ||
version: '7.0.4', | ||
screenWidth: 320, | ||
screenHeight: 568, | ||
SDKVersion: '2.8.0', | ||
brand: 'devtools', | ||
fontSizeSetting: 16, | ||
batteryLevel: 100, | ||
statusBarHeight: 20, | ||
safeArea: { | ||
right: 320, | ||
bottom: 568, | ||
left: 0, | ||
top: 20, | ||
width: 320, | ||
height: 548, | ||
}, | ||
deviceOrientation: 'portrait', | ||
platform: 'devtools', | ||
})) | ||
|
||
beforeAll(() => { | ||
jest.mock( | ||
'@tarojs/taro', | ||
() => | ||
({ | ||
getMenuButtonBoundingClientRect, | ||
getSystemInfoSync, | ||
} as typeof Taro), | ||
) | ||
}) | ||
|
||
test('表现正常', async () => { | ||
const { getTopBarInfo } = await import('./getTopBarInfo') | ||
|
||
expect(getTopBarInfo()).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
getMenuButtonBoundingClientRect, | ||
getSystemInfoSync, | ||
} from '@tarojs/taro' | ||
|
||
export interface GetTopBarInfoResult { | ||
/** | ||
* 状态栏高度。 | ||
*/ | ||
statusBarHeight: number | ||
|
||
/** | ||
* 菜单按钮宽度。 | ||
*/ | ||
menuButtonWidth: number | ||
|
||
/** | ||
* 菜单按钮高度。 | ||
*/ | ||
menuButtonHeight: number | ||
|
||
/** | ||
* 菜单按钮垂直外边距。 | ||
*/ | ||
menuButtonVerticalMargin: number | ||
|
||
/** | ||
* 菜单按钮水平外边距。 | ||
*/ | ||
menuButtonHorizontalMargin: number | ||
|
||
/** | ||
* 导航栏高度。 | ||
*/ | ||
navigationBarHeight: number | ||
|
||
/** | ||
* 顶栏高度。 | ||
*/ | ||
topBarHeight: number | ||
} | ||
|
||
/** | ||
* 获取顶栏信息。 | ||
* | ||
* @returns 返回获取到的顶栏信息 | ||
*/ | ||
export function getTopBarInfo(): GetTopBarInfoResult { | ||
const menuRect = getMenuButtonBoundingClientRect() | ||
const sysInfo = getSystemInfoSync() | ||
|
||
// 部分情况下 statusBarHeight 可能不存在或为 0,需手动计算,如: | ||
// 苹果手机 iOS 版本 < 13 时下开启热点等 | ||
if (!sysInfo.statusBarHeight) { | ||
sysInfo.statusBarHeight = sysInfo.screenHeight - sysInfo.windowHeight | ||
} | ||
|
||
const statusBarHeight = sysInfo.statusBarHeight | ||
const menuButtonWidth = menuRect.width | ||
const menuButtonHeight = menuRect.height | ||
const menuButtonVerticalMargin = menuRect.top - statusBarHeight | ||
const menuButtonHorizontalMargin = sysInfo.windowWidth - menuRect.right | ||
const navigationBarHeight = menuButtonHeight + menuButtonVerticalMargin * 2 | ||
const topBarHeight = statusBarHeight + navigationBarHeight | ||
|
||
return { | ||
statusBarHeight, | ||
menuButtonWidth, | ||
menuButtonHeight, | ||
menuButtonVerticalMargin, | ||
menuButtonHorizontalMargin, | ||
navigationBarHeight, | ||
topBarHeight, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { getTopBarInfo, GetTopBarInfoResult } from './getTopBarInfo' | ||
import { useMemo } from 'react' | ||
|
||
export function useTopBarInfo(): GetTopBarInfoResult { | ||
return useMemo(getTopBarInfo, []) | ||
} |