Skip to content
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

Improve arbitrary value support #5568

Merged
merged 9 commits into from
Sep 24, 2021
Merged

Conversation

RobinMalfait
Copy link
Contributor

@RobinMalfait RobinMalfait commented Sep 22, 2021

Edit: This PR is fairly big and contains multiple scopes, so I will be splitting this PR into multiple PR's!

PR 1: #5585
PR 2: #5587
PR 3: #5590
PR 4: #5588


This PR will improve arbitrary value support for various plugins. I had to introduce a few more typehints, the names can still be changed if we want to!

Here are the changes to various plugins:

Background images

Added an url typehint, that checks for the url prefix.

Input:

<div class="bg-[#0088cc]"></div>
<div class="bg-[url('/path-to-image.png')]"></div>
<div class="bg-[url:var(--maybe-a-url-at-runtime)]"></div>

Output:

.bg-\[\#0088cc\] {
    --tw-bg-opacity: 1;
    background-color: rgb(0 136 204 / var(--tw-bg-opacity))
}
.bg-\[url\(\'\/path-to-image\.png\'\)\] {
    background-image: url('/path-to-image.png')
}
.bg-\[url\:var\(--maybe-a-url-at-runtime\)\] {
    background-image: var(--maybe-a-url-at-runtime)
}

Background size & background position

Both background-size and background-position can accept length values like 200px 100px. This means that this is ambiguous, for this reason I introduced a size and position typehint.

Input:

<div class="bg-[size:200px_100px]"></div>
<div class="bg-[position:200px_100px]"></div>

Output:

.bg-\[size\:200px_100px\] {
    background-size: 200px 100px
}
.bg-\[position\:200px_100px\] {
    background-position: 200px 100px
}

Font family & font weight

Font families and font weights can be ambiguous but they aren't always. For example, font-[300] is a font-weight because you can't have a font-family of value 300. However font-[black] can both refer to font-family: black; and font-weight: black;

For this reason I introduced family and weight typehints.

Input:

<div class="font-[Georgia]"></div>
<div class="font-['Gill_Sans']"></div>
<div class="font-[300]"></div>
<div class="font-[family:var(--value)]"></div>
<div class="font-[weight:var(--value)]"></div>

Output:

.font-\[Georgia\] {
    font-family: Georgia
}
.font-\[\'Gill_Sans\'\] {
    font-family: 'Gill Sans'
}
.font-\[family\:var\(--value\)\] {
    font-family: var(--value)
}
.font-\[300\] {
    font-weight: 300
}
.font-\[weight\:var\(--value\)\] {
    font-weight: var(--value)
}

Outline

The outline plugin can be configured as an array in the config, therefore I mimicked this behaviour in the arbitrary value support for this plugin as well. The first argument will be the outline, the second argument will be the outline-offset which defaults to 0.

Input:

<div class="outline-[2px_solid_black]"></div>
<div class="outline-[2px_solid_black,2px]"></div>

Output:

.outline-\[2px_solid_black\] {
    outline: 2px solid black;
    outline-offset: 0
}
.outline-\[2px_solid_black\2c 2px\] {
    outline: 2px solid black;
    outline-offset: 2px
}

@RobinMalfait RobinMalfait changed the title improve arbitrary value support Improve arbitrary value support Sep 22, 2021
@RobinMalfait RobinMalfait force-pushed the improve-arbitrary-value-support branch 4 times, most recently from d3cb1cc to 60148c2 Compare September 22, 2021 13:29
Comment on lines 312 to 257
let idx = input.indexOf(delim)
if (idx === -1) return [undefined, input]
return [input.slice(0, idx), input.slice(idx + 1)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed this a little, because I wanted to get the [typehint, rest] and if there is no typehint, I wanted [undefined, rest]. The previous code would have generated [rest] for the last case.

@RobinMalfait RobinMalfait force-pushed the improve-arbitrary-value-support branch 7 times, most recently from db53b1b to 4194b96 Compare September 24, 2021 15:30
This will allow us to use `outline-[OUTLINE,OPTIONAL_OFFSET]`

Input:
```html
outline-[2px_solid_black]
```

Output:
```css
.outline-\[2px_solid_black\] {
  outline: 2px solid black;
  outline-offset: 0;
}
```

---

Input:
```html
outline-[2px_solid_black,2px]
```

Output:
```css
.outline-\[2px_solid_black\2c 2px\] {
  outline: 2px solid black;
  outline-offset: 2px;
}
```
Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Types

These data types will be used to "guess" the type of an arbitrary value
if there is some ambiguity going on. For example:

```
bg-[#0088cc]      -> This is a `color`  -> `background-color`
bg-[url('...')]   -> This is a `url`    -> `background-image`
```

If you are using css variables, then there is no way of knowing which
type it is referring to, in that case you can be explicit:

```
bg-[color:var(--value)]   -> This is a `color`  -> `background-color`
bg-[url:var(--value)]     -> This is a `url`    -> `background-image`
```

When you explicitly pass a data type, then we bypass the type system and
assume you are right. This is nice in a way because now we don't have to
run all of the guessing type code. On the other hand, you can introduce
runtime issues that we are not able to detect:

```
:root {
  --value: 12px;
}

/* Later... */
bg-[color:var(--value)] -> Assumes `color` -> *eventually* -> `background-color: 12px`
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant