|
| 1 | +import { Children, useState } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import cn from 'classnames' |
| 4 | + |
| 5 | +const Tabs = ({ |
| 6 | + children, |
| 7 | + tabs, |
| 8 | + disabled, |
| 9 | + selected, |
| 10 | + defaultSelected = 0, |
| 11 | + onChange |
| 12 | +}) => { |
| 13 | + let [activeTab, setActiveTab] = useState(defaultSelected) |
| 14 | + // override if it exists |
| 15 | + activeTab = selected || activeTab |
| 16 | + |
| 17 | + return ( |
| 18 | + <div className={cn('tabs', { disabled })}> |
| 19 | + <div className="tabs-row"> |
| 20 | + {tabs.map((tabName, index) => ( |
| 21 | + <button |
| 22 | + key={'tab-' + index} |
| 23 | + className="tab" |
| 24 | + disabled={index === activeTab} |
| 25 | + onClick={() => { |
| 26 | + if (disabled) return |
| 27 | + if (typeof selected === 'undefined') { |
| 28 | + setActiveTab(index) |
| 29 | + } |
| 30 | + onChange && onChange(index) |
| 31 | + }} |
| 32 | + > |
| 33 | + {tabName} |
| 34 | + </button> |
| 35 | + ))} |
| 36 | + </div> |
| 37 | + {children |
| 38 | + ? Children.map(children, (tab, index) => |
| 39 | + index === activeTab ? tab : null |
| 40 | + ) |
| 41 | + : null} |
| 42 | + <style jsx>{` |
| 43 | + .tab { |
| 44 | + height: 34px; |
| 45 | + padding: 0 24px; |
| 46 | + user-select: none; |
| 47 | + color: #999; |
| 48 | + font-size: 16px; |
| 49 | + overflow: hidden; |
| 50 | + transition: border 0.2s ease, background 0.2s ease, color 0.2s ease; |
| 51 | + border-radius: 0; |
| 52 | + white-space: nowrap; |
| 53 | + text-decoration: none; |
| 54 | + text-transform: capitalize; |
| 55 | + line-height: 0; |
| 56 | + appearance: none; |
| 57 | + outline: none; |
| 58 | + cursor: pointer; |
| 59 | + border: 0; |
| 60 | + position: relative; |
| 61 | + z-index: 1; |
| 62 | + border-bottom: 1px solid var(--accents-2); |
| 63 | + } |
| 64 | +
|
| 65 | + .tab::after { |
| 66 | + position: absolute; |
| 67 | + bottom: -1px; |
| 68 | + left: 0; |
| 69 | + width: 100%; |
| 70 | + height: 1px; |
| 71 | + background: var(--accents-2); |
| 72 | + content: ''; |
| 73 | + transition: background 0.12s ease; |
| 74 | + } |
| 75 | +
|
| 76 | + .tab[disabled] { |
| 77 | + position: relative; |
| 78 | + z-index: 1; |
| 79 | + color: var(--geist-foreground); |
| 80 | + background: var(--geist-background); |
| 81 | + border-color: var(--geist-foreground); |
| 82 | + cursor: default; |
| 83 | + } |
| 84 | +
|
| 85 | + .tab[disabled]::after { |
| 86 | + height: 2px; |
| 87 | + background: var(--geist-foreground); |
| 88 | + } |
| 89 | +
|
| 90 | + .tabs :global(._fieldset) { |
| 91 | + border-top-left-radius: 0; |
| 92 | + overflow: hidden; |
| 93 | + } |
| 94 | +
|
| 95 | + .tabs.disabled { |
| 96 | + pointer-events: none; |
| 97 | + } |
| 98 | +
|
| 99 | + .tabs.disabled .tab { |
| 100 | + background: var(--accents-1); |
| 101 | + color: #999; |
| 102 | + border-bottom: 1px solid var(--accents-1); |
| 103 | + } |
| 104 | +
|
| 105 | + .tabs-row { |
| 106 | + white-space: nowrap; |
| 107 | + overflow-y: hidden; |
| 108 | + overflow-x: auto; |
| 109 | + position: relative; |
| 110 | + margin-bottom: 32px; |
| 111 | + } |
| 112 | +
|
| 113 | + .tabs-row::after { |
| 114 | + height: 1px; |
| 115 | + width: 100%; |
| 116 | + position: absolute; |
| 117 | + content: ''; |
| 118 | + bottom: 0; |
| 119 | + left: 0; |
| 120 | + background: var(--accents-2); |
| 121 | + } |
| 122 | + `}</style> |
| 123 | + </div> |
| 124 | + ) |
| 125 | +} |
| 126 | + |
| 127 | +Tabs.propTypes = { |
| 128 | + children: PropTypes.array.isRequired |
| 129 | +} |
| 130 | + |
| 131 | +export default Tabs |
0 commit comments