Commit 409b3c68 authored by tom's avatar tom

better skeleton for tabs

parent 09ade688
...@@ -7,6 +7,7 @@ import React, { useEffect, useRef } from 'react'; ...@@ -7,6 +7,7 @@ import React, { useEffect, useRef } from 'react';
import type { RoutedTab } from './types'; import type { RoutedTab } from './types';
import TabsWithScroll from './TabsWithScroll'; import TabsWithScroll from './TabsWithScroll';
import useTabIndexFromQuery from './useTabIndexFromQuery';
interface Props extends ThemingProps<'Tabs'> { interface Props extends ThemingProps<'Tabs'> {
tabs: Array<RoutedTab>; tabs: Array<RoutedTab>;
...@@ -18,16 +19,7 @@ interface Props extends ThemingProps<'Tabs'> { ...@@ -18,16 +19,7 @@ interface Props extends ThemingProps<'Tabs'> {
const RoutedTabs = ({ tabs, tabListProps, rightSlot, stickyEnabled, className, ...themeProps }: Props) => { const RoutedTabs = ({ tabs, tabListProps, rightSlot, stickyEnabled, className, ...themeProps }: Props) => {
const router = useRouter(); const router = useRouter();
const tabIndex = useTabIndexFromQuery(tabs);
let tabIndex = 0;
const tabFromRoute = router.query.tab;
if (tabFromRoute) {
tabIndex = tabs.findIndex(({ id, subTabs }) => id === tabFromRoute || subTabs?.some((id) => id === tabFromRoute));
if (tabIndex < 0) {
tabIndex = 0;
}
}
const tabsRef = useRef<HTMLDivElement>(null); const tabsRef = useRef<HTMLDivElement>(null);
const handleTabChange = React.useCallback((index: number) => { const handleTabChange = React.useCallback((index: number) => {
......
import { useRouter } from 'next/router';
import type { RoutedTab } from './types';
import getQueryParamString from 'lib/router/getQueryParamString';
export default function useTabIndexFromQuery(tabs: Array<RoutedTab>) {
const router = useRouter();
const tabFromQuery = getQueryParamString(router.query.tab);
if (!tabFromQuery) {
return 0;
}
const tabIndex = tabs.findIndex(({ id, subTabs }) => id === tabFromQuery || subTabs?.some((id) => id === tabFromQuery));
if (tabIndex < 0) {
return 0;
}
return tabIndex;
}
import { Flex, Skeleton, chakra } from '@chakra-ui/react'; import { Flex, Skeleton, chakra, Box, useColorModeValue } from '@chakra-ui/react';
import React from 'react'; import React from 'react';
import type { RoutedTab } from '../Tabs/types'; import type { RoutedTab } from '../Tabs/types';
import useTabIndexFromQuery from 'ui/shared/Tabs/useTabIndexFromQuery';
interface Props { interface Props {
className?: string; className?: string;
tabs?: Array<RoutedTab>; tabs?: Array<RoutedTab>;
...@@ -10,6 +12,9 @@ interface Props { ...@@ -10,6 +12,9 @@ interface Props {
} }
const SkeletonTabs = ({ className, tabs, size = 'md' }: Props) => { const SkeletonTabs = ({ className, tabs, size = 'md' }: Props) => {
const bgColor = useColorModeValue('blackAlpha.50', 'whiteAlpha.50');
const tabIndex = useTabIndexFromQuery(tabs || []);
if (tabs) { if (tabs) {
if (tabs.length === 1) { if (tabs.length === 1) {
return null; return null;
...@@ -19,16 +24,34 @@ const SkeletonTabs = ({ className, tabs, size = 'md' }: Props) => { ...@@ -19,16 +24,34 @@ const SkeletonTabs = ({ className, tabs, size = 'md' }: Props) => {
const paddingVert = size === 'sm' ? 1 : 2; const paddingVert = size === 'sm' ? 1 : 2;
return ( return (
<Flex className={ className } my={ 8 } alignItems="center"> <Flex className={ className } my={ 8 } alignItems="center" overflow="hidden">
{ tabs.map(({ title, id }, index) => ( { tabs.slice(0, tabIndex).map(({ title, id }) => (
<Skeleton
key={ id }
mx={ paddingHor }
borderRadius="base"
fontWeight={ 600 }
borderWidth={ size === 'sm' ? '2px' : 0 }
flexShrink={ 0 }
>
{ typeof title === 'string' ? title : title() }
</Skeleton>
)) }
{ tabs.slice(tabIndex, tabIndex + 1).map(({ title, id }) => (
<Box key={ id } bgColor={ bgColor } px={ paddingHor } py={ paddingVert } borderRadius="base" flexShrink={ 0 }>
<Skeleton borderRadius="base" borderWidth={ size === 'sm' ? '2px' : 0 }>
{ typeof title === 'string' ? title : title() }
</Skeleton>
</Box>
)) }
{ tabs.slice(tabIndex + 1).map(({ title, id }) => (
<Skeleton <Skeleton
key={ id } key={ id }
py={ index === 0 ? paddingVert : 0 } mx={ paddingHor }
px={ index === 0 ? paddingHor : 0 }
mx={ index === 0 ? 0 : paddingHor }
borderRadius="base" borderRadius="base"
fontWeight={ 600 } fontWeight={ 600 }
borderWidth={ size === 'sm' ? '2px' : 0 } borderWidth={ size === 'sm' ? '2px' : 0 }
flexShrink={ 0 }
> >
{ typeof title === 'string' ? title : title() } { typeof title === 'string' ? title : title() }
</Skeleton> </Skeleton>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment