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.

5.2 KB
Raw Blame
.LemonSwitch {
    --lemon-switch-handle-size: 13px;
    --lemon-switch-height: calc(var(--lemon-switch-handle-size) + 2px);
    --lemon-switch-width: calc(2 * var(--lemon-switch-handle-size) + 2px);

    display: flex;
    gap: 0.5rem;
    align-items: center;
    width: fit-content;
    font-weight: 500;
    line-height: 1.5rem;

    label[for] {
        cursor: pointer; // A label with for=* also toggles the switch, so it shouldn't have the text select cursor
    }

    label {
        flex: 1;
        color: var(--color-text-primary);
        cursor: inherit;
    }

    &.LemonSwitch--xxsmall {
        --lemon-switch-handle-size: 6px;

        label {
            font-size: 0.75rem;
        }
    }

    &.LemonSwitch--xsmall {
        --lemon-switch-handle-size: 7px;

        label {
            font-size: 0.75rem;
        }
    }

    &.LemonSwitch--small {
        --lemon-switch-handle-size: 9px;

        label {
            font-size: 0.75rem;
        }
    }

    &.LemonSwitch--full-width {
        width: 100%;
        padding-right: 0.5rem;
        padding-left: 0.5rem; // Should be aligned with buttons
    }

    &.LemonSwitch--bordered {
        min-height: calc(2.125rem + 3px); // Medium size button height + button shadow height
        padding: 0 0.75rem;
        line-height: 1.4;
        background: var(--color-bg-surface-primary);
        border: 1px solid var(--color-border-primary);
        border-radius: var(--radius);

        &.LemonSwitch--small {
            gap: 0.5rem;
            min-height: calc(1.875rem + 3px); // Small size button height + button shadow height
            padding: 0 0.5rem;
        }

        &.LemonSwitch--xsmall {
            gap: 0.25rem;
            min-height: calc(1.625rem + 3px); // xSmall size button height + button shadow height
            padding: 0 0.375rem;
        }
    }

    > .LemonIcon {
        font-size: 1.5rem;
        color: var(--color-text-secondary);
    }

    &.LemonSwitch--disabled {
        cursor: not-allowed;
        opacity: var(--opacity-disabled);

        label[for] {
            cursor: not-allowed; // A label with for=* also toggles the switch, so it shouldn't have the text select cursor
        }
    }

    &.LemonSwitch--loading {
        .LemonSwitch__button {
            cursor: not-allowed;
            opacity: 0.7;
        }
    }
}

.LemonSwitch__button {
    --lemon-switch-handle-gutter: calc((var(--lemon-switch-height) - var(--lemon-switch-handle-size)) / 2);

    position: relative;
    display: inline-block;
    flex-shrink: 0;
    width: var(--lemon-switch-width);
    height: var(--lemon-switch-height);
    padding: var(--lemon-switch-handle-gutter);
    cursor: pointer;
    background-color: var(--color-bg-fill-switch);
    border: none;
    border-radius: var(--lemon-switch-height);
    outline: 1px solid var(--color-border-primary);
    transition: background-color 100ms ease;

    .LemonSwitch--checked & {
        background-color: var(--color-accent);
    }

    .LemonSwitch--disabled & {
        cursor: not-allowed;
    }
}

.LemonSwitch__handle {
    --lemon-switch-handle-height: var(--lemon-switch-handle-size);
    --lemon-switch-handle-width: var(--lemon-switch-handle-size);
    --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);
    height: var(--lemon-switch-handle-height);
    pointer-events: none;
    cursor: inherit;
    background-color: #fff;
    border: none;
    border-radius: var(--lemon-switch-handle-size);
    transition:
        background-color 100ms ease,
        transform 100ms ease,
        width 100ms ease,
        border-color 100ms ease;

    .LemonSwitch--checked & {
        background-color: var(--color-bg-surface-primary);
        border-color: var(--color-accent);
        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);

        transform: none;
    }

    .LemonSwitch--active.LemonSwitch--checked & {
        transform: var(--lemon-switch-active-translate);
    }

    .LemonSwitch--active.LemonSwitch--indeterminate & {
        transform: var(--lemon-switch-indeterminate-translate);
    }
}

.LemonSwitch__spinner-icon {
    width: calc(var(--lemon-switch-handle-size) + 2px);
    height: calc(var(--lemon-switch-handle-size) + 2px);
}