@tailwind-styled/runtime-css 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CssInjector.cjs +96 -0
- package/dist/CssInjector.d.cts +45 -0
- package/dist/CssInjector.d.ts +45 -0
- package/dist/CssInjector.js +60 -0
- package/dist/batchedInjector.cjs +98 -0
- package/dist/batchedInjector.d.cts +57 -0
- package/dist/batchedInjector.d.ts +57 -0
- package/dist/batchedInjector.js +68 -0
- package/package.json +37 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/CssInjector.tsx
|
|
31
|
+
var CssInjector_exports = {};
|
|
32
|
+
__export(CssInjector_exports, {
|
|
33
|
+
TwCssInjector: () => TwCssInjector,
|
|
34
|
+
useTwClasses: () => useTwClasses
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(CssInjector_exports);
|
|
37
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
|
38
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
39
|
+
var import_react = __toESM(require("react"), 1);
|
|
40
|
+
async function TwCssInjector({
|
|
41
|
+
cssDir,
|
|
42
|
+
route,
|
|
43
|
+
includeGlobal = true,
|
|
44
|
+
minify = true,
|
|
45
|
+
asLink = false
|
|
46
|
+
}) {
|
|
47
|
+
const resolvedDir = cssDir != null ? cssDir : import_node_path.default.join(process.cwd(), ".next", "static", "css", "tw");
|
|
48
|
+
const cssChunks = [];
|
|
49
|
+
if (includeGlobal) {
|
|
50
|
+
const globalCss = loadCssFile(import_node_path.default.join(resolvedDir, "_global.css"));
|
|
51
|
+
if (globalCss) cssChunks.push(globalCss);
|
|
52
|
+
}
|
|
53
|
+
const targetRoute = route != null ? route : "/";
|
|
54
|
+
const routeFile = routeToFilename(targetRoute);
|
|
55
|
+
const routeCss = loadCssFile(import_node_path.default.join(resolvedDir, routeFile));
|
|
56
|
+
if (routeCss) cssChunks.push(routeCss);
|
|
57
|
+
if (cssChunks.length === 0) return import_react.default.createElement(import_react.default.Fragment, null);
|
|
58
|
+
const combined = cssChunks.join("\n");
|
|
59
|
+
const final = minify ? minifyCss(combined) : combined;
|
|
60
|
+
if (asLink) {
|
|
61
|
+
return import_react.default.createElement("link", {
|
|
62
|
+
rel: "stylesheet",
|
|
63
|
+
href: `/_next/static/css/tw/${routeFile}`,
|
|
64
|
+
crossOrigin: "anonymous"
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return import_react.default.createElement("style", {
|
|
68
|
+
dangerouslySetInnerHTML: { __html: final },
|
|
69
|
+
"data-tw-route": targetRoute
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function useTwClasses(classes) {
|
|
73
|
+
return classes;
|
|
74
|
+
}
|
|
75
|
+
function loadCssFile(filepath) {
|
|
76
|
+
try {
|
|
77
|
+
if (import_node_fs.default.existsSync(filepath)) {
|
|
78
|
+
return import_node_fs.default.readFileSync(filepath, "utf-8");
|
|
79
|
+
}
|
|
80
|
+
} catch (e) {
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
function routeToFilename(route) {
|
|
85
|
+
if (route === "/") return "index.css";
|
|
86
|
+
if (route === "__global") return "_global.css";
|
|
87
|
+
return `${route.replace(/^\//, "").replace(/\//g, "_")}.css`;
|
|
88
|
+
}
|
|
89
|
+
function minifyCss(css) {
|
|
90
|
+
return css.replace(/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g, "").replace(/\s+/g, " ").replace(/\s*{\s*/g, "{").replace(/\s*}\s*/g, "}").replace(/\s*;\s*/g, ";").trim();
|
|
91
|
+
}
|
|
92
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
93
|
+
0 && (module.exports = {
|
|
94
|
+
TwCssInjector,
|
|
95
|
+
useTwClasses
|
|
96
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* tailwind-styled-v4 — CSS Injector (React Server Component)
|
|
5
|
+
*
|
|
6
|
+
* Inject CSS yang sudah di-generate per-route langsung ke <head>.
|
|
7
|
+
* Dipakai di Next.js App Router layout atau page.
|
|
8
|
+
*
|
|
9
|
+
* Di server component — inject inline CSS, zero client JS.
|
|
10
|
+
* Streaming friendly — CSS di-emit bersamaan dengan HTML.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* // app/layout.tsx
|
|
14
|
+
* import { TwCssInjector } from "tailwind-styled-v4/css"
|
|
15
|
+
* export default function Layout({ children }) {
|
|
16
|
+
* return <html><head><TwCssInjector/></head><body>{children}</body></html>
|
|
17
|
+
* }
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
interface CssInjectorProps {
|
|
21
|
+
/** Override CSS directory. Default: .next/static/css/tw */
|
|
22
|
+
cssDir?: string;
|
|
23
|
+
/** Specific route to inject. Default: auto-detect dari headers */
|
|
24
|
+
route?: string;
|
|
25
|
+
/** Inject global CSS juga. Default: true */
|
|
26
|
+
includeGlobal?: boolean;
|
|
27
|
+
/** Minify inline CSS. Default: true */
|
|
28
|
+
minify?: boolean;
|
|
29
|
+
/** Add <link> tag instead of inline <style> untuk cached CSS */
|
|
30
|
+
asLink?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Server Component — inject route-specific CSS into <head>.
|
|
34
|
+
* No client JS, no hydration overhead.
|
|
35
|
+
*/
|
|
36
|
+
declare function TwCssInjector({ cssDir, route, includeGlobal, minify, asLink, }: CssInjectorProps): Promise<React.ReactElement>;
|
|
37
|
+
/**
|
|
38
|
+
* Lightweight hook to get current route's CSS classes.
|
|
39
|
+
* Useful for dynamic class injection in client components.
|
|
40
|
+
*
|
|
41
|
+
* Returns empty string on server (SSR) — CSS already injected by TwCssInjector.
|
|
42
|
+
*/
|
|
43
|
+
declare function useTwClasses(classes: string): string;
|
|
44
|
+
|
|
45
|
+
export { TwCssInjector, useTwClasses };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* tailwind-styled-v4 — CSS Injector (React Server Component)
|
|
5
|
+
*
|
|
6
|
+
* Inject CSS yang sudah di-generate per-route langsung ke <head>.
|
|
7
|
+
* Dipakai di Next.js App Router layout atau page.
|
|
8
|
+
*
|
|
9
|
+
* Di server component — inject inline CSS, zero client JS.
|
|
10
|
+
* Streaming friendly — CSS di-emit bersamaan dengan HTML.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* // app/layout.tsx
|
|
14
|
+
* import { TwCssInjector } from "tailwind-styled-v4/css"
|
|
15
|
+
* export default function Layout({ children }) {
|
|
16
|
+
* return <html><head><TwCssInjector/></head><body>{children}</body></html>
|
|
17
|
+
* }
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
interface CssInjectorProps {
|
|
21
|
+
/** Override CSS directory. Default: .next/static/css/tw */
|
|
22
|
+
cssDir?: string;
|
|
23
|
+
/** Specific route to inject. Default: auto-detect dari headers */
|
|
24
|
+
route?: string;
|
|
25
|
+
/** Inject global CSS juga. Default: true */
|
|
26
|
+
includeGlobal?: boolean;
|
|
27
|
+
/** Minify inline CSS. Default: true */
|
|
28
|
+
minify?: boolean;
|
|
29
|
+
/** Add <link> tag instead of inline <style> untuk cached CSS */
|
|
30
|
+
asLink?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Server Component — inject route-specific CSS into <head>.
|
|
34
|
+
* No client JS, no hydration overhead.
|
|
35
|
+
*/
|
|
36
|
+
declare function TwCssInjector({ cssDir, route, includeGlobal, minify, asLink, }: CssInjectorProps): Promise<React.ReactElement>;
|
|
37
|
+
/**
|
|
38
|
+
* Lightweight hook to get current route's CSS classes.
|
|
39
|
+
* Useful for dynamic class injection in client components.
|
|
40
|
+
*
|
|
41
|
+
* Returns empty string on server (SSR) — CSS already injected by TwCssInjector.
|
|
42
|
+
*/
|
|
43
|
+
declare function useTwClasses(classes: string): string;
|
|
44
|
+
|
|
45
|
+
export { TwCssInjector, useTwClasses };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// src/CssInjector.tsx
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import React from "react";
|
|
5
|
+
async function TwCssInjector({
|
|
6
|
+
cssDir,
|
|
7
|
+
route,
|
|
8
|
+
includeGlobal = true,
|
|
9
|
+
minify = true,
|
|
10
|
+
asLink = false
|
|
11
|
+
}) {
|
|
12
|
+
const resolvedDir = cssDir != null ? cssDir : path.join(process.cwd(), ".next", "static", "css", "tw");
|
|
13
|
+
const cssChunks = [];
|
|
14
|
+
if (includeGlobal) {
|
|
15
|
+
const globalCss = loadCssFile(path.join(resolvedDir, "_global.css"));
|
|
16
|
+
if (globalCss) cssChunks.push(globalCss);
|
|
17
|
+
}
|
|
18
|
+
const targetRoute = route != null ? route : "/";
|
|
19
|
+
const routeFile = routeToFilename(targetRoute);
|
|
20
|
+
const routeCss = loadCssFile(path.join(resolvedDir, routeFile));
|
|
21
|
+
if (routeCss) cssChunks.push(routeCss);
|
|
22
|
+
if (cssChunks.length === 0) return React.createElement(React.Fragment, null);
|
|
23
|
+
const combined = cssChunks.join("\n");
|
|
24
|
+
const final = minify ? minifyCss(combined) : combined;
|
|
25
|
+
if (asLink) {
|
|
26
|
+
return React.createElement("link", {
|
|
27
|
+
rel: "stylesheet",
|
|
28
|
+
href: `/_next/static/css/tw/${routeFile}`,
|
|
29
|
+
crossOrigin: "anonymous"
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return React.createElement("style", {
|
|
33
|
+
dangerouslySetInnerHTML: { __html: final },
|
|
34
|
+
"data-tw-route": targetRoute
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function useTwClasses(classes) {
|
|
38
|
+
return classes;
|
|
39
|
+
}
|
|
40
|
+
function loadCssFile(filepath) {
|
|
41
|
+
try {
|
|
42
|
+
if (fs.existsSync(filepath)) {
|
|
43
|
+
return fs.readFileSync(filepath, "utf-8");
|
|
44
|
+
}
|
|
45
|
+
} catch (e) {
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
function routeToFilename(route) {
|
|
50
|
+
if (route === "/") return "index.css";
|
|
51
|
+
if (route === "__global") return "_global.css";
|
|
52
|
+
return `${route.replace(/^\//, "").replace(/\//g, "_")}.css`;
|
|
53
|
+
}
|
|
54
|
+
function minifyCss(css) {
|
|
55
|
+
return css.replace(/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g, "").replace(/\s+/g, " ").replace(/\s*{\s*/g, "{").replace(/\s*}\s*/g, "}").replace(/\s*;\s*/g, ";").trim();
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
TwCssInjector,
|
|
59
|
+
useTwClasses
|
|
60
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/batchedInjector.ts
|
|
21
|
+
var batchedInjector_exports = {};
|
|
22
|
+
__export(batchedInjector_exports, {
|
|
23
|
+
batchedInject: () => batchedInject,
|
|
24
|
+
flushBatchedCss: () => flushBatchedCss,
|
|
25
|
+
getBatchedCssStats: () => getBatchedCssStats,
|
|
26
|
+
isInjected: () => isInjected,
|
|
27
|
+
resetBatchedCss: () => resetBatchedCss,
|
|
28
|
+
syncInject: () => syncInject
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(batchedInjector_exports);
|
|
31
|
+
var injected = /* @__PURE__ */ new Set();
|
|
32
|
+
var pending = [];
|
|
33
|
+
var rafHandle = null;
|
|
34
|
+
var styleEl = null;
|
|
35
|
+
function getStyleElement() {
|
|
36
|
+
if (styleEl && document.head.contains(styleEl)) return styleEl;
|
|
37
|
+
styleEl = document.createElement("style");
|
|
38
|
+
styleEl.id = "__tw-runtime-css";
|
|
39
|
+
styleEl.setAttribute("data-tw-batched", "true");
|
|
40
|
+
document.head.appendChild(styleEl);
|
|
41
|
+
return styleEl;
|
|
42
|
+
}
|
|
43
|
+
function batchedInject(cssRule) {
|
|
44
|
+
if (typeof window === "undefined") return;
|
|
45
|
+
if (!cssRule || injected.has(cssRule)) return;
|
|
46
|
+
injected.add(cssRule);
|
|
47
|
+
pending.push(cssRule);
|
|
48
|
+
if (rafHandle === null) {
|
|
49
|
+
rafHandle = requestAnimationFrame(flushBatchedCss);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function flushBatchedCss() {
|
|
53
|
+
rafHandle = null;
|
|
54
|
+
if (pending.length === 0 || typeof document === "undefined") return;
|
|
55
|
+
const el = getStyleElement();
|
|
56
|
+
const css = pending.join("\n");
|
|
57
|
+
pending.length = 0;
|
|
58
|
+
el.textContent += `
|
|
59
|
+
${css}`;
|
|
60
|
+
}
|
|
61
|
+
function syncInject(cssRule) {
|
|
62
|
+
if (typeof document === "undefined") return;
|
|
63
|
+
if (!cssRule || injected.has(cssRule)) return;
|
|
64
|
+
injected.add(cssRule);
|
|
65
|
+
getStyleElement().textContent += `
|
|
66
|
+
${cssRule}`;
|
|
67
|
+
}
|
|
68
|
+
function isInjected(cssRule) {
|
|
69
|
+
return injected.has(cssRule);
|
|
70
|
+
}
|
|
71
|
+
function resetBatchedCss() {
|
|
72
|
+
injected.clear();
|
|
73
|
+
pending.length = 0;
|
|
74
|
+
if (rafHandle !== null) {
|
|
75
|
+
cancelAnimationFrame(rafHandle);
|
|
76
|
+
rafHandle = null;
|
|
77
|
+
}
|
|
78
|
+
if (styleEl && document.head.contains(styleEl)) {
|
|
79
|
+
document.head.removeChild(styleEl);
|
|
80
|
+
styleEl = null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function getBatchedCssStats() {
|
|
84
|
+
return {
|
|
85
|
+
totalInjected: injected.size,
|
|
86
|
+
pendingCount: pending.length,
|
|
87
|
+
hasBatchScheduled: rafHandle !== null
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
batchedInject,
|
|
93
|
+
flushBatchedCss,
|
|
94
|
+
getBatchedCssStats,
|
|
95
|
+
isInjected,
|
|
96
|
+
resetBatchedCss,
|
|
97
|
+
syncInject
|
|
98
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tailwind-styled-v4 — Batched CSS Injector (Client Runtime)
|
|
3
|
+
*
|
|
4
|
+
* Menggantikan pattern inject-per-komponen yang menyebabkan banyak
|
|
5
|
+
* style recalculation saat banyak komponen mount bersamaan.
|
|
6
|
+
*
|
|
7
|
+
* Cara kerja:
|
|
8
|
+
* - Semua CSS rules dari render cycle yang sama dikumpulkan
|
|
9
|
+
* - Satu requestAnimationFrame = satu DOM style update
|
|
10
|
+
* - Deduplication via Set<string> — rule yang sama tidak diinjeksi dua kali
|
|
11
|
+
* - Fallback synchronous untuk SSR / server context
|
|
12
|
+
*
|
|
13
|
+
* Usage (internal, dipakai oleh stateEngine dan containerQuery):
|
|
14
|
+
* import { batchedInject, flushBatchedCss } from "./batchedInjector"
|
|
15
|
+
*
|
|
16
|
+
* // Queue a rule
|
|
17
|
+
* batchedInject(".tw-s-abc123[data-active=\"true\"]{opacity:0.5}")
|
|
18
|
+
*
|
|
19
|
+
* // Force flush (biasanya tidak perlu — RAF melakukan ini otomatis)
|
|
20
|
+
* flushBatchedCss()
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Queue a CSS rule for batched injection.
|
|
24
|
+
* Multiple rules accumulated during one event loop tick are flushed together
|
|
25
|
+
* in one requestAnimationFrame → one style recalculation.
|
|
26
|
+
*/
|
|
27
|
+
declare function batchedInject(cssRule: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Immediately flush all pending CSS rules to the DOM.
|
|
30
|
+
* Called automatically by RAF each frame. Can also be called manually
|
|
31
|
+
* after synchronous component setup where RAF timing is too late.
|
|
32
|
+
*/
|
|
33
|
+
declare function flushBatchedCss(): void;
|
|
34
|
+
/**
|
|
35
|
+
* Synchronous inject — skips batching.
|
|
36
|
+
* Use for SSR / critical path where RAF is not available.
|
|
37
|
+
*/
|
|
38
|
+
declare function syncInject(cssRule: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* Check if a rule has already been injected (deduplication check).
|
|
41
|
+
*/
|
|
42
|
+
declare function isInjected(cssRule: string): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Clear all injected rules and remove the style element.
|
|
45
|
+
* Useful for testing / SSR resets. Not for production use.
|
|
46
|
+
*/
|
|
47
|
+
declare function resetBatchedCss(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Get stats about the current injection state (for devtools).
|
|
50
|
+
*/
|
|
51
|
+
declare function getBatchedCssStats(): {
|
|
52
|
+
totalInjected: number;
|
|
53
|
+
pendingCount: number;
|
|
54
|
+
hasBatchScheduled: boolean;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export { batchedInject, flushBatchedCss, getBatchedCssStats, isInjected, resetBatchedCss, syncInject };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tailwind-styled-v4 — Batched CSS Injector (Client Runtime)
|
|
3
|
+
*
|
|
4
|
+
* Menggantikan pattern inject-per-komponen yang menyebabkan banyak
|
|
5
|
+
* style recalculation saat banyak komponen mount bersamaan.
|
|
6
|
+
*
|
|
7
|
+
* Cara kerja:
|
|
8
|
+
* - Semua CSS rules dari render cycle yang sama dikumpulkan
|
|
9
|
+
* - Satu requestAnimationFrame = satu DOM style update
|
|
10
|
+
* - Deduplication via Set<string> — rule yang sama tidak diinjeksi dua kali
|
|
11
|
+
* - Fallback synchronous untuk SSR / server context
|
|
12
|
+
*
|
|
13
|
+
* Usage (internal, dipakai oleh stateEngine dan containerQuery):
|
|
14
|
+
* import { batchedInject, flushBatchedCss } from "./batchedInjector"
|
|
15
|
+
*
|
|
16
|
+
* // Queue a rule
|
|
17
|
+
* batchedInject(".tw-s-abc123[data-active=\"true\"]{opacity:0.5}")
|
|
18
|
+
*
|
|
19
|
+
* // Force flush (biasanya tidak perlu — RAF melakukan ini otomatis)
|
|
20
|
+
* flushBatchedCss()
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Queue a CSS rule for batched injection.
|
|
24
|
+
* Multiple rules accumulated during one event loop tick are flushed together
|
|
25
|
+
* in one requestAnimationFrame → one style recalculation.
|
|
26
|
+
*/
|
|
27
|
+
declare function batchedInject(cssRule: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Immediately flush all pending CSS rules to the DOM.
|
|
30
|
+
* Called automatically by RAF each frame. Can also be called manually
|
|
31
|
+
* after synchronous component setup where RAF timing is too late.
|
|
32
|
+
*/
|
|
33
|
+
declare function flushBatchedCss(): void;
|
|
34
|
+
/**
|
|
35
|
+
* Synchronous inject — skips batching.
|
|
36
|
+
* Use for SSR / critical path where RAF is not available.
|
|
37
|
+
*/
|
|
38
|
+
declare function syncInject(cssRule: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* Check if a rule has already been injected (deduplication check).
|
|
41
|
+
*/
|
|
42
|
+
declare function isInjected(cssRule: string): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Clear all injected rules and remove the style element.
|
|
45
|
+
* Useful for testing / SSR resets. Not for production use.
|
|
46
|
+
*/
|
|
47
|
+
declare function resetBatchedCss(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Get stats about the current injection state (for devtools).
|
|
50
|
+
*/
|
|
51
|
+
declare function getBatchedCssStats(): {
|
|
52
|
+
totalInjected: number;
|
|
53
|
+
pendingCount: number;
|
|
54
|
+
hasBatchScheduled: boolean;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export { batchedInject, flushBatchedCss, getBatchedCssStats, isInjected, resetBatchedCss, syncInject };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// src/batchedInjector.ts
|
|
2
|
+
var injected = /* @__PURE__ */ new Set();
|
|
3
|
+
var pending = [];
|
|
4
|
+
var rafHandle = null;
|
|
5
|
+
var styleEl = null;
|
|
6
|
+
function getStyleElement() {
|
|
7
|
+
if (styleEl && document.head.contains(styleEl)) return styleEl;
|
|
8
|
+
styleEl = document.createElement("style");
|
|
9
|
+
styleEl.id = "__tw-runtime-css";
|
|
10
|
+
styleEl.setAttribute("data-tw-batched", "true");
|
|
11
|
+
document.head.appendChild(styleEl);
|
|
12
|
+
return styleEl;
|
|
13
|
+
}
|
|
14
|
+
function batchedInject(cssRule) {
|
|
15
|
+
if (typeof window === "undefined") return;
|
|
16
|
+
if (!cssRule || injected.has(cssRule)) return;
|
|
17
|
+
injected.add(cssRule);
|
|
18
|
+
pending.push(cssRule);
|
|
19
|
+
if (rafHandle === null) {
|
|
20
|
+
rafHandle = requestAnimationFrame(flushBatchedCss);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function flushBatchedCss() {
|
|
24
|
+
rafHandle = null;
|
|
25
|
+
if (pending.length === 0 || typeof document === "undefined") return;
|
|
26
|
+
const el = getStyleElement();
|
|
27
|
+
const css = pending.join("\n");
|
|
28
|
+
pending.length = 0;
|
|
29
|
+
el.textContent += `
|
|
30
|
+
${css}`;
|
|
31
|
+
}
|
|
32
|
+
function syncInject(cssRule) {
|
|
33
|
+
if (typeof document === "undefined") return;
|
|
34
|
+
if (!cssRule || injected.has(cssRule)) return;
|
|
35
|
+
injected.add(cssRule);
|
|
36
|
+
getStyleElement().textContent += `
|
|
37
|
+
${cssRule}`;
|
|
38
|
+
}
|
|
39
|
+
function isInjected(cssRule) {
|
|
40
|
+
return injected.has(cssRule);
|
|
41
|
+
}
|
|
42
|
+
function resetBatchedCss() {
|
|
43
|
+
injected.clear();
|
|
44
|
+
pending.length = 0;
|
|
45
|
+
if (rafHandle !== null) {
|
|
46
|
+
cancelAnimationFrame(rafHandle);
|
|
47
|
+
rafHandle = null;
|
|
48
|
+
}
|
|
49
|
+
if (styleEl && document.head.contains(styleEl)) {
|
|
50
|
+
document.head.removeChild(styleEl);
|
|
51
|
+
styleEl = null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function getBatchedCssStats() {
|
|
55
|
+
return {
|
|
56
|
+
totalInjected: injected.size,
|
|
57
|
+
pendingCount: pending.length,
|
|
58
|
+
hasBatchScheduled: rafHandle !== null
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
batchedInject,
|
|
63
|
+
flushBatchedCss,
|
|
64
|
+
getBatchedCssStats,
|
|
65
|
+
isInjected,
|
|
66
|
+
resetBatchedCss,
|
|
67
|
+
syncInject
|
|
68
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tailwind-styled/runtime-css",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "RSC-aware CSS injector for tailwind-styled-v4 — streaming-friendly, zero client JS",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/CssInjector.d.ts",
|
|
13
|
+
"import": "./dist/CssInjector.js",
|
|
14
|
+
"require": "./dist/CssInjector.cjs"
|
|
15
|
+
},
|
|
16
|
+
"./batched": {
|
|
17
|
+
"types": "./dist/batchedInjector.d.ts",
|
|
18
|
+
"import": "./dist/batchedInjector.js",
|
|
19
|
+
"require": "./dist/batchedInjector.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": ["dist"],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsup src/CssInjector.tsx src/batchedInjector.ts --format cjs,esm --dts --out-dir dist --clean",
|
|
25
|
+
"dev": "tsup src/CssInjector.tsx src/batchedInjector.ts --format cjs,esm --dts --out-dir dist --watch"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"react": ">=18",
|
|
29
|
+
"react-dom": ">=18"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"tsup": "^8",
|
|
33
|
+
"typescript": "^5",
|
|
34
|
+
"@types/react": "^19",
|
|
35
|
+
"@types/node": "^20"
|
|
36
|
+
}
|
|
37
|
+
}
|