diff --git a/src/widgets/vc-carousel.jsx b/src/widgets/vc-carousel.jsx index 82ed1262..0f169cbb 100644 --- a/src/widgets/vc-carousel.jsx +++ b/src/widgets/vc-carousel.jsx @@ -9,39 +9,56 @@ import { VC_CAROUSEL_ITEM_GAP } from '../constants.js'; export default class VCCarousel extends React.Component { constructor(props) { super(props); - this.handlePrevClick = this.handlePrevClick.bind(this); - this.handleNextClick = this.handleNextClick.bind(this); - this.carouselRef = React.createRef(); + this.state = { + prevVisible: false, + nextVisible: false + }; + + this.handleScrollReference = this.handleScrollReference.bind(this); + this.handleScrollEvent = this.handleScrollEvent.bind(this); + this.handleScrollClick = this.handleScrollClick.bind(this); + + this.carouselRef = null; this.contentRef = React.createRef(); - this.prevRef = React.createRef(); - this.nextRef = React.createRef(); } - handlePrevClick() { - const gap = VC_CAROUSEL_ITEM_GAP; - const width = this.carouselRef.current.offsetWidth; - this.carouselRef.current.scrollBy(-(width + gap), 0); - if (this.carouselRef.current.scrollLeft - width - gap <= 0) { - this.prevRef.current.style.display = 'none'; - } - if (!this.contentRef.current.scrollWidth - width - gap <= this.carouselRef.current.scrollLeft + width) { - this.nextRef.current.style.display = 'flex'; + componentDidUpdate(prevProps) { + if (this.carouselRef && this.props.viewportWidth != prevProps.viewportWidth) { + this.handleScrollEvent({target: this.carouselRef}); } } - handleNextClick() { - const gap = VC_CAROUSEL_ITEM_GAP; - const width = this.carouselRef.current.offsetWidth; - this.carouselRef.current.scrollBy(width + gap, 0); - if (this.carouselRef.current.scrollWidth !== 0) { - this.prevRef.current.style.display = 'flex'; + componentWillUnmount() { + if (this.carouselRef) { + this.carouselRef.removeEventListener('scroll', this.handleScrollEvent); } - if (this.contentRef.current.scrollWidth - width - gap <= this.carouselRef.current.scrollLeft + width) { - this.nextRef.current.style.display = 'none'; + } + + // Don't use React.createRef as the ref.current is not available in componentDidMount in this component. + handleScrollReference(node) { + if (node) { + this.carouselRef = node; + this.carouselRef.addEventListener('scroll', this.handleScrollEvent); + this.handleScrollEvent({target: this.carouselRef}); } } + // Get older messages and show/hide [go to latest message] button. + handleScrollEvent(event) { + this.setState({ + // Show [prev] / [next] as appropriate. + prevVisible: event.target.scrollLeft > VC_CAROUSEL_ITEM_GAP, + nextVisible: event.target.scrollWidth - event.target.scrollLeft - event.target.offsetWidth > VC_CAROUSEL_ITEM_GAP + }); + } + + handleScrollClick(next) { + // this.carouselRef.offsetWidth - is the width of the visible window. + const val = this.carouselRef.offsetWidth + VC_CAROUSEL_ITEM_GAP; + this.carouselRef.scrollBy(next ? val : -val, 0); + } + render() { let peers = []; if (this.props.participants) { @@ -56,44 +73,32 @@ export default class VCCarousel extends React.Component { cameraPub={cameraPub ? cameraPub : null} micPub={micPub ? micPub : null} name={party.name} - identity={identity} + userId={identity} photo={party.photo} key={i} />); }); } return ( - <> -