Skip to content

ReactScan toolbar overlaps Next.js dev icon #430

Closed

Description

@paulrogov
Image

I have website interface elements in other corners, so the bottom-right is okay location.

Preferably, it would be handy to set some default position e.g. "bottom center", or "bottom-right" with offset 100px top.

Currently, I'm hacking the default toolbar creating a client-only component to implement this offset, so the reactscan is a little bit above next.js dev toolbar icon.

"react-scan": "^0.5.3",
"next": "^15.5.6",

Activity

  1. paulrogov commented on Mar 31, 2026

    @paulrogov
    Author

    Expanded:
    Image

    Minimzed:
    Image

    I've vibe-coded "the monster", but it works adding offset to the shadow root styles:

    Next.js layout.tsx:

    import ReactScan from "@/components/dev/ReactScanOffset";
    
    export default function RootLayout({ children }: Readonly<{ children: React.ReactNode }>) {
    
    return (
        <html>
          <head>
            <meta
              name="viewport"
              content="minimum-scale=1, initial-scale=1, width=device-width, user-scalable=no, viewport-fit=cover"
            />
          </head>
          <body>
            <ReactScan />
            {children}
          </body>
        </html>
      );

    Component ReactScan.tsx:

    "use client";
    
    import { useEffect } from "react";
    
    const ROOT_ID = "react-scan-root";
    const TOOLBAR_ID = "react-scan-toolbar";
    const STYLE_ID = "react-scan-toolbar-position-override";
    const COLLAPSED_KEY = "react-scan-widget-collapsed-v1";
    const DEFAULT_SAFE_AREA_PX = 24;
    const EXPANDED_RIGHT_OFFSET_PX = 96;
    const EXPANDED_BOTTOM_OFFSET_PX = 40;
    const COLLAPSED_RIGHT_MARGIN_WHEN_BOTTOM_ATTACHED_PX = 96;
    const COLLAPSED_BOTTOM_MARGIN_WHEN_RIGHT_ATTACHED_PX = 96;
    
    type CollapsedOrientation = "horizontal" | "vertical";
    type ToolbarCorner = "top-left" | "top-right" | "bottom-left" | "bottom-right";
    
    type CollapsedState = {
      corner: ToolbarCorner;
      orientation: CollapsedOrientation;
    };
    
    type ReactScanProps = {
      enabled?: boolean;
      showToolbar?: boolean;
      startMinimized?: boolean;
      collapsedOrientation?: CollapsedOrientation;
      pinExpandedToOffset?: boolean;
      pinCollapsedToOffset?: boolean;
    };
    
    function setDefaultCollapsedState(startMinimized: boolean, collapsedOrientation: CollapsedOrientation) {
      if (!startMinimized) {
        return;
      }
    
      window.localStorage.setItem(
        COLLAPSED_KEY,
        JSON.stringify({ corner: "bottom-right", orientation: collapsedOrientation }),
      );
    }
    
    function isCollapsedOrientation(value: unknown): value is CollapsedOrientation {
      return value === "horizontal" || value === "vertical";
    }
    
    function isToolbarCorner(value: unknown): value is ToolbarCorner {
      return value === "top-left" || value === "top-right" || value === "bottom-left" || value === "bottom-right";
    }
    
    function readCollapsedState(): CollapsedState | null {
      const raw = window.localStorage.getItem(COLLAPSED_KEY);
    
      if (!raw) {
        return null;
      }
    
      try {
        const parsed: unknown = JSON.parse(raw);
    
        if (typeof parsed !== "object" || parsed === null) {
          return null;
        }
    
        const maybeCollapsed = parsed as { corner?: unknown; orientation?: unknown };
    
        if (!isToolbarCorner(maybeCollapsed.corner) || !isCollapsedOrientation(maybeCollapsed.orientation)) {
          return null;
        }
    
        return {
          corner: maybeCollapsed.corner,
          orientation: maybeCollapsed.orientation,
        };
      } catch {
        return null;
      }
    }
    
    function buildExpandedRule() {
      const translateXPx = DEFAULT_SAFE_AREA_PX - EXPANDED_RIGHT_OFFSET_PX;
      const translateYPx = DEFAULT_SAFE_AREA_PX - EXPANDED_BOTTOM_OFFSET_PX;
    
      return `
        #${TOOLBAR_ID}.cursor-move {
          translate: ${translateXPx}px ${translateYPx}px !important;
        }
      `;
    }
    
    function buildCollapsedRule(collapsedState: CollapsedState | null) {
      const corner = collapsedState?.corner ?? "bottom-right";
      const orientation = collapsedState?.orientation ?? "vertical";
      let translateXPx = 0;
      let translateYPx = 0;
    
      if (orientation === "vertical") {
        const delta = COLLAPSED_RIGHT_MARGIN_WHEN_BOTTOM_ATTACHED_PX - DEFAULT_SAFE_AREA_PX;
        translateXPx = corner.endsWith("right") ? -delta : delta;
      } else {
        const delta = COLLAPSED_BOTTOM_MARGIN_WHEN_RIGHT_ATTACHED_PX - DEFAULT_SAFE_AREA_PX;
        translateYPx = corner.startsWith("bottom") ? -delta : delta;
      }
    
      return `
        #${TOOLBAR_ID}.cursor-pointer {
          translate: ${translateXPx}px ${translateYPx}px !important;
        }
      `;
    }
    
    function ensureShadowStyle(
      shadowRoot: ShadowRoot,
      options: { pinExpandedToOffset: boolean; pinCollapsedToOffset: boolean },
    ) {
      const rules: string[] = [];
    
      if (options.pinExpandedToOffset) {
        rules.push(buildExpandedRule());
      }
    
      if (options.pinCollapsedToOffset) {
        rules.push(buildCollapsedRule(readCollapsedState()));
      }
    
      if (rules.length === 0) {
        return;
      }
    
      const cssText = rules.join("\n");
      let styleElement = shadowRoot.getElementById(STYLE_ID);
    
      if (!(styleElement instanceof HTMLStyleElement)) {
        styleElement = document.createElement("style");
        styleElement.id = STYLE_ID;
        shadowRoot.appendChild(styleElement);
      }
    
      if (styleElement.textContent !== cssText) {
        styleElement.textContent = cssText;
      }
    }
    
    export default function ReactScan({
      enabled = true,
      showToolbar = true,
      startMinimized = true,
      collapsedOrientation = "vertical",
      pinExpandedToOffset = true,
      pinCollapsedToOffset = true,
    }: ReactScanProps) {
      useEffect(() => {
        if (process.env.NODE_ENV !== "development") {
          return;
        }
    
        setDefaultCollapsedState(startMinimized, collapsedOrientation);
    
        let isDisposed = false;
        let rootObserver: MutationObserver | null = null;
        let shadowObserver: MutationObserver | null = null;
        let observedShadowRoot: ShadowRoot | null = null;
    
        const connectShadowStyle = () => {
          const host = document.getElementById(ROOT_ID);
          if (!(host instanceof HTMLElement) || !host.shadowRoot) {
            return;
          }
    
          ensureShadowStyle(host.shadowRoot, { pinExpandedToOffset, pinCollapsedToOffset });
    
          if (observedShadowRoot === host.shadowRoot) {
            return;
          }
    
          observedShadowRoot = host.shadowRoot;
          shadowObserver?.disconnect();
    
          shadowObserver = new MutationObserver(() => {
            if (!observedShadowRoot) {
              return;
            }
    
            ensureShadowStyle(observedShadowRoot, { pinExpandedToOffset, pinCollapsedToOffset });
          });
    
          shadowObserver.observe(observedShadowRoot, {
            childList: true,
            subtree: true,
            attributes: true,
            attributeFilter: ["class", "style"],
          });
        };
    
        const run = async () => {
          const { scan } = await import("react-scan");
    
          if (isDisposed) {
            return;
          }
    
          scan({
            enabled,
            showToolbar,
          });
    
          if (!pinExpandedToOffset && !pinCollapsedToOffset) {
            return;
          }
    
          connectShadowStyle();
    
          rootObserver = new MutationObserver(() => {
            connectShadowStyle();
          });
    
          rootObserver.observe(document.documentElement, {
            childList: true,
            subtree: true,
          });
        };
    
        void run();
    
        return () => {
          isDisposed = true;
          rootObserver?.disconnect();
          shadowObserver?.disconnect();
        };
      }, [enabled, showToolbar, startMinimized, collapsedOrientation, pinExpandedToOffset, pinCollapsedToOffset]);
    
      return null;
    }
  2. RobPruzan commented on Apr 2, 2026

    @RobPruzan
    Contributor

    I like this. Would love to see the offset as configuration to the scan({...}) options, and maybe we increase the default concept to handle next.js always

  3. aidenybai commented on May 2, 2026

    @aidenybai
    Owner

    Fixed in v0.5.5 via #437.

    Added a new safeArea option that accepts either a single number (insets all edges) or a per-edge object:

    scan({
      // single value, applies to all four edges
      safeArea: 96,
    
      // or per-edge — set just what you need
      safeArea: { right: 96, bottom: 96 },
    })

    Default is still 24 so existing setups are unchanged. The constant was refactored out of widget positioning math (calculatePosition, calculateNewSizeAndPosition, WindowDimensions, collapsed-corner positioning, max-size CSS) into a getSafeArea() helper that resolves per-edge from options at runtime.

    Bugbot also flagged a pre-existing RTL bug surfaced by asymmetric values — the right-anchor positioning math was applying the wrong edge to the wrong physical side; that's also fixed in this release.

    Thanks for the detailed repro and the vibe-coded reference implementation 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions