: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
feat(lemon-ui): add indeterminate state to LemonSwitch (#69169)
Thomas Obermüllerac9135401da7
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 712d1c9cd2f0..aaf0bc16976d 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -3936,6 +3936,10 @@ snapshots:hash: v1.k794b7964.0dda5edf3c90441a3314fb162be33ba02438c6236899a15673c258cd02fd9447.Q-Dyq1OyDT8kuUH8dn5lwkGdpRnj_yqZU767KP9sgL8lemon-ui-lemon-switch--disabled--light:hash: v1.k794b7964.7a07f77443205915d54f15c9102b12f487814d755721bd132fa64f3b0e6371f0.PLzcpvxsLOxEGRAa-G28ZvpeNWk78NTxLZDWAYQ9nnQ+ lemon-ui-lemon-switch--indeterminate--dark:+ hash: v1.k794b7964.54e2b4f8325923115070f4493d3fd9043f9bbc7e4d49d49978fdd7418930bf72.TP6egk8otVWsZ2lnGdGirO1Yq1PeihCUt_eTC-kVSS8+ lemon-ui-lemon-switch--indeterminate--light:+ hash: v1.k794b7964.db207d14bbd34afcc50b0c8a38cf9aad5199fe0e683aaf9b822d4636bcf72b50.aFxgqsJsNz7GHYQwowEn6jiqS4yrUvmvScBMK9xTt8Alemon-ui-lemon-switch--overview--dark:hash: v1.k794b7964.872600c303709874583784febe9bad9aef0bb87f9843af9591bfff07058120cc.wimmvYiBqy4yV_njLni26wTERdJq0rnLFYWTIZH-y44lemon-ui-lemon-switch--overview--light:diff --git a/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.scss b/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.scssindex 8448b312061f..0245896054cf 100644--- a/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.scss+++ b/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.scss@@ -124,6 +124,9 @@--lemon-switch-active-translate: translateX(calc(var(--lemon-switch-width) - var(--lemon-switch-handle-width) - 2 * var(--lemon-switch-handle-gutter)));+ --lemon-switch-indeterminate-translate: translateX(+ calc((var(--lemon-switch-width) - var(--lemon-switch-handle-width) - 2 * var(--lemon-switch-handle-gutter)) / 2)+ );position: relative;width: var(--lemon-switch-handle-width);@@ -145,6 +148,24 @@transform: var(--lemon-switch-active-translate);}+ // Indeterminate: the handle parks mid-track on the neutral track, carrying a gray dash+ .LemonSwitch--indeterminate & {+ transform: var(--lemon-switch-indeterminate-translate);+ }++ // The spinner replaces the dash while loading+ .LemonSwitch--indeterminate:not(.LemonSwitch--loading) &::after {+ position: absolute;+ top: 50%;+ left: 50%;+ width: 55%;+ height: 2px;+ content: '';+ background-color: var(--color-text-secondary);+ border-radius: 1px;+ transform: translate(-50%, -50%);+ }+.LemonSwitch--active & {--lemon-switch-handle-width: calc(var(--lemon-switch-handle-size) * 1.2);@@ -154,6 +175,10 @@.LemonSwitch--active.LemonSwitch--checked & {transform: var(--lemon-switch-active-translate);}++ .LemonSwitch--active.LemonSwitch--indeterminate & {+ transform: var(--lemon-switch-indeterminate-translate);+ }}.LemonSwitch__spinner-icon {diff --git a/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.stories.tsx b/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.stories.tsxindex 57eba4638c2e..c1b74ae1ee08 100644--- a/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.stories.tsx+++ b/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.stories.tsx@@ -67,6 +67,22 @@ export const Standalone: Story = {args: { label: undefined },}+export const Indeterminate: Story = {+ render: function IndeterminateStory() {+ const [value, setValue] = useState<boolean | 'indeterminate'>('indeterminate')+ return (+ <div className="deprecated-space-y-2">+ <RawLemonSwitch label="Interactive (click resolves to checked)" checked={value} onChange={setValue} />+ <LemonSwitch label="Unchecked" checked={false} />+ <RawLemonSwitch label="Indeterminate" checked="indeterminate" onChange={() => {}} />+ <LemonSwitch label="Checked" checked />+ <RawLemonSwitch label="Indeterminate bordered" checked="indeterminate" onChange={() => {}} bordered />+ <RawLemonSwitch label="Indeterminate small" size="small" checked="indeterminate" onChange={() => {}} />+ </div>+ )+ },+}+export const Bordered: Story = {args: { bordered: true },}diff --git a/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.test.tsx b/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.test.tsxindex 80e6760a0867..0f6d07635c90 100644--- a/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.test.tsx+++ b/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.test.tsx@@ -1,6 +1,6 @@import '@testing-library/jest-dom'-import { cleanup, render, screen } from '@testing-library/react'+import { cleanup, fireEvent, render, screen } from '@testing-library/react'import { LemonSwitch } from './LemonSwitch'@@ -9,6 +9,23 @@ describe('LemonSwitch', () => {cleanup()})+ it.each([+ { checked: false as const, ariaChecked: 'false', nextChecked: true },+ { checked: true as const, ariaChecked: 'true', nextChecked: false },+ { checked: 'indeterminate' as const, ariaChecked: 'mixed', nextChecked: true },+ ])(+ 'exposes aria-checked "$ariaChecked" and resolves a click to $nextChecked',+ ({ checked, ariaChecked, nextChecked }) => {+ const onChange = jest.fn()+ render(<LemonSwitch checked={checked} onChange={onChange} />)++ const button = screen.getByRole('switch')+ expect(button).toHaveAttribute('aria-checked', ariaChecked)+ fireEvent.click(button)+ expect(onChange).toHaveBeenCalledWith(nextChecked)+ }+ )+it.each([{ value: true, expected: 'true' },{ value: false, expected: 'false' },diff --git a/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.tsx b/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.tsxindex d9f0fa81a536..6640e9ce2aaf 100644--- a/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.tsx+++ b/frontend/src/lib/lemon-ui/LemonSwitch/LemonSwitch.tsx@@ -10,7 +10,8 @@ import { cn } from 'lib/utils/css-classes'export interface LemonSwitchProps {className?: stringonChange?: (newChecked: boolean) => void- checked: boolean+ /** `'indeterminate'` shows a mixed state (centered handle with a dash); clicking it resolves to checked. */+ checked: boolean | 'indeterminate'label?: string | JSX.ElementlabelClassName?: stringid?: string@@ -84,14 +85,15 @@ export const LemonSwitch: React.FunctionComponent<LemonSwitchProps & React.RefAtid={id}className={`LemonSwitch__button ${sliderColorOverrideChecked || sliderColorOverrideUnchecked- ? `bg-${checked ? sliderColorOverrideChecked : sliderColorOverrideUnchecked}`+ ? `bg-${checked === true ? sliderColorOverrideChecked : sliderColorOverrideUnchecked}`: ''}`}type="button"role="switch"+ aria-checked={checked === 'indeterminate' ? 'mixed' : checked}onClick={() => {if (onChange && !loading) {- onChange(!checked)+ onChange(checked !== true)}}}onMouseDown={() => !loading && setIsActive(true)}@@ -128,7 +130,8 @@ export const LemonSwitch: React.FunctionComponent<LemonSwitchProps & React.RefAt<divref={ref}className={clsx('LemonSwitch', className, `LemonSwitch--${size}`, {- 'LemonSwitch--checked': checked,+ 'LemonSwitch--checked': checked === true,+ 'LemonSwitch--indeterminate': checked === 'indeterminate','LemonSwitch--active': isActive,'LemonSwitch--bordered': bordered,'LemonSwitch--disabled': isDisabled,