Skip to content

Commit

Permalink
Disable page scroll if user opens floating modal
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Sep 6, 2023
1 parent a597f04 commit c28759d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/components/chat/ChatFloatingModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useSendEvent } from 'src/providers/AnalyticContext'
import { useChatOpenState } from 'src/rtk/app/hooks'
import { useAppSelector } from 'src/rtk/app/store'
import { ChatEntity } from 'src/rtk/features/chat/chatSlice'
import { disablePageScroll, enablePageScroll } from 'src/utils/window'
import { useResponsiveSize } from '../responsive'
import styles from './ChatFloatingModal.module.sass'
import ChatIframe from './ChatIframe'
Expand All @@ -16,6 +17,7 @@ export default function ChatFloatingModal() {
const sendEvent = useSendEvent()
const [isOpen, setIsOpen] = useChatOpenState()
const entity = useAppSelector(state => state.chat.entity)
const totalMessageCount = useAppSelector(state => state.chat.totalMessageCount)

const [unreadCount, setUnreadCount] = useState(0)

Expand All @@ -40,6 +42,9 @@ export default function ChatFloatingModal() {
}
sendEvent(event)

if (!isOpen) disablePageScroll()
else enablePageScroll()

setIsOpen(!isOpen)
hasOpened.current = true
}
Expand Down Expand Up @@ -77,7 +82,7 @@ export default function ChatFloatingModal() {
<div className={styles.ChatFloatingWrapper}>
<Button className={styles.ChatFloatingButton} onClick={toggleChat}>
<img src='/images/grillchat.svg' alt='GrillChat' />
<span>Comment article</span>
<span>Comments {totalMessageCount ? `(${totalMessageCount})` : ''}</span>
</Button>
{!!unreadCount && <span className={styles.ChatUnreadCount}>{unreadCount}</span>}
</div>,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function disablePageScroll() {
document.body.style.overflow = 'hidden'
}
export function enablePageScroll() {
document.body.style.overflow = 'auto'
}

0 comments on commit c28759d

Please sign in to comment.