|
| 1 | +import { useRef, useState, useEffect, useCallback, ReactNode } from 'react'; |
| 2 | + |
| 3 | +import styled from 'styled-components'; |
| 4 | + |
| 5 | +import { spacings, mapElevationToBorder, Elevation, borders } from '@trezor/theme'; |
| 6 | +import { Row, useElevation } from '@trezor/components'; |
| 7 | + |
| 8 | +import { mapSizeToContainerPaddingBottom, TRANSFORM_OPTIONS } from './utils'; |
| 9 | +import { TabsSize } from './types'; |
| 10 | +import { |
| 11 | + FrameProps, |
| 12 | + FramePropsKeys, |
| 13 | + pickAndPrepareFrameProps, |
| 14 | + withFrameProps, |
| 15 | +} from '../../utils/frameProps'; |
| 16 | +import { TransientProps } from '../../utils/transientProps'; |
| 17 | +import { TabsContext } from './TabsContext'; |
| 18 | +import { TabsItem } from './TabsItem'; |
| 19 | + |
| 20 | +export const allowedTabsFrameProps = ['margin'] as const satisfies FramePropsKeys[]; |
| 21 | +type AllowedFrameProps = Pick<FrameProps, (typeof allowedTabsFrameProps)[number]>; |
| 22 | + |
| 23 | +type ContainerProps = TransientProps<AllowedFrameProps> & { |
| 24 | + $hasBorder?: boolean; |
| 25 | + $elevation: Elevation; |
| 26 | + $indicatorWidth: number; |
| 27 | + $size: TabsSize; |
| 28 | + $indicatorPosition: number; |
| 29 | +}; |
| 30 | + |
| 31 | +const Container = styled.div<ContainerProps>` |
| 32 | + width: 100%; |
| 33 | + padding-bottom: ${mapSizeToContainerPaddingBottom}; |
| 34 | + border-bottom: ${borders.widths.small} solid ${mapElevationToBorder}; |
| 35 | + position: relative; |
| 36 | +
|
| 37 | + ${({ $hasBorder }) => !$hasBorder && `border-bottom: 0;`} |
| 38 | +
|
| 39 | + &::after { |
| 40 | + content: ''; |
| 41 | + position: absolute; |
| 42 | + bottom: 0; |
| 43 | + left: 0; |
| 44 | + width: 1px; |
| 45 | + height: ${borders.widths.large}; |
| 46 | + background: ${({ theme }) => theme.iconDefault}; |
| 47 | + transform: ${({ $indicatorWidth, $indicatorPosition }) => |
| 48 | + `translateX(${$indicatorPosition}px) scaleX(${$indicatorWidth})`}; |
| 49 | + transform-origin: left; |
| 50 | + transition: transform ${TRANSFORM_OPTIONS}; |
| 51 | + } |
| 52 | +
|
| 53 | + ${withFrameProps} |
| 54 | +`; |
| 55 | + |
| 56 | +export type TabsProps = AllowedFrameProps & { |
| 57 | + children: ReactNode; |
| 58 | + activeItemId?: string; |
| 59 | + isDisabled?: boolean; |
| 60 | + hasBorder?: boolean; |
| 61 | + size?: TabsSize; |
| 62 | +}; |
| 63 | + |
| 64 | +const Tabs = ({ |
| 65 | + isDisabled = false, |
| 66 | + hasBorder = true, |
| 67 | + size = 'medium', |
| 68 | + activeItemId, |
| 69 | + children, |
| 70 | + ...rest |
| 71 | +}: TabsProps) => { |
| 72 | + const { elevation } = useElevation(); |
| 73 | + const [indicatorWidth, setIndicatorWidth] = useState(0); |
| 74 | + const [indicatorPosition, setIndicatorPosition] = useState(0); |
| 75 | + const tabsRefs = useRef<Map<string, HTMLDivElement | null>>(new Map()); |
| 76 | + const frameProps = pickAndPrepareFrameProps(rest, allowedTabsFrameProps); |
| 77 | + |
| 78 | + const setTabRef = useCallback( |
| 79 | + (id: string) => (el: HTMLDivElement) => { |
| 80 | + tabsRefs.current.set(id, el); |
| 81 | + }, |
| 82 | + [], |
| 83 | + ); |
| 84 | + |
| 85 | + const updateIndicator = useCallback(() => { |
| 86 | + if (!activeItemId) return; |
| 87 | + |
| 88 | + const activeItemEl = tabsRefs.current.get(activeItemId); |
| 89 | + const width = activeItemEl?.getBoundingClientRect()?.width; |
| 90 | + const position = activeItemEl?.offsetLeft; |
| 91 | + |
| 92 | + setIndicatorWidth(width ?? 0); |
| 93 | + setIndicatorPosition(position ?? 0); |
| 94 | + }, [activeItemId]); |
| 95 | + |
| 96 | + useEffect(() => { |
| 97 | + updateIndicator(); |
| 98 | + window.addEventListener('resize', updateIndicator); |
| 99 | + |
| 100 | + return () => { |
| 101 | + window.removeEventListener('resize', updateIndicator); |
| 102 | + }; |
| 103 | + }, [updateIndicator, size, children]); |
| 104 | + |
| 105 | + return ( |
| 106 | + <TabsContext.Provider value={{ activeItemId, isDisabled, size, setTabRef }}> |
| 107 | + <Container |
| 108 | + $hasBorder={hasBorder} |
| 109 | + $elevation={elevation} |
| 110 | + $indicatorWidth={indicatorWidth} |
| 111 | + $indicatorPosition={indicatorPosition} |
| 112 | + $size={size} |
| 113 | + {...frameProps} |
| 114 | + > |
| 115 | + <Row alignItems="stretch" gap={spacings.sm}> |
| 116 | + {children} |
| 117 | + </Row> |
| 118 | + </Container> |
| 119 | + </TabsContext.Provider> |
| 120 | + ); |
| 121 | +}; |
| 122 | + |
| 123 | +Tabs.Item = TabsItem; |
| 124 | + |
| 125 | +export { Tabs }; |
0 commit comments