tailwindcss/tests
Robin Malfait 5809c4d07c
Improve addVariant API (#5809)
* fix incorrect comment

Probably messed this up in another PR, so just a bit of cleaning.

* implement a formatVariantSelector function

This will be used to eventually simplify the addVariant API.

The idea is that it can take a list of strings that define a certain
format. Then it squashes everything to a single format how you would
expect it.

E.g.:

Input:
  - '&:hover'
  - '&:focus'
  - '.dark &'
  - ':merge(.group):hover &'
  - ':merge(.group):focus &'
Output:
  - ':merge(.group):focus:hover .dark &:focus:hover'

The API here is:
  - `&`, this means "The parent" or "The previous selector" (you can
    think of it like if you are using nested selectors)
  - `:merge(.group)`, this means insert a `.group` if it doesn't exist
    yet, but if it does exist already, then merge the new value with the
    old value. This allows us to merge group-focus, group-hover into a
    single `.group:focus:hover ...`

* add new `format`, `withRule` and `wrap` API for addVariant

* implement backwards compatibility

This will ensure that the backwards compatibility for `modifySelectors`
and direct mutations to the `container` will still work.

We will try to capture the changes made to the `rule.selector`, we will
also "backup" the existing selector. This allows us to diff the old and
new selectors and determine what actually happened.

Once we know this, we can restore the selector to the "old" selector and
add the diffed string e.g.: `.foo &`, to the `collectedFormats` as if
you called `format()` directly. This is a bunch of extra work, but it
allows us to be backwards compatible.

In the future we could also warn if you are using `modifySelectors`, but
it is going to be a little bit tricky, because usually that's
implemented by plugin authors and therefore you don't have direct
control over this. Maybe we can figure out the plugin this is used in
and change the warning somehow?

* fix incorrect test

This was clearly a bug, keyframes should not include escaped variants at
all. The reason this is here in the first place is because the nodes in
a keyframe are also "rule" nodes.

* swap the order of pseudo states

The current implementation had a strange side effect, that resulted in
incorrect class definitions. When you are combining the `:hover` and
`:focus` event, then there is no difference between `:hover:focus` and
`:focus:hover`.

However, when you use `:hover::file-selector-button` or `::file-selector-button:hover`,
then there is a big difference. In the first place, you can hover over the full file input
to apply changes to the `File selector button`.
In the second scenario you have to hover over the `File selector button` itself to apply changes.

You can think of it as function calls:
- focus(hover(text-center))

What you would expect is something like this:
`.focus\:hover\:text-center:hover:focus`, where `hover` is on the
inside, and `focus` is on the outside. However in the current
implementation this is implemented as
`.focus\:hover\:text-cener:focus:hover`

* add more variant tests for the new API

* update parallel variants tests to make use of new API

* implement core variants with new API

* simplify/cleanup existing plugin utils

We can get rid of this because we drastically simplified the new
addVariant API.

* add addVariant shorthand signature

The current API looks like this:

```js
addVariant('name', ({ format, wrap }) => {
  // Wrap in an atRule
  wrap(postcss.atRule({ name: 'media', params: '(prefers-reduced-motion: reduce)' }))

  // "Mutate" the selector, for example prepend `.dark`
  format('.dark &')
})
```

It is also pretty common to have this:
```js
addVariant('name', ({ format }) => format('.dark &'))
```
So we simplified this to:
```js
addVariant('name', '.dark &')
```

It is also pretty common to have this:
```js
addVariant('name', ({ wrap }) => wrap(postcss.atRule({ name: 'media', params: '(prefers-reduced-motion: reduce)' })))
```
So we simplified this to:
```js
addVariant('name', '@media (prefers-reduced-motion: reduce)')
```

* improve fontVariantNumeric implementation

We will use `@defaults`, so that only the resets are injected for the
utilities we actually use.

* fix typo

* allow for nested addVariant shorthand

This will allow to write something like:

```js
addVariant('name', `
  @supports (hover: hover) {
    @media (print) {
      &:hover
    }
  }
`)
// Or as a one-liner
addVariant('name', '@supports (hover: hover) { @media (print) { &:hover } }')
```

* update changelog
2021-10-18 11:26:11 +02:00
..
fixtures Remove AOT (#5340) 2021-09-01 17:13:59 +02:00
plugins Enable optimize universal defaults by default (#5635) 2021-10-06 17:45:26 +02:00
postcss-plugins/nesting Remove AOT (#5340) 2021-09-01 17:13:59 +02:00
util Remove variants related code (#5465) 2021-09-10 09:34:27 -04:00
animations.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
apply.test.css Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
apply.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
apply.test.js Update color parsing and formatting (#5442) 2021-09-09 16:15:53 +02:00
arbitrary-values.test.css Add text-decoration-color utilities (#5760) 2021-10-12 13:35:59 -04:00
arbitrary-values.test.html Add text-decoration-color utilities (#5760) 2021-10-12 13:35:59 -04:00
arbitrary-values.test.js Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
basic-usage.test.css Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
basic-usage.test.html Add text-decoration-color utilities (#5760) 2021-10-12 13:35:59 -04:00
basic-usage.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
collapse-adjacent-rules.test.css JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
collapse-adjacent-rules.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
collapse-adjacent-rules.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
color-opacity-modifiers.test.js Handle old to new config when normalizing the config (#5658) 2021-10-01 12:56:54 +02:00
color.test.js Update color parsing and formatting (#5442) 2021-09-09 16:15:53 +02:00
combined-selectors.test.js Ensure full node stays, if only a partial is used (#5617) 2021-09-28 11:54:39 +02:00
configurePlugins.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
containerPlugin.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
context-reuse.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
context-reuse.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
context-reuse.worker.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
custom-extractors.test.css Update color parsing and formatting (#5442) 2021-09-09 16:15:53 +02:00
custom-extractors.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
custom-extractors.test.js Handle old to new config when normalizing the config (#5658) 2021-10-01 12:56:54 +02:00
custom-plugins.test.js Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
custom-separator.test.css JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
custom-separator.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
custom-separator.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
custom-transformers.test.js Handle old to new config when normalizing the config (#5658) 2021-10-01 12:56:54 +02:00
customConfig.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
dark-mode.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
defaultConfig.test.js Improve public API (#5526) 2021-09-26 12:44:13 +02:00
defaultTheme.test.js Improve public API (#5526) 2021-09-26 12:44:13 +02:00
detect-nesting.test.js Warn when nesting is detected (#5489) 2021-09-16 14:13:45 +02:00
escapeClassName.test.js [WIP] Unify JIT and AOT code paths (#4188) 2021-05-07 13:56:15 -04:00
evaluateTailwindFunctions.test.js Refactor internals to decouple watch strategies and extract IO 2021-05-26 09:43:05 -04:00
experimental.test.js Enable optimize universal defaults by default (#5635) 2021-10-06 17:45:26 +02:00
extractor-edge-cases.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
flattenColorPalette.test.js [WIP] Unify JIT and AOT code paths (#4188) 2021-05-07 13:56:15 -04:00
format-variant-selector.test.js Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
getClassList.test.js Make negative values a first-class feature, rather than theme-driven (#5709) 2021-10-06 13:42:05 -04:00
import-syntax.test.css Update color parsing and formatting (#5442) 2021-09-09 16:15:53 +02:00
import-syntax.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
import-syntax.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
important-boolean.test.css JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
important-boolean.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
important-boolean.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
important-modifier-prefix.test.css Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
important-modifier-prefix.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
important-modifier-prefix.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
important-modifier.test.css Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
important-modifier.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
important-modifier.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
important-selector.test.css JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
important-selector.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
important-selector.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
kitchen-sink.test.css Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
kitchen-sink.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
kitchen-sink.test.js Improve public API (#5526) 2021-09-26 12:44:13 +02:00
layer-at-rules.test.js Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
layer-without-tailwind.test.css JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
layer-without-tailwind.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
layer-without-tailwind.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
match-components.test.js Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
minimum-impact-selector.test.js Enable optimize universal defaults by default (#5635) 2021-10-06 17:45:26 +02:00
modify-selectors.test.css JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
modify-selectors.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
modify-selectors.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
mutable.test.css JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
mutable.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
mutable.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
negated-content-ignore.test.html Fix using negated content globs (#5625) 2021-09-29 11:15:31 +01:00
negated-content-include.test.html Fix using negated content globs (#5625) 2021-09-29 11:15:31 +01:00
negated-content.test.js Fix using negated content globs (#5625) 2021-09-29 11:15:31 +01:00
negateValue.test.js Make negative values a first-class feature, rather than theme-driven (#5709) 2021-10-06 13:42:05 -04:00
negative-prefix.test.js Make negative values a first-class feature, rather than theme-driven (#5709) 2021-10-06 13:42:05 -04:00
normalize-config.test.js Handle old to new config when normalizing the config (#5658) 2021-10-01 12:56:54 +02:00
opacity.test.js Support using functions as colors when disabling color opacity utilities (#5470) 2021-09-10 09:35:15 -04:00
parallel-variants.test.js Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
parseAnimationValue.test.js ensure we don't override the name (#5602) 2021-09-26 07:44:41 -04:00
parseObjectStyles.test.js [WIP] Unify JIT and AOT code paths (#4188) 2021-05-07 13:56:15 -04:00
prefix.fn.test.css JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
prefix.fn.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
prefix.fn.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
prefix.test.css Update color parsing and formatting (#5442) 2021-09-09 16:15:53 +02:00
prefix.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
prefix.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
prefixSelector.test.js [WIP] Unify JIT and AOT code paths (#4188) 2021-05-07 13:56:15 -04:00
raw-content.test.css Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
raw-content.test.html Add scroll-behavior utilities (#5388) 2021-09-11 09:13:28 -04:00
raw-content.test.js Handle old to new config when normalizing the config (#5658) 2021-10-01 12:56:54 +02:00
relative-purge-paths.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
relative-purge-paths.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
resolve-defaults-at-rules.test.js Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
resolveConfig.test.js Make negative values a first-class feature, rather than theme-driven (#5709) 2021-10-06 13:42:05 -04:00
responsive-and-variants-atrules.test.css JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
responsive-and-variants-atrules.test.html JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
responsive-and-variants-atrules.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
safelist.test.js Pattern safelisting (#5511) 2021-09-16 14:47:41 +02:00
screenAtRule.test.js Refactor internals to decouple watch strategies and extract IO 2021-05-26 09:43:05 -04:00
syntax-lit-html.test.js Add test for lit-html syntax 2021-09-21 12:41:58 -04:00
syntax-svelte.test.js Fix svelte test 2021-09-21 12:43:44 -04:00
syntax-svelte.test.svelte Rename svelte tests 2021-09-21 12:37:49 -04:00
tailwind-screens.test.js JIT by default - move tests and make them consistent (#5374) 2021-09-03 13:48:16 +02:00
to-path.test.js Remove lodash (#5390) 2021-09-06 14:15:10 -04:00
variants.test.css Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
variants.test.html Polish match APIs (#5664) 2021-10-01 17:31:09 +02:00
variants.test.js Improve addVariant API (#5809) 2021-10-18 11:26:11 +02:00
withAlphaVariable.test.js Update color parsing and formatting (#5442) 2021-09-09 16:15:53 +02:00