Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

1.2 KB
Raw Blame
import { SidebarRefs, SidebarState, SidebarInfo } from "@editor/types/sidebar";

/**
 * Gets the All tools sidebar information using React refs and state
 * @param refs - Object containing refs to sidebar elements
 * @param state - Current sidebar state
 * @returns Object containing the sidebar rect and whether the tool panel is active
 */
export function getSidebarInfo(
  refs: SidebarRefs,
  state: SidebarState,
): SidebarInfo {
  const { quickAccessRef, toolPanelRef } = refs;
  const { readerMode } = state;

  // Reading swaps the tool panel out for the reader's own rail.
  const isToolPanelActive = !readerMode;

  let rect: DOMRect | null = null;

  if (isToolPanelActive && toolPanelRef.current) {
    // Tool panel is expanded: use its rect
    rect = toolPanelRef.current.getBoundingClientRect();
  } else if (quickAccessRef.current) {
    // Fall back to quick access bar
    // This probably isn't needed but if we ever have tooltips or modals that need to be positioned relative to the quick access bar, we can use this
    rect = quickAccessRef.current.getBoundingClientRect();
  }

  return {
    rect,
    isToolPanelActive,
    sidebarState: state,
  };
}