Skip to content

Commit 3412a96

Browse files
Add bg-{position,size}-* utilities (#17432)
This PR adds `bg-{position,size}-*` utilities that support arbitrary values. This exist as the new preferred version of something like this: ``` bg-[120px_120px] ``` Is it size or position (hint: it's the 2nd one). To override it you'd have to provide a data type: ``` bg-[size:120px_120px] ``` Now you can be more explicit and have better intellisense by writing these: ``` bg-position-[120px_120px] bg-size-[120px_120px] ```
1 parent ab868c6 commit 3412a96

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- _Experimental_: Add `@source inline(…)` ([#17147](https://github.com/tailwindlabs/tailwindcss/pull/17147))
2222
- _Experimental_: Add `@source not` ([#17255](https://github.com/tailwindlabs/tailwindcss/pull/17255))
2323
- Added new `bg-{top,bottom}-{left,right}` utilities ([#17378](https://github.com/tailwindlabs/tailwindcss/pull/17378))
24+
- Added new `bg-{position,size}-*` utilities for arbitrary values ([#17432](https://github.com/tailwindlabs/tailwindcss/pull/17432))
2425

2526
### Fixed
2627

packages/tailwindcss/src/utilities.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10809,6 +10809,84 @@ test('bg', async () => {
1080910809
`)
1081010810
})
1081110811

10812+
test('bg-position', async () => {
10813+
expect(
10814+
await compileCss(
10815+
css`
10816+
@theme {
10817+
--color-red-500: #ef4444;
10818+
}
10819+
@tailwind utilities;
10820+
`,
10821+
['bg-position-[120px]', 'bg-position-[120px_120px]', 'bg-position-[var(--some-var)]'],
10822+
),
10823+
).toMatchInlineSnapshot(`
10824+
".bg-position-\\[120px\\] {
10825+
background-position: 120px;
10826+
}
10827+
10828+
.bg-position-\\[120px_120px\\] {
10829+
background-position: 120px 120px;
10830+
}
10831+
10832+
.bg-position-\\[var\\(--some-var\\)\\] {
10833+
background-position: var(--some-var);
10834+
}"
10835+
`)
10836+
expect(
10837+
await run([
10838+
'bg-position',
10839+
'bg-position/foo',
10840+
'-bg-position',
10841+
'-bg-position/foo',
10842+
10843+
'bg-position-[120px_120px]/foo',
10844+
10845+
'-bg-position-[120px_120px]',
10846+
'-bg-position-[120px_120px]/foo',
10847+
]),
10848+
).toEqual('')
10849+
})
10850+
10851+
test('bg-size', async () => {
10852+
expect(
10853+
await compileCss(
10854+
css`
10855+
@theme {
10856+
--color-red-500: #ef4444;
10857+
}
10858+
@tailwind utilities;
10859+
`,
10860+
['bg-size-[120px]', 'bg-size-[120px_120px]', 'bg-size-[var(--some-var)]'],
10861+
),
10862+
).toMatchInlineSnapshot(`
10863+
".bg-size-\\[120px\\] {
10864+
background-size: 120px;
10865+
}
10866+
10867+
.bg-size-\\[120px_120px\\] {
10868+
background-size: 120px 120px;
10869+
}
10870+
10871+
.bg-size-\\[var\\(--some-var\\)\\] {
10872+
background-size: var(--some-var);
10873+
}"
10874+
`)
10875+
expect(
10876+
await run([
10877+
'bg-size',
10878+
'bg-size/foo',
10879+
'-bg-size',
10880+
'-bg-size/foo',
10881+
10882+
'bg-size-[120px_120px]/foo',
10883+
10884+
'-bg-size-[120px_120px]',
10885+
'-bg-size-[120px_120px]/foo',
10886+
]),
10887+
).toEqual('')
10888+
})
10889+
1081210890
test('from', async () => {
1081310891
expect(
1081410892
await compileCss(

packages/tailwindcss/src/utilities.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,12 @@ export function createUtilities(theme: Theme) {
23852385
staticUtility('bg-auto', [['background-size', 'auto']])
23862386
staticUtility('bg-cover', [['background-size', 'cover']])
23872387
staticUtility('bg-contain', [['background-size', 'contain']])
2388+
functionalUtility('bg-size', {
2389+
handle(value) {
2390+
if (!value) return
2391+
return [decl('background-size', value)]
2392+
},
2393+
})
23882394

23892395
staticUtility('bg-fixed', [['background-attachment', 'fixed']])
23902396
staticUtility('bg-local', [['background-attachment', 'local']])
@@ -2399,6 +2405,12 @@ export function createUtilities(theme: Theme) {
23992405
staticUtility('bg-left', [['background-position', 'left']])
24002406
staticUtility('bg-right', [['background-position', 'right']])
24012407
staticUtility('bg-center', [['background-position', 'center']])
2408+
functionalUtility('bg-position', {
2409+
handle(value) {
2410+
if (!value) return
2411+
return [decl('background-position', value)]
2412+
},
2413+
})
24022414

24032415
staticUtility('bg-repeat', [['background-repeat', 'repeat']])
24042416
staticUtility('bg-no-repeat', [['background-repeat', 'no-repeat']])

0 commit comments

Comments
 (0)