Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-3733 Close data editor tabs with the middle mouse button #2805

Merged

Conversation

sergeyteleshev
Copy link
Contributor

No description provided.

@sergeyteleshev sergeyteleshev self-assigned this Jul 25, 2024
@@ -27,9 +27,15 @@ export const Tab = observer<TabProps>(function Tab(props) {
const styles = useS(style);
const canClose = getComputed(() => !!onClose || (tab.closable && tab.state.closable));

function onMouseUpHandler(event: React.MouseEvent<HTMLDivElement>) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first, I used onAuxClick event. It helps avoid miss-clicks and closes the correct tab. But since it is not compatible with Safari, I decided to go with this one (onMouseUp) to support all browsers since users can learn after view clicks how the feature closes tabs

we can also go with our custom hook like this, but this solution looks a bit satanic to me:

export function useAuxClick<T extends HTMLElement>(ref: RefObject<T>, handler: AuxClickHandler): void {
  const handlerRef = useRef<AuxClickHandler>(handler);
  let isAuxButton = false;
  let preventDefault = false;

  const handleMouseDown = (event: MouseEvent) => {
    isAuxButton = true;
    preventDefault = event.defaultPrevented;
    event.preventDefault();
  };

  const handleMouseUp = (event: MouseEvent) => {
    if (isAuxButton) {
      isAuxButton = false;
      if (!preventDefault) {
        handlerRef.current(event);
      }
    }
  };

  useEffect(() => {
    handlerRef.current = handler;
  }, [handler]);

  useEffect(() => {
    const targetElement = ref.current;
    if (!targetElement) {
      return;
    }

    targetElement.addEventListener('mousedown', handleMouseDown);
    targetElement.addEventListener('mouseup', handleMouseUp);

    return () => {
      targetElement.removeEventListener('mousedown', handleMouseDown);
      targetElement.removeEventListener('mouseup', handleMouseUp);
    };
  }, [ref]);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added not only for data viewer but for:

  • left and right sidebar (scripts, dataset, explorer)
  • bottom sidebar (query manager, log viewer, cloud explorer)

P.S. Explorer works fine and does not close

@Wroud Wroud merged commit 45683f1 into devel Jul 29, 2024
6 checks passed
@Wroud Wroud deleted the CB-3733-close-data-editor-tabs-with-the-middle-mouse-button branch July 29, 2024 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants