Skip to content

Commit

Permalink
fixes for CSS priority
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Dec 16, 2020
1 parent 9cf4fb6 commit 7c06620
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
5 changes: 4 additions & 1 deletion packages/make-styles/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": ["plugin:@fluentui/eslint-plugin/react"],
"root": true
"root": true,
"rules": {
"@typescript-eslint/naming-convention": "off"
}
}
4 changes: 1 addition & 3 deletions packages/make-styles/src/runtime/compileCSS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ export function compileCSS(options: CompileCSSOptions): string {
// https://github.com/thysultan/stylis.js/issues/252
if (pseudo.indexOf(':global(') === 0) {
const globalSelector = /global\((.+)\)/.exec(pseudo)?.[1];
const classNameSelector = repeatSelector(`.${className}`, unstable_cssPriority);

const shouldIncludeClassName = pseudo.indexOf('&') === pseudo.length - 1;

const cssRule = shouldIncludeClassName
? `${globalSelector} { ${classNameSelector} ${cssDeclaration} }`
? `${globalSelector} { .${className} ${cssDeclaration} }`
: `${globalSelector} ${cssDeclaration}`;

return serialize(compile(cssRule), middleware([stringify]));
Expand Down
8 changes: 0 additions & 8 deletions packages/make-styles/src/runtime/resolveStyleRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,6 @@ describe('resolveStyleRules', () => {
`);
});

it('allows to increase for globals', () => {
expect(resolveStyleRules({ ':global &': { color: 'red' } }, 1)).toMatchInlineSnapshot(`
:global .fluno3u1.fluno3u1 {
color: red;
}
`);
});

it('allows to increase for RTL', () => {
expect(resolveStyleRules({ left: '5px' }, 1)).toMatchInlineSnapshot(`
.f5b3q4t1.f5b3q4t1 {
Expand Down
14 changes: 8 additions & 6 deletions packages/make-styles/src/runtime/resolveStyleRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import { normalizeNestedProperty } from './utils/normalizeNestedProperty';

export function resolveStyleRules(
styles: MakeStyles,
unstable_cssPriority: number = 0,
pseudo = '',
media = '',
support = '',
unstable_cssPriority: number = 0,
result: Record<string, MakeStylesResolvedRule> = {},
): Record<string, MakeStylesResolvedRule> {
const expandedStyles = (expand(styles) as unknown) as MakeStyles;
Expand All @@ -36,7 +36,9 @@ export function resolveStyleRules(
const key = pseudo + media + support + property;

// trimming of values is required to generate consistent hashes
const className = HASH_PREFIX + hashString(pseudo + media + support + property + value.toString().trim());
const classNameHash = hashString(pseudo + media + support + property + value.toString().trim());
const className = HASH_PREFIX + classNameHash + (unstable_cssPriority === 0 ? '' : unstable_cssPriority);

const css = compileCSS({
className,
media,
Expand Down Expand Up @@ -70,20 +72,20 @@ export function resolveStyleRules(
if (isNestedSelector(property)) {
resolveStyleRules(
value,
unstable_cssPriority,
pseudo + normalizeNestedProperty(property),
media,
support,
unstable_cssPriority,
result,
);
} else if (isMediaQuerySelector(property)) {
const combinedMediaQuery = generateCombinedQuery(media, property.slice(6).trim());

resolveStyleRules(value, pseudo, combinedMediaQuery, support, unstable_cssPriority, result);
resolveStyleRules(value, unstable_cssPriority, pseudo, combinedMediaQuery, support, result);
} else if (isSupportQuerySelector(property)) {
const combinedSupportQuery = generateCombinedQuery(support, property.slice(9).trim());

resolveStyleRules(value, pseudo, media, combinedSupportQuery, unstable_cssPriority, result);
resolveStyleRules(value, unstable_cssPriority, pseudo, media, combinedSupportQuery, result);
} else if (property === 'animationName') {
// TODO: support RTL!
const keyframe = compileKeyframeRule(value);
Expand All @@ -94,7 +96,7 @@ export function resolveStyleRules(

result[animationName] = [animationName, keyframeCSS /* rtlCSS */];

resolveStyleRules({ animationName }, pseudo, media, support, unstable_cssPriority, result);
resolveStyleRules({ animationName }, unstable_cssPriority, pseudo, media, support, result);
}
}
});
Expand Down

0 comments on commit 7c06620

Please sign in to comment.