|
| 1 | +import * as React from 'react'; |
| 2 | +import * as RadixTabs from '@radix-ui/react-tabs'; |
| 3 | + |
| 4 | +type TabsRootProps = { |
| 5 | + defaultValue: string; |
| 6 | + children: React.ReactNode; |
| 7 | +}; |
| 8 | + |
| 9 | +type TabsListProps = { |
| 10 | + children: React.ReactNode; |
| 11 | +}; |
| 12 | + |
| 13 | +type TabsTriggerProps = { |
| 14 | + value: string; |
| 15 | + children: React.ReactNode; |
| 16 | +}; |
| 17 | + |
| 18 | +type TabsContentProps = { |
| 19 | + value: string; |
| 20 | + children: React.ReactNode; |
| 21 | +}; |
| 22 | + |
| 23 | +const Root = ({ defaultValue, children }: TabsRootProps) => { |
| 24 | + const [activeTab, setActiveTab] = React.useState(defaultValue); |
| 25 | + const rootRef = React.useRef<HTMLDivElement>(null); |
| 26 | + const lastProcessedHash = React.useRef<string>(''); |
| 27 | + |
| 28 | + React.useEffect(() => { |
| 29 | + const timeouts: NodeJS.Timeout[] = []; |
| 30 | + |
| 31 | + const switchToTabContainingHash = () => { |
| 32 | + const hash = window.location.hash.slice(1); |
| 33 | + if (!hash || hash === lastProcessedHash.current) return; |
| 34 | + |
| 35 | + const timeoutId = setTimeout(() => { |
| 36 | + const heading = document.getElementById(hash); |
| 37 | + if (!heading) return; |
| 38 | + |
| 39 | + const tabContent = heading.closest('[data-value]'); |
| 40 | + if (tabContent && rootRef.current?.contains(tabContent)) { |
| 41 | + const tabValue = tabContent.getAttribute('data-value'); |
| 42 | + if (tabValue) { |
| 43 | + setActiveTab(tabValue); |
| 44 | + lastProcessedHash.current = hash; |
| 45 | + const scrollTimeoutId = setTimeout(() => { |
| 46 | + heading.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
| 47 | + }, 100); |
| 48 | + timeouts.push(scrollTimeoutId); |
| 49 | + } |
| 50 | + } |
| 51 | + }, 100); |
| 52 | + timeouts.push(timeoutId); |
| 53 | + }; |
| 54 | + |
| 55 | + switchToTabContainingHash(); |
| 56 | + window.addEventListener('hashchange', switchToTabContainingHash); |
| 57 | + |
| 58 | + const interval = setInterval(() => { |
| 59 | + switchToTabContainingHash(); |
| 60 | + }, 500); |
| 61 | + |
| 62 | + return () => { |
| 63 | + window.removeEventListener('hashchange', switchToTabContainingHash); |
| 64 | + clearInterval(interval); |
| 65 | + timeouts.forEach(clearTimeout); |
| 66 | + }; |
| 67 | + }, []); |
| 68 | + |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <style>{` |
| 72 | + .tabs-root { |
| 73 | + display: flex; |
| 74 | + flex-direction: column; |
| 75 | + margin: 1.5rem 0; |
| 76 | + } |
| 77 | +
|
| 78 | + .tabs-list { |
| 79 | + display: flex; |
| 80 | + border-bottom: 1px solid var(--vocs-color_border); |
| 81 | + gap: 0; |
| 82 | + } |
| 83 | +
|
| 84 | + .tabs-trigger { |
| 85 | + all: unset; |
| 86 | + font-family: inherit; |
| 87 | + background-color: transparent; |
| 88 | + padding: 0.75rem 1.5rem; |
| 89 | + height: 45px; |
| 90 | + flex: 1; |
| 91 | + display: flex; |
| 92 | + align-items: center; |
| 93 | + justify-content: center; |
| 94 | + font-size: 0.9375rem; |
| 95 | + line-height: 1; |
| 96 | + color: var(--vocs-color_text3); |
| 97 | + user-select: none; |
| 98 | + cursor: pointer; |
| 99 | + border-bottom: 2px solid transparent; |
| 100 | + transition: all 0.2s ease; |
| 101 | + } |
| 102 | +
|
| 103 | + .tabs-trigger:hover { |
| 104 | + color: var(--vocs-color_text); |
| 105 | + background-color: var(--vocs-color_background2); |
| 106 | + } |
| 107 | +
|
| 108 | + .tabs-trigger[data-state="active"] { |
| 109 | + color: var(--vocs-color_text); |
| 110 | + border-bottom-color: var(--vocs-color_link); |
| 111 | + font-weight: 500; |
| 112 | + } |
| 113 | +
|
| 114 | + .tabs-trigger:focus-visible { |
| 115 | + outline: 2px solid var(--vocs-color_link); |
| 116 | + outline-offset: 2px; |
| 117 | + } |
| 118 | +
|
| 119 | + .tabs-content { |
| 120 | + padding: 1.5rem 0; |
| 121 | + outline: none; |
| 122 | + } |
| 123 | +
|
| 124 | + .tabs-content[data-state="inactive"] { |
| 125 | + display: block !important; |
| 126 | + position: absolute; |
| 127 | + opacity: 0; |
| 128 | + pointer-events: none; |
| 129 | + height: 0; |
| 130 | + overflow: hidden; |
| 131 | + visibility: hidden; |
| 132 | + } |
| 133 | +
|
| 134 | + .tabs-content:focus { |
| 135 | + outline: none; |
| 136 | + } |
| 137 | +
|
| 138 | + .tabs-content > *:not(:last-child) { |
| 139 | + margin-bottom: var(--vocs-space_24); |
| 140 | + } |
| 141 | +
|
| 142 | + .tabs-content > *:last-child { |
| 143 | + margin-bottom: 0; |
| 144 | + } |
| 145 | + `}</style> |
| 146 | + <RadixTabs.Root |
| 147 | + value={activeTab} |
| 148 | + onValueChange={setActiveTab} |
| 149 | + className="tabs-root" |
| 150 | + ref={rootRef} |
| 151 | + > |
| 152 | + {children} |
| 153 | + </RadixTabs.Root> |
| 154 | + </> |
| 155 | + ); |
| 156 | +}; |
| 157 | + |
| 158 | +const List = ({ children }: TabsListProps) => ( |
| 159 | + <RadixTabs.List className="tabs-list">{children}</RadixTabs.List> |
| 160 | +); |
| 161 | + |
| 162 | +const Trigger = ({ value, children }: TabsTriggerProps) => ( |
| 163 | + <RadixTabs.Trigger value={value} className="tabs-trigger"> |
| 164 | + {children} |
| 165 | + </RadixTabs.Trigger> |
| 166 | +); |
| 167 | + |
| 168 | +const Content = ({ value, children }: TabsContentProps) => ( |
| 169 | + <RadixTabs.Content |
| 170 | + value={value} |
| 171 | + className="tabs-content" |
| 172 | + forceMount |
| 173 | + data-value={value} |
| 174 | + > |
| 175 | + {children} |
| 176 | + </RadixTabs.Content> |
| 177 | +); |
| 178 | + |
| 179 | +export const Tabs = { Root, List, Trigger, Content }; |
0 commit comments