Skip to content

Commit

Permalink
[#482] Fix SSR issues when accessing document object
Browse files Browse the repository at this point in the history
Fixes #482
  • Loading branch information
stefcameron committed Sep 27, 2021
1 parent d5e1c2b commit 46468bc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/unlucky-frogs-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'focus-trap-react': patch
---

Fix SSR issues when accessing `document` object (#482)
2 changes: 2 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ __Please leave this checklist in your PR.__
- E2E test coverage added/updated.
- Prop-types added/updated.
- Typings added/updated.
- Changes do not break SSR:
- Careful to test `typeof document/window !== 'undefined'` before using it in code that gets executed on load.
- README updated (API changes, instructions, etc.).
- Changes to dependencies explained.
- Changeset added (run `yarn changeset` locally to add one, and follow the prompts).
Expand Down
5 changes: 4 additions & 1 deletion src/focus-trap-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ class FocusTrap extends React.Component {

/** Update the previously focused element with the currently focused element. */
updatePreviousElement() {
const currentDocument = this.props.focusTrapOptions.document || document;
// SSR: careful to check if `document` exists before accessing it as a variable
const currentDocument =
this.props.focusTrapOptions.document ||
(typeof document !== 'undefined' ? document : undefined);
if (currentDocument) {
this.previouslyFocusedElement = currentDocument.activeElement;
}
Expand Down

0 comments on commit 46468bc

Please sign in to comment.