| 37 | } |
| 38 | |
| 39 | export const ShellRow = ({ |
| 40 | children, |
| 41 | kind = ShellRowKind.Tabs, |
| 42 | }: { |
| 43 | children: ReactNode; |
| 44 | kind?: ShellRowKind; |
| 45 | }): ReactElement => { |
| 46 | const rowRef = useRef<HTMLDivElement>(null); |
| 47 | const barRef = useRef<HTMLSpanElement>(null); |
| 48 | const isTabs = kind === ShellRowKind.Tabs; |
| 49 | |
| 50 | useEffect(() => { |
| 51 | const row = rowRef.current; |
| 52 | |
| 53 | if (!row) { |
| 54 | return undefined; |
| 55 | } |
| 56 | |
| 57 | // The bar follows the lit label; it only slides once it has a place. |
| 58 | const placeBar = () => { |
| 59 | const bar = barRef.current; |
| 60 | const lit = row.querySelector<HTMLElement>(litSelector); |
| 61 | const label = lit?.querySelector<HTMLElement>('[data-tab-label]'); |
| 62 | |
| 63 | if (!bar || !lit || !label) { |
| 64 | if (bar) { |
| 65 | bar.style.opacity = '0'; |
| 66 | } |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | const left = |
| 71 | lit.offsetLeft + |
| 72 | label.offsetLeft + |
| 73 | label.offsetWidth / 2 - |
| 74 | barWidth / 2; |
| 75 | const isPlaced = bar.style.opacity === '1'; |
| 76 | bar.style.transition = isPlaced ? '' : 'none'; |
| 77 | bar.style.transform = `translateX(${left}px)`; |
| 78 | bar.style.opacity = '1'; |
| 79 | }; |
| 80 | |
| 81 | const reveal = () => { |
| 82 | placeBar(); |
| 83 | const lit = row.querySelector(litSelector); |
| 84 | |
| 85 | if (!row.clientWidth || !lit) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // Not scrollIntoView: it would also scroll the page to a row below the fold. |
| 90 | const rowBox = row.getBoundingClientRect(); |
| 91 | const litBox = lit.getBoundingClientRect(); |
| 92 | |
| 93 | if (litBox.right > rowBox.right - rowInset) { |
| 94 | row.scrollLeft += litBox.right - rowBox.right + rowInset; |
| 95 | } else if (litBox.left < rowBox.left + rowInset) { |
| 96 | row.scrollLeft -= rowBox.left + rowInset - litBox.left; |