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; } @observer export class ToggleBar extends React.Component{ @observable forwardTimeline: anime.AnimeTimelineInstance; @observable _toggleButton: React.RefObject; @observable _originalStatus: boolean = this.props.originalStatus; @observable _curStatus: boolean = this.props.originalStatus; constructor(props: ToggleBarProps) { super(props); this._toggleButton = React.createRef(); this.forwardTimeline = anime.timeline({ loop: false, 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; if (this._originalStatus) { this.forwardTimeline.add({ targets: this._toggleButton.current, translateX: totalWidth, easing: "easeInOutQuad", duration: 500 }); } else { this.forwardTimeline.add({ targets: this._toggleButton.current, translateX: -totalWidth, easing: "easeInOutQuad", duration: 500 }); } } @action.bound onclick() { this._curStatus = !this._curStatus; this.forwardTimeline.play(); this.forwardTimeline.reverse(); this.props.changeStatus(); } render() { return (
{this.props.optionOne}
{this.props.optionTwo}
); } }