Skip to content

Commit ffd2d90

Browse files
committed
fix: column width in list
1 parent c6de62e commit ffd2d90

File tree

5 files changed

+52
-20
lines changed

5 files changed

+52
-20
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/ui/src/components/List/Cell.tsx

Lines changed: 26 additions & 4 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
@@ -20,6 +28,17 @@ export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
2028
({ children, className, 'data-testid': dataTestid, colSpan, style }, ref) => {
2129
const { maxWidth, minWidth, width } = useColumnProvider()
2230

31+
/** Remove padding from width to avoid overflow since boxSizing = 'content-box' */
32+
const widthChildren = width?.includes('%')
33+
? '100%'
34+
: `calc(${widthCell} - ${listCellPadding} - ${listCellPadding})`
35+
const maxWidthChildren = maxWidth?.includes('%')
36+
? '100%'
37+
: `calc(${maxWidth} - ${listCellPadding} - ${listCellPadding})`
38+
const minWidthChildren = minWidth?.includes('%')
39+
? '100%'
40+
: `calc(${minWidth} - ${listCellPadding} - ${listCellPadding})`
41+
2342
return (
2443
<td
2544
className={cn(className, listCell)}
@@ -28,9 +47,12 @@ export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
2847
ref={ref}
2948
style={{
3049
...assignInlineVars({
31-
[widthCell]: width,
32-
[minWidthCell]: minWidth,
33-
[maxWidthCell]: maxWidth,
50+
[widthCell]: width ?? 'auto',
51+
[minWidthCell]: minWidth ?? 'auto',
52+
[maxWidthCell]: maxWidth ?? 'none',
53+
[widthChildrenCell]: width ? widthChildren : 'auto',
54+
[maxWidthChildrenCell]: maxWidth ? maxWidthChildren : 'none',
55+
[minWidthChildrenCell]: minWidth ? minWidthChildren : 'auto',
3456
}),
3557
...style,
3658
}}

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

Lines changed: 7 additions & 11 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,9 +25,9 @@ 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}
@@ -39,11 +39,7 @@ export const useColumnProvider = () => {
3939
const context = useContext(ColumnContext)
4040

4141
if (!context) {
42-
return {
43-
maxWidth: 'none',
44-
minWidth: 'auto',
45-
width: 'auto',
46-
}
42+
throw new Error('List.Cell should be inside a List to work properly.')
4743
}
4844

4945
return context

packages/ui/src/components/List/styles.css.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import { SELECTABLE_CHECKBOX_SIZE } from './constants'
99
import { recipe } from '@vanilla-extract/recipes'
1010
import { SENTIMENTS } from '../../theme'
1111
import {
12+
listCellPadding,
1213
maxWidthCell,
14+
maxWidthChildrenCell,
1315
maxWidthHeaderCell,
1416
minWidthCell,
17+
minWidthChildrenCell,
1518
minWidthHeaderCell,
1619
paddingExpandableCell,
1720
widthCell,
21+
widthChildrenCell,
1822
widthHeaderCell,
1923
} from './variables.css'
2024

@@ -27,8 +31,6 @@ const colorChange = keyframes({
2731
},
2832
})
2933

30-
const listCellPadding = theme.space[2]
31-
3234
function makeRowStyleSentiment(sentiment: (typeof SENTIMENTS)[number]) {
3335
const color = theme.colors[sentiment]
3436
const base = {
@@ -234,9 +236,9 @@ export const listCell = style({
234236

235237
globalStyle(`${listCell} > *`, {
236238
/** Remove padding from width to avoid overflow since boxSizing = 'content-box' */
237-
width: `calc(${widthCell} - ${listCellPadding} - ${listCellPadding})`,
238-
maxWidth: `calc(${maxWidthCell} - ${listCellPadding} - ${listCellPadding})`,
239-
minWidth: `calc(${minWidthCell} - ${listCellPadding} - ${listCellPadding})`,
239+
width: widthChildrenCell,
240+
maxWidth: maxWidthChildrenCell,
241+
minWidth: minWidthChildrenCell,
240242
})
241243

242244
globalStyle(`${listRowBase} > td:first-child`, {

packages/ui/src/components/List/variables.css.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { theme } from '@ultraviolet/themes'
12
import { createVar } from '@vanilla-extract/css'
23

34
export const widthHeaderCell = createVar()
@@ -8,3 +9,9 @@ export const paddingExpandableCell = createVar()
89
export const widthCell = createVar()
910
export const maxWidthCell = createVar()
1011
export const minWidthCell = createVar()
12+
13+
export const widthChildrenCell = createVar()
14+
export const maxWidthChildrenCell = createVar()
15+
export const minWidthChildrenCell = createVar()
16+
17+
export const listCellPadding = theme.space[2]

0 commit comments

Comments
 (0)