or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

component-theming.mdconfig.mdcss-processing.mdindex.mdpseudo-responsive.mdtheme-vars.md
tile.json

css-processing.mddocs/

CSS Processing and Style Functions

Core CSS-in-JS processing engine with theme integration, responsive value expansion, pseudo-selector support, and comprehensive style property management. This system transforms style objects into CSS with full theme token resolution.

Capabilities

Core CSS Processing

Main CSS processing functions that transform style objects with theme integration and pseudo-selector support.

/**
 * CSS processing function that transforms styles with theme integration
 * @param styles - Style object or theme function
 * @returns Function that processes styles with given theme
 */
function css(styles: StyleObjectOrFn): (theme: any) => Record<string, any>;

/**
 * Options for creating CSS processing function
 */
interface GetCSSOptions {
  theme: CssTheme;
  configs?: Config;
  pseudos?: Record<string, CSS.Pseudos | (string & {})>;
}

/**
 * Creates customizable CSS processing function
 * @param options - Configuration options
 * @returns CSS processing function
 */
function getCss(options: GetCSSOptions): (
  styles: Record<string, any>, 
  nested?: boolean
) => Record<string, any>;

/**
 * Style object or function that returns style object
 */
type StyleObjectOrFn = 
  | SystemStyleObject 
  | ((theme: any) => SystemStyleObject);

/**
 * Theme function type that can receive theme and return value
 */
type ThemeThunk<T> = T | ((theme: Record<string, any>) => T);

/**
 * Deep object property access utility
 * @param object - Object to traverse
 * @param path - Property path (dot notation or array)
 * @param fallback - Fallback value if path not found
 * @returns Value at path or fallback
 */
function get<T = any>(
  object: Record<string, any>, 
  path: string | number | (string | number)[], 
  fallback?: T
): T;

/**
 * Memoized version of get function for performance
 */
const memoizedGet: typeof get;

Usage Examples:

import { css, getCss } from "@chakra-ui/styled-system";

// Basic CSS processing
const buttonStyles = css({
  bg: "blue.500",
  color: "white",
  px: 4,
  py: 2,
  _hover: {
    bg: "blue.600"
  },
  _disabled: {
    opacity: 0.4
  }
});

// Apply with theme
const processedStyles = buttonStyles(theme);
// Result:
// {
//   backgroundColor: "var(--chakra-colors-blue-500)",
//   color: "var(--chakra-colors-white)",
//   paddingLeft: "var(--chakra-space-4)",
//   paddingRight: "var(--chakra-space-4)",
//   paddingTop: "var(--chakra-space-2)",
//   paddingBottom: "var(--chakra-space-2)",
//   "&:hover": {
//     backgroundColor: "var(--chakra-colors-blue-600)"
//   },
//   "&:disabled": {
//     opacity: 0.4
//   }
// }

// Functional styles with theme access
const dynamicStyles = css(({ colors, space }) => ({
  bg: colors.primary,
  p: space[4],
  border: `1px solid ${colors.border}`,
  _dark: {
    bg: colors.primaryDark
  }
}));

// Custom CSS processing
const customCss = getCss({
  theme: myTheme,
  configs: { ...systemProps, customProp: { property: "custom" } },
  pseudos: { _custom: "&[data-custom]" }
});

const customStyles = customCss({
  customProp: "value",
  _custom: { bg: "red.500" }
});

System Style Types

Comprehensive type system for style objects with responsive values, pseudo-selectors, and theme token support.

/**
 * Combined interface of all style property interfaces
 */
interface StyleProps extends 
  SpaceProps, ColorProps, LayoutProps, FlexboxProps,
  GridProps, TypographyProps, BorderProps, BackgroundProps,
  EffectProps, FilterProps, TransformProps, TransitionProps,
  PositionProps, InteractivityProps, ListProps, RingProps,
  ScrollProps, TextDecorationProps, OtherProps {}

/**
 * CSS properties enhanced with system overrides
 */
interface SystemCSSProperties extends 
  CSS.Properties, 
  Omit<StyleProps, keyof CSS.Properties> {}

/**
 * CSS properties with responsive value support
 */
type CSSWithMultiValues = {
  [K in keyof SystemCSSProperties]?: 
    K extends keyof StyleProps
      ? StyleProps[K] | PropertyValue<K>
      : PropertyValue<K>;
};

/**
 * Recursive pseudo-selector support
 */
type RecursivePseudo<D> = {
  [K in PseudoKeys]?: PseudoSelectorDefinition<D> & D;
};

/**
 * Recursive CSS selector support  
 */
interface RecursiveCSSSelector<D> {
  [selector: string]: CSSDefinition<D> & D;
}

/**
 * Complete system style object with recursive pseudo and CSS selector support
 */
type SystemStyleObject = RecursiveCSSObject<CSSWithMultiValues>;

/**
 * Style props with pseudo-selector properties
 */
interface SystemProps extends StyleProps, PseudoProps {}

/**
 * Pseudo-selector properties interface
 */
type PseudoProps = {
  [K in PseudoKey]?: SystemStyleObject;
};

Usage Examples:

import { SystemStyleObject, SystemProps } from "@chakra-ui/styled-system";

// Complete style object with all features
const complexStyles: SystemStyleObject = {
  // Basic styles
  bg: "white",
  color: "gray.800",
  p: 4,
  
  // Responsive values
  fontSize: { base: "md", lg: "lg" },
  width: [300, 400, 500],
  
  // Pseudo-selectors
  _hover: {
    bg: "gray.50",
    transform: "scale(1.02)"
  },
  _focus: {
    outline: "2px solid",
    outlineColor: "blue.500"
  },
  _disabled: {
    opacity: 0.5,
    cursor: "not-allowed"
  },
  
  // Custom CSS selectors
  "& > *": {
    marginBottom: 2
  },
  "&[data-active='true']": {
    bg: "blue.50",
    borderColor: "blue.500"
  },
  
  // Nested responsive and pseudo
  _dark: {
    bg: "gray.800",
    color: "white",
    _hover: {
      bg: "gray.700"
    }
  },
  
  // Media queries
  "@media print": {
    display: "none"
  }
};

// Component props interface
interface ButtonProps extends SystemProps {
  variant?: "solid" | "outline";
  size?: "sm" | "md" | "lg";
}

// Using in component
function Button({ variant = "solid", size = "md", ...styleProps }: ButtonProps) {
  const styles = css({
    // Base styles
    display: "inline-flex",
    alignItems: "center",
    justifyContent: "center",
    
    // Apply style props
    ...styleProps
  });
  
  return styles;
}

System Configuration

Core system properties and utilities for managing style configurations and property detection.

/**
 * Merged configuration of all system properties
 */
const systemProps: Config;

/**
 * Array of layout-specific property names
 */
const layoutPropNames: string[];

/**
 * Array of all system property names including pseudo-selectors
 */
const propNames: string[];

/**
 * Checks if a property name is a valid style property
 * @param prop - Property name to check
 * @returns True if the property is a style property
 */
function isStyleProp(prop: string): boolean;

Usage Examples:

import { 
  systemProps, 
  layoutPropNames, 
  propNames, 
  isStyleProp 
} from "@chakra-ui/styled-system";

// Check if properties are style props
console.log(isStyleProp("bg"));        // true
console.log(isStyleProp("_hover"));    // true  
console.log(isStyleProp("onClick"));   // false

// Filter style props from component props
function separateProps(props: Record<string, any>) {
  const styleProps: Record<string, any> = {};
  const otherProps: Record<string, any> = {};
  
  Object.entries(props).forEach(([key, value]) => {
    if (isStyleProp(key)) {
      styleProps[key] = value;
    } else {
      otherProps[key] = value;
    }
  });
  
  return { styleProps, otherProps };
}

// Get layout-specific props
const layoutProps = layoutPropNames.reduce((acc, prop) => {
  if (prop in props) {
    acc[prop] = props[prop];
  }
  return acc;
}, {} as Record<string, any>);

// Validate props against system
function validateStyleProps(props: Record<string, any>) {
  return Object.keys(props).filter(key => !propNames.includes(key));
}

// Custom system configuration
const customSystem = {
  ...systemProps,
  customSpacing: { property: "margin", scale: "space" },
  customColor: { property: "borderColor", scale: "colors" }
};

Responsive Value Processing

Utilities for handling responsive values with breakpoint-aware expansion and array/object syntax support.

/**
 * Expands responsive style objects with breakpoint support
 * @param styles - Style object with potential responsive values
 * @returns Function that processes responsive values with theme breakpoints
 */
function expandResponsive(
  styles: Record<string, any>
): (theme: Record<string, any>) => Record<string, any>;

/**
 * Responsive value types
 */
type ResponsiveValue<T> = T | ResponsiveArray<T> | ResponsiveObject<T>;
type ResponsiveArray<T> = Array<T | null>;
type ResponsiveObject<T> = Partial<Record<string, T>>;

/**
 * CSS theme with breakpoint information
 */
interface CssTheme {
  __breakpoints?: {
    isResponsive: (value: any) => boolean;
    toArrayValue: (value: any) => any[];
    media: string[];
  };
  [key: string]: any;
}

Usage Examples:

import { expandResponsive } from "@chakra-ui/styled-system";

// Responsive values - array syntax
const arrayResponsive = {
  fontSize: ["sm", "md", "lg", "xl"],  // mobile, tablet, desktop, wide
  padding: [2, 4, 6],                 // mobile, tablet, desktop+
  margin: [1, 2, null, 4]             // mobile, tablet, skip desktop, wide
};

// Responsive values - object syntax  
const objectResponsive = {
  fontSize: { base: "sm", md: "md", lg: "lg" },
  padding: { base: 2, tablet: 4, desktop: 6 },
  display: { base: "block", lg: "flex" }
};

// Process with theme
const processResponsive = expandResponsive(arrayResponsive);
const result = processResponsive(theme);

// Result (example):
// {
//   fontSize: "var(--chakra-fontSizes-sm)",
//   padding: "var(--chakra-space-2)",  
//   margin: "var(--chakra-space-1)",
//   "@media screen and (min-width: 768px)": {
//     fontSize: "var(--chakra-fontSizes-md)",
//     padding: "var(--chakra-space-4)",
//     margin: "var(--chakra-space-2)"
//   },
//   "@media screen and (min-width: 1024px)": {
//     fontSize: "var(--chakra-fontSizes-lg)",
//     padding: "var(--chakra-space-6)"
//   },
//   "@media screen and (min-width: 1280px)": {
//     fontSize: "var(--chakra-fontSizes-xl)",
//     margin: "var(--chakra-space-4)"
//   }
// }

// Custom responsive processing
function processCustomResponsive(
  styles: Record<string, any>,
  customBreakpoints: string[]
) {
  return Object.entries(styles).reduce((acc, [key, value]) => {
    if (Array.isArray(value)) {
      // Process array syntax
      acc[key] = value[0]; // base value
      
      value.slice(1).forEach((val, index) => {
        if (val != null && customBreakpoints[index]) {
          acc[customBreakpoints[index]] = acc[customBreakpoints[index]] || {};
          acc[customBreakpoints[index]][key] = val;
        }
      });
    } else {
      acc[key] = value;
    }
    
    return acc;
  }, {} as Record<string, any>);
}

Import Examples:

// Import all CSS processing utilities
import { 
  css, 
  getCss, 
  systemProps, 
  isStyleProp, 
  propNames,
  layoutPropNames,
  expandResponsive
} from "@chakra-ui/styled-system";

// Import specific modules
import { css } from "@chakra-ui/styled-system/css";
import { systemProps } from "@chakra-ui/styled-system/system";

// Import types
import type {
  SystemStyleObject,
  SystemProps,
  StyleObjectOrFn,
  StyleProps,
  SystemCSSProperties,
  ResponsiveValue,
  CssTheme,
  ThemeThunk
} from "@chakra-ui/styled-system";