StyledTransition.react.stories.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import React, { useState, Component, isValidElement, PureComponent } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { storiesOf } from '@storybook/react';
  4. import { withKnobs, text, boolean } from '@storybook/addon-knobs';
  5. import withPropsCombinations from 'react-storybook-addon-props-combinations';
  6. import StyledTransition from '../lib/StyledTransition';
  7. const stories = storiesOf('semi-animation-react/StyledTransition', module);
  8. stories.addDecorator(withKnobs);
  9. const bigSquareStyle = {
  10. backgroundColor: 'rgb(241, 101, 101)',
  11. width: 100,
  12. height: 100,
  13. margin: 20,
  14. marginLeft: 50,
  15. borderRadius: 4,
  16. fontSize: 16,
  17. color: 'white',
  18. display: 'flex',
  19. alignItems: 'center',
  20. justifyContent: 'center',
  21. };
  22. const printArgs = (...args) => console.log(...args);
  23. const durations = ['200ms', '500ms', '750ms', '1s', '1.5s'];
  24. stories.add('bounceInOut', () => {
  25. class App extends PureComponent {
  26. constructor(props = {}) {
  27. super(props);
  28. }
  29. state = { visible: true, duration: '500ms' };
  30. setVisible = visible => this.setState({ visible });
  31. setDuration = e => {
  32. this.setState({
  33. duration: e.target.value,
  34. });
  35. };
  36. render() {
  37. const { visible, duration } = this.state;
  38. return (
  39. <div>
  40. <div>
  41. <StyledTransition
  42. duration={duration}
  43. enter={'bounceIn'}
  44. leave={'bounceOut'}
  45. onFrame={() => console.log('frame')}
  46. didEnter={() => console.log('enter')}
  47. didLeave={() => console.log('leave')}
  48. >
  49. {visible
  50. ? ({ animateCls, animateStyle, animateEvents }) => (
  51. <div
  52. style={{ ...animateStyle, ...bigSquareStyle }}
  53. className={animateCls}
  54. {...animateEvents}
  55. onAnimationIteration={() => console.log('onFrame')}
  56. >
  57. bounceInOut
  58. </div>
  59. )
  60. : null}
  61. </StyledTransition>
  62. </div>
  63. <div>
  64. <button onClick={() => this.setVisible(true)}>入场</button>
  65. <button onClick={() => this.setVisible(false)}>出场</button>
  66. <div style={{ display: 'inline-flex', marginLeft: 10, alignItems: 'center' }}>
  67. <label>持续时长:</label>
  68. <select onChange={this.setDuration}>
  69. {durations.map(itDur => (
  70. <option selected={duration === itDur} key={itDur} label={itDur} value={itDur} />
  71. ))}
  72. </select>
  73. </div>
  74. </div>
  75. </div>
  76. );
  77. }
  78. }
  79. return <App />;
  80. });