TimeLineText.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. import { Component, createRef, useRef, useEffect } from "react";
  2. import gsap from "gsap";
  3. import PropTypes from "prop-types";
  4. import { Action } from "../action";
  5. import { css } from "@emotion/react";
  6. import styled from "@emotion/styled";
  7. import { Style } from "../style/section";
  8. export function TimeLineTransText(props) {
  9. const containerRef = useRef();
  10. const from = props.from || {
  11. autoAlpha: 0,
  12. y: 100,
  13. };
  14. const start = props.start || "top bottom";
  15. const end = props.end || "top center+=10%";
  16. useEffect(() => {
  17. gsap.from(
  18. containerRef.current,
  19. Object.assign(
  20. from,
  21. {},
  22. {
  23. scrollTrigger: {
  24. scrub: 0.2,
  25. trigger: containerRef.current,
  26. start: start,
  27. end: end,
  28. },
  29. }
  30. )
  31. );
  32. });
  33. return <div ref={containerRef}>{props.children}</div>;
  34. }
  35. TimeLineTransText.propTypes = {
  36. start: PropTypes.string,
  37. end: PropTypes.string,
  38. from: PropTypes.object,
  39. children: PropTypes.any,
  40. };
  41. export function ButtonText(props) {
  42. const containerRef = useRef();
  43. const contentRef = useRef();
  44. const ButtonRef = useRef();
  45. let isOpen = false;
  46. useEffect(() => {
  47. gsap.set(contentRef.current, {
  48. autoAlpha: 0,
  49. });
  50. });
  51. const handlerClick = () => {
  52. if (!isOpen) {
  53. gsap.to(contentRef.current, {
  54. autoAlpha: 1,
  55. });
  56. isOpen = true;
  57. ButtonRef.current.classList.add("open");
  58. ButtonRef.current.innerText = "x";
  59. } else {
  60. gsap.to(contentRef.current, {
  61. autoAlpha: 0,
  62. });
  63. isOpen = false;
  64. ButtonRef.current.innerText = "+";
  65. ButtonRef.current.classList.remove("open");
  66. }
  67. };
  68. const Warpper = styled.div`
  69. display: flex;
  70. position: relative;
  71. align-items: center;
  72. margin-top: 12px;
  73. z-index: 10;
  74. `;
  75. const ButtonWrapper = styled.button`
  76. appearance: none;
  77. position: relative;
  78. font-size: 10px;
  79. border: none;
  80. margin: 0;
  81. align-self: start;
  82. outline: none;
  83. padding: 0;
  84. color: #000;
  85. background-color: transparent;
  86. height: 20px;
  87. width: 20px;
  88. min-width: 20px;
  89. border-radius: 2px;
  90. cursor: pointer;
  91. opacity: 1;
  92. pointer-events: all;
  93. &:before,
  94. &:after {
  95. content: "";
  96. position: absolute;
  97. background-color: hsla(0, 0%, 100%, 0.8);
  98. transition: transform 0.25s ease-out;
  99. }
  100. &.open {
  101. &:before {
  102. transform: rotate(90deg);
  103. }
  104. &:after {
  105. transform: rotate(180deg);
  106. }
  107. }
  108. &:after {
  109. top: 50%;
  110. left: 0;
  111. width: 100%;
  112. height: 4px;
  113. margin-top: -2px;
  114. }
  115. &:before {
  116. top: 0;
  117. left: 50%;
  118. width: 4px;
  119. height: 100%;
  120. margin-left: -2px;
  121. }
  122. `;
  123. const TextWrapper = styled.div`
  124. margin-left: 10px;
  125. margin-right: 10px;
  126. font-size: 12px;
  127. `;
  128. return (
  129. <Warpper ref={containerRef}>
  130. <ButtonWrapper ref={ButtonRef} onClick={handlerClick}>
  131. +
  132. </ButtonWrapper>
  133. <TextWrapper ref={contentRef}>{props.children}</TextWrapper>
  134. </Warpper>
  135. );
  136. }
  137. ButtonText.propTypes = {
  138. content: PropTypes.string,
  139. children: PropTypes.any,
  140. };
  141. export function TimeLineLabelText(props) {
  142. const progressPosition =
  143. void 0 !== props.progressPosition ? props.progressPosition : 0.5;
  144. const fade = props.fade || 0.04;
  145. const duration = props.duration || 0.1;
  146. const verticalOffset = props.verticalOffset || "50vh";
  147. const trigger = props.trigger || Action.scrubPin;
  148. const keyframes = [
  149. Object.assign(
  150. {},
  151. Action.fadeUp,
  152. {},
  153. {
  154. start: progressPosition,
  155. end: progressPosition + fade,
  156. }
  157. ),
  158. Object.assign(
  159. {},
  160. Action.fadeDown,
  161. {},
  162. {
  163. start: progressPosition + duration + fade,
  164. end: progressPosition + duration + fade + fade,
  165. }
  166. ),
  167. ];
  168. // console.log("keyframes-TimeLineLabelText", keyframes);
  169. return (
  170. <TimeLineText
  171. trigger={trigger}
  172. keyframes={keyframes}
  173. verticalOffset={verticalOffset}
  174. className={props.className}
  175. parentHeight={props.parentHeight}
  176. progressPosition={props.progressPosition}
  177. style={props.style}
  178. >
  179. <div css={Style.LabelStyle}>{props.children}</div>
  180. </TimeLineText>
  181. );
  182. }
  183. export function TimeLineWallText(props) {
  184. const progressPosition =
  185. void 0 !== props.progressPosition ? props.progressPosition : 0.5;
  186. const fade = props.fade || 0.025;
  187. const duration = props.duration || 0.05;
  188. const verticalOffset = props.verticalOffset || "46.5vh";
  189. const trigger = props.trigger || Action.scrubPin;
  190. const keyframes = [
  191. Object.assign({}, Action.fadeIn, {
  192. start: progressPosition,
  193. end: progressPosition + fade,
  194. }),
  195. Object.assign({}, Action.fadeOut, {
  196. start: progressPosition + duration + fade,
  197. end: progressPosition + duration + fade + fade,
  198. }),
  199. ];
  200. // console.log("keyframes-TimeLineWallText", keyframes);
  201. return (
  202. <TimeLineText
  203. trigger={trigger}
  204. keyframes={keyframes}
  205. verticalOffset={verticalOffset}
  206. className={props.className}
  207. parentHeight={props.parentHeight}
  208. style={props.style}
  209. progressPosition={props.progressPosition}
  210. {...props}
  211. >
  212. {props.children}
  213. </TimeLineText>
  214. );
  215. }
  216. export function TimeLineTitleText(props) {
  217. const verticalOffset = props.verticalOffset || "100vh";
  218. const trigger = props.trigger || Action.scrubPin;
  219. return (
  220. <TimeLineText
  221. trigger={trigger}
  222. verticalOffset={verticalOffset}
  223. {...props}
  224. ></TimeLineText>
  225. );
  226. }
  227. class TimeLineText extends Component {
  228. constructor(props) {
  229. super();
  230. this.containerRef = createRef();
  231. this.timeline = null;
  232. this.top = 0;
  233. this.progress = 0;
  234. this.keyframes = [];
  235. this.trigger = false;
  236. this.init(props);
  237. }
  238. init(props) {
  239. // console.log("props", props);
  240. this.props = props;
  241. if (props.trigger && props.keyframes) {
  242. var top = props.keyframes[0].start;
  243. this.setTop(top);
  244. } else {
  245. if (this.props.progressPosition) {
  246. this.setTop(this.props.progressPosition);
  247. }
  248. }
  249. }
  250. componentWillUnmount() {
  251. if (this.timeline) {
  252. this.timeline.kill(true);
  253. this.timeline = false;
  254. }
  255. }
  256. componentDidMount() {
  257. if (this.props.keyframes && this.props.trigger) {
  258. this.createTimeLine();
  259. }
  260. }
  261. setTop(height) {
  262. const parentHeight = parseFloat(this.props.parentHeight);
  263. const lastHeight =
  264. ((height * window.innerHeight * ((parentHeight - 100) / 100)) /
  265. ((window.innerHeight * parentHeight) / 100)) *
  266. 100;
  267. this.top = lastHeight + "%";
  268. console.log("this.top", this.top);
  269. }
  270. componentDidCatch(error) {
  271. console.error("error", error);
  272. }
  273. createTimeLine() {
  274. if (!this.timeline) {
  275. console.log("createTimeLine");
  276. const start = this.props.keyframes[0].start;
  277. const end = this.props.keyframes[0].end;
  278. const dif = end - start;
  279. const lastStart =
  280. this.props.keyframes[this.props.keyframes.length - 1].start;
  281. const lastEnd = this.props.keyframes[this.props.keyframes.length - 1].end;
  282. const lDif = lastEnd - lastStart;
  283. const gapDiff = lastStart - end;
  284. const timeRes =
  285. (lastEnd - start) * (parseFloat(this.props.parentHeight) - 100);
  286. const flag = `+=${timeRes}%`;
  287. console.log("timeRes", flag);
  288. this.timeline = gsap.timeline({
  289. scrollTrigger: Object.assign(
  290. {
  291. trigger: this.containerRef.current,
  292. start: "top top",
  293. end: flag,
  294. },
  295. this.props.trigger
  296. ),
  297. });
  298. for (var i = 0; i < this.props.keyframes.length; i += 1) {
  299. const cFrame = this.props.keyframes[i];
  300. if (1 === i && gapDiff > 0.001) {
  301. this.timeline.to(this.containerRef.current, {
  302. duration: gapDiff,
  303. });
  304. }
  305. if (cFrame.type === "set") {
  306. this.timeline.set(
  307. this.containerRef.current,
  308. Object.assign({}, cFrame.animation)
  309. );
  310. }
  311. if (cFrame.type === "from") {
  312. this.timeline.from(
  313. this.containerRef.current,
  314. Object.assign({}, cFrame.animation, {
  315. duration: dif,
  316. })
  317. );
  318. }
  319. if (cFrame.type === "to") {
  320. this.timeline.to(
  321. this.containerRef.current,
  322. Object.assign(cFrame.animation, {
  323. duration: lDif,
  324. })
  325. );
  326. }
  327. }
  328. }
  329. }
  330. render() {
  331. const marginTop =
  332. "calc(var(--vh, 1vh) *" + parseFloat(this.props.verticalOffset) + ")";
  333. return (
  334. <div
  335. css={css`
  336. top: ${this.top};
  337. position: absolute;
  338. width: 100%;
  339. text-align: center;
  340. pointer-events: none;
  341. `}
  342. ref={this.containerRef}
  343. >
  344. <div
  345. css={css`
  346. position: relative;
  347. display: flex;
  348. margin-left: var(--cl);
  349. max-width: calc(var(--cw) * 100);
  350. z-index: 20;
  351. direction: ltr;
  352. direction: var(--text-direction);
  353. text-align: left;
  354. text-align: var(--alignment);
  355. `}
  356. >
  357. <div
  358. css={css`
  359. position: absolute;
  360. width: 100%;
  361. `}
  362. >
  363. <div
  364. className="text-wrapper"
  365. css={css`
  366. margin-top: ${marginTop};
  367. `}
  368. >
  369. {this.props.children}
  370. </div>
  371. </div>
  372. </div>
  373. </div>
  374. );
  375. }
  376. }
  377. TimeLineText.propTypes = {
  378. verticalOffset: PropTypes.string,
  379. children: PropTypes.any,
  380. trigger: PropTypes.object,
  381. parentHeight: PropTypes.string,
  382. keyframes: PropTypes.arrayOf(PropTypes.object),
  383. progressPosition: PropTypes.number,
  384. };
  385. TimeLineText.defaultProps = {
  386. verticalOffset: "100vh",
  387. };
  388. TimeLineTitleText.propTypes = {
  389. ...TimeLineText.propTypes,
  390. };
  391. TimeLineLabelText.propTypes = {
  392. ...TimeLineText.propTypes,
  393. };
  394. TimeLineWallText.propTypes = {
  395. ...TimeLineText.propTypes,
  396. };