Skip to content

Commit 4328c3d

Browse files
thecrypticacephilipp-spiess
authored andcommitted
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 7e0b418 commit 4328c3d

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
@@ -10833,6 +10833,84 @@ test('bg', async () => {
1083310833
`)
1083410834
})
1083510835

10836+
test('bg-position', async () => {
10837+
expect(
10838+
await compileCss(
10839+
css`
10840+
@theme {
10841+
--color-red-500: #ef4444;
10842+
}
10843+
@tailwind utilities;
10844+
`,
10845+
['bg-position-[120px]', 'bg-position-[120px_120px]', 'bg-position-[var(--some-var)]'],
10846+
),
10847+
).toMatchInlineSnapshot(`
10848+
".bg-position-\\[120px\\] {
10849+
background-position: 120px;
10850+
}
10851+
10852+
.bg-position-\\[120px_120px\\] {
10853+
background-position: 120px 120px;
10854+
}
10855+
10856+
.bg-position-\\[var\\(--some-var\\)\\] {
10857+
background-position: var(--some-var);
10858+
}"
10859+
`)
10860+
expect(
10861+
await run([
10862+
'bg-position',
10863+
'bg-position/foo',
10864+
'-bg-position',
10865+
'-bg-position/foo',
10866+
10867+
'bg-position-[120px_120px]/foo',
10868+
10869+
'-bg-position-[120px_120px]',
10870+
'-bg-position-[120px_120px]/foo',
10871+
]),
10872+
).toEqual('')
10873+
})
10874+
10875+
test('bg-size', async () => {
10876+
expect(
10877+
await compileCss(
10878+
css`
10879+
@theme {
10880+
--color-red-500: #ef4444;
10881+
}
10882+
@tailwind utilities;
10883+
`,
10884+
['bg-size-[120px]', 'bg-size-[120px_120px]', 'bg-size-[var(--some-var)]'],
10885+
),
10886+
).toMatchInlineSnapshot(`
10887+
".bg-size-\\[120px\\] {
10888+
background-size: 120px;
10889+
}
10890+
10891+
.bg-size-\\[120px_120px\\] {
10892+
background-size: 120px 120px;
10893+
}
10894+
10895+
.bg-size-\\[var\\(--some-var\\)\\] {
10896+
background-size: var(--some-var);
10897+
}"
10898+
`)
10899+
expect(
10900+
await run([
10901+
'bg-size',
10902+
'bg-size/foo',
10903+
'-bg-size',
10904+
'-bg-size/foo',
10905+
10906+
'bg-size-[120px_120px]/foo',
10907+
10908+
'-bg-size-[120px_120px]',
10909+
'-bg-size-[120px_120px]/foo',
10910+
]),
10911+
).toEqual('')
10912+
})
10913+
1083610914
test('from', async () => {
1083710915
expect(
1083810916
await compileCss(

packages/tailwindcss/src/utilities.ts

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

23902396
staticUtility('bg-fixed', [['background-attachment', 'fixed']])
23912397
staticUtility('bg-local', [['background-attachment', 'local']])
@@ -2400,6 +2406,12 @@ export function createUtilities(theme: Theme) {
24002406
staticUtility('bg-left', [['background-position', 'left']])
24012407
staticUtility('bg-right', [['background-position', 'right']])
24022408
staticUtility('bg-center', [['background-position', 'center']])
2409+
functionalUtility('bg-position', {
2410+
handle(value) {
2411+
if (!value) return
2412+
return [decl('background-position', value)]
2413+
},
2414+
})
24032415

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

0 commit comments

Comments
 (0)