Skip to content

Commit 5d91b93

Browse files
committed
fix: column width in list
1 parent 44aa0e5 commit 5d91b93

File tree

8 files changed

+1279
-1252
lines changed

8 files changed

+1279
-1252
lines changed

.changeset/clear-steaks-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ultraviolet/ui": minor
3+
---
4+
5+
Fix `List.Cell` width when it is in %

packages/plus/src/components/OfferList/__tests__/__snapshots__/index.test.tsx.snap

Lines changed: 336 additions & 336 deletions
Large diffs are not rendered by default.

packages/ui/src/components/List/Cell.tsx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ import type { CSSProperties, ReactNode } from 'react'
66
import { forwardRef } from 'react'
77
import { useColumnProvider } from './ColumnProvider'
88
import { listCell } from './styles.css'
9-
import { maxWidthCell, minWidthCell, widthCell } from './variables.css'
9+
import {
10+
listCellPadding,
11+
maxWidthCell,
12+
maxWidthChildrenCell,
13+
minWidthCell,
14+
minWidthChildrenCell,
15+
widthCell,
16+
widthChildrenCell,
17+
} from './variables.css'
1018

1119
type CellProps = {
1220
children?: ReactNode
@@ -18,7 +26,22 @@ type CellProps = {
1826

1927
export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
2028
({ children, className, 'data-testid': dataTestid, colSpan, style }, ref) => {
21-
const { maxWidth, minWidth, width } = useColumnProvider()
29+
const context = useColumnProvider()
30+
31+
const width = context?.width
32+
const maxWidth = context?.width
33+
const minWidth = context?.width
34+
35+
/** Remove padding from width to avoid overflow since boxSizing = 'content-box' */
36+
const widthChildren = width?.includes('%')
37+
? '100%'
38+
: `calc(${widthCell} - ${listCellPadding} - ${listCellPadding})`
39+
const maxWidthChildren = maxWidth?.includes('%')
40+
? '100%'
41+
: `calc(${maxWidth} - ${listCellPadding} - ${listCellPadding})`
42+
const minWidthChildren = minWidth?.includes('%')
43+
? '100%'
44+
: `calc(${minWidth} - ${listCellPadding} - ${listCellPadding})`
2245

2346
return (
2447
<td
@@ -28,9 +51,12 @@ export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
2851
ref={ref}
2952
style={{
3053
...assignInlineVars({
31-
[widthCell]: width,
32-
[minWidthCell]: minWidth,
33-
[maxWidthCell]: maxWidth,
54+
[widthCell]: width ?? 'auto',
55+
[minWidthCell]: minWidth ?? 'auto',
56+
[maxWidthCell]: maxWidth ?? 'none',
57+
[widthChildrenCell]: width ? widthChildren : 'auto',
58+
[maxWidthChildrenCell]: maxWidth ? maxWidthChildren : 'none',
59+
[minWidthChildrenCell]: minWidth ? minWidthChildren : 'auto',
3460
}),
3561
...style,
3662
}}

packages/ui/src/components/List/ColumnProvider.tsx

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { createContext, useContext } from 'react'
33

44
type ContextType =
55
| {
6-
width: string
7-
maxWidth: string
8-
minWidth: string
6+
width?: string
7+
maxWidth?: string
8+
minWidth?: string
99
}
1010
| undefined
1111

@@ -25,26 +25,14 @@ export const ColumnProvider = ({
2525
}: ColumnProviderProps) => (
2626
<ColumnContext.Provider
2727
value={{
28-
maxWidth: maxWidth ?? 'none',
29-
minWidth: minWidth ?? 'auto',
30-
width: width ?? 'auto',
28+
maxWidth,
29+
minWidth,
30+
width,
3131
}}
3232
>
3333
{children}
3434
</ColumnContext.Provider>
3535
)
3636

3737
// oxlint-disable-next-line react/only-export-components
38-
export const useColumnProvider = () => {
39-
const context = useContext(ColumnContext)
40-
41-
if (!context) {
42-
return {
43-
maxWidth: 'none',
44-
minWidth: 'auto',
45-
width: 'auto',
46-
}
47-
}
48-
49-
return context
50-
}
38+
export const useColumnProvider = () => useContext(ColumnContext)

0 commit comments

Comments
 (0)