1
0

StyledAnimation.react.stories.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import React, { useState, Component, isValidElement } 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 {
  7. types
  8. } from '@douyinfe/semi-animation-styled';
  9. import StyledAnimation from '../lib/StyledAnimation';
  10. /** semi-ui demos */
  11. import SemiCollapseAnimation from './semi-collapse-animation-styled';
  12. const stories = storiesOf('semi-animation-react/StyledAnimation', module);
  13. stories.addDecorator(withKnobs);
  14. const {
  15. bouncingEntrances,
  16. bouncingExits,
  17. fadingEntrances,
  18. fadingExits,
  19. rotatingEntrances,
  20. rotatingExits,
  21. slidingEntrances,
  22. slidingExits,
  23. zoomingEntrances,
  24. zoomingExits,
  25. } = types;
  26. const rectStyle = {
  27. backgroundColor: 'rgb(241, 101, 101)',
  28. padding: '20px',
  29. // width: '200px',
  30. // height: '100px',
  31. margin: '20px',
  32. marginLeft: '50px',
  33. borderRadius: '4px',
  34. fontSize: '20px',
  35. color: 'white',
  36. display: 'flex',
  37. alignItems: 'center',
  38. justifyContent: 'center',
  39. };
  40. const bgContainerStyle = { border: '1px solid gray', backgroundColor: 'tan', borderRadius: 4, marginBottom: 10 };
  41. const itemStyle = {
  42. backgroundColor: 'rgb(241, 101, 101)',
  43. color: 'white',
  44. borderRadius: '4px',
  45. padding: '10px',
  46. textAlign: 'center',
  47. };
  48. const durations = ['200ms', '500ms', '750ms', '1s', '1.5s'];
  49. const containerStyle = { display: 'flex', width: '960px', flexWrap: 'wrap' };
  50. const printArgs = (...args) => console.log(...args);
  51. class AnimItem extends Component {
  52. state = {
  53. type: undefined,
  54. duration: '500ms',
  55. speed: 'faster',
  56. };
  57. constructor(props = {}) {
  58. super(props);
  59. }
  60. componentDidMount() {
  61. this.setState({
  62. type: this.props.enterType,
  63. });
  64. }
  65. _reset = () => {
  66. let currentType = this.state.type;
  67. this.setState(
  68. {
  69. type: undefined,
  70. },
  71. () => {
  72. this.setState({
  73. type: currentType,
  74. });
  75. }
  76. );
  77. };
  78. setSpeed = e => {
  79. this.setState({
  80. speed: e.target.value,
  81. });
  82. this._reset();
  83. };
  84. setDuration = e => {
  85. this.setState({
  86. duration: e.target.value,
  87. });
  88. this._reset();
  89. };
  90. enter = () => {
  91. this.setState(
  92. {
  93. type: undefined,
  94. },
  95. () =>
  96. this.setState({
  97. type: this.props.enterType,
  98. })
  99. );
  100. };
  101. leave = () => {
  102. this.setState(
  103. {
  104. type: undefined,
  105. },
  106. () =>
  107. this.setState({
  108. type: this.props.leaveType,
  109. })
  110. );
  111. };
  112. render() {
  113. let { type, speed, duration } = this.state;
  114. let { children } = this.props;
  115. if (type) {
  116. return (
  117. <div style={bgContainerStyle}>
  118. <div style={containerStyle}>
  119. <StyledAnimation
  120. key={type}
  121. type={type}
  122. speed={speed}
  123. duration={duration}
  124. // loop="infinite"
  125. // className={`semi-${type} semi-animated semi-duration-slow semi-count-infinite`}
  126. onStart={printArgs}
  127. onRest={printArgs}
  128. >
  129. {({ animateCls, animateStyle, animateEvents }) =>
  130. typeof children === 'function' ? (
  131. children(animateCls)
  132. ) : (
  133. <div
  134. className={animateCls}
  135. style={{ ...rectStyle, ...animateStyle }}
  136. {...animateEvents}
  137. // onAnimationStart={(...args) => console.log(...args)}
  138. >
  139. {type}
  140. </div>
  141. )
  142. }
  143. </StyledAnimation>
  144. </div>
  145. <div>
  146. <button onClick={this.enter}>入场</button>
  147. <button onClick={this.leave}>离场</button>
  148. {/* <div style={{ display: 'inline-flex', marginLeft: 10, alignItems: 'center' }}>
  149. <label>速度:</label>
  150. <select onChange={this.setSpeed}>
  151. {speeds.map(speedVal => (
  152. <option
  153. selected={speedVal === speed}
  154. key={speedVal}
  155. label={speedVal}
  156. value={speedVal}
  157. />
  158. ))}
  159. </select>
  160. </div> */}
  161. <div style={{ display: 'inline-flex', marginLeft: 10, alignItems: 'center' }}>
  162. <label>持续时长:</label>
  163. <select onChange={this.setDuration}>
  164. {durations.map(itDur => (
  165. <option selected={duration === itDur} key={itDur} label={itDur} value={itDur} />
  166. ))}
  167. </select>
  168. </div>
  169. </div>
  170. </div>
  171. );
  172. }
  173. return null;
  174. }
  175. }
  176. stories.add('bounce', () =>
  177. bouncingEntrances.map((type, idx) => <AnimItem key={type} enterType={type} leaveType={bouncingExits[idx]} />)
  178. );
  179. stories.add('fading', () =>
  180. fadingEntrances.map((type, idx) => <AnimItem key={type} enterType={type} leaveType={fadingExits[idx]} />)
  181. );
  182. stories.add('rotating', () =>
  183. rotatingEntrances.map((type, idx) => <AnimItem key={type} enterType={type} leaveType={rotatingExits[idx]} />)
  184. );
  185. stories.add('sliding', () =>
  186. slidingEntrances.map((type, idx) => <AnimItem key={type} enterType={type} leaveType={slidingExits[idx]} />)
  187. );
  188. stories.add('zooming', () =>
  189. zoomingEntrances.map((type, idx) => <AnimItem key={type} enterType={type} leaveType={zoomingExits[idx]} />)
  190. );
  191. stories.add('SemiCollapseAnimation', () => <SemiCollapseAnimation />);