Skip to content

Commit

Permalink
fix: check if ssr for useLayoutEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Mar 31, 2021
1 parent a0961ce commit 13ed197
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/FocusRing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ interface FocusRingComponentProps extends FocusRingProps {
children: React.ReactElement<FocusableChildProps>;
}

// ref: https://github.com/facebook/react/issues/14927
const useLayoutEffect =
typeof window !== "undefined" && window.document && window.document.createElement
? React.useLayoutEffect
: React.useEffect;

/**
* Augments the child component to be able to render a focus ring
*/
Expand Down Expand Up @@ -66,7 +72,7 @@ export default function FocusRing(props: FocusRingComponentProps) {

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

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

const target = focusTarget?.current;
Expand Down

0 comments on commit 13ed197

Please sign in to comment.