2022-11-25 20:25:03 +00:00
|
|
|
import type { MouseEvent as ReactMouseEvent, ReactNode } from 'react';
|
|
|
|
import { createRef, PureComponent } from 'react';
|
|
|
|
import styled from 'styled-components';
|
2020-07-03 21:19:05 +00:00
|
|
|
import tw from 'twin.macro';
|
2022-11-25 20:25:03 +00:00
|
|
|
|
|
|
|
import FadeTransition from '@/components/elements/transitions/FadeTransition';
|
2020-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
interface Props {
|
2022-11-25 20:25:03 +00:00
|
|
|
children: ReactNode;
|
|
|
|
renderToggle: (onClick: (e: ReactMouseEvent<unknown>) => void) => any;
|
2020-04-10 05:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const DropdownButtonRow = styled.button<{ danger?: boolean }>`
|
|
|
|
${tw`p-2 flex items-center rounded w-full text-neutral-500`};
|
|
|
|
transition: 150ms all ease;
|
|
|
|
|
|
|
|
&:hover {
|
2022-11-25 20:25:03 +00:00
|
|
|
${props => (props.danger ? tw`text-red-700 bg-red-100` : tw`text-neutral-700 bg-neutral-100`)};
|
2020-04-10 05:08:09 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-07-11 18:12:59 +00:00
|
|
|
interface State {
|
|
|
|
posX: number;
|
|
|
|
visible: boolean;
|
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
class DropdownMenu extends PureComponent<Props, State> {
|
2020-07-11 18:12:59 +00:00
|
|
|
menu = createRef<HTMLDivElement>();
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
override state: State = {
|
2020-07-11 18:12:59 +00:00
|
|
|
posX: 0,
|
|
|
|
visible: false,
|
|
|
|
};
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
override componentWillUnmount() {
|
2020-07-11 18:12:59 +00:00
|
|
|
this.removeListeners();
|
|
|
|
}
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
override componentDidUpdate(_prevProps: Readonly<Props>, prevState: Readonly<State>) {
|
2020-07-11 18:12:59 +00:00
|
|
|
const menu = this.menu.current;
|
|
|
|
|
|
|
|
if (this.state.visible && !prevState.visible && menu) {
|
|
|
|
document.addEventListener('click', this.windowListener);
|
|
|
|
document.addEventListener('contextmenu', this.contextMenuListener);
|
2020-12-27 18:49:33 +00:00
|
|
|
menu.style.left = `${Math.round(this.state.posX - menu.clientWidth)}px`;
|
2020-07-11 18:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.state.visible && prevState.visible) {
|
|
|
|
this.removeListeners();
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
removeListeners() {
|
2020-07-11 18:12:59 +00:00
|
|
|
document.removeEventListener('click', this.windowListener);
|
|
|
|
document.removeEventListener('contextmenu', this.contextMenuListener);
|
2022-11-25 20:25:03 +00:00
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
onClickHandler(e: ReactMouseEvent<unknown>) {
|
2020-07-11 18:12:59 +00:00
|
|
|
e.preventDefault();
|
|
|
|
this.triggerMenu(e.clientX);
|
2022-11-25 20:25:03 +00:00
|
|
|
}
|
2020-07-11 18:12:59 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
contextMenuListener() {
|
|
|
|
this.setState({ visible: false });
|
|
|
|
}
|
2020-07-11 18:12:59 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
windowListener(e: MouseEvent): any {
|
2020-07-11 18:12:59 +00:00
|
|
|
const menu = this.menu.current;
|
2020-07-11 18:19:38 +00:00
|
|
|
|
2020-07-11 18:12:59 +00:00
|
|
|
if (e.button === 2 || !this.state.visible || !menu) {
|
2020-04-10 05:08:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-11 18:12:59 +00:00
|
|
|
if (e.target === menu || menu.contains(e.target as Node)) {
|
2020-04-10 05:08:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-11 18:12:59 +00:00
|
|
|
if (e.target !== menu && !menu.contains(e.target as Node)) {
|
|
|
|
this.setState({ visible: false });
|
2020-04-10 05:08:09 +00:00
|
|
|
}
|
2022-11-25 20:25:03 +00:00
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
triggerMenu(posX: number) {
|
|
|
|
this.setState(s => ({
|
2022-06-26 19:13:52 +00:00
|
|
|
posX: !s.visible ? posX : s.posX,
|
|
|
|
visible: !s.visible,
|
|
|
|
}));
|
2022-11-25 20:25:03 +00:00
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
override render() {
|
2020-07-11 18:12:59 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{this.props.renderToggle(this.onClickHandler)}
|
2022-11-25 20:25:03 +00:00
|
|
|
|
|
|
|
<FadeTransition duration="duration-150" show={this.state.visible} appear unmount>
|
2020-07-11 18:12:59 +00:00
|
|
|
<div
|
|
|
|
ref={this.menu}
|
2022-11-25 20:25:03 +00:00
|
|
|
onClick={e => {
|
2020-07-11 18:12:59 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
this.setState({ visible: false });
|
|
|
|
}}
|
2020-12-27 18:49:33 +00:00
|
|
|
style={{ width: '12rem' }}
|
2020-12-26 18:41:25 +00:00
|
|
|
css={tw`absolute bg-white p-2 rounded border border-neutral-700 shadow-lg text-neutral-500 z-50`}
|
2020-07-11 18:12:59 +00:00
|
|
|
>
|
|
|
|
{this.props.children}
|
|
|
|
</div>
|
2022-11-25 20:25:03 +00:00
|
|
|
</FadeTransition>
|
2020-07-11 18:12:59 +00:00
|
|
|
</div>
|
2020-04-10 05:08:09 +00:00
|
|
|
);
|
2020-07-11 18:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
export default DropdownMenu;
|