ericclemmons / click-to-component

Option+Click React components in your browser to instantly open the source in VS Code
MIT License
1.94k stars 75 forks source link

Option+Click doesn't work with material-ui components: "Couldn't find a React instance for the element" #43

Closed Lwdthe1 closed 11 months ago

Lwdthe1 commented 2 years ago

Describe the bug Option+Right-click works to open the context menu and then go to a selected component.

However, just Option+Click doesn't do anything despite showing the green border around the hovered element. This appears to only be a problem with material UI styled elements.

To Reproduce This is a private product, so I can't share any useful code. However, the problem might be with the fact that we use MaterialUI's ("@material-ui/core": "^4.12.2",) makeStyles() for styling our components. That might mess up the logic for how you're finding the target component.

Screen Shot 2022-05-11 at 10 21 54 AM

Expected behavior It should open the target component's file in VSCode as desired.

Desktop (please complete the following information):

jens-duttke commented 2 years ago

I have the same problem, not with MaterialUI, but with a Gatsby project. It seems like some FiberNodes don't have _debugSource set: image

wojiangkuanglong commented 2 years ago

Same problem, try to replace react.development.js and react-dom.development.js

LeonMueller-OneAndOnly commented 2 years ago

Same problem, try to replace react.development.js and react-dom.development.js

@wojiangkuanglong Good evening. I am currently facing the same issue as @Lwdthe1. Could you please elaborate with which what I shall replace react.development.js & react-dom.development.js ? And where would i typically find these files?

LeonMueller-OneAndOnly commented 2 years ago

Hello folks, i have found a solution four your problem. I implemented @ma101an's solution. When no valid source is found the next parent element will be used as a fallback an so on until a valid source is found.

I forked the codebase to TypeScript in order to debug in my application, therefore i will provide the source code directly. I modifed src/getSourceForElement slightly:

import { getReactInstanceForElement } from "./getReactInstanceForElement"
import { getSourceForInstance } from "./getSourceForInstance"

/**
 * @typedef {import('react-reconciler').Fiber} Fiber
 */

export function getSourceForElement(element: HTMLElement) {
  const instance = getReactInstanceForElement(element)
  const source = getSourceForInstance(instance)

  if (source) return source

  // console.warn("Couldn't find a React instance for the element", element)
  // console.info("Let us try to find a React instance ancestor which has a source")

  const fallbackSource = getFirstParentElementWithSource(element)
  return fallbackSource
}

function getFirstParentElementWithSource(element: HTMLElement): any {
  const parentElement = element.parentElement
  if (parentElement === null) throw new Error("No parent found")

  const instance = getReactInstanceForElement(parentElement)
  const source = getSourceForInstance(instance)

  if (source) return source
  else return getFirstParentElementWithSource(element)
}
ma101an commented 2 years ago

34

wojiangkuanglong commented 2 years ago

Same problem, try to replace react.development.js and react-dom.development.js

@wojiangkuanglong Good evening. I am currently facing the same issue as @Lwdthe1. Could you please elaborate with which what I shall replace react.development.js & react-dom.development.js ? And where would i typically find these files?

Don't use react.production(.min).js or react.profiling(.min).js, use react.development.js

LeonMueller-OneAndOnly commented 11 months ago

This issue has been resolved with following pull-request: https://github.com/ericclemmons/click-to-component/pull/79

The fix is already merged into the main branch now.