Skip to content

Commit a2806a0

Browse files
authored
[#482] Fix SSR issues when accessing document object (#483)
Fixes #482
1 parent d5e1c2b commit a2806a0

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

.changeset/unlucky-frogs-push.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'focus-trap-react': patch
3+
---
4+
5+
Fix SSR issues when accessing `document` object (#482)

.github/pull_request_template.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ __Please leave this checklist in your PR.__
2020
- E2E test coverage added/updated.
2121
- Prop-types added/updated.
2222
- Typings added/updated.
23+
- Changes do not break SSR:
24+
- Careful to test `typeof document/window !== 'undefined'` before using it in code that gets executed on load.
2325
- README updated (API changes, instructions, etc.).
2426
- Changes to dependencies explained.
2527
- Changeset added (run `yarn changeset` locally to add one, and follow the prompts).

src/focus-trap-react.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ class FocusTrap extends React.Component {
8787

8888
/** Update the previously focused element with the currently focused element. */
8989
updatePreviousElement() {
90-
const currentDocument = this.props.focusTrapOptions.document || document;
90+
// SSR: careful to check if `document` exists before accessing it as a variable
91+
const currentDocument =
92+
this.props.focusTrapOptions.document ||
93+
(typeof document !== 'undefined' ? document : undefined);
9194
if (currentDocument) {
9295
this.previouslyFocusedElement = currentDocument.activeElement;
9396
}

0 commit comments

Comments
 (0)