Skip to content

Commit

Permalink
fix(makeStyles): handle comma separated selectors (#20348)
Browse files Browse the repository at this point in the history
* fix(makeStyles): handle comma separated selectors

* Change files

* fix fmt

* add comments

* update tests
  • Loading branch information
layershifter committed Oct 26, 2021
1 parent b5b7401 commit c6bc2b4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
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"`);
});

it('handles multiple 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

0 comments on commit c6bc2b4

Please sign in to comment.