From 76c4b69990712abc0c9f293704a1f8a59158a59c Mon Sep 17 00:00:00 2001 From: Andrey Yamanov Date: Fri, 22 Aug 2025 12:00:12 +0200 Subject: [PATCH 01/79] feat: new style injector --- src/tasty/injector/README.md | 153 +++++++++ src/tasty/injector/flatten.test.ts | 318 +++++++++++++++++ src/tasty/injector/flatten.ts | 182 ++++++++++ src/tasty/injector/hash.test.ts | 153 +++++++++ src/tasty/injector/hash.ts | 82 +++++ src/tasty/injector/index.test.ts | 321 +++++++++++++++++ src/tasty/injector/index.ts | 104 ++++++ src/tasty/injector/injector.test.ts | 416 +++++++++++++++++++++++ src/tasty/injector/injector.ts | 244 +++++++++++++ src/tasty/injector/sheet-manager.test.ts | 377 ++++++++++++++++++++ src/tasty/injector/sheet-manager.ts | 345 +++++++++++++++++++ src/tasty/injector/types.ts | 61 ++++ 12 files changed, 2756 insertions(+) create mode 100644 src/tasty/injector/README.md create mode 100644 src/tasty/injector/flatten.test.ts create mode 100644 src/tasty/injector/flatten.ts create mode 100644 src/tasty/injector/hash.test.ts create mode 100644 src/tasty/injector/hash.ts create mode 100644 src/tasty/injector/index.test.ts create mode 100644 src/tasty/injector/index.ts create mode 100644 src/tasty/injector/injector.test.ts create mode 100644 src/tasty/injector/injector.ts create mode 100644 src/tasty/injector/sheet-manager.test.ts create mode 100644 src/tasty/injector/sheet-manager.ts create mode 100644 src/tasty/injector/types.ts diff --git a/src/tasty/injector/README.md b/src/tasty/injector/README.md new file mode 100644 index 000000000..e300dcd51 --- /dev/null +++ b/src/tasty/injector/README.md @@ -0,0 +1,153 @@ +# Tasty Style Injector + +A high-performance CSS-in-JS solution designed to replace styled-components in the Tasty design system. + +## Overview + +The Style Injector provides: +- **Hash-based deduplication** - identical CSS gets the same className +- **Reference counting** - automatic cleanup when components unmount +- **CSS nesting flattening** - handles `&`, `.Class`, `SubElement` patterns +- **Garbage collection** - configurable cleanup thresholds +- **SSR support** - deterministic class names and CSS extraction +- **Multiple roots** - works with Document and ShadowRoot +- **Adopted stylesheets** - uses modern APIs with fallbacks + +## Quick Start + +```typescript +import { inject, injectGlobal, configure } from './tasty/injector'; + +// Configure once (optional) +configure({ + gcThreshold: 100, + useAdoptedStyleSheets: true, +}); + +// Inject component styles +const { className, dispose } = inject('&{ color: red; padding: 10px; }'); + +// Inject global styles +const globalDispose = injectGlobal('body', 'margin: 0; font-family: Arial;'); + +// Cleanup when component unmounts +useEffect(() => dispose, [dispose]); +``` + +## API Reference + +### `inject(cssText: string, options?): InjectResult` + +Injects CSS and returns a className with dispose function. + +```typescript +const result = inject('&{ color: red; &:hover{ color: blue; } }'); +// Returns: { className: 't-abc123', dispose: () => void } +``` + +### `injectGlobal(selector: string, cssText: string, options?): DisposeFunction` + +Injects global CSS rules. + +```typescript +const dispose = injectGlobal('body', 'margin: 0; background: white;'); +``` + +### `configure(config: Partial): void` + +Configures the global injector instance. + +```typescript +configure({ + maxRulesPerSheet: 8000, // Cap rules per sheet (infinite by default) + mode: 'nested', // 'nested' | 'atomic' + gcThreshold: 100, // Cleanup trigger threshold + useAdoptedStyleSheets: true, // Use modern APIs when available + nonce: 'csp-nonce', // CSP nonce for style elements +}); +``` + +### `getCssText(options?): string` + +Extracts CSS text for SSR. + +```typescript +const cssForSSR = getCssText(); +``` + +## Architecture + +``` +┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐ +│ tastyElement │────│ StyleInjector │────│ SheetManager │ +│ tastyGlobal │ │ │ │ │ +└─────────────────┘ └──────────────────┘ └─────────────────┘ + │ │ │ + │ │ │ + ▼ ▼ ▼ +┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐ +│ renderStyles │ │ KeyframesManager │ │ RootRegistry │ +│ │ │ │ │ │ +└─────────────────┘ └──────────────────┘ └─────────────────┘ + │ │ + │ │ + ▼ ▼ +┌─────────────────┐ ┌─────────────────┐ +│ flattenNestedCss│ │ CSSStyleSheet │ +│ │ │