Skip to content

Commit

Permalink
Remove color opacity when visited variant
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-rogerson committed Apr 25, 2022
1 parent e8d091b commit 8b9ea6d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
6 changes: 6 additions & 0 deletions __fixtures__/visitedOpacity/visitedOpacity.js
@@ -0,0 +1,6 @@
import tw from './macro'

tw`visited:border-red-500 visited:bg-red-500 visited:text-red-500`
tw`visited:border-red-500/20 visited:bg-red-500/20 visited:text-red-500/20`
tw`visited:border-red-500/[20] visited:bg-red-500/[20] visited:text-red-500/[20]`
tw`visited:(border-red-500 border-opacity-50) visited:(bg-red-500 bg-opacity-50) visited:(text-red-500 text-opacity-50)`
46 changes: 46 additions & 0 deletions __snapshots__/plugin.test.js.snap
Expand Up @@ -59663,6 +59663,52 @@ tw\`invisible\`
})


`;

exports[`twin.macro visitedOpacity.js: visitedOpacity.js 1`] = `

import tw from './macro'

tw\`visited:border-red-500 visited:bg-red-500 visited:text-red-500\`
tw\`visited:border-red-500/20 visited:bg-red-500/20 visited:text-red-500/20\`
tw\`visited:border-red-500/[20] visited:bg-red-500/[20] visited:text-red-500/[20]\`
tw\`visited:(border-red-500 border-opacity-50) visited:(bg-red-500 bg-opacity-50) visited:(text-red-500 text-opacity-50)\`

↓ ↓ ↓ ↓ ↓ ↓

;({
':visited': {
borderColor: '#ef4444',
backgroundColor: '#ef4444',
color: '#ef4444',
},
})
;({
':visited': {
borderColor: '#ef4444',
backgroundColor: '#ef4444',
color: '#ef4444',
},
})
;({
':visited': {
borderColor: '#ef4444',
backgroundColor: '#ef4444',
color: '#ef4444',
},
})
;({
':visited': {
borderColor: '#ef4444',
'--tw-border-opacity': '0.5',
backgroundColor: '#ef4444',
color: '#ef4444',
'--tw-text-opacity': '0.5',
'--tw-bg-opacity': '0.5',
},
})


`;

exports[`twin.macro whitespace.js: whitespace.js 1`] = `
Expand Down
8 changes: 1 addition & 7 deletions src/coerced.js
@@ -1,7 +1,6 @@
import deepMerge from 'lodash.merge'
import {
throwIf,
withAlpha,
toAlpha,
splitOnFirst,
isSpaceSeparatedColor,
Expand Down Expand Up @@ -50,12 +49,7 @@ const coercedTypeMap = {
.map(p =>
typeof value === 'string' && value.startsWith('var(')
? null
: withAlpha({
color: value,
property: p,
pieces,
...(variable && { variable }),
})
: toAlpha({ pieces, variable, property: p })(value, pieces.alpha)
)
.filter(Boolean)

Expand Down
8 changes: 3 additions & 5 deletions src/pieces.js
Expand Up @@ -28,10 +28,7 @@ const fullVariantConfig = variantConfig({
createPeer,
})

/**
* Validate variants against the variants config key
*/
const validateVariants = ({ variants, state, ...rest }) => {
const getVariants = ({ variants, state, ...rest }) => {
if (!variants) return []

const screens = get(state.config, ['theme', 'screens'])
Expand Down Expand Up @@ -124,7 +121,7 @@ const splitVariants = ({ classNameRaw, state }) => {
const hasGroupVariant = variantsList.some(v => v.startsWith('group-'))

// Match the filtered variants
const variants = validateVariants({
const variants = getVariants({
variants: variantsList,
state,
hasDarkVariant,
Expand All @@ -140,6 +137,7 @@ const splitVariants = ({ classNameRaw, state }) => {
className,
variants,
hasVariants,
hasVariantVisited: variants.includes(':visited'),
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/utils/alpha.js
Expand Up @@ -21,11 +21,15 @@ const maybeAddAlpha = (value, { pieces, variable = '' }) =>
const toAlpha =
({ pieces, property, variable }) =>
(color, alpha, fallBackColor) => {
const newPieces = alpha ? { ...pieces, alpha, hasAlpha: true } : pieces
const newPieces =
(pieces.hasVariantVisited && { ...pieces, alpha: '', hasAlpha: false }) ||
(alpha && { ...pieces, alpha, hasAlpha: true }) ||
pieces

return withAlpha({
color,
property,
variable,
...(!pieces.hasVariantVisited && { variable }),
pieces: newPieces,
fallBackColor,
})
Expand Down

0 comments on commit 8b9ea6d

Please sign in to comment.