Animation.react.stories.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 { Animation } from '../index';
  7. /** demos */
  8. import SimpleAnimation from './simple';
  9. import Morph from './morph';
  10. import Auto from './auto';
  11. import DashOffset from './dashoffset';
  12. import Scroll from './scroll';
  13. /** semi-ui components * */
  14. import ModalAnimation from './semi-modal-animation';
  15. /** semi-animation-react mock animate.css */
  16. import BounceInAnimation from './animate-react/BounceIn';
  17. const stories = storiesOf('semi-animation-react/Animation', module);
  18. stories.addDecorator(withKnobs);
  19. const itemStyle = {
  20. backgroundColor: 'rgb(241, 101, 101)',
  21. color: 'white',
  22. borderRadius: 4,
  23. padding: 10,
  24. textAlign: 'center',
  25. };
  26. const bigSquareStyle = {
  27. backgroundColor: 'rgb(241, 101, 101)',
  28. width: 100,
  29. height: 100,
  30. margin: 20,
  31. marginLeft: 50,
  32. borderRadius: 4,
  33. fontSize: 32,
  34. color: 'white',
  35. display: 'flex',
  36. alignItems: 'center',
  37. justifyContent: 'center',
  38. };
  39. stories.add('Animaiton', () => {
  40. class App extends Component {
  41. constructor(props) {
  42. super(props);
  43. this.state = {
  44. value: 0,
  45. };
  46. }
  47. onFrame = props => {
  48. const value = props.value.toFixed(2);
  49. this.setState({ value });
  50. };
  51. onStop = props => {
  52. console.log('onStop', props);
  53. const value = props.value.toFixed(2);
  54. this.setState({ value });
  55. };
  56. invokehandlerFn = (funcName, ...args) => {
  57. if (
  58. funcName
  59. && typeof funcName === 'string'
  60. && this.handler
  61. && this.handler[funcName]
  62. && typeof this.handler[funcName] === 'function'
  63. ) {
  64. console.log(this.handler);
  65. this.handler[funcName](...args);
  66. if (['reset', 'reverse'].includes(funcName)) {
  67. this.handler.start();
  68. }
  69. }
  70. };
  71. render() {
  72. return (
  73. <Animation
  74. autoStart
  75. // immediate
  76. ref={handler => (this.handler = handler)}
  77. config={{ duration: 20000 }}
  78. from={{ value: 0 }}
  79. to={{ value: 10 }}
  80. // onFrame={this.onFrame}
  81. onStop={this.onStop}
  82. >
  83. {props => {
  84. const value = props.value.toFixed(2);
  85. const hsl = `hsl(${value * 25 + 60}, 90%, 50%)`;
  86. const x = 100 + 20 * value;
  87. const y = 100 + 20 * Math.cos(value);
  88. return (
  89. <>
  90. <div style={{ ...itemStyle, width: 300 }}>prop.value changed to: {value}</div>
  91. <div style={{ ...bigSquareStyle }}>{value}</div>
  92. <div style={{ height: 200, position: 'relative' }}>
  93. <span
  94. style={{
  95. ...itemStyle,
  96. left: x,
  97. top: y,
  98. position: 'absolute',
  99. }}
  100. >
  101. I'am a block.
  102. </span>
  103. </div>
  104. <div style={{ ...bigSquareStyle, width: 200, backgroundColor: hsl, fontSize: 16 }}>
  105. {hsl}
  106. </div>
  107. <div>
  108. {['reset', 'reverse', 'pause', 'resume', 'stop', 'end'].map(funcName => (
  109. <button
  110. key={funcName}
  111. onClick={(...args) => this.invokehandlerFn(funcName, ...args)}
  112. >
  113. {funcName}
  114. </button>
  115. ))}
  116. </div>
  117. </>
  118. );
  119. }}
  120. </Animation>
  121. );
  122. }
  123. }
  124. return <App />;
  125. });
  126. stories.add('SimpleAnimation', () => <SimpleAnimation />);
  127. stories.add('Morph', () => <Morph />);
  128. stories.add('Auto', () => <Auto />);
  129. stories.add('DashOffset', () => <DashOffset />);
  130. stories.add('Scroll', () => <Scroll />);
  131. stories.add('Semi-ModalAnimation', () => <ModalAnimation />);
  132. stories.add('BounceInAnimation', () => (
  133. <BounceInAnimation>
  134. {(currentStyle = {}) => <div style={{ ...bigSquareStyle, fontSize: 16, ...currentStyle }}>BounceIn</div>}
  135. </BounceInAnimation>
  136. ));