Skip to content

Commit 1515e0f

Browse files
authored
Merge pull request #725 from devtron-labs/fix/tab-group-animation
fix: tab group animation
2 parents b74517a + bf53978 commit 1515e0f

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.12.0-pre-7",
3+
"version": "1.12.0-pre-9",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Shared/Components/TabGroup/TabGroup.component.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { useMemo } from 'react'
18-
import { Link, NavLink, useRouteMatch } from 'react-router-dom'
17+
import { useEffect, useMemo, useRef, useState } from 'react'
18+
import { Link, NavLink, useLocation } from 'react-router-dom'
1919
import { motion } from 'framer-motion'
2020

2121
import { Tooltip } from '@Common/Tooltip'
2222
import { ComponentSizeType } from '@Shared/constants'
2323

24-
import { getPathnameToMatch, getTabBadge, getTabDescription, getTabIcon, getTabIndicator } from './TabGroup.helpers'
24+
import { getTabBadge, getTabDescription, getTabIcon, getTabIndicator } from './TabGroup.helpers'
2525
import { AdditionalTabProps, TabGroupProps, TabProps } from './TabGroup.types'
2626
import { getClassNameBySizeMap, tabGroupClassMap } from './TabGroup.utils'
2727

@@ -57,13 +57,17 @@ const Tab = ({
5757
tooltipProps,
5858
uniqueGroupId,
5959
}: TabProps & Pick<TabGroupProps, 'size' | 'hideTopPadding'> & AdditionalTabProps) => {
60-
const { path } = useRouteMatch()
61-
const pathToMatch = tabType === 'navLink' || tabType === 'link' ? getPathnameToMatch(props.to, path) : ''
62-
63-
// using match to define if tab is active as useRouteMatch return an object if path is matched otherwise return null/undefined
64-
const match = useRouteMatch(pathToMatch)
65-
66-
const isTabActive = tabType === 'button' ? active : !!match
60+
const { pathname, search } = useLocation()
61+
const ref = useRef<HTMLAnchorElement>(null)
62+
const [isTabActive, setIsTabActive] = useState(tabType === 'button' && active)
63+
64+
useEffect(() => {
65+
if (tabType === 'navLink') {
66+
setIsTabActive(ref.current?.classList.contains('active') || false)
67+
return
68+
}
69+
setIsTabActive(active)
70+
}, [active, tabType, pathname, search])
6771

6872
const { tabClassName, iconClassName, badgeClassName } = getClassNameBySizeMap({
6973
hideTopPadding,
@@ -110,6 +114,7 @@ const Tab = ({
110114
case 'navLink':
111115
return (
112116
<NavLink
117+
ref={ref}
113118
className={`${tabClassName} dc__no-decor flexbox-col tab-group__tab__nav-link ${disabled ? 'cursor-not-allowed' : ''}`}
114119
aria-disabled={disabled}
115120
{...props}

src/Shared/Components/TabGroup/TabGroup.helpers.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { LinkProps, NavLinkProps } from 'react-router-dom'
18-
1917
import { ReactComponent as ICErrorExclamation } from '@Icons/ic-error-exclamation.svg'
2018
import { ReactComponent as ICWarning } from '@Icons/ic-warning.svg'
2119

@@ -67,14 +65,3 @@ export const getTabDescription = (description: TabProps['description']) =>
6765
: description}
6866
</ul>
6967
)
70-
71-
const replaceTrailingSlash = (pathname: string) => pathname.replace(/\/+$/, '')
72-
73-
export const getPathnameToMatch = (to: NavLinkProps['to'] | LinkProps['to'], currentPathname: string): string => {
74-
if (typeof to === 'string' || (to && typeof to === 'object' && 'pathname' in to)) {
75-
const pathname = typeof to === 'string' ? to : to.pathname || ''
76-
// handling absolute and relative paths
77-
return pathname.startsWith('/') ? pathname : `${replaceTrailingSlash(currentPathname)}/${pathname}`
78-
}
79-
return ''
80-
}

0 commit comments

Comments
 (0)