import { h, Component } from 'preact'; import PropTypes from 'prop-types'; import { Button, ButtonGroup, Dropdown, FormField, RadioButton, } from '@crayons'; const COMMENT_SUBSCRIPTION_TYPE = { ALL: 'all_comments', TOP: 'top_level_comments', AUTHOR: 'only_author_comments', }; export class CommentSubscription extends Component { state = { showOptions: false, commentSubscriptionType: COMMENT_SUBSCRIPTION_TYPE.ALL, subscribed: false, }; componentDidMount() { window.addEventListener('scroll', this.dropdownPlacementHandler); } componentDidUpdate() { const { showOptions } = this.state; if (showOptions) { this.dropdownPlacementHandler(); } } componentWillUnmount() { window.removeEventListener('scroll', this.dropdownPlacementHandler); } dropdownPlacementHandler = () => { const { base: element } = this.dropdownElement; // Reset the top before doing any calculations element.style.bottom = ''; const { bottom: dropDownBottom } = element.getBoundingClientRect(); const { height } = this.buttonGroupElement.base.getBoundingClientRect(); if ( Math.sign(dropDownBottom) === -1 || dropDownBottom > window.innerHeight ) { // The 4 pixels is the box shadow from the drop down. element.style.bottom = `${height + 4}px`; } }; commentSubscriptionClick = (event) => { this.setState({ commentSubscriptionType: event.target.value, }); }; render() { const { showOptions, commentSubscriptionType, subscribed } = this.state; const { onSubscribe, onUnsubscribe } = this.props; const CogIcon = () => ( ); return (