Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix navigators for new core, add tab navigator. Bump to 0.3.0
- Loading branch information
Ivan Zotov
committed
Jan 29, 2020
1 parent
de648a1
commit dfb0eac
Showing
8 changed files
with
175 additions
and
26 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
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,52 @@ | ||
import { History } from '@navigationjs/core'; | ||
|
||
export default class Navigator { | ||
constructor(name) { | ||
this.name = name; | ||
this.scenes = {}; | ||
this.history = new History(this.name); | ||
} | ||
|
||
addScenes = (...scenes) => { | ||
scenes.forEach(it => (this.scenes[it.name] = it)); | ||
}; | ||
|
||
removeScenes = (...scenes) => { | ||
scenes.forEach(it => delete this.scenes[it]); | ||
}; | ||
|
||
go = async (name, duration) => { | ||
const scene = this.scenes[name]; | ||
if (!scene) return Promise.reject(); | ||
const promises = []; | ||
Object.values(this.scenes).forEach(it => { | ||
promises.push(it.name === name ? it.show(duration) : it.hide(duration)); | ||
}); | ||
await Promise.all(promises); | ||
this.history.push(name); | ||
}; | ||
|
||
current = () => this.history.current(); | ||
|
||
back = async duration => { | ||
if (this.history.isEmpty()) return Promise.resolve(); | ||
const name = this.current(); | ||
const scene = this.scenes[name]; | ||
if (!scene) return Promise.reject(); | ||
const promises = []; | ||
promises.push(scene.hide(duration)); | ||
const newSceneName = this.history.chain[this.history.chain.length - 2]; | ||
const newScene = this.scenes[newSceneName]; | ||
if (!newScene) return Promise.reject(); | ||
promises.push(newScene.show(duration)); | ||
await Promise.all(promises); | ||
this.history.pop(); | ||
}; | ||
|
||
reset = async () => { | ||
await Promise.all( | ||
Object.keys(this.scenes).map(key => this.scenes[key].hide(0)) | ||
); | ||
this.history = new History(this.name); | ||
}; | ||
} |
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,11 @@ | ||
import Value from '../Value'; | ||
|
||
export default class Scene { | ||
constructor(name) { | ||
this.name = name; | ||
this.active = new Value('active', 0, 0); | ||
} | ||
|
||
show = duration => this.active.to(1, duration); | ||
hide = duration => this.active.to(0, duration); | ||
} |
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,69 @@ | ||
import React, { Component } from 'react'; | ||
import navigation, { toId } from '@navigationjs/core'; | ||
|
||
export default class Wrap extends Component { | ||
onValue = () => { | ||
const eventNames = [ | ||
'transitionend', | ||
'webkitTransitionEnd', | ||
'oTransitionEnd', | ||
'otransitionend', | ||
'MSTransitionEnd', | ||
]; | ||
const callback = () => { | ||
eventNames.forEach(eventName => | ||
this.element.removeEventListener(eventName, callback, false) | ||
); | ||
}; | ||
eventNames.forEach(eventName => { | ||
this.element.addEventListener(eventName, callback, false); | ||
}); | ||
this.forceUpdate(); | ||
}; | ||
|
||
componentDidMount() { | ||
const { navigator: navigatorName, scene: sceneName } = this.props; | ||
const navigator = navigation.navigators[navigatorName]; | ||
const scene = navigator.scenes[sceneName]; | ||
scene.active.on('value', this.onValue); | ||
} | ||
|
||
componentWillUnmount() { | ||
const { navigator: navigatorName, scene: sceneName } = this.props; | ||
const navigator = navigation.navigators[navigatorName]; | ||
const scene = navigator.scenes[sceneName]; | ||
scene.active.off('value', this.onValue); | ||
} | ||
|
||
render() { | ||
const { navigator: navigatorName, scene: sceneName, children } = this.props; | ||
|
||
const id = toId(navigatorName, sceneName); | ||
const pass = { id }; | ||
|
||
const navigator = navigation.navigators[navigatorName]; | ||
const scene = navigator.scenes[sceneName]; | ||
|
||
return ( | ||
<div | ||
ref={ref => { | ||
this.element = ref; | ||
}} | ||
style={{ | ||
transform: `translateY(${(1 - scene.active.value) * 100}%)`, | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
right: 0, | ||
bottom: 0, | ||
transitionProperty: 'transform', | ||
transitionDuration: '300ms', | ||
overflowY: 'auto', | ||
backgroundColor: 'white', | ||
}} | ||
> | ||
{typeof children === 'function' ? children(pass) : children} | ||
</div> | ||
); | ||
} | ||
} |
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,9 @@ | ||
import Navigator from './Navigator'; | ||
import Scene from './Scene'; | ||
import Wrap from './Wrap'; | ||
|
||
export default { | ||
Navigator, | ||
Scene, | ||
Wrap, | ||
}; |
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 |
---|---|---|
@@ -1,13 +1,9 @@ | ||
import navigation, { | ||
Navigation, | ||
Base, | ||
Value, | ||
toId, | ||
fromId, | ||
} from '@navigationjs/core'; | ||
import Modal from './Modal'; | ||
import Stack from './Stack'; | ||
import Wrap from './Wrap'; | ||
import navigation, { Navigation, toId, fromId } from '@navigationjs/core'; | ||
import Stack from './src/Stack'; | ||
import Tab from './src/Tab'; | ||
import Modal from './src/Modal'; | ||
import Value from './src/Value'; | ||
import Wrap from './src/Wrap'; | ||
|
||
export { Navigation, Base, Modal, Stack, Wrap, Value, toId, fromId }; | ||
export { Navigation, Stack, Tab, Modal, Value, Wrap, toId, fromId }; | ||
export default navigation; |