Skip to content

Always use 1rem = 16px for pixel equivalents in media queries #1190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 12, 2025
Merged
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
21 changes: 12 additions & 9 deletions packages/tailwindcss-language-service/src/util/pixelEquivalents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ export function addPixelEquivalentsToValue(
return value
}

function getPixelEquivalentsForMediaQuery(params: string, rootFontSize: number): Comment[] {
function getPixelEquivalentsForMediaQuery(params: string): Comment[] {
// Media queries in browsers are not affected by the font size on the `html` element but the
// initial value of font-size as provided by the browser. This is defaults to 16px universally
// so we'll always assume a value of 16px for media queries for correctness.
let rootFontSize = 16

let comments: Comment[] = []

try {
Expand All @@ -65,9 +70,9 @@ function getPixelEquivalentsForMediaQuery(params: string, rootFontSize: number):
return comments
}

export function addPixelEquivalentsToMediaQuery(query: string, rootFontSize: number): string {
export function addPixelEquivalentsToMediaQuery(query: string): string {
return query.replace(/(?<=^\s*@media\s*).*?$/, (params) => {
let comments = getPixelEquivalentsForMediaQuery(params, rootFontSize)
let comments = getPixelEquivalentsForMediaQuery(params)
return applyComments(params, comments)
})
}
Expand All @@ -88,12 +93,10 @@ export function equivalentPixelValues({
}

comments.push(
...getPixelEquivalentsForMediaQuery(atRule.params, rootFontSize).map(
({ index, value }) => ({
index: index + atRule.source.start.offset + `@media${atRule.raws.afterName}`.length,
value,
}),
),
...getPixelEquivalentsForMediaQuery(atRule.params).map(({ index, value }) => ({
index: index + atRule.source.start.offset + `@media${atRule.raws.afterName}`.length,
value,
})),
)
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Fix parsing of `@custom-variant` shorthand in Tailwind CSS language mode ([#1183](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1183))
- Make sure custom regexes apply in Vue `<script>` blocks ([#1177](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1177))
- Fix suggestion of utilities with slashes in them in v4 ([#1182](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1182))
- Assume 16px font size for `1rem` in media queries ([#1190](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1190))

## 0.14.3

Expand Down