Only apply hover styles when supported (future) (#8394)

* Only apply hover styles when supported (future)

Co-Authored-By: Andrew Brown <browner12@gmail.com>

* update changelog

Co-authored-by: Andrew Brown <browner12@gmail.com>
Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
This commit is contained in:
Adam Wathan 2022-05-20 11:20:20 -04:00 • committed by GitHub
parent bb4f5dab6b
commit 128030fcfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 14 deletions

View file

@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Try using local `postcss` installation first in the CLI ([#8270](https://github.com/tailwindlabs/tailwindcss/pull/8270))
- Only apply hover styles when supported (future) ([#8394](https://github.com/tailwindlabs/tailwindcss/pull/8394))
### Added

View file

@ -14,6 +14,7 @@ import { version as tailwindVersion } from '../package.json'
import log from './util/log'
import { normalizeScreens } from './util/normalizeScreens'
import { formatBoxShadowValue, parseBoxShadowValue } from './util/parseBoxShadowValue'
import { flagEnabled } from './featureFlags'
export let variantPlugins = {
pseudoElementVariants: ({ addVariant }) => {
@ -60,14 +61,14 @@ export let variantPlugins = {
})
},
pseudoClassVariants: ({ addVariant }) => {
pseudoClassVariants: ({ addVariant, config }) => {
let pseudoVariants = [
// Positional
['first', ':first-child'],
['last', ':last-child'],
['only', ':only-child'],
['odd', ':nth-child(odd)'],
['even', ':nth-child(even)'],
['first', '&:first-child'],
['last', '&:last-child'],
['only', '&:only-child'],
['odd', '&:nth-child(odd)'],
['even', '&:nth-child(even)'],
'first-of-type',
'last-of-type',
'only-of-type',
@ -92,11 +93,11 @@ export let variantPlugins = {
}
})
return ':visited'
return '&:visited'
},
],
'target',
['open', '[open]'],
['open', '&[open]'],
// Forms
'default',
@ -116,19 +117,24 @@ export let variantPlugins = {
// Interactive
'focus-within',
'hover',
[
'hover',
!flagEnabled(config(), 'hoverOnlyWhenSupported')
? '&:hover'
: '@media (hover: hover) and (pointer: fine) { &:hover }',
],
'focus',
'focus-visible',
'active',
'enabled',
'disabled',
].map((variant) => (Array.isArray(variant) ? variant : [variant, `:${variant}`]))
].map((variant) => (Array.isArray(variant) ? variant : [variant, `&:${variant}`]))
for (let [variantName, state] of pseudoVariants) {
addVariant(variantName, (ctx) => {
let result = typeof state === 'function' ? state(ctx) : state
return `&${result}`
return result
})
}
@ -136,7 +142,7 @@ export let variantPlugins = {
addVariant(`group-${variantName}`, (ctx) => {
let result = typeof state === 'function' ? state(ctx) : state
return `:merge(.group)${result} &`
return result.replace(/&(\S+)/, ':merge(.group)$1 &')
})
}
@ -144,7 +150,7 @@ export let variantPlugins = {
addVariant(`peer-${variantName}`, (ctx) => {
let result = typeof state === 'function' ? state(ctx) : state
return `:merge(.peer)${result} ~ &`
return result.replace(/&(\S+)/, ':merge(.peer)$1 ~ &')
})
}
},

View file

@ -6,7 +6,7 @@ let defaults = {
}
let featureFlags = {
future: [],
future: ['hoverOnlyWhenSupported'],
experimental: ['optimizeUniversalDefaults'],
}

View file

@ -769,3 +769,39 @@ it('variants only picks the used selectors in a group (apply)', () => {
`)
})
})
test('hoverOnlyWhenSupported adds hover and pointer media features by default', () => {
let config = {
future: {
hoverOnlyWhenSupported: true,
},
content: [
{ raw: html`<div class="hover:underline group-hover:underline peer-hover:underline"></div>` },
],
corePlugins: { preflight: false },
}
let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`
return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
${defaults}
@media (hover: hover) and (pointer: fine) {
.hover\:underline:hover {
text-decoration-line: underline;
}
.group:hover .group-hover\:underline {
text-decoration-line: underline;
}
.peer:hover ~ .peer-hover\:underline {
text-decoration-line: underline;
}
}
`)
})
})