Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add container utility that mimics the v3 container component
  • Loading branch information
philipp-spiess committed Nov 13, 2024
commit 8f159f4b06b5e352130c9a2c00ed877dafff7588
Original file line number Diff line number Diff line change
Expand Up @@ -3375,6 +3375,7 @@ exports[`getClassList 1`] = `
"contain-size",
"contain-strict",
"contain-style",
"container",
"content-around",
"content-baseline",
"content-between",
Expand Down
2 changes: 2 additions & 0 deletions packages/tailwindcss/src/property-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export default [
'float',
'clear',

'--tw-container-component',

// How do we make `mx-0` come before `mt-0`?
// Idea: `margin-x` property that we compile away with a Visitor plugin?
'margin',
Expand Down
164 changes: 163 additions & 1 deletion packages/tailwindcss/src/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3148,6 +3148,168 @@ test('max-height', async () => {
).toEqual('')
})

describe('container', () => {
test('creates the right media queries and sorts it before width', async () => {
expect(
await compileCss(
css`
@theme {
--breakpoint-sm: 40rem;
--breakpoint-md: 48rem;
--breakpoint-lg: 64rem;
--breakpoint-xl: 80rem;
--breakpoint-2xl: 96rem;
}
@tailwind utilities;
`,
['w-1/2', 'container', 'max-w-[var(--breakpoint-sm)]'],
),
).toMatchInlineSnapshot(`
":root {
--breakpoint-sm: 40rem;
--breakpoint-md: 48rem;
--breakpoint-lg: 64rem;
--breakpoint-xl: 80rem;
--breakpoint-2xl: 96rem;
}

.container {
width: 100%;
}

@media (width >= 40rem) {
.container {
max-width: 40rem;
}
}

@media (width >= 48rem) {
.container {
max-width: 48rem;
}
}

@media (width >= 64rem) {
.container {
max-width: 64rem;
}
}

@media (width >= 80rem) {
.container {
max-width: 80rem;
}
}

@media (width >= 96rem) {
.container {
max-width: 96rem;
}
}

.w-1\\/2 {
width: 50%;
}

.max-w-\\[var\\(--breakpoint-sm\\)\\] {
max-width: var(--breakpoint-sm);
}"
`)
})

test('custom `@utility container` always follow the core utility ', async () => {
expect(
await compileCss(
css`
@theme {
--breakpoint-sm: 40rem;
--breakpoint-md: 48rem;
--breakpoint-lg: 64rem;
--breakpoint-xl: 80rem;
--breakpoint-2xl: 96rem;
}
@tailwind utilities;

@utility container {
margin-left: auto;
margin-right: auto;
padding-right: 2rem;
padding-left: 2rem;

@media (min-width: 126rem) {
max-width: 126rem;
}
}
`,
['w-1/2', 'container', 'max-w-[var(--breakpoint-sm)]'],
),
).toMatchInlineSnapshot(`
":root {
--breakpoint-sm: 40rem;
--breakpoint-md: 48rem;
--breakpoint-lg: 64rem;
--breakpoint-xl: 80rem;
--breakpoint-2xl: 96rem;
}

.container {
width: 100%;
}

@media (width >= 40rem) {
.container {
max-width: 40rem;
}
}

@media (width >= 48rem) {
.container {
max-width: 48rem;
}
}

@media (width >= 64rem) {
.container {
max-width: 64rem;
}
}

@media (width >= 80rem) {
.container {
max-width: 80rem;
}
}

@media (width >= 96rem) {
.container {
max-width: 96rem;
}
}

.container {
margin-left: auto;
margin-right: auto;
padding-left: 2rem;
padding-right: 2rem;
}

@media (width >= 126rem) {
.container {
max-width: 126rem;
}
}

.w-1\\/2 {
width: 50%;
}

.max-w-\\[var\\(--breakpoint-sm\\)\\] {
max-width: var(--breakpoint-sm);
}"
`)
})
})

test('flex', async () => {
expect(
await run([
Expand Down Expand Up @@ -16680,7 +16842,7 @@ describe('spacing utilities', () => {
`)
})

test('only multiples of 0.25 with no trailing zeroes are supported with the spacing multipler', async () => {
test('only multiples of 0.25 with no trailing zeroes are supported with the spacing multiplier', async () => {
let { build } = await compile(css`
@theme {
--spacing: 4px;
Expand Down
12 changes: 12 additions & 0 deletions packages/tailwindcss/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,18 @@ export function createUtilities(theme: Theme) {
})
}

utilities.static('container', () => {
let breakpoints = [...theme.namespace('--breakpoint').values()]
breakpoints.sort((a, b) => parseInt(a) - parseInt(b))

let decls: AstNode[] = [decl('--tw-sort', '--tw-container-component'), decl('width', '100%')]
for (let breakpoint of breakpoints) {
decls.push(atRule('@media', `(min-width: ${breakpoint})`, [decl('max-width', breakpoint)]))
}

return decls
})

/**
* @css `flex`
*/
Expand Down