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): let a resized column crop its content (#95213)

Arthur Moreira de Deus94fcea6fea88
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 159459d5d461..45259a82c513 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -4573,9 +4573,9 @@ snapshots:
lemon-ui-lemon-table--with-mandatory-sorting--light:
hash: v1.k794b7964.50be4384d7a67308b28bd740d1e84347fe4b471eaf03a2bc3ea52c840255de91.gLmHFSKz9ruru1cWO0PIyhFJdPojiPF4aqpyCDEDihY
lemon-ui-lemon-table--with-resizable-columns--dark:
- hash: v1.k794b7964.54ad02003e46c769c1a4922b18ecc52df3d26d2f9e275c5e2754f22998306393.DZzLC997g28Q_2trYwyrvGRGg8AH8SBeROXMllqmIE8
+ hash: v1.k794b7964.1cb00b6e77685711c6926ec213f1f4fd2f9559a5c77128155db1a41ded495e5b.j0y93JOxVUo1b9EHJEh6eAHtAF2Sw6_ClOKzMZtBQ58
lemon-ui-lemon-table--with-resizable-columns--light:
- hash: v1.k794b7964.54aa0c1313dd591719f5c0331638101ee99487143c2b71e62c6f4612dfc5555e.dAPm8Ua9mRunaLEPuaIXPDuQqkl3u8F9GLPHKfpFhds
+ hash: v1.k794b7964.56c9ae9e008c32f6d08d808b6be3ebf8987bdb7f9f35927dedb7863450db225d.WyvvIP-NCZeoTOmN9pnYXV_SI44Kmqjqvh13VFy3uQ0
lemon-ui-lemon-table--with-row-actions--dark:
hash: v1.k794b7964.1bd19ec7e185db60fad4f360b831a15233afc37b27dba90b47fcf029139369eb.XGFyuA7rFcNPBpu5h8CXQEo25X9V6FJkl2HJ2JMoArY
lemon-ui-lemon-table--with-row-actions--light:
diff --git a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.stories.tsx b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.stories.tsx
index fea0b92df497..559e582fe581 100644
--- a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.stories.tsx
+++ b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.stories.tsx
@@ -486,18 +486,33 @@ export const WithRowActions: Story = {
},
}
+const RESIZABLE_COLUMNS: LemonTableColumn<MockPerson, keyof MockPerson | undefined>[] = [
+ {
+ title: 'Identifier',
+ key: 'identifier',
+ width: 300,
+ render: (_, person) => (
+ <span className="whitespace-nowrap">{`0192b2c8-4f4c-7c3a-9e6d-${person.name.length}b2c3d4e5f6`}</span>
+ ),
+ },
+ ...WIDE_COLUMNS.slice(0, 3),
+]
+
function ResizableColumnsTable(): JSX.Element {
const [widths, setWidths] = useState<Record<number, number>>({})
return (
<LemonTable
- columns={WIDE_COLUMNS.slice(0, 3).map((column, index) => ({
+ columns={RESIZABLE_COLUMNS.map((column, index) => ({
...column,
width: widths[index] ?? column.width,
resizable: true,
onResize: (width: number) => setWidths((currentWidths) => ({ ...currentWidths, [index]: width })),
}))}
dataSource={MANY_PEOPLE.slice(0, 5)}
+ // Size the table to its content once a column is resized, the same as tables that persist
+ // column widths. Intrinsic sizing is the mode a cropped column shows in.
+ tableStyle={Object.keys(widths).length > 0 ? { width: 'max-content' } : undefined}
/>
)
}
diff --git a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx
index 96044d60a66f..0fadbcfd5333 100644
--- a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx
+++ b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx
@@ -99,6 +99,55 @@ describe('LemonTable', () => {
expect(onResizeEnd).toHaveBeenCalledTimes(1)
})
+ it('caps a resizable column at its width so the content crops', () => {
+ render(
+ <LemonTable
+ rowKey="id"
+ dataSource={DATA}
+ columns={[
+ {
+ title: 'Value',
+ key: 'value',
+ dataIndex: 'value',
+ width: 90,
+ resizable: true,
+ onResize: jest.fn(),
+ },
+ { title: 'Name', key: 'name', dataIndex: 'name', width: 90 },
+ ]}
+ />
+ )
+
+ expect(screen.getByText('Value').closest('th')).toHaveStyle({ maxWidth: '90px' })
+ expect(document.querySelector('tbody tr:first-child > td')).toHaveStyle({ maxWidth: '90px' })
+ expect(screen.getByText('Name').closest('th')).not.toHaveStyle({ maxWidth: '90px' })
+ })
+
+ it('leaves a cell that spans several columns uncapped', () => {
+ render(
+ <LemonTable
+ rowKey="id"
+ dataSource={DATA}
+ columns={[
+ {
+ title: 'Value',
+ key: 'value',
+ width: 90,
+ resizable: true,
+ onResize: jest.fn(),
+ render: () => ({ children: 'spans the row', props: { colSpan: 2 } }),
+ },
+ { title: 'Name', key: 'name', dataIndex: 'name', width: 90 },
+ ]}
+ />
+ )
+
+ const spanningCell = document.querySelector('tbody tr:first-child > td')
+ expect(spanningCell).toHaveAttribute('colspan', '2')
+ expect(spanningCell).not.toHaveStyle({ maxWidth: '90px' })
+ expect(spanningCell).not.toHaveClass('whitespace-nowrap')
+ })
+
it('keeps headers, expanded rows, and empty states aligned when the row expansion toggle is hidden', () => {
const { rerender } = render(
<LemonTable
diff --git a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx
index 8c90a2c9293d..f46d824ca99d 100644
--- a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx
+++ b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx
@@ -18,7 +18,7 @@ import { useColumnWidths } from '../../hooks/useColumnWidths'
import { PaginationAuto, PaginationControl, PaginationManual, usePagination } from '../PaginationControl'
import { Tooltip } from '../Tooltip'
import { BulkSelectionBar } from './BulkSelectionBar'
-import { determineColumnKey, getStickyColumnInfo } from './columnLayoutUtils'
+import { determineColumnKey, getColumnWidthCap, getStickyColumnInfo } from './columnLayoutUtils'
import { LemonTableLoader } from './LemonTableLoader'
import { Sorting, SortingIndicator, getNextSorting } from './sorting'
import { TableColumnResizeHandle } from './TableColumnResizeHandle'
@@ -503,6 +503,8 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
// Truncate only when a max width is set and the column isn't sized by its author.
const truncateHeader =
!!maxHeaderWidth && !column.width && !column.fullWidth
+ const widthCap = getColumnWidthCap(column)
+ const clipTitle = truncateHeader || !!widthCap
return (
<th
@@ -522,6 +524,7 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
/* eslint-disable-next-line react/forbid-dom-props */
style={{
textAlign: column.align,
+ ...(widthCap ? { maxWidth: widthCap } : {}),
...(isPinned ? { left: `${leftPosition}px` } : {}),
}}
>
@@ -575,7 +578,7 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
'flex items-center',
// Clip at maxWidth: sticky headers keep `overflow: visible` on the th, so
// without this an over-wide title spills across the neighbouring headers
- truncateHeader && 'overflow-hidden',
+ clipTitle && 'min-w-0 overflow-hidden',
column?.fullWidth && 'w-full',
column.sorter && 'cursor-pointer'
)}
@@ -593,7 +596,7 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
<IconInfo className="ml-1 text-base" />
</div>
</Tooltip>
- ) : truncateHeader &&
+ ) : clipTitle &&
typeof column.title === 'string' ? (
<div
className="min-w-0 truncate"
diff --git a/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx b/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx
index 753b13bf8d74..240e2063031b 100644
--- a/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx
+++ b/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx
@@ -6,7 +6,7 @@ import { IconCollapse, IconExpand } from '@posthog/icons'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { More } from 'lib/lemon-ui/LemonButton/More'
-import { getStickyColumnInfo } from './columnLayoutUtils'
+import { getColumnWidthCap, getStickyColumnInfo } from './columnLayoutUtils'
import { ExpandableConfig, LemonTableColumn, LemonTableColumnGroup, TableCellRepresentation } from './types'
export interface TableRowProps<T extends Record<string, any>> {
@@ -146,11 +146,17 @@ function TableRowRaw<T extends Record<string, any>>({
const extraCellProps =
isTableCellRepresentation(contents) && contents.props ? contents.props : {}
+ // A cell that spans several columns is not bound by the width of the one it starts in
+ const spansColumns = extraCellProps.colSpan !== undefined && extraCellProps.colSpan !== 1
+ const widthCap = spansColumns ? undefined : getColumnWidthCap(column)
return (
<td
key={`col-${columnGroupIndex}-${columnKeyOrIndex}`}
className={clsx(
columnIndex === 0 && 'LemonTable__boundary',
+ // Hold the value on one line, because a capped cell that wraps grows the row
+ // taller instead of cropping
+ widthCap && 'whitespace-nowrap',
isSticky && 'LemonTable__cell--sticky',
isColumnSticky && 'LemonTable__cell--pinned',
column.align && `text-${column.align}`,
@@ -163,6 +169,7 @@ function TableRowRaw<T extends Record<string, any>>({
...(typeof column.style === 'function'
? column.style(value as T[keyof T], record, recordIndex)
: column.style),
+ ...(widthCap ? { maxWidth: widthCap } : {}),
...(isColumnSticky ? { left: `${leftPosition}px` } : {}),
}}
{...extraCellProps}
diff --git a/frontend/src/lib/lemon-ui/LemonTable/columnLayoutUtils.ts b/frontend/src/lib/lemon-ui/LemonTable/columnLayoutUtils.ts
index 533b35a7e14f..0509af92e5c9 100644
--- a/frontend/src/lib/lemon-ui/LemonTable/columnLayoutUtils.ts
+++ b/frontend/src/lib/lemon-ui/LemonTable/columnLayoutUtils.ts
@@ -74,6 +74,17 @@ export function getStickyColumnInfo<T extends Record<string, any>>(
return { isSticky, leftPosition }
}
+/**
+ * The maximum width for the cells of a resizable column. A `<col>` width is only a hint: when the
+ * table sizes itself intrinsically, the widest cell can make the column wider than that hint. The
+ * cap holds the column at the width a person drags it to, and the content crops instead.
+ */
+export function getColumnWidthCap<T extends Record<string, any>>(
+ column: LemonTableColumn<T, any>
+): string | number | undefined {
+ return column.resizable && column.width ? column.width : undefined
+}
+
/**
* Determine the column's key, using `dataIndex` as fallback.
* If `obligationReason` is specified, will throw an error if the key can't be determined.