Skip to content

Commit d7f3bd2

Browse files
authored
fix: important with state specificity (#585)
1 parent af3e7bf commit d7f3bd2

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

packages/uniwind/src/core/native/store.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,17 @@ class UniwindStoreBuilder {
147147
for (const [property, valueGetter] of style.entries) {
148148
const previousBest = bestBreakpoints.get(property)
149149

150-
if (
151-
previousBest
152-
&& (
153-
previousBest.minWidth > style.minWidth
150+
if (previousBest) {
151+
const previousWins = previousBest.minWidth > style.minWidth
154152
|| previousBest.complexity > style.complexity
155-
|| previousBest.importantProperties.includes(property)
156-
)
157-
) {
158-
continue
153+
|| (
154+
previousBest.complexity === style.complexity
155+
&& previousBest.importantProperties.includes(property)
156+
)
157+
158+
if (previousWins) {
159+
continue
160+
}
159161
}
160162

161163
if (property[0] === '-') {

packages/uniwind/tests/native/styles-parsing/specificity.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { fireEvent } from '@testing-library/react-native'
12
import * as React from 'react'
3+
import TextInput from '../../../src/components/native/TextInput'
24
import View from '../../../src/components/native/View'
35
import { TW_BLUE_500, TW_GREEN_500, TW_RED_500 } from '../../consts'
46
import { renderUniwind } from '../utils'
@@ -38,4 +40,31 @@ describe('Specificity', () => {
3840
expect(getStylesFromId('important-middle').color).toBe(TW_GREEN_500)
3941
expect(getStylesFromId('important-last').color).toBe(TW_BLUE_500)
4042
})
43+
44+
test('Important with state variant', () => {
45+
const { getByTestId, getStylesFromId } = renderUniwind(
46+
<TextInput
47+
className="bg-red-500! focus:bg-blue-500!"
48+
testID="text-input-focus"
49+
/>,
50+
)
51+
52+
const component = getByTestId('text-input-focus')
53+
const initialStyle = getStylesFromId('text-input-focus')
54+
55+
// Initial state
56+
expect(initialStyle.backgroundColor).toBe(TW_RED_500)
57+
58+
// Focus
59+
fireEvent(component, 'focus')
60+
61+
const focusedStyle = getStylesFromId('text-input-focus')
62+
expect(focusedStyle.backgroundColor).toBe(TW_BLUE_500)
63+
64+
// Blur
65+
fireEvent(component, 'blur')
66+
67+
const blurredStyle = getStylesFromId('text-input-focus')
68+
expect(blurredStyle.backgroundColor).toBe(TW_RED_500)
69+
})
4170
})

0 commit comments

Comments
 (0)