|
@@ -6,6 +6,26 @@ import {tabStore} from '@stores';
|
|
|
|
|
|
export function useIndicator() {
|
|
|
const [style, setStyle] = useState<CSSProperties>({display: 'none'});
|
|
|
+ const tabs = useContextSection(context, state => state[0]);
|
|
|
+ const {key} = useStore(tabStore);
|
|
|
+
|
|
|
+ useEffect(
|
|
|
+ function () {
|
|
|
+ const activeEl = document.getElementById(`p_tab_item_${key}`);
|
|
|
+ if (!activeEl) return setStyle({display: 'none'});
|
|
|
+
|
|
|
+ const {width} = activeEl.getBoundingClientRect();
|
|
|
+ const left = activeEl.offsetLeft;
|
|
|
+
|
|
|
+ setStyle({width, transform: `translate(${left}px)`});
|
|
|
+ },
|
|
|
+ [key, tabs],
|
|
|
+ );
|
|
|
+
|
|
|
+ return style;
|
|
|
+}
|
|
|
+
|
|
|
+export function useScrollToView() {
|
|
|
const visibleTab = useRef<Set<string>>(new Set());
|
|
|
const tabs = useContextSection(context, state => state[0]);
|
|
|
const {key} = useStore(tabStore);
|
|
@@ -43,15 +63,6 @@ export function useIndicator() {
|
|
|
|
|
|
useEffect(
|
|
|
function () {
|
|
|
- const activeEl = document.getElementById(`p_tab_item_${key}`);
|
|
|
- if (!activeEl) return setStyle({display: 'none'});
|
|
|
-
|
|
|
- const {width} = activeEl.getBoundingClientRect();
|
|
|
- const left = activeEl.offsetLeft;
|
|
|
-
|
|
|
- setStyle({width, transform: `translate(${left}px)`});
|
|
|
-
|
|
|
- // 判断是否需要滚动到指定位置
|
|
|
if (!visibleTab.current.has(key)) {
|
|
|
document
|
|
|
.querySelector('#p_tab_list')
|
|
@@ -63,8 +74,6 @@ export function useIndicator() {
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
- [key, tabs],
|
|
|
+ [key],
|
|
|
);
|
|
|
-
|
|
|
- return style;
|
|
|
}
|