mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 04:14:18 +00:00
🔨 refactor: Account Navigation
This commit is contained in:
parent
25dcf95592
commit
398b1f5148
@ -1,8 +1,8 @@
|
|||||||
@import '../../../../styles/utilities';
|
@import '../../../../styles/utilities';
|
||||||
|
|
||||||
.accountNavigation {
|
.accountNavigation {
|
||||||
border-left: 2px solid #FBFBFB;
|
@apply flex;
|
||||||
max-width: 28.9rem;
|
width: 100%;
|
||||||
|
|
||||||
.slider {
|
.slider {
|
||||||
@apply inline-block;
|
@apply inline-block;
|
||||||
@ -14,4 +14,14 @@
|
|||||||
left: 11.2rem;
|
left: 11.2rem;
|
||||||
transition: all .2s linear;
|
transition: all .2s linear;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.tabList {
|
||||||
|
margin-top: 3.8rem;
|
||||||
|
margin-right: 12.4rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tabBody {
|
||||||
|
margin-top: -4.7rem;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,49 +1,68 @@
|
|||||||
import React, { useRef, RefObject, useEffect } from "react"
|
import React, { useRef, useEffect, Children, ReactElement, PropsWithChildren, useState, cloneElement } from "react"
|
||||||
import s from './AccountNavigation.module.scss'
|
import s from './AccountNavigation.module.scss'
|
||||||
|
|
||||||
import AccountNavigationItem from './components/AccountNavigationItem'
|
import AccountNavigationItem from './components/AccountNavigationItem/AccountNavigationItem'
|
||||||
|
import {TabPaneProps} from '../../../common/TabCommon/components/TabPane/TabPane'
|
||||||
|
|
||||||
interface AccountNavigationProps {
|
interface AccountNavigationProps {
|
||||||
items: {ref: RefObject<HTMLDivElement>, active: boolean, itemName: string, onClick: (tabIndex: number)=>void}[];
|
|
||||||
defaultActiveIndex: number;
|
defaultActiveIndex: number;
|
||||||
|
children: React.ReactNode
|
||||||
}
|
}
|
||||||
|
|
||||||
const AccountNavigation = ({ items, defaultActiveIndex } : AccountNavigationProps) => {
|
const AccountNavigation = ({ defaultActiveIndex, children } : AccountNavigationProps) => {
|
||||||
|
const [active, setActive] = useState(defaultActiveIndex)
|
||||||
const sliderRef = useRef<HTMLDivElement>(null);
|
const sliderRef = useRef<HTMLDivElement>(null);
|
||||||
|
const headerRef = useRef<HTMLUListElement>(null)
|
||||||
|
|
||||||
|
const onTabClick = (index: number) => {
|
||||||
|
setActive(index)
|
||||||
|
}
|
||||||
|
|
||||||
function slide(index: number) {
|
function slide(index: number) {
|
||||||
const previousItem = items[index].ref.current;
|
const active = headerRef.current?.children.item(index)?.getBoundingClientRect()
|
||||||
const slider = sliderRef.current;
|
const header = headerRef.current?.getBoundingClientRect()
|
||||||
if (previousItem && slider) {
|
const current = sliderRef.current
|
||||||
const top = previousItem.offsetTop;
|
|
||||||
slider.style.top = top.toString()+"px";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleClick = (item: {ref: RefObject<HTMLDivElement>, active: boolean, itemName: string, onClick: (tabIndex: number)=>void},
|
|
||||||
index: number) => {
|
|
||||||
slide(index);
|
|
||||||
item.onClick(index);
|
|
||||||
|
|
||||||
|
if (current && active && header) {
|
||||||
|
const top = active.top;
|
||||||
|
current.style.top = top.toString()+"px";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
slide(defaultActiveIndex);
|
slide(active);
|
||||||
}, [])
|
}, [active])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className={s.accountNavigation}>
|
<section className={s.accountNavigation}>
|
||||||
|
<ul className={s.tabList} ref={headerRef}>
|
||||||
{
|
{
|
||||||
items.map((item, i) => {
|
Children.map(children, (tab, index) => {
|
||||||
|
let item = tab as ReactElement<PropsWithChildren<TabPaneProps>>
|
||||||
return (
|
return (
|
||||||
<div key={item.itemName} ref={item.ref}>
|
<li key={item.props.tabName}>
|
||||||
<AccountNavigationItem onClick={() => {handleClick(item, i)}} active={item.active}>{item.itemName}</AccountNavigationItem>
|
<AccountNavigationItem
|
||||||
</div>
|
active={active === index}
|
||||||
|
onClick={onTabClick}
|
||||||
|
tabIndex={index}
|
||||||
|
>
|
||||||
|
{item.props.tabName}
|
||||||
|
</AccountNavigationItem>
|
||||||
|
</li>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
<div ref={sliderRef} className={s.slider}></div>
|
<div ref={sliderRef} className={s.slider}></div>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<div className={s.tabBody}>
|
||||||
|
{
|
||||||
|
Children.map(children, (tab, index) => {
|
||||||
|
let item = tab as ReactElement<PropsWithChildren<TabPaneProps>>
|
||||||
|
return cloneElement(item, { active: index === active });
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</div>
|
||||||
</section>
|
</section>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
@import '../../../../../styles/utilities';
|
@import '../../../../../../styles/utilities';
|
||||||
|
|
||||||
.accountNavigationItem {
|
.accountNavigationItem {
|
||||||
width: 28rem;
|
width: 28rem;
|
||||||
|
@ -5,12 +5,17 @@ import s from './AccountNavigationItem.module.scss'
|
|||||||
interface AccountNavigationItemProps {
|
interface AccountNavigationItemProps {
|
||||||
children?: string;
|
children?: string;
|
||||||
active?: boolean;
|
active?: boolean;
|
||||||
onClick: () => void;
|
tabIndex: number
|
||||||
|
onClick: (index: number) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const AccountNavigationItem = ({ children, active, onClick } : AccountNavigationItemProps) => {
|
const AccountNavigationItem = ({ children, active, tabIndex, onClick } : AccountNavigationItemProps) => {
|
||||||
|
|
||||||
|
const handleClick = () => {
|
||||||
|
onClick(tabIndex)
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<div onClick={onClick} className={classNames(s.accountNavigationItem, {
|
<div onClick={handleClick} className={classNames(s.accountNavigationItem, {
|
||||||
[s.active]:active
|
[s.active]:active
|
||||||
})}>
|
})}>
|
||||||
{children}
|
{children}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user