Skip to content

Commit

Permalink
refactor(FOROME-871): add useScrollShadow hook
Browse files Browse the repository at this point in the history
  • Loading branch information
QYaroslavTrefilov committed Apr 14, 2022
1 parent 68cc02d commit 55b0082
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
38 changes: 38 additions & 0 deletions src/components/variant/drawer.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useState } from 'react'

export enum DrawerClass {
normClass = 'norm',
normHitClass = 'norm hit',
Expand All @@ -13,3 +15,39 @@ export const getLeftDistance = (

return trackedTdNode?.getBoundingClientRect().left
}

export const useScrollShadow = (
element: HTMLDivElement | null,
): {
shouldAddShadow: boolean
handleScroll: () => void
handleStartScroll: () => void
} => {
const [startedLeftDistance, setStartedLeftDistance] = useState<number | null>(
null,
)

const [shouldAddShadow, setShouldAddShadow] = useState(false)

const handleStartScroll = () => {
const currentLeftDistance = getLeftDistance(element)

if (!currentLeftDistance || startedLeftDistance) return

const fixedLeftDistance = Math.round(currentLeftDistance)
setStartedLeftDistance(fixedLeftDistance)
}

const handleScroll = () => {
const currentLeftDistance = getLeftDistance(element)

if (!currentLeftDistance) return

const fixedCurrentLeftDistance = Math.round(currentLeftDistance)
const isStartPosition = fixedCurrentLeftDistance === startedLeftDistance

setShouldAddShadow(!isStartPosition)
}

return { shouldAddShadow, handleScroll, handleStartScroll }
}
31 changes: 3 additions & 28 deletions src/components/variant/ui/drawer-window.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
ITableAspectDescriptor,
TRecCntResponse,
} from '@service-providers/dataset-level/dataset-level.interface'
import { DrawerClass, getLeftDistance } from '../drawer.utils'
import { DrawerClass, useScrollShadow } from '../drawer.utils'
import { DrawerPreView } from './drawer-pre-view'
import { DrawerTable } from './drawer-table'
import { IgvButton } from './igv-button'
Expand All @@ -44,33 +44,8 @@ export const DrawerWindow = observer(
DrawerClass.normClass,
)

const [startedLeftDistance, setStartedLeftDistance] = useState<
number | null
>(null)

const [shouldAddShadow, setShouldAddShadow] = useState(false)

const handleStartScroll = () => {
const currentLeftDistance = getLeftDistance(ref.current)

if (!currentLeftDistance || startedLeftDistance) return

const fixedLeftDistance = Math.round(currentLeftDistance)

setStartedLeftDistance(fixedLeftDistance)
}

const handleScroll = () => {
const currentLeftDistance = getLeftDistance(ref.current)

if (!currentLeftDistance) return

const fixedCurrentLeftDistance = Math.round(currentLeftDistance)

const isStartPosition = fixedCurrentLeftDistance === startedLeftDistance

setShouldAddShadow(!isStartPosition)
}
const { shouldAddShadow, handleScroll, handleStartScroll } =
useScrollShadow(ref.current)

const handleSelection = (checked: boolean) => {
checked
Expand Down

0 comments on commit 55b0082

Please sign in to comment.