focus-trap-react icon indicating copy to clipboard operation
focus-trap-react copied to clipboard

Cannot read property 'ownerDocument' of null

Open lukeschunk opened this issue 7 years ago • 12 comments

I'm getting this error thrown by the tabbable library which focus-trap-react depends on by way of focus-trap. At first I thought this might be a jest/enzyme problem but I'm also seeing it in non-test code.

TypeError: Cannot read property 'ownerDocument' of null
    at tabbable (index.js:21)
    at updateTabbableNodes (index.js:267)
    at Object.activate (index.js:72)
    at FocusTrap.componentDidMount (focus-trap-react.js:55)

I also noticed that the line that's throwing this error is an older version than your most recent update of tabbable...perhaps a version update would help? I haven't tested that yet, but I would love any input you have.

I've used this library before and had really great results with it, so thanks!

lukeschunk avatar May 06 '19 14:05 lukeschunk

Same here, I upgraded from 4.0.1 to 6.0.0 and facing this issue

pradel avatar May 08 '19 09:05 pradel

Also happening in 5.0.1

pradel avatar May 08 '19 09:05 pradel

@lukeschunk can you please be more specific about the situation in which this behavior is happening? This does not happen universally — for example, I'm not seeing it in the demos — so I think we need to identify what you and @pradel are doing that triggers the error.

davidtheclark avatar May 19 '19 18:05 davidtheclark

I get the same error when trapping a SemanticUI react Modal

taasan avatar May 22 '19 14:05 taasan

I think the source of the error is https://github.com/davidtheclark/tabbable/blob/b8ceed435664fdf51b5f70729ab50c63e1877190/index.js#L142. Somehow in special cases we're arriving at that point and node is null. Probably adding an additional check would stop the error — but it would still be nice to understand why node might be null at that point.

There's a chain of libraries to update here — tabbable, focus-trap, then this one.

davidtheclark avatar May 22 '19 15:05 davidtheclark

I haven't had the chance to take a look yet, but I will when I can. 💥

lukeschunk avatar May 22 '19 15:05 lukeschunk

I found that if the child of FocusTrap isn't a HTML element then this error pops up.

E.g

Errors with Cannot read property 'ownerDocument' of null

<FocusTrap>
  <Fragment>
    {/* ...content */}
  </Fragment>
</FocusTrap>

Works

<FocusTrap>
  <div>
    {/* ...content */}
  </div>
</FocusTrap>

You can see an example here. Make sure its set to current module view. And checkout will-error.js & works.js

jimmaaay avatar Oct 18 '19 14:10 jimmaaay

I just ran in to this issue and @jimmaaay's fix works. Is it possible to enable the use of <Fragment> also?

lambert-velir avatar Nov 01 '19 15:11 lambert-velir

import FocusTrap from "focus-trap-react";
import { CSSTransition } from "react-transition-group";

<FocusTrap>
  <CSSTransition>
   <div>
    {/* ...content */}
  </div>
  </CSSTransition>
</FocusTrap>

I got the same error in this way. is there anyway to define the ownerDocument on FocusTrap props?

ziven27 avatar Mar 01 '20 07:03 ziven27

I think https://github.com/focus-trap/focus-trap-react/pull/139 fixed it

rivajunior avatar Oct 31 '20 00:10 rivajunior

@rivajunior That would be awesome! I just published #139 in 8.1.1.

stefcameron avatar Oct 31 '20 21:10 stefcameron

Closing very old issues. There have been a lot of improvements to the library (and tabbable and focus-trap) since I took over in July 2020, and this may be fixed now. If it's still an issue with the current release, feel free to re-open.

stefcameron avatar Apr 22 '22 14:04 stefcameron