import * as React from 'react'; import { observer } from 'mobx-react'; import { observable, action, runInAction, computed } from 'mobx'; import "./SearchBox.scss"; import "./ToggleBar.scss"; import * as anime from 'animejs'; export interface ToggleBarProps { //false = right, true = left changeStatus(): void; originalStatus: boolean; optionOne: string; optionTwo: string; } //TODO: justify content will align to specific side. Maybe do status passed in and out? @observer export class ToggleBar extends React.Component{ @observable _wordStatus: boolean = true; @observable forwardTimeline: anime.AnimeTimelineInstance; @observable reverseTimeline: anime.AnimeTimelineInstance; @observable _toggleButton: React.RefObject; @observable _originalStatus: boolean = this.props.originalStatus; constructor(props: ToggleBarProps) { super(props); this._toggleButton = React.createRef(); this.forwardTimeline = anime.timeline({ autoplay: false, direction: "reverse" }); this.reverseTimeline = anime.timeline({ autoplay: false, direction: "reverse" }); } @computed get totalWidth() { return this.getTotalWidth(); } getTotalWidth() { let bar = document.getElementById("toggle-bar"); let tog = document.getElementById("toggle-button"); let barwidth = 0; let togwidth = 0; if (bar && tog) { barwidth = bar.clientWidth; togwidth = tog.clientWidth; } let totalWidth = (barwidth - togwidth - 10); return totalWidth; } componentDidMount = () => { let totalWidth = this.totalWidth; this.forwardTimeline.add({ targets: this._toggleButton.current, loop: false, translateX: totalWidth, easing: "easeInOutQuad", duration: 500, }); this.reverseTimeline.add({ targets: this._toggleButton.current, loop: false, translateX: -totalWidth, easing: "easeInOutQuad", duration: 500, }); } @action.bound onclick() { console.log(this._originalStatus) if (this._originalStatus) { this.forwardTimeline.play(); this.forwardTimeline.reverse(); } else { this.reverseTimeline.play(); this.reverseTimeline.reverse(); } this._wordStatus = !this._wordStatus; this.props.changeStatus(); } render() { return (
{this.props.optionOne}
{this.props.optionTwo}
); }; }