|
| 1 | +import { styled } from '@linaria/react' |
| 2 | +import { useState } from 'preact/hooks' |
| 3 | +import { |
| 4 | + ACTIVE_TAB_COLOR, |
| 5 | + INACTIVE_TAB_COLOR, |
| 6 | + SECONDARY_COLOR, |
| 7 | + TAB_HEIGHT, |
| 8 | + TAB_WIDTH, |
| 9 | + TABS_RADIO_GROUP_NAME, |
| 10 | +} from '../../constants' |
| 11 | + |
| 12 | +type TabBarProps<T extends string> = { |
| 13 | + options: { |
| 14 | + label: string |
| 15 | + id: T |
| 16 | + }[] |
| 17 | + defaultTab?: T |
| 18 | + onChange?: (value: T) => void |
| 19 | +} |
| 20 | + |
| 21 | +const TabBarContainer = styled.div` |
| 22 | + display: flex; |
| 23 | + position: relative; |
| 24 | + background-color: #f4f5f7; |
| 25 | + box-shadow: |
| 26 | + 0 0 1px 0 rgba(24, 94, 224, 0.15), |
| 27 | + 0 6px 12px 0 rgba(24, 94, 224, 0.15); |
| 28 | + padding: 4px; |
| 29 | + border-radius: 4px; |
| 30 | + width: fit-content; |
| 31 | +
|
| 32 | + input[type='radio'] { |
| 33 | + position: fixed; |
| 34 | + top: -100px; |
| 35 | + } |
| 36 | +
|
| 37 | + input[type='radio']:checked + label { |
| 38 | + color: ${ACTIVE_TAB_COLOR}; |
| 39 | + font-weight: 500; |
| 40 | + } |
| 41 | +
|
| 42 | + input[type='radio']:focus + label { |
| 43 | + outline: 2px solid ${ACTIVE_TAB_COLOR}; |
| 44 | + } |
| 45 | +` |
| 46 | + |
| 47 | +const TabBarLabel = styled.label` |
| 48 | + display: flex; |
| 49 | + align-items: center; |
| 50 | + justify-content: center; |
| 51 | + color: ${INACTIVE_TAB_COLOR}; |
| 52 | + width: ${TAB_WIDTH}; |
| 53 | + height: ${TAB_HEIGHT}; |
| 54 | + font-size: 1rem; |
| 55 | + font-weight: 400; |
| 56 | + border-radius: 8px; |
| 57 | + cursor: pointer; |
| 58 | + transition: color 0.15s ease-in; |
| 59 | + z-index: 2; |
| 60 | +` |
| 61 | + |
| 62 | +const TabBarSlider = styled.span` |
| 63 | + position: absolute; |
| 64 | + display: flex; |
| 65 | + width: ${TAB_WIDTH}; |
| 66 | + height: ${TAB_HEIGHT}; |
| 67 | + background-color: ${SECONDARY_COLOR}; |
| 68 | + z-index: 1; |
| 69 | + border-radius: 8px; |
| 70 | + transition: 0.25s ease-out; |
| 71 | + transform: translateX(calc(100% * ${(props) => props['data-selectedIndex']})); |
| 72 | +` |
| 73 | + |
| 74 | +export const TabBar = <T extends string>({ |
| 75 | + options, |
| 76 | + defaultTab, |
| 77 | + onChange, |
| 78 | +}: TabBarProps<T>) => { |
| 79 | + const [selectedTab, setSelectedTab] = useState(defaultTab) |
| 80 | + const selectedTabIndex = options.findIndex(({ id }) => id === selectedTab) |
| 81 | + |
| 82 | + return ( |
| 83 | + <TabBarContainer role="tablist"> |
| 84 | + {options.map((option) => ( |
| 85 | + <> |
| 86 | + <input |
| 87 | + type="radio" |
| 88 | + id={option.id} |
| 89 | + name={TABS_RADIO_GROUP_NAME} |
| 90 | + checked={selectedTab === option.id} |
| 91 | + onChange={() => { |
| 92 | + setSelectedTab(option.id) |
| 93 | + onChange?.(option.id) |
| 94 | + }} |
| 95 | + /> |
| 96 | + |
| 97 | + <TabBarLabel for={option.id} role="tab"> |
| 98 | + {option.label} |
| 99 | + </TabBarLabel> |
| 100 | + </> |
| 101 | + ))} |
| 102 | + |
| 103 | + <TabBarSlider |
| 104 | + data-selectedIndex={selectedTabIndex === -1 ? 0 : selectedTabIndex} |
| 105 | + /> |
| 106 | + </TabBarContainer> |
| 107 | + ) |
| 108 | +} |
0 commit comments