-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,58 @@ | ||
import { useEffect } from 'react'; | ||
import { RefObject, useEffect, useRef } from 'react'; | ||
|
||
let counter = 0; | ||
let originalOverflow: string | null = null; | ||
export function getClosestBody(el: Element | HTMLElement | HTMLIFrameElement | null): HTMLElement | null { | ||
if (!el) { | ||
return null; | ||
} else if (el.tagName === 'BODY') { | ||
return el as HTMLElement; | ||
} else if (el.tagName === 'IFRAME') { | ||
const document = (el as HTMLIFrameElement).contentDocument; | ||
return document ? document.body : null; | ||
} else if (!(el as HTMLElement).offsetParent) { | ||
return null; | ||
} | ||
|
||
const lock = () => { | ||
originalOverflow = window.getComputedStyle(document.body).overflow; | ||
document.body.style.overflow = 'hidden'; | ||
}; | ||
return getClosestBody((el as HTMLElement).offsetParent!); | ||
} | ||
|
||
const unlock = () => { | ||
document.body.style.overflow = originalOverflow; | ||
originalOverflow = null; | ||
}; | ||
export interface BodyInfoItem { | ||
counter: number; | ||
initialOverflow: string | null; | ||
} | ||
|
||
const increment = () => { | ||
counter++; | ||
if (counter === 1) { | ||
lock(); | ||
} | ||
}; | ||
const bodies: Map<HTMLElement, BodyInfoItem> = new Map(); | ||
|
||
const decrement = () => { | ||
counter--; | ||
if (counter === 0) { | ||
unlock(); | ||
} | ||
}; | ||
const doc: Document | undefined = typeof document === 'object' ? document : undefined; | ||
|
||
export default !doc | ||
? function useLockBodyMock(_locked: boolean = true, _elementRef?: RefObject<HTMLElement>) {} | ||
: function useLockBody(locked: boolean = true, elementRef?: RefObject<HTMLElement>) { | ||
elementRef = elementRef || useRef(doc!.body); | ||
|
||
useEffect(() => { | ||
const body = getClosestBody(elementRef!.current); | ||
if (!body) { | ||
return; | ||
} | ||
|
||
const useLockBodyScroll = (enabled: boolean = true) => { | ||
useEffect(() => (enabled ? (increment(), decrement) : undefined), [enabled]); | ||
}; | ||
const bodyInfo = bodies.get(body); | ||
|
||
export default useLockBodyScroll; | ||
if (locked) { | ||
if (!bodyInfo) { | ||
bodies.set(body, { counter: 1, initialOverflow: body.style.overflow }); | ||
body.style.overflow = 'hidden'; | ||
} else { | ||
bodies.set(body, { counter: bodyInfo.counter + 1, initialOverflow: bodyInfo.initialOverflow }); | ||
} | ||
} else { | ||
if (bodyInfo) { | ||
if (bodyInfo.counter === 1) { | ||
bodies.delete(body); | ||
body.style.overflow = bodyInfo.initialOverflow; | ||
} else { | ||
bodies.set(body, { counter: bodyInfo.counter - 1, initialOverflow: bodyInfo.initialOverflow }); | ||
} | ||
} | ||
} | ||
}, [locked, elementRef.current]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters