Skip to content

Commit

Permalink
Only generate positive grid-cols-* and grid-rows-* utilities (#16020
Browse files Browse the repository at this point in the history
)

This PR fixes an issue where `grid-cols-0` and `grid-rows-0` generated
invalid CSS. We now ensure that the value is any positive integer
(except 0).

Fixes: #16012
  • Loading branch information
RobinMalfait authored Jan 29, 2025
1 parent 0655209 commit ea24995
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Only generate positive `grid-cols-*` and `grid-rows-*` utilities ([#16020](https://github.com/tailwindlabs/tailwindcss/pull/16020))

## [4.0.1] - 2025-01-29

Expand Down
2 changes: 2 additions & 0 deletions packages/tailwindcss/src/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6995,6 +6995,7 @@ test('grid-cols', async () => {
expect(
await run([
'grid-cols',
'grid-cols-0',
'-grid-cols-none',
'-grid-cols-subgrid',
'grid-cols--12',
Expand Down Expand Up @@ -7043,6 +7044,7 @@ test('grid-rows', async () => {
expect(
await run([
'grid-rows',
'grid-rows-0',
'-grid-rows-none',
'-grid-rows-subgrid',
'grid-rows--12',
Expand Down
5 changes: 3 additions & 2 deletions packages/tailwindcss/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DefaultMap } from './utils/default-map'
import {
inferDataType,
isPositiveInteger,
isStrictPositiveInteger,
isValidOpacityValue,
isValidSpacingMultiplier,
} from './utils/infer-data-type'
Expand Down Expand Up @@ -1752,7 +1753,7 @@ export function createUtilities(theme: Theme) {
functionalUtility('grid-cols', {
themeKeys: ['--grid-template-columns'],
handleBareValue: ({ value }) => {
if (!isPositiveInteger(value)) return null
if (!isStrictPositiveInteger(value)) return null
return `repeat(${value}, minmax(0, 1fr))`
},
handle: (value) => [decl('grid-template-columns', value)],
Expand All @@ -1763,7 +1764,7 @@ export function createUtilities(theme: Theme) {
functionalUtility('grid-rows', {
themeKeys: ['--grid-template-rows'],
handleBareValue: ({ value }) => {
if (!isPositiveInteger(value)) return null
if (!isStrictPositiveInteger(value)) return null
return `repeat(${value}, minmax(0, 1fr))`
},
handle: (value) => [decl('grid-template-rows', value)],
Expand Down
5 changes: 5 additions & 0 deletions packages/tailwindcss/src/utils/infer-data-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,11 @@ export function isPositiveInteger(value: any) {
return Number.isInteger(num) && num >= 0 && String(num) === String(value)
}

export function isStrictPositiveInteger(value: any) {
let num = Number(value)
return Number.isInteger(num) && num > 0 && String(num) === String(value)
}

export function isValidSpacingMultiplier(value: any) {
return isMultipleOf(value, 0.25)
}
Expand Down

0 comments on commit ea24995

Please sign in to comment.