diff options
author | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2017-09-29 15:03:12 +0200 |
---|---|---|
committer | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2017-09-29 17:09:48 +0200 |
commit | 059cf8688eafd9f151a1fad84997ce77e5af511e (patch) | |
tree | b0d0941506386d23955ee9b893c8cc73e0c07b2d /server/sonar-web/src/main/js/components/common/ScreenPositionHelper.tsx | |
parent | ee41573b8b309a123bc23ac623663107cc410af3 (diff) | |
download | sonarqube-059cf8688eafd9f151a1fad84997ce77e5af511e.tar.gz sonarqube-059cf8688eafd9f151a1fad84997ce77e5af511e.zip |
SONAR-9792 Fix sidebar position with a notification
Diffstat (limited to 'server/sonar-web/src/main/js/components/common/ScreenPositionHelper.tsx')
-rw-r--r-- | server/sonar-web/src/main/js/components/common/ScreenPositionHelper.tsx | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/server/sonar-web/src/main/js/components/common/ScreenPositionHelper.tsx b/server/sonar-web/src/main/js/components/common/ScreenPositionHelper.tsx new file mode 100644 index 00000000000..00f8f375766 --- /dev/null +++ b/server/sonar-web/src/main/js/components/common/ScreenPositionHelper.tsx @@ -0,0 +1,67 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as React from 'react'; +import { throttle } from 'lodash'; + +interface Props { + className?: string; + children: (position: { top: number; left: number }) => React.ReactElement<any>; +} + +interface State { + position: { top: number; left: number }; +} + +export default class ScreenPositionHelper extends React.PureComponent<Props, State> { + container: HTMLDivElement; + throttledUpdatePosition: () => void; + + constructor(props: Props) { + super(props); + this.state = { position: { top: 0, left: 0 } }; + this.throttledUpdatePosition = throttle(this.updatePosition, 100); + } + + componentDidMount() { + this.updatePosition(); + window.addEventListener('resize', this.throttledUpdatePosition); + } + + componentWillUnmount() { + window.removeEventListener('resize', this.throttledUpdatePosition); + } + + updatePosition = () => { + const containerPos = this.container.getBoundingClientRect(); + this.setState({ + position: { top: window.scrollY + containerPos.top, left: window.scrollX + containerPos.left } + }); + }; + + render() { + return ( + <div + className={this.props.className} + ref={container => (this.container = container as HTMLDivElement)}> + {this.props.children(this.state.position)} + </div> + ); + } +} |