Skip to content

Commit

Permalink
breaking: remove "unstable_cssPriority" from makeStyles() (#21263)
Browse files Browse the repository at this point in the history
* breaking: remove "unstable_cssPriority" from makeStyles()

* Change files

* update API

* adopt API changes from makeStyles

* Change files

* remove wrapping functions

* update changelog entry
  • Loading branch information
layershifter authored Jan 13, 2022
1 parent 5a51c79 commit fc9a2a4
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 144 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "makeStyles: remove \"unstable_cssPriority\"",
"packageName": "@fluentui/babel-make-styles",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "breaking: remove \"unstable_cssPriority\" from makeStyles()",
"packageName": "@fluentui/make-styles",
"email": "[email protected]",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion packages/babel-make-styles/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ export const plugin = declare<Partial<BabelPluginOptions>, PluginObj<BabelPlugin
}

const stylesBySlots: Record<string /* slot */, MakeStylesStyle> = evaluationResult.value;
const [classnamesMapping, cssRules] = resolveStyleRulesForSlots(stylesBySlots, 0);
const [classnamesMapping, cssRules] = resolveStyleRulesForSlots(stylesBySlots);

// TODO: find a better way to replace arguments
callExpressionPath.node.arguments = [astify(classnamesMapping), astify(dedupeCSSRules(cssRules))];
Expand Down
6 changes: 3 additions & 3 deletions packages/make-styles/etc/make-styles.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export type MakeStaticStylesStyle = {
// Warning: (ae-forgotten-export) The symbol "StylesBySlots" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export function makeStyles<Slots extends string | number>(stylesBySlots: StylesBySlots<Slots>, unstable_cssPriority?: number): (options: MakeStylesOptions) => Record<Slots, string>;
export function makeStyles<Slots extends string | number>(stylesBySlots: StylesBySlots<Slots>): (options: MakeStylesOptions) => Record<Slots, string>;

// Warning: (ae-forgotten-export) The symbol "MakeStylesCSSObjectCustomL1" needs to be exported by the entry point index.d.ts
//
Expand Down Expand Up @@ -137,10 +137,10 @@ export function resolveProxyValues<T>(value: T): T;
// Warning: (ae-internal-missing-underscore) The name "resolveStyleRules" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal
export function resolveStyleRules(styles: MakeStylesStyle, unstable_cssPriority?: number): [CSSClassesMap, CSSRulesByBucket];
export function resolveStyleRules(styles: MakeStylesStyle, pseudo?: string, media?: string, support?: string, cssClassesMap?: CSSClassesMap, cssRulesByBucket?: CSSRulesByBucket, rtlValue?: string): [CSSClassesMap, CSSRulesByBucket];

// @public
export function resolveStyleRulesForSlots<Slots extends string | number>(stylesBySlots: StylesBySlots<Slots>, unstable_cssPriority: number): [CSSClassesMapBySlot<Slots>, CSSRulesByBucket];
export function resolveStyleRulesForSlots<Slots extends string | number>(stylesBySlots: StylesBySlots<Slots>): [CSSClassesMapBySlot<Slots>, CSSRulesByBucket];

// Warning: (ae-internal-missing-underscore) The name "SEQUENCE_HASH_LENGTH" should be prefixed with an underscore because the declaration is marked as @internal
//
Expand Down
7 changes: 2 additions & 5 deletions packages/make-styles/src/makeStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { reduceToClassNameForSlots } from './runtime/reduceToClassNameForSlots';
import { resolveStyleRulesForSlots } from './resolveStyleRulesForSlots';
import { CSSClassesMapBySlot, CSSRulesByBucket, MakeStylesOptions, StylesBySlots } from './types';

export function makeStyles<Slots extends string | number>(
stylesBySlots: StylesBySlots<Slots>,
unstable_cssPriority: number = 0,
) {
export function makeStyles<Slots extends string | number>(stylesBySlots: StylesBySlots<Slots>) {
const insertionCache: Record<string, boolean> = {};

let classesMapBySlot: CSSClassesMapBySlot<Slots> | null = null;
Expand All @@ -18,7 +15,7 @@ export function makeStyles<Slots extends string | number>(
const { dir, renderer } = options;

if (classesMapBySlot === null) {
[classesMapBySlot, cssRules] = resolveStyleRulesForSlots(stylesBySlots, unstable_cssPriority);
[classesMapBySlot, cssRules] = resolveStyleRulesForSlots(stylesBySlots);
}

const isLTR = dir === 'ltr';
Expand Down
10 changes: 0 additions & 10 deletions packages/make-styles/src/mergeClasses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,4 @@ describe('mergeClasses', () => {
expect(mergeClasses(rtlClassName1, rtlClassName2)).toBe('___w1tqsn0 fe3e8s9 f81rol6');
});
});

describe('unstable functionality', () => {
it('deduplicates classes with mixed priority', () => {
// Classnames with numeric suffix has increased specificity
const className1 = makeStyles({ root: { display: 'grid' } })(options).root;
const className2 = makeStyles({ root: { display: 'flex' } }, 1)(options).root;

expect(mergeClasses(className1, className2)).toBe(className2);
});
});
});
2 changes: 1 addition & 1 deletion packages/make-styles/src/resolveStyleRulesForSlots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('resolveStyleRulesForSlots', () => {
icon: { color: 'blue', backgroundColor: 'lightblue' },
};

expect(resolveStyleRulesForSlots(stylesBySlots, 0)).toMatchInlineSnapshot(`
expect(resolveStyleRulesForSlots(stylesBySlots)).toMatchInlineSnapshot(`
Array [
Object {
"icon": Object {
Expand Down
4 changes: 1 addition & 3 deletions packages/make-styles/src/resolveStyleRulesForSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ import { CSSClassesMapBySlot, CSSRulesByBucket, MakeStylesStyle, StyleBucketName
* Calls resolveStyleRules() for each slot, is also used by build time transform.
*
* @param stylesBySlots - An object with makeStyles rules where a key is a slot name
* @param unstable_cssPriority - Defines priority for selectors of generated CSS rules
*
* @return - A tuple with an object classnames mapping where a key is a slot name and an array with CSS rules
*/
export function resolveStyleRulesForSlots<Slots extends string | number>(
stylesBySlots: StylesBySlots<Slots>,
unstable_cssPriority: number,
): [CSSClassesMapBySlot<Slots>, CSSRulesByBucket] {
const classesMapBySlot = {} as CSSClassesMapBySlot<Slots>;
const cssRules: CSSRulesByBucket = {};

// eslint-disable-next-line guard-for-in
for (const slotName in stylesBySlots) {
const slotStyles: MakeStylesStyle = stylesBySlots[slotName];
const [cssClassMap, cssRulesByBucket] = resolveStyleRules(slotStyles, unstable_cssPriority);
const [cssClassMap, cssRulesByBucket] = resolveStyleRules(slotStyles);

classesMapBySlot[slotName] = cssClassMap;

Expand Down
7 changes: 1 addition & 6 deletions packages/make-styles/src/runtime/compileCSS.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { compileCSS, CompileCSSOptions, normalizePseudoSelector } from './compileCSS';

const defaultOptions: Pick<
CompileCSSOptions,
'rtlClassName' | 'className' | 'media' | 'pseudo' | 'support' | 'unstable_cssPriority'
> = {
const defaultOptions: Pick<CompileCSSOptions, 'rtlClassName' | 'className' | 'media' | 'pseudo' | 'support'> = {
className: 'foo',
rtlClassName: 'rtl-foo',
media: '',
pseudo: '',
support: '',

unstable_cssPriority: 0,
};

describe('compileCSS', () => {
Expand Down
24 changes: 4 additions & 20 deletions packages/make-styles/src/runtime/compileCSS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface CompileCSSOptions {

property: string;
value: number | string;
unstable_cssPriority: number;

rtlClassName?: string;
rtlProperty?: string;
Expand All @@ -21,10 +20,6 @@ export interface CompileCSSOptions {

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.
*
Expand Down Expand Up @@ -65,27 +60,16 @@ export function compileCSSRules(cssRules: string): string[] {
}

export function compileCSS(options: CompileCSSOptions): [string /* ltr definition */, string? /* rtl definition */] {
const {
className,
media,
pseudo,
support,
property,
rtlClassName,
rtlProperty,
rtlValue,
value,
unstable_cssPriority,
} = options;

const classNameSelector = repeatSelector(`.${className}`, unstable_cssPriority);
const { className, media, pseudo, support, property, rtlClassName, rtlProperty, rtlValue, value } = options;

const classNameSelector = `.${className}`;
const cssDeclaration = `{ ${hyphenateProperty(property)}: ${value}; }`;

let rtlClassNameSelector: string | null = null;
let rtlCSSDeclaration: string | null = null;

if (rtlProperty && rtlClassName) {
rtlClassNameSelector = repeatSelector(`.${rtlClassName}`, unstable_cssPriority);
rtlClassNameSelector = `.${rtlClassName}`;
rtlCSSDeclaration = `{ ${hyphenateProperty(rtlProperty)}: ${rtlValue}; }`;
}

Expand Down
55 changes: 0 additions & 55 deletions packages/make-styles/src/runtime/resolveStyleRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,59 +617,4 @@ describe('resolveStyleRules', () => {
});
});
});

describe('experimental', () => {
it('allows to increase specificity', () => {
expect(resolveStyleRules({ color: 'red' }, 1)).toMatchInlineSnapshot(`
.fe3e8s91.fe3e8s91 {
color: red;
}
`);
expect(resolveStyleRules({ color: 'red' }, 2)).toMatchInlineSnapshot(`
.fe3e8s92.fe3e8s92.fe3e8s92 {
color: red;
}
`);
});

it('allows to increase for media queries', () => {
expect(
resolveStyleRules(
{
'@media screen and (max-width: 992px)': {
color: 'red',
},
},
1,
),
).toMatchInlineSnapshot(`
@media screen and (max-width: 992px) {
.f1ojdyje1.f1ojdyje1 {
color: red;
}
}
`);
});

it('allows to increase for RTL', () => {
expect(resolveStyleRules({ left: '5px' }, 1)).toMatchInlineSnapshot(`
.f5b3q4t1.f5b3q4t1 {
left: 5px;
}
.flgfsvn1.flgfsvn1 {
right: 5px;
}
`);
});

it('generates unique classnames with different specificity', () => {
const classnamesSet = new Set<string>();

classnamesSet.add(getFirstClassName(resolveStyleRules({ color: 'red' })));
classnamesSet.add(getFirstClassName(resolveStyleRules({ color: 'red' }, 1)));
classnamesSet.add(getFirstClassName(resolveStyleRules({ color: 'red' }, 2)));

expect(classnamesSet.size).toBe(3);
});
});
});
40 changes: 10 additions & 30 deletions packages/make-styles/src/runtime/resolveStyleRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { normalizeNestedProperty } from './utils/normalizeNestedProperty';
import { isObject } from './utils/isObject';
import { getStyleBucketName } from './getStyleBucketName';
import { hashClassName } from './utils/hashClassName';
import { resolveProxyValues } from './createCSSVariablesProxy';
import { hashPropertyKey } from './utils/hashPropertyKey';

function pushToClassesMap(
Expand All @@ -39,9 +38,13 @@ function pushToCSSRules(
}
}

function resolveStyleRulesInner(
/**
* Transforms input styles to classes maps & CSS rules.
*
* @internal
*/
export function resolveStyleRules(
styles: MakeStylesStyle,
unstable_cssPriority: number = 0,
pseudo = '',
media = '',
support = '',
Expand All @@ -67,7 +70,6 @@ function resolveStyleRulesInner(
support,
pseudo,
property,
unstable_cssPriority,
});

const rtlDefinition = (rtlValue && { key: property, value: rtlValue }) || convertProperty(property, value);
Expand All @@ -80,7 +82,6 @@ function resolveStyleRulesInner(
pseudo,
media,
support,
unstable_cssPriority,
})
: undefined;
const rtlCompileOptions: Partial<CompileCSSOptions> | undefined = flippedInRtl
Expand All @@ -99,7 +100,6 @@ function resolveStyleRulesInner(
property,
support,
value,
unstable_cssPriority,
...rtlCompileOptions,
});

Expand Down Expand Up @@ -145,9 +145,8 @@ function resolveStyleRulesInner(
rtlAnimationNames.push(rtlAnimationName);
}

resolveStyleRulesInner(
resolveStyleRules(
{ animationName: animationNames.join(', ') },
unstable_cssPriority,
pseudo,
media,
support,
Expand All @@ -157,9 +156,8 @@ function resolveStyleRulesInner(
);
} else if (isObject(value)) {
if (isNestedSelector(property)) {
resolveStyleRulesInner(
resolveStyleRules(
value as MakeStylesStyle,
unstable_cssPriority,
pseudo + normalizeNestedProperty(property),
media,
support,
Expand All @@ -169,9 +167,8 @@ function resolveStyleRulesInner(
} else if (isMediaQuerySelector(property)) {
const combinedMediaQuery = generateCombinedQuery(media, property.slice(6).trim());

resolveStyleRulesInner(
resolveStyleRules(
value as MakeStylesStyle,
unstable_cssPriority,
pseudo,
combinedMediaQuery,
support,
Expand All @@ -181,9 +178,8 @@ function resolveStyleRulesInner(
} else if (isSupportQuerySelector(property)) {
const combinedSupportQuery = generateCombinedQuery(support, property.slice(9).trim());

resolveStyleRulesInner(
resolveStyleRules(
value as MakeStylesStyle,
unstable_cssPriority,
pseudo,
media,
combinedSupportQuery,
Expand All @@ -201,19 +197,3 @@ function resolveStyleRulesInner(

return [cssClassesMap, cssRulesByBucket];
}

/**
* Transforms input styles to classes maps & CSS rules.
*
* @internal
*/
export function resolveStyleRules(
styles: MakeStylesStyle,
unstable_cssPriority: number = 0,
): [CSSClassesMap, CSSRulesByBucket] {
return resolveStyleRulesInner(
// resolveProxyValues() is recursive function and should be evaluated once for a style object
resolveProxyValues(styles),
unstable_cssPriority,
);
}
12 changes: 2 additions & 10 deletions packages/make-styles/src/runtime/utils/hashClassName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,11 @@ export interface HashedClassNameParts {
pseudo: string;
media: string;
support: string;
unstable_cssPriority: number;
}

export function hashClassName({
media,
property,
pseudo,
support,
value,
unstable_cssPriority,
}: HashedClassNameParts): string {
export function hashClassName({ media, property, pseudo, support, value }: HashedClassNameParts): string {
// Trimming of value is required to generate consistent hashes
const classNameHash = hashString(pseudo + media + support + property + value.trim());

return HASH_PREFIX + classNameHash + (unstable_cssPriority === 0 ? '' : unstable_cssPriority);
return HASH_PREFIX + classNameHash;
}

0 comments on commit fc9a2a4

Please sign in to comment.