Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
Add support for completely arbitrary width/height
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Mar 19, 2021
1 parent 3ea5421 commit 76ba529
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/corePlugins/height.js
@@ -1,9 +1,9 @@
const { asLength, nameClass } = require('../pluginUtils')
const { asValue, nameClass } = require('../pluginUtils')

module.exports = function ({ matchUtilities, jit: { theme } }) {
matchUtilities({
h: (modifier, { theme }) => {
let value = asLength(modifier, theme['height'])
let value = asValue(modifier, theme['height'])

if (value === undefined) {
return []
Expand Down
4 changes: 2 additions & 2 deletions src/corePlugins/width.js
@@ -1,9 +1,9 @@
const { asLength, nameClass } = require('../pluginUtils')
const { asValue, nameClass } = require('../pluginUtils')

module.exports = function ({ matchUtilities, jit: { theme } }) {
matchUtilities({
w: (modifier, { theme }) => {
let value = asLength(modifier, theme['width'])
let value = asValue(modifier, theme['width'])

if (value === undefined) {
return []
Expand Down
6 changes: 3 additions & 3 deletions src/pluginUtils.js
Expand Up @@ -111,7 +111,8 @@ function asValue(modifier, lookup = {}, { validate = () => true, transform = (v)
return undefined
}

return transform(value)
// add spaces around operators inside calc() that do not follow an operator or (
return transform(value).replace(/(?<=^calc\(.+?)(?<![-+*/(])([-+*/])/g, ' $1 ')
}

function asUnit(modifier, units, lookup = {}) {
Expand All @@ -124,8 +125,7 @@ function asUnit(modifier, units, lookup = {}) {
)
},
transform: (value) => {
// add spaces around operators inside calc() that do not follow an operator or (
return value.replace(/(?<=^calc\(.+?)(?<![-+*/(])([-+*/])/g, ' $1 ')
return value
},
})
}
Expand Down
12 changes: 12 additions & 0 deletions tests/08-arbitrary-values.test.css
Expand Up @@ -16,12 +16,24 @@
.mt-\[clamp\(30px\2c 100px\)\] {
margin-top: clamp(30px, 100px);
}
.h-\[3\.23rem\] {
height: 3.23rem;
}
.h-\[calc\(100\%\+1rem\)\] {
height: calc(100% + 1rem);
}
.h-\[var\(--height\)\] {
height: var(--height);
}
.w-\[3\.23rem\] {
width: 3.23rem;
}
.w-\[calc\(100\%\+1rem\)\] {
width: calc(100% + 1rem);
}
.w-\[var\(--width\)\] {
width: var(--width);
}
.rotate-\[23deg\] {
--tw-rotate: 23deg;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/08-arbitrary-values.test.html
Expand Up @@ -16,6 +16,10 @@
<div class="border-[2.5px]"></div>
<div class="w-[3.23rem]"></div>
<div class="w-[calc(100%+1rem)]"></div>
<div class="w-[var(--width)]"></div>
<div class="h-[3.23rem]"></div>
<div class="h-[calc(100%+1rem)]"></div>
<div class="h-[var(--height)]"></div>
<div class="space-x-[20cm]"></div>
<div class="space-x-[calc(20%-1cm)]"></div>
<div class="grid-cols-[200px,repeat(auto-fill,minmax(15%,100px)),300px]"></div>
Expand Down

3 comments on commit 76ba529

@fabd
Copy link

@fabd fabd commented on 76ba529 Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this matters, at a quick glance, cf. notes https://developer.mozilla.org/en-US/docs/Web/CSS/calc()#syntax

calc(100%+1rem) in the test is not. same as calc(100% + 1rem) afaik

@adamwathan
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at the actual test output, you’ll see we add the spaces for you 👍🏻 Classes can’t have spaces so we have to transform it in the CSS.

@fabd
Copy link

@fabd fabd commented on 76ba529 Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad didn’t pay attention to the regexp. Keep up the good work :)

Please sign in to comment.