|
| 1 | +import { useState, useRef, type ReactNode, type KeyboardEvent } from 'react' |
| 2 | + |
| 3 | +type Tab = 'react' | 'native' |
| 4 | + |
| 5 | +type PlatformTabsProps = { |
| 6 | + mobile?: ReactNode |
| 7 | + children?: ReactNode |
| 8 | +} |
| 9 | + |
| 10 | +const TABS: { id: Tab; label: string }[] = [ |
| 11 | + { id: 'react', label: 'React' }, |
| 12 | + { id: 'native', label: 'React Native' }, |
| 13 | +] |
| 14 | + |
| 15 | +export const PlatformTabs = ({ mobile, children }: PlatformTabsProps) => { |
| 16 | + const [activeTab, setActiveTab] = useState<Tab>('react') |
| 17 | + const tabRefs = useRef<(HTMLButtonElement | null)[]>([]) |
| 18 | + |
| 19 | + if (!mobile) return <>{children}</> |
| 20 | + |
| 21 | + const handleKeyDown = (e: KeyboardEvent, index: number) => { |
| 22 | + if (e.key === 'ArrowRight') { |
| 23 | + const next = (index + 1) % TABS.length |
| 24 | + tabRefs.current[next]?.focus() |
| 25 | + setActiveTab(TABS[next].id) |
| 26 | + } else if (e.key === 'ArrowLeft') { |
| 27 | + const prev = (index - 1 + TABS.length) % TABS.length |
| 28 | + tabRefs.current[prev]?.focus() |
| 29 | + setActiveTab(TABS[prev].id) |
| 30 | + } else if (e.key === 'Home') { |
| 31 | + e.preventDefault() |
| 32 | + tabRefs.current[0]?.focus() |
| 33 | + setActiveTab(TABS[0].id) |
| 34 | + } else if (e.key === 'End') { |
| 35 | + e.preventDefault() |
| 36 | + tabRefs.current[TABS.length - 1]?.focus() |
| 37 | + setActiveTab(TABS[TABS.length - 1].id) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return ( |
| 42 | + <div> |
| 43 | + <div |
| 44 | + role="tablist" |
| 45 | + aria-label="Platform" |
| 46 | + style={{ |
| 47 | + display: 'flex', |
| 48 | + borderBottom: '1px solid var(--eds-color-border-medium, #DCDCDC)', |
| 49 | + marginBottom: '1.5rem', |
| 50 | + }} |
| 51 | + > |
| 52 | + {TABS.map(({ id, label }, index) => ( |
| 53 | + <button |
| 54 | + key={id} |
| 55 | + id={`platform-tab-${id}`} |
| 56 | + ref={(el) => { |
| 57 | + tabRefs.current[index] = el |
| 58 | + }} |
| 59 | + role="tab" |
| 60 | + aria-selected={activeTab === id} |
| 61 | + aria-controls={`platform-tabpanel-${id}`} |
| 62 | + tabIndex={activeTab === id ? 0 : -1} |
| 63 | + onClick={() => setActiveTab(id)} |
| 64 | + onKeyDown={(e) => handleKeyDown(e, index)} |
| 65 | + style={{ |
| 66 | + padding: '0.5rem 1rem', |
| 67 | + border: 'none', |
| 68 | + background: 'none', |
| 69 | + cursor: 'pointer', |
| 70 | + fontFamily: |
| 71 | + 'var(--eds-typography-ui-body-font-family), sans-serif', |
| 72 | + fontSize: '0.875rem', |
| 73 | + fontWeight: activeTab === id ? 700 : 400, |
| 74 | + color: |
| 75 | + activeTab === id |
| 76 | + ? 'var(--eds-color-interactive-primary, #007079)' |
| 77 | + : '#585858', |
| 78 | + borderBottom: |
| 79 | + activeTab === id |
| 80 | + ? '1px solid var(--eds-color-interactive-primary, #007079)' |
| 81 | + : '1px solid transparent', |
| 82 | + marginBottom: '-1px', |
| 83 | + }} |
| 84 | + > |
| 85 | + {label} |
| 86 | + </button> |
| 87 | + ))} |
| 88 | + </div> |
| 89 | + <div |
| 90 | + role="tabpanel" |
| 91 | + id={`platform-tabpanel-${activeTab}`} |
| 92 | + aria-labelledby={`platform-tab-${activeTab}`} |
| 93 | + tabIndex={0} |
| 94 | + > |
| 95 | + {activeTab === 'react' ? children : mobile} |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + ) |
| 99 | +} |
0 commit comments