Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clear-steaks-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ultraviolet/ui": minor
---

Fix `List.Cell` width when it is in %

Large diffs are not rendered by default.

36 changes: 31 additions & 5 deletions packages/ui/src/components/List/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import type { CSSProperties, ReactNode } from 'react'
import { forwardRef } from 'react'
import { useColumnProvider } from './ColumnProvider'
import { listCell } from './styles.css'
import { maxWidthCell, minWidthCell, widthCell } from './variables.css'
import {
listCellPadding,
maxWidthCell,
maxWidthChildrenCell,
minWidthCell,
minWidthChildrenCell,
widthCell,
widthChildrenCell,
} from './variables.css'

type CellProps = {
children?: ReactNode
Expand All @@ -18,7 +26,22 @@ type CellProps = {

export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
({ children, className, 'data-testid': dataTestid, colSpan, style }, ref) => {
const { maxWidth, minWidth, width } = useColumnProvider()
const context = useColumnProvider()

const width = context?.width
const maxWidth = context?.width
const minWidth = context?.width

/** Remove padding from width to avoid overflow since boxSizing = 'content-box' */
const widthChildren = width?.includes('%')
? '100%'
: `calc(${widthCell} - ${listCellPadding} - ${listCellPadding})`
const maxWidthChildren = maxWidth?.includes('%')
? '100%'
: `calc(${maxWidth} - ${listCellPadding} - ${listCellPadding})`
const minWidthChildren = minWidth?.includes('%')
? '100%'
: `calc(${minWidth} - ${listCellPadding} - ${listCellPadding})`

return (
<td
Expand All @@ -28,9 +51,12 @@ export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
ref={ref}
style={{
...assignInlineVars({
[widthCell]: width,
[minWidthCell]: minWidth,
[maxWidthCell]: maxWidth,
[widthCell]: width ?? 'auto',
[minWidthCell]: minWidth ?? 'auto',
[maxWidthCell]: maxWidth ?? 'none',
[widthChildrenCell]: width ? widthChildren : 'auto',
[maxWidthChildrenCell]: maxWidth ? maxWidthChildren : 'none',
[minWidthChildrenCell]: minWidth ? minWidthChildren : 'auto',
}),
...style,
}}
Expand Down
26 changes: 7 additions & 19 deletions packages/ui/src/components/List/ColumnProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { createContext, useContext } from 'react'

type ContextType =
| {
width: string
maxWidth: string
minWidth: string
width?: string
maxWidth?: string
minWidth?: string
}
| undefined

Expand All @@ -25,26 +25,14 @@ export const ColumnProvider = ({
}: ColumnProviderProps) => (
<ColumnContext.Provider
value={{
maxWidth: maxWidth ?? 'none',
minWidth: minWidth ?? 'auto',
width: width ?? 'auto',
maxWidth,
minWidth,
width,
}}
>
{children}
</ColumnContext.Provider>
)

// oxlint-disable-next-line react/only-export-components
export const useColumnProvider = () => {
const context = useContext(ColumnContext)

if (!context) {
return {
maxWidth: 'none',
minWidth: 'auto',
width: 'auto',
}
}

return context
}
export const useColumnProvider = () => useContext(ColumnContext)
Loading
Loading