Skip to content

Commit

Permalink
Fix: check and use parents scale css for tabs underline position (#2178)
Browse files Browse the repository at this point in the history
* fix: check and use parents scale css

* fix: move refs access back in useEffect

* fix: handle NaN value

* fix: check if window is defined for ssr
  • Loading branch information
Isaacmaamouche committed Aug 24, 2023
1 parent ffb5bbc commit b57cacb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/Tabs/src/ActiveBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useViewportSize } from '@welcome-ui/utils'
import { useIsomorphicLayoutEffect } from '@welcome-ui/utils'

import * as S from './styles'
import { getParentScale } from './utils'

import { UseTabState } from '.'

Expand All @@ -26,6 +27,7 @@ function useActiveBar(

const listRect = list.getBoundingClientRect()
const activeTabRect = activeTab.getBoundingClientRect()
const scale = getParentScale(list)

if (orientation === 'vertical') {
const top = activeTabRect.top - listRect.top
Expand All @@ -36,8 +38,9 @@ function useActiveBar(
orientation,
})
} else {
const left = activeTabRect.left - listRect.left + list.scrollLeft
const width = activeTabRect.width
const left = (activeTabRect.left - listRect.left + list.scrollLeft) / scale
const width = activeTabRect.width / scale

setState({
size: width,
offset: left,
Expand Down
14 changes: 14 additions & 0 deletions packages/Tabs/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//Check if the element, or its parent, has a scale property
export const getParentScale = (element: HTMLElement | null): number => {
//If the element doesn't exist, or if window is undefined, return 1
if (!element || typeof window === 'undefined') return 1
const elementScale = Number(window.getComputedStyle(element).scale)

//If the scale property is not unvalid or undefined, return its value
//Else check the parent's scale property
if (!isNaN(elementScale)) {
return elementScale
} else {
return getParentScale(element.parentElement)
}
}

0 comments on commit b57cacb

Please sign in to comment.