# tinyhref/use-stylex architecture

> Architecture diagram of the GitHub repository tinyhref/use-stylex, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tinyhref/use-stylex
- Repository: https://github.com/tinyhref/use-stylex

## Overview

##### Project type and purpose
- This is an open-source React styling hook library (“use-stylex”) that builds on top of the StyleX CSS-in-JS engine and Tailwind utilities.
- Purpose: Provide React developers a `useStyleX` hook that generates, manages, and applies CSS class names based on StyleX definitions, with an optional Tailwind integration and a “lite” mode.

##### File structure
- Top level:
- package.json, tsconfig.json, tsup.config.ts – build & packaging
- lite.js / lite.d.ts & lite.d.ts / lite.js – prebuilt “lite” bundle
- tailwind.js / tailwind.d.ts – prebuilt Tailwind integration
- clean-package.config.json – package cleanup rules
- src/ directory:
- index.ts – exports core hook (`useStyleX`) and sets up the main API
- lite.ts – constructs a lighter-weight version of the hook (no StyleX core)
- tailwind.ts – adapters for tailwind-style helper functions
- types.ts – shared TypeScript type definitions

##### README for additional insights
- Shows how to import the hook from the package.
- Demonstrates creating a `styles` object via `stylex.create`, then calling `useStyleX(styles)`.
- Highlights three class-retrieval methods: `classes.wrapper`, `classes.getClass(...)`, and `classes.tailwind(...)`.

a. Main components
1. Consumer React Component
2. useStyleX Hook
3. StyleX Core Engine (external dependency)
4. Tailwind Adapter Layer (optional)
5. Lite Mode Implementation
6. TypeScript/Build System (tsup, configs)
7. NPM Packaging Pipeline
b. Relationships & data flow
- React Component → imports `useStyleX` → calls the hook
- useStyleX
– loads/initializes StyleX Core (unless in lite mode)
– receives a `styles` definition object
– returns a `classes` interface with three accessors
- Tailwind Adapter
– invoked by `classes.tailwind(...)`
– maps arguments to utility class strings
- Output Bundles
– built by tsup according to tsconfig and clean-package rules
– published as separate entry points (main + lite + tailwind)
c. Architectural patterns & design principles
– Modular design: core hook, adapters, and “lite” variant are separate modules.
– Single Responsibility: each file handles one concern (hook logic, tailwind, types).
– Build-time bundling: tsup generates multiple entry points to optimize bundle size.
d. Key technologies & libraries
- React Hooks API
- StyleX CSS-in-JS Engine (`@stylexjs/stylex`)
- Tailwind CSS utilities
- TypeScript for types & code safety
- tsup for zero-config bundling
- npm package ecosystem

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Runtime Modules
    subgraph "Runtime Modules"
        A["React Component"]:::runtime
        B["useStyleX Hook"]:::runtime
        C["StyleX Core (external)"]:::external
        D["Lite Mode Implementation"]:::runtime
        E["Tailwind Adapter"]:::runtime
        F["Shared Type Definitions"]:::runtime
    end

    %% Build & Packaging
    subgraph "Build & Packaging"
        G["tsup.config.ts"]:::build
        H["tsconfig.json"]:::build
        I["clean-package.config.json"]:::build
        J["package.json"]:::build
        K["Source Files (src/*.ts)"]:::build
        L["Bundle: main"]:::output
        M1["lite.js"]:::output
        M2["lite.d.ts"]:::output
        N1["tailwind.js"]:::output
        N2["tailwind.d.ts"]:::output
        O["Published to NPM"]:::output
    end

    %% Runtime Flow
    A -->|"calls hook"| B
    B -->|"uses types from"| F
    B -->|"initializes"| C
    B -->|"or uses"| D
    B -->|"invokes tailwind adapter"| E
    E -->|"returns utility classes"| B

    %% Build Flow
    K -->|build inputs| G
    K -->|build inputs| H
    K -->|packaging rules| I
    K -->|package metadata| J
    G -->|outputs| L
    H -->|types & transpile config| L
    I -->|cleanup| L
    J -->|defines entry points| L
    G -->|outputs| M1
    G -->|outputs| M2
    G -->|outputs| N1
    G -->|outputs| N2
    L -->|"publish"| O
    M1 -->|"publish"| O
    M2 -->|"publish"| O
    N1 -->|"publish"| O
    N2 -->|"publish"| O

    %% Click events
    click B "https://github.com/tinyhref/use-stylex/blob/main/src/index.ts"
    click D "https://github.com/tinyhref/use-stylex/blob/main/src/lite.ts"
    click E "https://github.com/tinyhref/use-stylex/blob/main/src/tailwind.ts"
    click F "https://github.com/tinyhref/use-stylex/blob/main/src/types.ts"
    click G "https://github.com/tinyhref/use-stylex/blob/main/tsup.config.ts"
    click H "https://github.com/tinyhref/use-stylex/blob/main/tsconfig.json"
    click I "https://github.com/tinyhref/use-stylex/blob/main/clean-package.config.json"
    click J "https://github.com/tinyhref/use-stylex/blob/main/package.json"
    click M1 "https://github.com/tinyhref/use-stylex/blob/main/lite.js"
    click M2 "https://github.com/tinyhref/use-stylex/blob/main/lite.d.ts"
    click N1 "https://github.com/tinyhref/use-stylex/blob/main/tailwind.js"
    click N2 "https://github.com/tinyhref/use-stylex/blob/main/tailwind.d.ts"

    %% Styles
    classDef runtime fill:#bbdefb,stroke:#90caf9
    classDef build fill:#cfd8dc,stroke:#90a4ae
    classDef output fill:#c8e6c9,stroke:#66bb6a
    classDef external fill:#fff9c4,stroke:#fdd835
```
