Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
bc0dfb37
Commit
bc0dfb37
authored
Dec 13, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
scroll to active tab
parent
96e87610
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
18 deletions
+38
-18
RoutedTabs.tsx
ui/shared/RoutedTabs/RoutedTabs.tsx
+32
-13
useAdaptiveTabs.tsx
ui/shared/RoutedTabs/useAdaptiveTabs.tsx
+6
-5
No files found.
ui/shared/RoutedTabs/RoutedTabs.tsx
View file @
bc0dfb37
...
...
@@ -40,19 +40,6 @@ const RoutedTabs = ({ tabs, tabListProps, rightSlot, stickyEnabled }: Props) =>
const
scrollDirection
=
useScrollDirection
();
const
[
activeTabIndex
,
setActiveTabIndex
]
=
useState
<
number
>
(
tabs
.
length
+
1
);
useEffect
(()
=>
{
if
(
router
.
isReady
)
{
let
tabIndex
=
0
;
if
(
router
.
query
.
tab
)
{
tabIndex
=
tabs
.
findIndex
(({
id
})
=>
id
===
router
.
query
.
tab
);
if
(
tabIndex
<
0
)
{
tabIndex
=
0
;
}
}
setActiveTabIndex
(
tabIndex
);
}
},
[
tabs
,
router
]);
const
isMobile
=
useIsMobile
();
const
{
tabsCut
,
tabsList
,
tabsRefs
,
listRef
,
rightSlotRef
}
=
useAdaptiveTabs
(
tabs
,
isMobile
);
const
isSticky
=
useIsSticky
(
listRef
,
5
,
stickyEnabled
);
...
...
@@ -68,6 +55,38 @@ const RoutedTabs = ({ tabs, tabListProps, rightSlot, stickyEnabled }: Props) =>
);
},
[
tabs
,
router
]);
useEffect
(()
=>
{
if
(
router
.
isReady
)
{
let
tabIndex
=
0
;
if
(
router
.
query
.
tab
)
{
tabIndex
=
tabs
.
findIndex
(({
id
})
=>
id
===
router
.
query
.
tab
);
if
(
tabIndex
<
0
)
{
tabIndex
=
0
;
}
}
setActiveTabIndex
(
tabIndex
);
}
},
[
tabs
,
router
,
activeTabIndex
]);
useEffect
(()
=>
{
if
(
activeTabIndex
<
tabs
.
length
&&
isMobile
)
{
window
.
setTimeout
(()
=>
{
const
activeTabRef
=
tabsRefs
[
activeTabIndex
];
if
(
activeTabRef
.
current
&&
listRef
.
current
)
{
const
activeTabRect
=
activeTabRef
.
current
.
getBoundingClientRect
();
listRef
.
current
.
scrollTo
({
left
:
activeTabRect
.
left
+
listRef
.
current
.
scrollLeft
-
16
,
behavior
:
'
smooth
'
,
});
}
// have to wait until DOM is updated and all styles to tabs is applied
},
300
);
}
// run only when tab index or device type is updated
// eslint-disable-next-line react-hooks/exhaustive-deps
},
[
activeTabIndex
,
isMobile
]);
return
(
<
Tabs
variant=
"soft-rounded"
...
...
ui/shared/RoutedTabs/useAdaptiveTabs.tsx
View file @
bc0dfb37
...
...
@@ -51,19 +51,20 @@ export default function useAdaptiveTabs(tabs: Array<RoutedTab>, disabled?: boole
},
[
tabs
,
disabled
]);
React
.
useEffect
(()
=>
{
setTabsRefs
(
disabled
?
[]
:
tabsList
.
map
((
_
,
index
)
=>
tabsRefs
[
index
]
||
React
.
createRef
()));
setTabsRefs
(
tabsList
.
map
((
_
,
index
)
=>
tabsRefs
[
index
]
||
React
.
createRef
()));
setTabsCut
(
disabled
?
tabs
.
length
:
0
);
// update refs only when disabled prop changes
// eslint-disable-next-line react-hooks/exhaustive-deps
},
[
disabled
]);
React
.
useEffect
(()
=>
{
if
(
tabsRefs
.
length
>
0
)
{
if
(
tabsRefs
.
length
>
0
&&
!
disabled
)
{
setTabsCut
(
calculateCut
());
}
},
[
calculateCut
,
tabsRefs
]);
},
[
calculateCut
,
disabled
,
tabsRefs
]);
React
.
useEffect
(()
=>
{
if
(
tabsRefs
.
length
===
0
)
{
if
(
tabsRefs
.
length
===
0
||
disabled
)
{
return
;
}
...
...
@@ -76,7 +77,7 @@ export default function useAdaptiveTabs(tabs: Array<RoutedTab>, disabled?: boole
return
function
cleanup
()
{
resizeObserver
.
unobserve
(
document
.
body
);
};
},
[
calculateCut
,
tabsRefs
.
length
]);
},
[
calculateCut
,
disabled
,
tabsRefs
.
length
]);
return
React
.
useMemo
(()
=>
{
return
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment