Skip to content

Commit

Permalink
Revert "fix: BodyScrollSuppressor を styled-components に依存しない形で実装 (#4492
Browse files Browse the repository at this point in the history
…)" (#4559)

This reverts commit 11fc83e.
  • Loading branch information
uknmr authored Apr 9, 2024
1 parent b3062e4 commit 09912ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react'
import React, { FC, useEffect, useState } from 'react'
import { createGlobalStyle, css } from 'styled-components'

export const useBodyScrollLock = () => {
export const BodyScrollSuppressor: FC = () => {
const [scrollBarWidth, setScrollBarWidth] = useState<number | null>(null)
const [paddingRight, setPaddingRight] = useState<number | null>(null)

Expand All @@ -16,15 +17,21 @@ export const useBodyScrollLock = () => {
setPaddingRight(scrollBarWidth + parseInt(originalPaddingRight, 10))
}, [scrollBarWidth])

useEffect(() => {
if (paddingRight !== null) {
document.body.style.paddingInlineEnd = `${paddingRight}px`
}
document.body.style.overflow = 'hidden'

return () => {
document.body.style.paddingInlineEnd = ''
document.body.style.overflow = ''
}
}, [paddingRight])
if (scrollBarWidth === null) {
return null
}
return <ScrollSuppressing paddingRight={paddingRight} />
}

const ScrollSuppressing = createGlobalStyle<{
paddingRight: number | null
}>`
body {
overflow: hidden;
${({ paddingRight }) =>
paddingRight &&
css`
padding-right: ${paddingRight}px !important;
`}
}
`
6 changes: 3 additions & 3 deletions src/components/Dialog/DialogContentInner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { tv } from 'tailwind-variants'
import { useHandleEscape } from '../../hooks/useHandleEscape'
import { useTheme } from '../../hooks/useTailwindTheme'

import { BodyScrollSuppressor } from './BodyScrollSuppressor'
import { DialogOverlap } from './DialogOverlap'
import { DialogPositionProvider } from './DialogPositionProvider'
import { FocusTrap } from './FocusTrap'
import { useBodyScrollLock } from './useBodyScrollLock'

export type DialogContentInnerProps = PropsWithChildren<{
/**
Expand Down Expand Up @@ -148,8 +148,6 @@ export const DialogContentInner: FC<DialogContentInnerProps & ElementProps> = ({
onClickOverlay && onClickOverlay()
}, [isOpen, onClickOverlay])

useBodyScrollLock()

return (
<DialogPositionProvider top={top} bottom={bottom}>
<DialogOverlap isOpen={isOpen}>
Expand All @@ -167,6 +165,8 @@ export const DialogContentInner: FC<DialogContentInnerProps & ElementProps> = ({
>
<FocusTrap firstFocusTarget={firstFocusTarget}>{children}</FocusTrap>
</div>
{/* Suppresses scrolling of body while modal is displayed */}
<BodyScrollSuppressor />
</div>
</DialogOverlap>
</DialogPositionProvider>
Expand Down

0 comments on commit 09912ec

Please sign in to comment.