: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import './LemonTabs.scss'
import { IconCheckCircle, IconInfo } from '@posthog/icons'
import { cn } from 'lib/utils/css-classes'
import { useSliderPositioning } from '../hooks'
import { Link } from '../Link'
import { Tooltip } from '../Tooltip'
/** A tab that represents one of the options, but doesn't have any content. Render tab-dependent UI yourself. */
export interface AbstractLemonTab<T extends string | number> {
key: T
label: string | JSX.Element
tooltip?: string | JSX.Element
/** When set, the tab is shown but greyed out and not selectable; the reason is its tooltip. */
disabledReason?: string
/** URL of the tab if it can be linked to (which is usually a good practice). */
link?: string
tooltipDocLink?: string
completed?: boolean
/** data-attr to be placed on the tab button, useful for autocapture */
'data-attr'?: string
}
/** A tab with content. In this case the LemonTabs component automatically renders content of the active tab. */
export interface ConcreteLemonTab<T extends string | number> extends AbstractLemonTab<T> {
content: JSX.Element
keepMounted?: boolean
}
export type LemonTab<T extends string | number> = AbstractLemonTab<T> | ConcreteLemonTab<T>
export interface LemonTabsProps<T extends string | number> {
activeKey: T
onChange?: (key: T) => void
/** List of tabs. Falsy entries are ignored - they're there to make conditional tabs convenient. */
tabs: (LemonTab<T> | null | false)[]
size?: 'xsmall' | 'small' | 'medium'
/** data-attr to be placed on the tab container, useful for autocapture */
'data-attr'?: string
barClassName?: string
className?: string
sceneInset?: boolean
/** Pass in JSX to be sticky to the right of the tabs. */
rightSlot?: React.ReactNode
rightSlotClassName?: string
/** Keeps the underline in place when a tab label changes width, as in an in-place rename. */
trackTabSizes?: boolean
}
interface LemonTabsCSSProperties extends React.CSSProperties {
'--lemon-tabs-slider-width': `${number}px`
'--lemon-tabs-slider-offset': `${number}px`
}
export function LemonTabs<T extends string | number>({
activeKey,
onChange,
tabs,
barClassName,
size = 'medium',
className,
'data-attr': dataAttr,
sceneInset = false,
rightSlot,
rightSlotClassName,
trackTabSizes = false,
}: 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, realTabs.map((tab) => tab.key).join('|'), trackTabSizes)
return (
<div
className={cn(
'LemonTabs',
transitioning && 'LemonTabs--transitioning',
`LemonTabs--${size}`,
sceneInset && '-mt-4 -mx-4 [&>ul]:pl-4 [&>ul]:mb-0',
className
)}
// eslint-disable-next-line react/forbid-dom-props
style={
{
'--lemon-tabs-slider-width': `${sliderWidth}px`,
'--lemon-tabs-slider-offset': `${sliderOffset}px`,
} as LemonTabsCSSProperties
}
data-attr={dataAttr}
>
<ul className={cn('LemonTabs__bar', barClassName)} role="tablist" ref={containerRef}>
<div
className={cn('flex gap-x-4 md:gap-x-8', {
'gap-x-2': size === 'small',
'pr-4': rightSlot,
})}
>
{realTabs.map((tab) => {
const disabled = !!tab.disabledReason
const content = (
<div className="relative flex items-center gap-1" data-attr={tab['data-attr']}>
{tab.label}
{tab.completed && (
<div className="absolute left-full -top-2 md:top-auto -ml-[2px] md:ml-1 size-4 shrink-0 flex items-center justify-center">
<IconCheckCircle className="size-4 text-success" />
</div>
)}
{tab.tooltip && <IconInfo className="ml-1 text-base shrink-0" />}
</div>
)
return (
<Tooltip
key={tab.key}
title={tab.disabledReason || tab.tooltip}
placement="top"
offset={0}
docLink={tab.tooltipDocLink}
>
<li
className={cn(
'LemonTabs__tab',
tab.key === activeKey && 'LemonTabs__tab--active',
disabled && 'LemonTabs__tab--disabled'
)}
onClick={onChange && !disabled ? () => onChange(tab.key) : undefined}
role="tab"
aria-selected={tab.key === activeKey}
aria-disabled={disabled || undefined}
tabIndex={disabled ? -1 : 0}
onKeyDown={
onChange && !disabled
? (e) => {
if (e.key === 'Enter') {
onChange(tab.key)
}
}
: undefined
}
ref={tab.key === activeKey ? selectionRef : undefined}
>
{tab.link ? (
<Link className="LemonTabs__tab-content" to={tab.link}>
{content}
</Link>
) : (
<div className="LemonTabs__tab-content">{content}</div>
)}
</li>
</Tooltip>
)
})}
</div>
{rightSlot && (
<div
className={cn(
'mb-[1px] flex gap-x-2 shrink-0 items-center justify-end sticky right-0 bg-primary pr-4',
rightSlotClassName
)}
>
{rightSlot}
</div>
)}
</ul>
{realTabs.map((tab) => {
const isActive = tab.key === activeKey
const shouldRender = 'content' in tab && (isActive || tab.keepMounted)
return shouldRender ? (
<div className={cn('LemonTabs__content', sceneInset && 'p-4')} key={tab.key} hidden={!isActive}>
{tab.content}
</div>
) : null
})}
</div>
)
}