mirror of
https://github.com/mue/mue.git
synced 2026-06-08 14:10:42 +02:00
feat(Tabs): add scroll to top functionality when changing tabs or resetting
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect, useRef } from 'react';
|
||||||
import { useT } from 'contexts/TranslationContext';
|
import { useT } from 'contexts/TranslationContext';
|
||||||
import variables from 'config/variables';
|
import variables from 'config/variables';
|
||||||
import Tab from './Tab';
|
import Tab from './Tab';
|
||||||
@@ -39,6 +39,7 @@ const Tabs = ({
|
|||||||
const [currentTab, setCurrentTab] = useState(initial.label);
|
const [currentTab, setCurrentTab] = useState(initial.label);
|
||||||
const [currentName, setCurrentName] = useState(initial.name);
|
const [currentName, setCurrentName] = useState(initial.name);
|
||||||
const [showReminder, setShowReminder] = useState(localStorage.getItem('showReminder') === 'true');
|
const [showReminder, setShowReminder] = useState(localStorage.getItem('showReminder') === 'true');
|
||||||
|
const contentRef = useRef(null);
|
||||||
|
|
||||||
const handleTabClick = (tab, name) => {
|
const handleTabClick = (tab, name) => {
|
||||||
if (name !== currentName) {
|
if (name !== currentName) {
|
||||||
@@ -48,6 +49,11 @@ const Tabs = ({
|
|||||||
setCurrentTab(tab);
|
setCurrentTab(tab);
|
||||||
setCurrentName(name);
|
setCurrentName(name);
|
||||||
|
|
||||||
|
// Scroll content to top when changing tabs
|
||||||
|
if (contentRef.current) {
|
||||||
|
contentRef.current.scrollTop = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Notify parent of section change with both label and name
|
// Notify parent of section change with both label and name
|
||||||
if (onSectionChange) {
|
if (onSectionChange) {
|
||||||
onSectionChange(tab, name);
|
onSectionChange(tab, name);
|
||||||
@@ -80,6 +86,10 @@ const Tabs = ({
|
|||||||
const label = t(section.label);
|
const label = t(section.label);
|
||||||
setCurrentTab(label);
|
setCurrentTab(label);
|
||||||
setCurrentName(section.name);
|
setCurrentName(section.name);
|
||||||
|
// Scroll content to top when navigating via browser history
|
||||||
|
if (contentRef.current) {
|
||||||
|
contentRef.current.scrollTop = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [navigationTrigger, sections, t]);
|
}, [navigationTrigger, sections, t]);
|
||||||
@@ -89,6 +99,10 @@ const Tabs = ({
|
|||||||
if (resetToFirst) {
|
if (resetToFirst) {
|
||||||
setCurrentTab(children[0]?.props.label);
|
setCurrentTab(children[0]?.props.label);
|
||||||
setCurrentName(children[0]?.props.name);
|
setCurrentName(children[0]?.props.name);
|
||||||
|
// Scroll content to top when resetting to first tab
|
||||||
|
if (contentRef.current) {
|
||||||
|
contentRef.current.scrollTop = 0;
|
||||||
|
}
|
||||||
if (onSectionChange) {
|
if (onSectionChange) {
|
||||||
onSectionChange(children[0]?.props.label, children[0]?.props.name);
|
onSectionChange(children[0]?.props.label, children[0]?.props.name);
|
||||||
}
|
}
|
||||||
@@ -119,7 +133,7 @@ const Tabs = ({
|
|||||||
<ReminderInfo isVisible={showReminder} onHide={handleHideReminder} />
|
<ReminderInfo isVisible={showReminder} onHide={handleHideReminder} />
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
<div className="modalTabContent">
|
<div className="modalTabContent" ref={contentRef}>
|
||||||
{children.map((tab, index) => {
|
{children.map((tab, index) => {
|
||||||
if (tab.props.label !== currentTab) {
|
if (tab.props.label !== currentTab) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user