-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add <ExitSensor> and its story
- Loading branch information
Showing
3 changed files
with
137 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,49 @@ | ||
import {Component} from 'react'; | ||
import {h} from '../../util'; | ||
import {ExitSensor} from '..'; | ||
|
||
const Rectangle = ({exiting}: any) => { | ||
return ( | ||
<div style={{ | ||
width: 200, | ||
height: 200, | ||
background: 'tomato', | ||
transition: 'all 2s', | ||
opacity: exiting ? 0 : 1 | ||
}} /> | ||
); | ||
}; | ||
|
||
const Circle = ({exiting}: any) => { | ||
return ( | ||
<div style={{ | ||
width: 200, | ||
height: 200, | ||
background: 'tomato', | ||
borderRadius: '100px', | ||
transition: 'all 2s', | ||
opacity: exiting ? 0 : 1 | ||
}} /> | ||
); | ||
}; | ||
|
||
export class StoryExitSensorExample extends Component<any, any> { | ||
state = { | ||
shape: 'rectangle' | ||
}; | ||
|
||
render () { | ||
const shape = this.state.shape === 'rectangle' ? | ||
<Rectangle key='rectangle' /> : <Circle key='circle' />; | ||
|
||
return ( | ||
<div> | ||
<ExitSensor time={2000}> | ||
{shape} | ||
</ExitSensor> | ||
<button onClick={() => this.setState({shape: 'rectangle'})}>Rectangle</button> | ||
<button onClick={() => this.setState({shape: 'circle'})}>Circle</button> | ||
</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,10 @@ | ||
import {Component, createElement as h} from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {action} from '@storybook/addon-actions'; | ||
import {linkTo} from '@storybook/addon-links'; | ||
import {ExitSensor} from '..'; | ||
import ShowDocs from '../../../.storybook/ShowDocs' | ||
import {StoryExitSensorExample} from './StoryExitSensorExample'; | ||
|
||
storiesOf('Sensors/ExitSensor', module) | ||
.add('Example', () => <StoryExitSensorExample />); |
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,78 @@ | ||
import {Component, cloneElement} from 'react'; | ||
import {h} from '../util'; | ||
import renderProp from '../util/renderProp'; | ||
|
||
export interface IExitSensorProps { | ||
children?: React.ReactElement<any>; | ||
time?: number; | ||
} | ||
|
||
export interface IExitSensorState { | ||
|
||
} | ||
|
||
export class ExitSensor extends Component<IExitSensorProps, IExitSensorState> { | ||
static defaultProps = { | ||
time: 200 | ||
}; | ||
|
||
element: React.ReactElement<any>; | ||
nextElement: React.ReactElement<any>; | ||
exitInFlight: boolean = false; | ||
exitFinish: boolean = false; | ||
timeout; | ||
|
||
state: IExitSensorState = { | ||
|
||
}; | ||
|
||
componentWillUnmount () { | ||
clearTimeout(this.timeout); | ||
} | ||
|
||
transition = () => { | ||
this.exitFinish = true; | ||
this.forceUpdate(); | ||
}; | ||
|
||
render () { | ||
if (this.exitFinish) { | ||
this.exitFinish = false; | ||
this.exitInFlight = false; | ||
this.element = this.nextElement; | ||
|
||
return this.element; | ||
} | ||
|
||
const element = this.props.children; | ||
|
||
if (this.exitInFlight) { | ||
this.nextElement = element; | ||
|
||
return this.element; | ||
} | ||
|
||
if (!this.element) { | ||
this.element = element; | ||
|
||
return element; | ||
} | ||
|
||
if (!element || (this.element.key !== element.key)) { | ||
this.exitInFlight = true; | ||
this.nextElement = element; | ||
|
||
this.timeout = setTimeout(this.transition, this.props.time); | ||
|
||
return cloneElement(this.element, { | ||
exiting: true | ||
}); | ||
} | ||
|
||
if (this.exitInFlight) { | ||
return this.element; | ||
} | ||
|
||
return element; | ||
} | ||
} |