Skip to content

Commit 22e37a1

Browse files
authored
Merge pull request #5 from Dev-CasperTheGhost/main
SSR support
2 parents e8f5445 + 13ed197 commit 22e37a1

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/FocusRing.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ interface FocusRingComponentProps extends FocusRingProps {
1919
children: React.ReactElement<FocusableChildProps>;
2020
}
2121

22+
// ref: https://github.com/facebook/react/issues/14927
23+
const useLayoutEffect =
24+
typeof window !== "undefined" && window.document && window.document.createElement
25+
? React.useLayoutEffect
26+
: React.useEffect;
27+
2228
/**
2329
* Augments the child component to be able to render a focus ring
2430
*/
@@ -66,7 +72,7 @@ export default function FocusRing(props: FocusRingComponentProps) {
6672

6773
// Force the ring to update itself whenever this component re-renders
6874
// to stay up-to-date with the active element's properties.
69-
React.useLayoutEffect(() => {
75+
useLayoutEffect(() => {
7076
if (!enabled) return;
7177
ringContext.invalidate();
7278
});
@@ -94,7 +100,7 @@ export default function FocusRing(props: FocusRingComponentProps) {
94100

95101
// When using a remote focus ring (both `focusTarget` and `ringTarget` are
96102
// set), use native DOM event listeners to track when focus happens.
97-
React.useLayoutEffect(() => {
103+
useLayoutEffect(() => {
98104
if (focused != null) return;
99105

100106
const target = focusTarget?.current;

src/FocusRingContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class FocusRingContextManager {
7878
let current: Element | null = element;
7979
while (current != null) {
8080
elements.push(current);
81-
styles.push(window.getComputedStyle(current));
81+
typeof window !== "undefined" && styles.push(window.getComputedStyle(current));
8282
current = current.parentElement;
8383
}
8484
return { elements, styles };
@@ -168,7 +168,7 @@ export class FocusRingContextManager {
168168
}
169169

170170
const GLOBAL_FOCUS_RING_CONTEXT = new FocusRingContextManager();
171-
GLOBAL_FOCUS_RING_CONTEXT.setContainer(document.body);
171+
typeof window !== "undefined" && GLOBAL_FOCUS_RING_CONTEXT.setContainer(document.body);
172172

173173
const FocusRingContext = React.createContext(GLOBAL_FOCUS_RING_CONTEXT);
174174

0 commit comments

Comments
 (0)