Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@stylexjs/stylex

facebook143.4kMIT0.13.1TypeScript support: included

A library for defining styles for optimized user interfaces.

readme

@stylexjs/stylex

StyleX is a JavaScript library for defining styles for optimized user interfaces.

Installation

To start playing with StyleX without having to set up any build settings you can install just two packages:

npm install --save @stylexjs/stylex

Compiler

StyleX is designed to extract styles to a static CSS style sheet during an app's build process. StyleX provides a Babel plugin along with plugin integrations for Webpack, Rollup and NextJS.

npm install --save-dev @stylexjs/babel-plugin

For more information on working with the compiler, please see the documentation for @stylexjs/babel-plugin.

API

stylex.create()

Styles are defined as a map of CSS rules using stylex.create(). In the example below, there are 2 different CSS rules. The names "root" and "highlighted" are arbitrary names given to the rules.

import * as stylex from '@stylexjs/stylex';

const styles = stylex.create({
  root: {
    width: '100%',
    color: 'rgb(60,60,60)',
  },
  highlighted: {
    color: 'yellow',
  },
});

Pseudo-classes and Media Queries can be nested within style definitions:

import * as stylex from '@stylexjs/stylex';

const styles = stylex.create({
  root: {
    width: '100%',
    color: 'rgb(60,60,60)',
    maxWidth: {
      '@media (min-width: 800px)': '800px',
    },
  },
  highlighted: {
    color: 'yellow',
    opacity: {
      ':hover': '0.9',
    },
  },
});

The compiler will extract the rules to CSS and replace the rules in the source code with a "compiled style" object.

stylex.props()

Applying style rules to specific elements is done using stylex.props. Each argument to this function must be a reference to a compiled style object, or an array of compiled style objects. The function merges styles from left to right.

<div {...stylex.props(styles.root, styles.highlighted)} />

The stylex.props function returns React props as required to render an element. StyleX styles can still be passed to other components via props, but only the components rendering host platform elements will use stylex.props(). For example:

const styles = stylex.create({
  internalRoot: {
    padding: 10,
  },
  exportedRoot: {
    position: 'relative',
  },
});

function InternalComponent(props) {
  return (
    <div {...props} {...stylex.props(styles.internalRoot, props.style)} />
  );
}

export function ExportedComponent(props) {
  return <InternalComponent style={[styles.exportedRoot, props.style]} />;
}

Styles can be conditionally included using standard JavaScript.

<div {...stylex.props(styles.root, isHighlighted && styles.highlighted)} />

And the local merging of styles can be used to control the relative priority of rules. For example, to allow a component's local styles to take priority over style property values passed in via props.

<div {...stylex.props(props.style, styles.root)} />

stylex.firstThatWorks()

Defining fallback styles is done with stylex.firstThatWorks(). This is useful for engines that may not support a specific style property.

import * as stylex from '@stylexjs/stylex';

const styles = stylex.create({
  header: {
    position: stylex.firstThatWorks('sticky', '-webkit-sticky', 'fixed'),
  },
});

This is equivalent to defining CSS as follows:

.header {
  position: fixed;
  position: -webkit-sticky;
  position: sticky;
}

Types

StyleX comes with full support for Static Types.

StyleXStyles<>

The most common type you might need to use is StyleXStyles<>. This lets you accept an object of arbitrary StyleX styles.

type Props = {
  ...
  style?: StyleXStyles<>,
};

function MyComponent({style, ...}: Props) {
  return (
    <div {...stylex.props(localStyles.foo, localStyles.bar, style)} />
  );
}

StyleXStylesWithout<>

To disallow specific style properties, use the StyleXStylesWithout<> type.

type Props = {
  // ...
  style?: StyleXStylesWithout<{
    position: unknown;
    display: unknown;
  }>;
};

StaticStyles<>

To constrain the styles to specific properties and values, use the StaticStyles<> type. For example, if a component should accept marginTop but only accept one of 0, 4, or 8 pixels as values.

type Props = {
  ...
  style?: StaticStyles<{
    marginTop?: 0 | 4 | 8;
  }>,
};

How StyleX works

StyleX produces atomic styles, which means that each CSS rule contains only a single declaration and uses a unique class name. For example:

import * as stylex from '@stylexjs/stylex';

const styles = stylex.create({
  root: {
    width: '100%',
    color: 'red',
  }
}

From this code, StyleX will generate 2 classes. One for the width: '100%' declaration, and one for the color: 'red' declaration. If you use the declaration width: '100%' anywhere else in your application, it will reuse the same CSS class rather than creating a new one.

One of the benefits of this approach is that the generated CSS file grows logarithmically as you add new styled components to your app. As more style declarations are added to components, they are more likely to already be in use elsewhere in the app. As a result of this CSS optimization, the generated CSS style sheet for an app is usually small enough to be contained in a single file and used across routes, avoiding style recalculation and layout thrashing as users navigate through your app.

changelog

Changelog

0.13.1 (May 21, 2025)

Fixes

  • Export additional Types.

0.13.0 (May 19, 2025)

New features

  • Add positionTry API for creating @property-try declarations.
  • Add defineConsts API for inlining constant values.
  • Re-write of the runtime style injection module to be more reliable.

Breaking changes

  • The runtimeInjection compiler option is now disabled by default when dev is true.
  • The ESLint rule no-legacy-conditional-styles is renamed to no-legacy-contextual-styles.
  • The useRemForFontSize compiler option is renamed to enableFontSizePxToRem. It is disabled by default and should not be used directly.
  • The genConditionalClasses compiler option is renamed to enableInlinedConditionalMerge. It is enabled by default and should not be used directly.
  • The attrs API is removed due to low usage and redundancy with the props API.

Fixes

  • Fix the TypeScript types for themes and types functions.
  • Fix the creation of duplicate classNames when defining nested pseudo-classes.
  • Fix that allows the ESLint plugin to support use of importSources object syntax in validImports.
  • Fix incorrect compiler error messages.
  • Fix a bug that incorrectly wrapped CSS variables in quotes when used in the content property.
  • Fix a bug in the firstThatWorks API when the last value was a variable.
  • Allow importSources to be configured in the PostCSS plugin for React Strict DOM compatibility.

Deprecations

  • Deprecate @stylexjs/shared package.

0.12.0 (Apr 10, 2025)

New features

  • Hash keys in compiled style objects to reduce generated code size.
  • New eslint rule to flag use of legacy Media Query and pseudo-class syntax.

Fixes

  • Fix pseudo-elements bug in dynamic styles.
  • Performance improvements to createTheme compilation by caching object evaluation.
  • Disallow spreading in create calls.

Deprecations

  • Deprecate @stylexjs/dev-runtime package.
  • Deprecate @stylexjs/esbuild-plugin package.
  • Deprecate @stylexjs/nextjs-plugin package.
  • Deprecate @stylexjs/open-props package.
  • Deprecate @stylexjs/webpack-plugin package.

0.11.1 (Mar 3, 2025)

Fixes

  • Fix create compilation regression for string and number keys.
  • Fix babel path resolution within monorepos.

0.11.0 (Feb 27, 2025)