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

fix(makeStyles): handle comma separated selectors #20348

Merged
merged 5 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "fix(makeStyles): handle comma separated selectors",
"packageName": "@fluentui/make-styles",
"email": "olfedias@microsoft.com",
"dependentChangeType": "patch"
}
22 changes: 21 additions & 1 deletion packages/make-styles/src/runtime/compileCSS.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { compileCSS, CompileCSSOptions } from './compileCSS';
import { compileCSS, CompileCSSOptions, normalizePseudoSelector } from './compileCSS';

const defaultOptions: Pick<
CompileCSSOptions,
Expand Down Expand Up @@ -155,3 +155,23 @@ describe('compileCSS', () => {
});
});
});

describe('normalizePseudoSelector', () => {
it('handles basic ', () => {
expect(normalizePseudoSelector(':hover')).toMatchInlineSnapshot(`"&:hover"`);
});

it('handles spacing', () => {
expect(normalizePseudoSelector(' :hover')).toMatchInlineSnapshot(`"& :hover"`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, does the spacing have any impact on the final result ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

it('handles multiple pseudos', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test has the same name as the one under it

Suggested change
it('handles multiple pseudos', () => {
it('handles comma separated pseudos', () => {

expect(normalizePseudoSelector(':focus:hover')).toMatchInlineSnapshot(`"&:focus:hover"`);
});

it('handles comma separated pseudos', () => {
expect(normalizePseudoSelector('& :hover, & :focus')).toMatchInlineSnapshot(`"& :hover, & :focus"`);
expect(normalizePseudoSelector(':focus,:hover')).toMatchInlineSnapshot(`"&:focus,&:hover"`);
expect(normalizePseudoSelector(':focus, :hover')).toMatchInlineSnapshot(`"&:focus,& :hover"`);
});
});
28 changes: 26 additions & 2 deletions packages/make-styles/src/runtime/compileCSS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,32 @@ export interface CompileCSSOptions {
rtlValue?: number | string;
}

const PSEUDO_SELECTOR_REGEX = /,( *[^ &])/g;

function repeatSelector(selector: string, times: number) {
return new Array(times + 2).join(selector);
}

/**
* Normalizes pseudo selectors to always contain &, requires to work properly with comma-separated selectors.
*
* @example
* ":hover" => "&:hover"
* " :hover" => "& :hover"
* ":hover,:focus" => "&:hover,&:focus"
* " :hover, :focus" => "& :hover,& :focus"
*/
export function normalizePseudoSelector(pseudoSelector: string): string {
return (
'&' +
normalizeNestedProperty(
// Regex there replaces a comma, spaces and an ampersand if it's present with comma and an ampersand.
// This allows to normalize input, see examples in JSDoc.
pseudoSelector.replace(PSEUDO_SELECTOR_REGEX, ',&$1'),
)
);
}

export function compileCSSRules(cssRules: string): string[] {
const rules: string[] = [];

Expand Down Expand Up @@ -86,10 +108,12 @@ export function compileCSS(options: CompileCSSOptions): [string /* ltr definitio

cssRule = `${globalSelector} { ${ltrRule}; ${rtlRule} }`;
} else {
cssRule = `${classNameSelector}${pseudo} ${cssDeclaration};`;
const normalizedPseudo = normalizePseudoSelector(pseudo);

cssRule = `${classNameSelector}{${normalizedPseudo} ${cssDeclaration}};`;

if (rtlProperty) {
cssRule = `${cssRule}; ${rtlClassNameSelector}${pseudo} ${rtlCSSDeclaration};`;
cssRule = `${cssRule}; ${rtlClassNameSelector}${normalizedPseudo} ${rtlCSSDeclaration};`;
}
}

Expand Down