Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(lemon-ui): move the tab underline when tabs are added or removed (#116316)

Daniel Marchuk4d2090c8e9cf
diff --git a/frontend/src/lib/lemon-ui/LemonTabs/LemonTabs.test.tsx b/frontend/src/lib/lemon-ui/LemonTabs/LemonTabs.test.tsx
index f5af9cbbd290..8016d719ff27 100644
--- a/frontend/src/lib/lemon-ui/LemonTabs/LemonTabs.test.tsx
+++ b/frontend/src/lib/lemon-ui/LemonTabs/LemonTabs.test.tsx
@@ -44,4 +44,32 @@ describe('LemonTabs', () => {
expect(screen.getByLabelText('Lineage value')).toBe(lineageInput)
expect(lineageInput).toHaveValue('retained')
})
+
+ it('moves the underline to the active tab when a tab is inserted before it', () => {
+ // jsdom has no layout, so place each tab 100px after the previous one.
+ const offsetLeft = jest
+ .spyOn(HTMLElement.prototype, 'offsetLeft', 'get')
+ .mockImplementation(function (this: HTMLElement) {
+ return this.parentElement ? Array.from(this.parentElement.children).indexOf(this) * 100 : 0
+ })
+ const offsetWidth = jest.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(80)
+ try {
+ const workflowTab = { key: 'workflow', label: 'Workflow', content: null }
+ const { container, rerender } = render(<LemonTabs activeKey="workflow" tabs={[workflowTab]} />)
+ const tabsElement = container.querySelector<HTMLElement>('.LemonTabs')!
+ expect(tabsElement.style.getPropertyValue('--lemon-tabs-slider-offset')).toBe('0px')
+
+ rerender(
+ <LemonTabs
+ activeKey="workflow"
+ tabs={[{ key: 'overview', label: 'Overview', content: null }, workflowTab]}
+ />
+ )
+
+ expect(tabsElement.style.getPropertyValue('--lemon-tabs-slider-offset')).toBe('100px')
+ } finally {
+ offsetLeft.mockRestore()
+ offsetWidth.mockRestore()
+ }
+ })
})
diff --git a/frontend/src/lib/lemon-ui/LemonTabs/LemonTabs.tsx b/frontend/src/lib/lemon-ui/LemonTabs/LemonTabs.tsx
index 372b15330a0e..289791bda49e 100644
--- a/frontend/src/lib/lemon-ui/LemonTabs/LemonTabs.tsx
+++ b/frontend/src/lib/lemon-ui/LemonTabs/LemonTabs.tsx
@@ -64,13 +64,12 @@ export function LemonTabs<T extends string | number>({
rightSlot,
rightSlotClassName,
}: LemonTabsProps<T>): JSX.Element {
+ /** Tabs with falsy entries filtered out. */
+ const realTabs = tabs.filter(Boolean) as LemonTab<T>[]
const { containerRef, selectionRef, sliderWidth, sliderOffset, transitioning } = useSliderPositioning<
HTMLUListElement,
HTMLLIElement
- >(activeKey, 200)
-
- /** Tabs with falsy entries filtered out. */
- const realTabs = tabs.filter(Boolean) as LemonTab<T>[]
+ >(activeKey, 200, realTabs.map((tab) => tab.key).join('|'))
return (
<div
diff --git a/frontend/src/lib/lemon-ui/hooks.ts b/frontend/src/lib/lemon-ui/hooks.ts
index 4b3807d163aa..de3666d5b9f1 100644
--- a/frontend/src/lib/lemon-ui/hooks.ts
+++ b/frontend/src/lib/lemon-ui/hooks.ts
@@ -8,7 +8,9 @@ import { useResizeObserver } from 'lib/hooks/useResizeObserver'
*/
export function useSliderPositioning<C extends HTMLElement, S extends HTMLElement>(
currentValue: React.Key | null | undefined,
- transitionMs: number
+ transitionMs: number,
+ /** Changes when the options change, so the slider follows a selection that moved without changing. */
+ layoutKey?: string
): {
containerRef: React.RefObject<C>
selectionRef: React.RefObject<S>
@@ -33,7 +35,7 @@ export function useSliderPositioning<C extends HTMLElement, S extends HTMLElemen
}
hasRenderedInitiallyRef.current = true
}
- }, [currentValue, containerWidth]) // oxlint-disable-line react-hooks/exhaustive-deps
+ }, [currentValue, containerWidth, layoutKey]) // oxlint-disable-line react-hooks/exhaustive-deps
return {
containerRef,