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

feat: Allow ampersand suffixes #923

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@
]
}
}
}
},
"packageManager": "[email protected]+sha1.a428b12202bc4f23b17e6dffe730734dae5728e2"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ exports[`Fela with Fela Tools integration Rendering rules should remove undefine

exports[`Fela with Fela Tools integration Rendering rules should render any nested selector with the &-prefix 1`] = `".a{color:red}.b~#foo{color:blue}.c .bar{color:green}"`;

exports[`Fela with Fela Tools integration Rendering rules should render any nested selector with the &-suffix 1`] = `".a{color:red}.bar:hover .b{color:green}"`;

exports[`Fela with Fela Tools integration Rendering rules should render attribute selectors 1`] = `".a{color:red}.b[bool=true]{color:blue}"`;

exports[`Fela with Fela Tools integration Rendering rules should render attribute selectors and add specificity prefix 1`] = `".parent .a{color:red}.parent .b[bool=true]{color:blue}"`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ describe('Fela with Fela Tools integration', () => {
expect(renderToString(renderer)).toMatchSnapshot()
})

it('should render any nested selector with the &-suffix', () => {
const rule = () => ({
color: 'red',
'.bar:hover &': {
color: 'green',
},
})
const renderer = createRenderer()

renderer.renderRule(rule)

expect(renderToString(renderer)).toMatchSnapshot()
})

it('should render media queries', () => {
const rule = () => ({
color: 'red',
Expand Down
2 changes: 2 additions & 0 deletions packages/fela-utils/src/__tests__/isNestedSelector-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ describe('Validating nested selectors', () => {
expect(isNestedSelector('> div')).toEqual(true)
expect(isNestedSelector('& .foo')).toEqual(true)
expect(isNestedSelector('& ~ #id')).toEqual(true)
expect(isNestedSelector('.foo &')).toEqual(true)
})

it('should return false', () => {
expect(isNestedSelector('.foo')).toEqual(false)
expect(isNestedSelector(' .foo')).toEqual(false)
expect(isNestedSelector('~ #id')).toEqual(false)
expect(isNestedSelector('#id & .foo')).toEqual(false)
})
})
7 changes: 5 additions & 2 deletions packages/fela-utils/src/generateCSSSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ export default function generateCSSSelector(
className,
pseudo = '',
specificityPrefix = '',
propertyPriority = 1
propertyPriority = 1,
psuedoPrefix = false
) {
const classNameSelector = `.${className}`.repeat(propertyPriority)

if (psuedoPrefix) {
return `${specificityPrefix}${pseudo}${classNameSelector}`
}
return `${specificityPrefix}${classNameSelector}${pseudo}`
}
4 changes: 2 additions & 2 deletions packages/fela-utils/src/isNestedSelector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const regex = /^(:|\[|>|&)/
const prefix = /^(:|\[|>|&)/

export default function isNestedSelector(property) {
return regex.test(property)
return prefix.test(property) || property.endsWith('&')
}
3 changes: 3 additions & 0 deletions packages/fela-utils/src/normalizeNestedProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export default function normalizeNestedProperty(nestedProperty) {
if (nestedProperty.charAt(0) === '&') {
return nestedProperty.substr(1)
}
if (nestedProperty.endsWith('&')) {
return nestedProperty.substr(0, nestedProperty.length - 1)
}

return nestedProperty
}
27 changes: 22 additions & 5 deletions packages/fela/src/createRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ export default function createRenderer(config = {}) {
)
},

_renderStyleToClassNames(style, pseudo = '', media = '', support = '') {
_renderStyleToClassNames(
style,
pseudo = '',
media = '',
support = '',
psuedoPrefix = false
) {
let classNames = ''

const applyPlugin = (processed, plugin) => plugin(processed, renderer)
Expand All @@ -214,7 +220,8 @@ export default function createRenderer(config = {}) {
value,
pseudo + normalizeNestedProperty(property),
media,
support
support,
property.endsWith('&')
)
} else if (isMediaQuery(property)) {
const combinedMediaQuery = generateCombinedMediaQuery(
Expand Down Expand Up @@ -300,7 +307,8 @@ Check http://fela.js.org/docs/basics/Rules.html#styleobject for more information
value,
pseudo,
media,
support
support,
psuedoPrefix
)
}

Expand All @@ -316,7 +324,15 @@ Check http://fela.js.org/docs/basics/Rules.html#styleobject for more information
return classNames
},

_renderStyleToCache(reference, property, value, pseudo, media, support) {
_renderStyleToCache(
reference,
property,
value,
pseudo,
media,
support,
psuedoPrefix
) {
// we remove undefined values to enable
// usage of optional props without side-effects
if (isUndefinedValue(value)) {
Expand All @@ -336,7 +352,8 @@ Check http://fela.js.org/docs/basics/Rules.html#styleobject for more information
className,
pseudo,
config.specificityPrefix,
renderer.propertyPriority[property]
renderer.propertyPriority[property],
psuedoPrefix
)

const change = {
Expand Down
Loading