Style loading utilities that provide CSS-in-JS functionality with automatic vendor prefixing, RTL support, and server-side rendering capabilities.
npx @tessl/cli install tessl/npm-uifabric--merge-styles@7.20.0Merge Styles is a CSS-in-JS library that provides utilities for loading styles through JavaScript with performance optimizations. It generates CSS classes dynamically rather than using inline styling, enabling advanced CSS features like pseudo-selectors, media queries, and parent/child relationships. The library is designed for speed and size, weighing only 2.62k gzipped with zero runtime dependencies except tslib.
npm install @uifabric/merge-stylesimport { mergeStyles, mergeStyleSets } from '@uifabric/merge-styles';For CommonJS:
const { mergeStyles, mergeStyleSets } = require('@uifabric/merge-styles');import { mergeStyles, mergeStyleSets } from '@uifabric/merge-styles';
// Single class name generation
const redBackgroundClass = mergeStyles({
background: 'red',
':hover': { background: 'darkred' }
});
// Multiple class names from a style set
const classNames = mergeStyleSets({
root: {
background: 'lightblue',
padding: '10px'
},
button: {
backgroundColor: 'green',
color: 'white',
border: 'none',
borderRadius: '4px'
}
});
// Usage in React components
function MyComponent() {
return (
<div className={classNames.root}>
<button className={classNames.button}>Click me</button>
</div>
);
}Merge Styles is built around several key design patterns:
Primary functions for creating CSS classes from JavaScript style objects, with support for arrays, conditionals, and advanced CSS features.
function mergeStyles(...args: (IStyle | IStyleBaseArray | false | null | undefined)[]): string;
function mergeCss(
args: (IStyle | IStyleBaseArray | false | null | undefined) | (IStyle | IStyleBaseArray | false | null | undefined)[],
options?: IStyleOptions
): string;Functions for managing multiple related styles as sets, concatenating style objects, and working with component style hierarchies.
function mergeStyleSets(...styleSets: Array<IStyleSet | undefined | false | null>): IProcessedStyleSet<any>;
function mergeCssSets(
styleSets: Array<IStyleSet | undefined | false | null>,
options?: IStyleOptions
): IProcessedStyleSet<any>;
function concatStyleSets(...styleSets: (IStyleSet | false | null | undefined)[]): IConcatenatedStyleSet<any>;
function concatStyleSetsWithProps<TStyleProps, TStyleSet extends IStyleSet<TStyleSet>>(
styleProps: TStyleProps,
...allStyles: (IStyleFunctionOrObject<TStyleProps, TStyleSet> | undefined)[]
): DeepPartial<TStyleSet>;Utilities for rendering styles on the server and extracting CSS rules for client hydration. Note: Server-side rendering functions are available from a separate import path.
// Available from '@uifabric/merge-styles/lib/server'
function renderStatic(onRender: () => string, namespace?: string): { html: string; css: string };Functions for registering custom fonts and CSS animations using JavaScript objects.
function fontFace(font: IFontFace): void;
function keyframes(timeline: IKeyframes): string;Functions and classes for configuring the library's behavior, managing RTL state, and controlling CSS injection.
function setRTL(isRTL: boolean): void;
class Stylesheet {
static getInstance(): Stylesheet;
setConfig(config: IStyleSheetConfig): void;
insertRule(rule: string, preserve?: boolean): void;
getRules(forceUpdate?: boolean): string;
reset(): void;
}Configuration and State Management
type IStyle = IStyleBase | IStyleBaseArray;
type IStyleBase = IRawStyle | string | false | null | undefined;
interface IStyleBaseArray extends Array<IStyle> {}
interface IRawStyle extends IRawStyleBase {
[key: string]: any;
displayName?: string;
selectors?: {
[key: string]: IStyle;
};
}
type IStyleSet<TStyleSet extends IStyleSet<TStyleSet> = { [key: string]: any }> = {
[P in keyof Omit<TStyleSet, 'subComponentStyles'>]: IStyle;
} & {
subComponentStyles?: { [P in keyof TStyleSet['subComponentStyles']]: IStyleFunctionOrObject<any, any> };
};
type IProcessedStyleSet<TStyleSet extends IStyleSet<TStyleSet>> = {
[P in keyof Omit<TStyleSet, 'subComponentStyles'>]: string;
} & {
subComponentStyles: {
[P in keyof TStyleSet['subComponentStyles']]: __MapToFunctionType<
TStyleSet['subComponentStyles'] extends infer J ? (P extends keyof J ? J[P] : never) : never
>;
};
};
interface IStyleOptions {
rtl?: boolean;
specificityMultiplier?: number;
}
interface IStyleSheetConfig {
injectionMode?: InjectionMode;
defaultPrefix?: string;
namespace?: string;
cspSettings?: ICSPSettings;
}
type IKeyframes = Record<string, IRawStyle>;