-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
306 additions
and
67 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
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,84 @@ | ||
@import '../../styles/settings.scss'; | ||
|
||
.m-navigation-bar { | ||
&__container { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
box-sizing: border-box; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
z-index: 8000; | ||
} | ||
|
||
&__left { | ||
position: absolute; | ||
left: 0; | ||
} | ||
|
||
&__menu { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-around; | ||
font-weight: bold; | ||
font-size: 18px; | ||
border: 1px solid; | ||
|
||
&-left, &-right { | ||
flex-grow: 1; | ||
text-align: center; | ||
} | ||
|
||
&-divider { | ||
flex-shrink: 0; | ||
width: 1px; | ||
height: 1em; | ||
} | ||
} | ||
|
||
&__title { | ||
font-weight: bold; | ||
font-size: 18px; | ||
} | ||
|
||
&_white &__menu { | ||
background: rgba(black, 0.15); | ||
border-color: rgba(white, 0.25); | ||
} | ||
|
||
&_white &__menu-divider { | ||
background: rgba(white, 0.25); | ||
} | ||
|
||
&_black &__menu { | ||
background: rgba(white, 0.55); | ||
border-color: rgba(black, 0.25); | ||
} | ||
|
||
&_black &__menu-divider { | ||
background: rgba(black, 0.15); | ||
} | ||
} | ||
|
||
@font-face { | ||
font-family: "m-navigation-bar-iconfont"; | ||
src: url("data:font/woff;charset=utf-8;base64,d09GRgABAAAAAAUQAAsAAAAACCAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1V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AAJACeeJxjYGRgYGBm8GNgYgABEMkFhAwM/8F8BgAQZwFqAAB4nJ2SPU7DQBCFn/OHSCQKEEh020ABsvOD0qSNlHQUKdI7ztpxZHujzSZSzsABOAUtd+AScAguwJMzaUApglceffvezOyPFsAlPuFh/13z37OHW872XMEZAuEq9b5wjfwsXEcLiXCDuhVu4hEvwi1c4Z0dvNo5Zw/4EPbQw7dwBRfenXAVPe9JuEbWwnXceK/CDepvwk1MvS/hFu4r/ebQ6tDpuZrtVBqZIjaFa+Z+EW7TJHSpKfxZaP2DM9HJJgvtUf+oMdV2TU11g87RnLEutD3sZb1Nes7FKrYmVyO6OsuMWlmz1JELFs6tBu12LHoQmZxHG/JKNUI4xjkUZtgxpohgUCAuo2NeDp8UYksvKfPT0vNZEbKH/6dmwo4JNshK//T60yumXNFiLXkKXT6vzj/6jNmnKHv9vpc1uyR8Vo4j5jxmjuEKCiOp1TxvRlZYld6SSkQ9wKKsWmGANkf8Kz8o95D/AJjlo1sAeJxjYGKAAC4G7ICZkYmRmZGFgSUpMTmbJSM/N5WBAQAZ7QNe") format("woff"); | ||
} | ||
|
||
.m-navigation-bar-iconfont { | ||
font-family: "m-navigation-bar-iconfont" !important; | ||
font-size: 1em; | ||
font-style: normal; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
.m-navigation-bar-icon-home:before { | ||
content: "\e601"; | ||
} | ||
|
||
.m-navigation-bar-icon-back:before { | ||
content: "\e638"; | ||
} |
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,128 @@ | ||
import './index.scss' | ||
import Taro from '@tarojs/taro' | ||
import { component } from '../component' | ||
import { internalStore } from '../internal' | ||
import { last } from 'vtils' | ||
import { NavigationBarProps } from './props' | ||
import { View } from '@tarojs/components' | ||
|
||
function onlyPath(url: string) { | ||
return url ? url.split('?')[0].replace(/^\/+/, '') : '' | ||
} | ||
|
||
export default class NavigationBar extends component({ | ||
props: NavigationBarProps, | ||
state: { | ||
verticalPadding: 0 as number, | ||
horizontalPadding: 0 as number, | ||
navigationBarHeight: 0 as number, | ||
navigationBarFullHeight: 0 as number, | ||
menuButtonHeight: 0 as number, | ||
menuButtonWidth: 0 as number, | ||
backButtonVisible: false as boolean, | ||
homeButtonVisible: false as boolean, | ||
}, | ||
}) { | ||
componentDidShow() { | ||
const menuRect = Taro.getMenuButtonBoundingClientRect() | ||
const sysInfo = Taro.getSystemInfoSync() | ||
const verticalPadding = menuRect.top - sysInfo.statusBarHeight | ||
const horizontalPadding = sysInfo.windowWidth - menuRect.right | ||
const height = menuRect.height + verticalPadding * 2 | ||
const fullHeight = sysInfo.statusBarHeight + height | ||
|
||
const pages = Taro.getCurrentPages() | ||
const backButtonVisible = pages.length > 1 | ||
const homeButtonVisible = onlyPath(last(pages).route) !== onlyPath(this.props.homePath) | ||
|
||
internalStore.customNavigationBarFullHeight = fullHeight | ||
|
||
this.setState({ | ||
verticalPadding: verticalPadding, | ||
horizontalPadding: horizontalPadding, | ||
navigationBarHeight: height, | ||
navigationBarFullHeight: fullHeight, | ||
menuButtonHeight: menuRect.height, | ||
menuButtonWidth: menuRect.width, | ||
backButtonVisible: backButtonVisible, | ||
homeButtonVisible: homeButtonVisible, | ||
}) | ||
} | ||
|
||
componentDidHide() { | ||
internalStore.customNavigationBarFullHeight = 0 | ||
} | ||
|
||
componentWillUnmount() { | ||
internalStore.customNavigationBarFullHeight = 0 | ||
} | ||
|
||
handleBackClick = () => { | ||
Taro.navigateBack() | ||
} | ||
|
||
handleHomeClick = () => { | ||
Taro.navigateTo({ | ||
url: this.props.homePath, | ||
}) | ||
} | ||
|
||
render() { | ||
const { backgroundColor, textStyle, className } = this.props | ||
const { verticalPadding, horizontalPadding, navigationBarHeight, navigationBarFullHeight, menuButtonHeight, menuButtonWidth, backButtonVisible, homeButtonVisible } = this.state | ||
|
||
const actualBackgroundColor = backgroundColor !== 'auto' | ||
? backgroundColor | ||
: textStyle === 'white' | ||
? '#000000' | ||
: '#FFFFFF' | ||
|
||
return ( | ||
<View className={`m-navigation-bar m-navigation-bar_${textStyle} ${className}`}> | ||
<View | ||
className='m-navigation-bar__placeholder' | ||
style={{ height: `${navigationBarFullHeight}px` }} | ||
/> | ||
<View | ||
className='m-navigation-bar__container' | ||
style={{ | ||
backgroundColor: actualBackgroundColor, | ||
color: textStyle, | ||
height: `${navigationBarFullHeight}px`, | ||
padding: `${navigationBarFullHeight - navigationBarHeight + verticalPadding}px ${horizontalPadding}px ${verticalPadding}px ${horizontalPadding}px`, | ||
}}> | ||
{!backButtonVisible && !homeButtonVisible ? null : ( | ||
<View className='m-navigation-bar__left' style={{ left: `${verticalPadding}px` }}> | ||
<View | ||
className='m-navigation-bar__menu' | ||
style={{ | ||
width: `${backButtonVisible && homeButtonVisible ? menuButtonWidth : menuButtonWidth / 2}px`, | ||
height: `${menuButtonHeight}px`, | ||
borderRadius: `${menuButtonHeight / 2}px`, | ||
}}> | ||
{!backButtonVisible ? null : ( | ||
<View | ||
className='m-navigation-bar__menu-left m-navigation-bar-iconfont m-navigation-bar-icon-back' | ||
onClick={this.handleBackClick} | ||
/> | ||
)} | ||
{!(backButtonVisible && homeButtonVisible) ? null : ( | ||
<View className='m-navigation-bar__menu-divider' /> | ||
)} | ||
{!homeButtonVisible ? null : ( | ||
<View | ||
className='m-navigation-bar__menu-right m-navigation-bar-iconfont m-navigation-bar-icon-home' | ||
onClick={this.handleHomeClick} | ||
/> | ||
)} | ||
</View> | ||
</View> | ||
)} | ||
<View className='m-navigation-bar__title'> | ||
{this.props.children} | ||
</View> | ||
</View> | ||
</View> | ||
) | ||
} | ||
} |
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,26 @@ | ||
import { RequiredProp } from '../component' | ||
|
||
export const NavigationBarProps = { | ||
/** | ||
* 小程序主页的绝对路径,可带参数。 | ||
* | ||
* @example '/pages/index/index?id=40' | ||
*/ | ||
homePath: '' as any as RequiredProp<string>, | ||
|
||
/** | ||
* 导航栏字体颜色,务必同时设置 `app.json` 中的 `window.navigationBarTextStyle` 且保持二者一致。 | ||
* | ||
* @default 'white' | ||
*/ | ||
textStyle: 'white' as 'white' | 'black', | ||
|
||
/** | ||
* 导航栏背景颜色。默认值是 `auto`,表示: | ||
* - 若 `textStyle='white'`,则 `backgroundColor='#000000'`; | ||
* - 若 `textStyle='black'`,则 `backgroundColor='#FFFFFF'`。 | ||
* | ||
* @default 'auto' | ||
*/ | ||
backgroundColor: 'auto' as string, | ||
} |
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
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,4 @@ | ||
export const internalStore = { | ||
zIndex: 5000, | ||
customNavigationBarFullHeight: 0, | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.