@typestyles/next 0.2.0 → 0.3.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/README.md ADDED
@@ -0,0 +1,334 @@
1
+ # @typestyles/next
2
+
3
+ Next.js integration for typestyles with full support for App Router, Pages Router, and React Server Components.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @typestyles/next typestyles
9
+ # or
10
+ pnpm add @typestyles/next typestyles
11
+ # or
12
+ yarn add @typestyles/next typestyles
13
+ ```
14
+
15
+ ## Requirements
16
+
17
+ - Next.js >= 13.0.0
18
+ - React >= 18.0.0
19
+ - typestyles >= 0.1.0
20
+
21
+ ## Quick Start
22
+
23
+ ### App Router (Recommended)
24
+
25
+ Import `getRegisteredCss` in your root layout to inject styles during SSR:
26
+
27
+ ```tsx
28
+ // app/layout.tsx
29
+ import { getRegisteredCss } from '@typestyles/next';
30
+
31
+ export default function RootLayout({ children }: { children: React.ReactNode }) {
32
+ const css = getRegisteredCss();
33
+
34
+ return (
35
+ <html lang="en">
36
+ <head>{css && <style dangerouslySetInnerHTML={{ __html: css }} />}</head>
37
+ <body>{children}</body>
38
+ </html>
39
+ );
40
+ }
41
+ ```
42
+
43
+ ### Pages Router
44
+
45
+ Wrap your pages with the stylesheet component:
46
+
47
+ ```tsx
48
+ // pages/_app.tsx
49
+ import { TypestylesStylesheet } from '@typestyles/next';
50
+ import type { AppProps } from 'next/app';
51
+
52
+ export default function App({ Component, pageProps }: AppProps) {
53
+ return (
54
+ <TypestylesStylesheet>
55
+ <Component {...pageProps} />
56
+ </TypestylesStylesheet>
57
+ );
58
+ }
59
+ ```
60
+
61
+ ## React Server Components (RSC)
62
+
63
+ The package provides multiple approaches for RSC support:
64
+
65
+ ### Option 1: Layout SSR (Simplest)
66
+
67
+ Import `getRegisteredCss` in your root layout - this works in both Server and Client Components:
68
+
69
+ ```tsx
70
+ // app/layout.tsx
71
+ import { getRegisteredCss } from '@typestyles/next';
72
+
73
+ export default function RootLayout({ children }) {
74
+ const css = getRegisteredCss();
75
+
76
+ return (
77
+ <html>
78
+ <head>{css && <style dangerouslySetInnerHTML={{ __html: css }} />}</head>
79
+ <body>{children}</body>
80
+ </html>
81
+ );
82
+ }
83
+ ```
84
+
85
+ ### Option 2: Client Component Provider
86
+
87
+ For complex cases with dynamic styles, create a client component wrapper:
88
+
89
+ ```tsx
90
+ // components/TypestylesProvider.tsx
91
+ 'use client';
92
+
93
+ import { getRegisteredCss } from 'typestyles/server';
94
+ import { useEffect, useState } from 'react';
95
+
96
+ export function TypestylesProvider({ children }) {
97
+ const [css, setCss] = useState('');
98
+ const [mounted, setMounted] = useState(false);
99
+
100
+ useEffect(() => {
101
+ setMounted(true);
102
+ setCss(getRegisteredCss());
103
+ }, []);
104
+
105
+ return (
106
+ <>
107
+ {children}
108
+ {mounted && css && <style id="typestyles" dangerouslySetInnerHTML={{ __html: css }} />}
109
+ </>
110
+ );
111
+ }
112
+ ```
113
+
114
+ Then use it in your layout:
115
+
116
+ ```tsx
117
+ // app/layout.tsx
118
+ import { TypestylesProvider } from '@/components/TypestylesProvider';
119
+
120
+ export default function RootLayout({ children }) {
121
+ return (
122
+ <html>
123
+ <body>
124
+ <TypestylesProvider>{children}</TypestylesProvider>
125
+ </body>
126
+ </html>
127
+ );
128
+ }
129
+ ```
130
+
131
+ ### Option 3: Collecting from a specific component tree
132
+
133
+ `getTypestylesMetadata` (alias of `collectStylesFromComponent`) renders a React element on the server and returns the CSS registered during that render. Use it when you need CSS scoped to a subtree rather than the whole app. For App Router pages, injecting the result still belongs in `layout.tsx` / `<head>` (Next.js `Metadata` does not provide a supported hook for arbitrary `<style>` payloads).
134
+
135
+ ```tsx
136
+ import { getTypestylesMetadata } from '@typestyles/next/server';
137
+ import { Home } from './Home';
138
+
139
+ const css = await getTypestylesMetadata(<Home />);
140
+ // Pass `css` into your layout <style> or streaming head pipeline.
141
+ ```
142
+
143
+ ## Build-time CSS + Turbopack
144
+
145
+ To ship a static `typestyles.css` and avoid client-side `<style>` injection (uses `typestyles/build` under the hood):
146
+
147
+ 1. Run `buildTypestylesForNext` before `next build` to emit CSS (and optional manifest).
148
+ 2. Import that CSS from your root layout (e.g. `import './typestyles.css'`).
149
+ 3. Wrap config with `withTypestylesExtract` from `@typestyles/next/build`.
150
+
151
+ `withTypestylesExtract` sets **`NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED`** via `next.config` `env` (works with **webpack and Turbopack**) and adds webpack **`DefinePlugin`** for `__TYPESTYLES_RUNTIME_DISABLED__` on client bundles when webpack runs. Example app: `examples/next-app`.
152
+
153
+ ### Runtime in development, zero-runtime in production
154
+
155
+ During `next dev`, keeping the client runtime **enabled** avoids tying every style tweak to a pre-build extraction step. In production, disable injection so the browser loads one cacheable stylesheet.
156
+
157
+ ```js
158
+ // next.config.mjs
159
+ import { withTypestylesExtract } from '@typestyles/next/build';
160
+
161
+ const base = {
162
+ /* your config */
163
+ };
164
+
165
+ export default process.env.NODE_ENV === 'production' ? withTypestylesExtract(base) : base;
166
+ ```
167
+
168
+ You still typically generate `typestyles.css` before production builds (CI or `prebuild`). The example app uses this pattern.
169
+
170
+ ## API Reference
171
+
172
+ ### getRegisteredCss
173
+
174
+ Returns all currently registered CSS as a string. This is the simplest way to get styles for SSR.
175
+
176
+ ```tsx
177
+ import { getRegisteredCss } from '@typestyles/next';
178
+
179
+ const css = getRegisteredCss();
180
+ ```
181
+
182
+ ### collectStylesFromComponent
183
+
184
+ Collect styles from a React component tree. Useful when you need explicit control over style collection.
185
+
186
+ ```tsx
187
+ import { collectStylesFromComponent } from '@typestyles/next/server';
188
+ import { YourComponent } from './YourComponent';
189
+
190
+ const css = await collectStylesFromComponent(<YourComponent />);
191
+ ```
192
+
193
+ ### getTypestylesMetadata
194
+
195
+ Same as `collectStylesFromComponent` — renders the given element on the server and returns registered CSS. Use when you want a dedicated name for “CSS for this subtree.”
196
+
197
+ ```tsx
198
+ import { getTypestylesMetadata } from '@typestyles/next/server';
199
+ import { Home } from './Home';
200
+
201
+ const css = await getTypestylesMetadata(<Home />);
202
+ ```
203
+
204
+ ### TypestylesStylesheet (Pages Router)
205
+
206
+ A React component that renders typestyles CSS. Works with Pages Router.
207
+
208
+ ```tsx
209
+ import { TypestylesStylesheet } from '@typestyles/next';
210
+
211
+ <TypestylesStylesheet>
212
+ <YourApp />
213
+ </TypestylesStylesheet>;
214
+ ```
215
+
216
+ ## Examples
217
+
218
+ ### Basic Usage with App Router
219
+
220
+ ```tsx
221
+ // app/page.tsx
222
+ import { styles } from 'typestyles';
223
+
224
+ const button = styles.component('button', {
225
+ base: {
226
+ padding: '12px 24px',
227
+ backgroundColor: '#0066ff',
228
+ color: 'white',
229
+ border: 'none',
230
+ borderRadius: '6px',
231
+ cursor: 'pointer',
232
+ },
233
+ });
234
+
235
+ export default function Home() {
236
+ return (
237
+ <main>
238
+ <button className={button()}>Click me</button>
239
+ </main>
240
+ );
241
+ }
242
+ ```
243
+
244
+ ### With Design Tokens
245
+
246
+ ```tsx
247
+ // app/tokens.ts
248
+ import { tokens } from 'typestyles';
249
+
250
+ export const colors = tokens.create('color', {
251
+ primary: '#0066ff',
252
+ secondary: '#64748b',
253
+ });
254
+
255
+ export const spacing = tokens.create('space', {
256
+ sm: '8px',
257
+ md: '16px',
258
+ lg: '24px',
259
+ });
260
+
261
+ // app/page.tsx
262
+ import { styles } from 'typestyles';
263
+ import { colors, spacing } from './tokens';
264
+
265
+ const card = styles.component('card', {
266
+ base: {
267
+ padding: spacing.md,
268
+ backgroundColor: colors.primary,
269
+ borderRadius: '8px',
270
+ },
271
+ });
272
+
273
+ export default function Page() {
274
+ return <div className={card()}>Hello World</div>;
275
+ }
276
+ ```
277
+
278
+ ### With Dark Mode
279
+
280
+ ```tsx
281
+ // app/layout.tsx
282
+ import { tokens } from 'typestyles';
283
+ import { getRegisteredCss } from '@typestyles/next';
284
+
285
+ const darkTheme = tokens.createTheme('dark', {
286
+ base: {
287
+ color: {
288
+ background: '#1a1a1a',
289
+ text: '#ffffff',
290
+ },
291
+ },
292
+ });
293
+
294
+ export default function RootLayout({ children }) {
295
+ const css = getRegisteredCss();
296
+
297
+ return (
298
+ <html className={darkTheme.className}>
299
+ <head>{css && <style dangerouslySetInnerHTML={{ __html: css }} />}</head>
300
+ <body>{children}</body>
301
+ </html>
302
+ );
303
+ }
304
+ ```
305
+
306
+ ## Fonts and local files
307
+
308
+ `global.fontFace()` registers `@font-face` like any other TypeStyles global. For **Next’s pre-build extraction** (`buildTypestylesForNext`), prefer fonts under **`public/fonts/`** and a root-relative `url('/fonts/…')` in `src` so Node does not need to resolve binary imports.
309
+
310
+ For **Vite**, you can use `import file from './font.woff2?url'` and `url(\`${file}\`) format('woff2')` so production CSS points at hashed assets.
311
+
312
+ See the **Fonts** guide in the TypeStyles docs (`docs/content/docs/fonts.md`, route `/docs/fonts` on the docs site).
313
+
314
+ ## Troubleshooting
315
+
316
+ ### Flash of Unstyled Content (FOUC)
317
+
318
+ If you see a flash of unstyled content, ensure `getRegisteredCss()` is called in the layout and styles are injected before the body renders.
319
+
320
+ ### Styles not appearing
321
+
322
+ Make sure `typestyles` is installed in your project:
323
+
324
+ ```bash
325
+ npm install typestyles
326
+ ```
327
+
328
+ ### TypeScript errors
329
+
330
+ Ensure you have `"moduleResolution": "bundler"` or `"node"` in your `tsconfig.json`.
331
+
332
+ ## License
333
+
334
+ MIT
package/dist/build.cjs ADDED
@@ -0,0 +1,89 @@
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/build.ts
21
+ var build_exports = {};
22
+ __export(build_exports, {
23
+ buildTypestylesForNext: () => buildTypestylesForNext,
24
+ withTypestylesExtract: () => withTypestylesExtract
25
+ });
26
+ module.exports = __toCommonJS(build_exports);
27
+ var import_promises = require("fs/promises");
28
+ var import_node_module = require("module");
29
+ var import_node_path = require("path");
30
+ var import_node_url = require("url");
31
+ var import_build = require("typestyles/build");
32
+ async function buildTypestylesForNext(options) {
33
+ const { root, modules, cssOutFile, manifestOutFile, manifestCssPath } = options;
34
+ const loaders = modules.map((mod) => {
35
+ const abs = (0, import_node_path.resolve)(root, mod);
36
+ const href = (0, import_node_url.pathToFileURL)(abs).href;
37
+ return () => import(href);
38
+ });
39
+ const css = await (0, import_build.collectStylesFromModules)(loaders);
40
+ const outAbs = (0, import_node_path.resolve)(root, cssOutFile);
41
+ await (0, import_promises.mkdir)((0, import_node_path.dirname)(outAbs), { recursive: true });
42
+ await (0, import_promises.writeFile)(outAbs, css, "utf8");
43
+ if (manifestOutFile) {
44
+ const manifest = {
45
+ version: 1,
46
+ css: manifestCssPath ?? cssOutFile
47
+ };
48
+ const manAbs = (0, import_node_path.resolve)(root, manifestOutFile);
49
+ await (0, import_promises.mkdir)((0, import_node_path.dirname)(manAbs), { recursive: true });
50
+ await (0, import_promises.writeFile)(manAbs, `${JSON.stringify(manifest, null, 2)}
51
+ `, "utf8");
52
+ }
53
+ }
54
+ function withTypestylesExtract(nextConfig = {}, options = {}) {
55
+ const disableClientRuntime = options.disableClientRuntime !== false;
56
+ return {
57
+ ...nextConfig,
58
+ ...disableClientRuntime ? {
59
+ env: {
60
+ ...typeof nextConfig.env === "object" && nextConfig.env !== null && !Array.isArray(nextConfig.env) ? nextConfig.env : {},
61
+ NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED: "true"
62
+ }
63
+ } : {},
64
+ webpack(config, context) {
65
+ const nextWebpack = typeof nextConfig.webpack === "function" ? nextConfig.webpack(config, context) : config;
66
+ if (disableClientRuntime && !context.isServer) {
67
+ try {
68
+ const require2 = (0, import_node_module.createRequire)((0, import_node_path.join)(process.cwd(), "package.json"));
69
+ const webpackLib = require2("webpack");
70
+ nextWebpack.plugins.push(
71
+ new webpackLib.DefinePlugin({
72
+ __TYPESTYLES_RUNTIME_DISABLED__: JSON.stringify("true")
73
+ })
74
+ );
75
+ } catch {
76
+ console.warn(
77
+ "[@typestyles/next/build] Could not load webpack; client bundles may still inject <style>."
78
+ );
79
+ }
80
+ }
81
+ return nextWebpack;
82
+ }
83
+ };
84
+ }
85
+ // Annotate the CommonJS export names for ESM import in node:
86
+ 0 && (module.exports = {
87
+ buildTypestylesForNext,
88
+ withTypestylesExtract
89
+ });
package/dist/build.js ADDED
@@ -0,0 +1,63 @@
1
+ // src/build.ts
2
+ import { mkdir, writeFile } from "fs/promises";
3
+ import { createRequire } from "module";
4
+ import { dirname, join, resolve } from "path";
5
+ import { pathToFileURL } from "url";
6
+ import { collectStylesFromModules } from "typestyles/build";
7
+ async function buildTypestylesForNext(options) {
8
+ const { root, modules, cssOutFile, manifestOutFile, manifestCssPath } = options;
9
+ const loaders = modules.map((mod) => {
10
+ const abs = resolve(root, mod);
11
+ const href = pathToFileURL(abs).href;
12
+ return () => import(href);
13
+ });
14
+ const css = await collectStylesFromModules(loaders);
15
+ const outAbs = resolve(root, cssOutFile);
16
+ await mkdir(dirname(outAbs), { recursive: true });
17
+ await writeFile(outAbs, css, "utf8");
18
+ if (manifestOutFile) {
19
+ const manifest = {
20
+ version: 1,
21
+ css: manifestCssPath ?? cssOutFile
22
+ };
23
+ const manAbs = resolve(root, manifestOutFile);
24
+ await mkdir(dirname(manAbs), { recursive: true });
25
+ await writeFile(manAbs, `${JSON.stringify(manifest, null, 2)}
26
+ `, "utf8");
27
+ }
28
+ }
29
+ function withTypestylesExtract(nextConfig = {}, options = {}) {
30
+ const disableClientRuntime = options.disableClientRuntime !== false;
31
+ return {
32
+ ...nextConfig,
33
+ ...disableClientRuntime ? {
34
+ env: {
35
+ ...typeof nextConfig.env === "object" && nextConfig.env !== null && !Array.isArray(nextConfig.env) ? nextConfig.env : {},
36
+ NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED: "true"
37
+ }
38
+ } : {},
39
+ webpack(config, context) {
40
+ const nextWebpack = typeof nextConfig.webpack === "function" ? nextConfig.webpack(config, context) : config;
41
+ if (disableClientRuntime && !context.isServer) {
42
+ try {
43
+ const require2 = createRequire(join(process.cwd(), "package.json"));
44
+ const webpackLib = require2("webpack");
45
+ nextWebpack.plugins.push(
46
+ new webpackLib.DefinePlugin({
47
+ __TYPESTYLES_RUNTIME_DISABLED__: JSON.stringify("true")
48
+ })
49
+ );
50
+ } catch {
51
+ console.warn(
52
+ "[@typestyles/next/build] Could not load webpack; client bundles may still inject <style>."
53
+ );
54
+ }
55
+ }
56
+ return nextWebpack;
57
+ }
58
+ };
59
+ }
60
+ export {
61
+ buildTypestylesForNext,
62
+ withTypestylesExtract
63
+ };
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ "use client";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/client.tsx
22
+ var client_exports = {};
23
+ __export(client_exports, {
24
+ TypestylesStylesheet: () => TypestylesStylesheet,
25
+ createTypestylesLayout: () => createTypestylesLayout,
26
+ useServerInsertedHTML: () => import_react.useServerInsertedHTML,
27
+ useTypestyles: () => useTypestyles
28
+ });
29
+ module.exports = __toCommonJS(client_exports);
30
+ var import_react = require("react");
31
+ var import_server = require("typestyles/server");
32
+ var import_jsx_runtime = require("react/jsx-runtime");
33
+ function useTypestyles() {
34
+ return (0, import_react.useSyncExternalStore)(
35
+ () => () => {
36
+ },
37
+ () => (0, import_server.getRegisteredCss)(),
38
+ () => ""
39
+ );
40
+ }
41
+ function TypestylesStylesheet({ children }) {
42
+ const [css, setCss] = (0, import_react.useState)("");
43
+ (0, import_react.useServerInsertedHTML)(() => {
44
+ const registeredCss = (0, import_server.getRegisteredCss)();
45
+ if (registeredCss) {
46
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: registeredCss } });
47
+ }
48
+ return null;
49
+ });
50
+ (0, import_react.useEffect)(() => {
51
+ setCss((0, import_server.getRegisteredCss)());
52
+ }, []);
53
+ if (children) {
54
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
55
+ children,
56
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles-client", dangerouslySetInnerHTML: { __html: css } })
57
+ ] });
58
+ }
59
+ return null;
60
+ }
61
+ function createTypestylesLayout(layout) {
62
+ return function TypestylesLayout(props) {
63
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TypestylesStylesheet, { children: layout(props) });
64
+ };
65
+ }
66
+ // Annotate the CommonJS export names for ESM import in node:
67
+ 0 && (module.exports = {
68
+ TypestylesStylesheet,
69
+ createTypestylesLayout,
70
+ useServerInsertedHTML,
71
+ useTypestyles
72
+ });
package/dist/client.js ADDED
@@ -0,0 +1,45 @@
1
+ "use client";
2
+
3
+ // src/client.tsx
4
+ import { useState, useEffect, useServerInsertedHTML, useSyncExternalStore } from "react";
5
+ import { getRegisteredCss } from "typestyles/server";
6
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
+ function useTypestyles() {
8
+ return useSyncExternalStore(
9
+ () => () => {
10
+ },
11
+ () => getRegisteredCss(),
12
+ () => ""
13
+ );
14
+ }
15
+ function TypestylesStylesheet({ children }) {
16
+ const [css, setCss] = useState("");
17
+ useServerInsertedHTML(() => {
18
+ const registeredCss = getRegisteredCss();
19
+ if (registeredCss) {
20
+ return /* @__PURE__ */ jsx("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: registeredCss } });
21
+ }
22
+ return null;
23
+ });
24
+ useEffect(() => {
25
+ setCss(getRegisteredCss());
26
+ }, []);
27
+ if (children) {
28
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
29
+ children,
30
+ /* @__PURE__ */ jsx("style", { id: "typestyles-client", dangerouslySetInnerHTML: { __html: css } })
31
+ ] });
32
+ }
33
+ return null;
34
+ }
35
+ function createTypestylesLayout(layout) {
36
+ return function TypestylesLayout(props) {
37
+ return /* @__PURE__ */ jsx(TypestylesStylesheet, { children: layout(props) });
38
+ };
39
+ }
40
+ export {
41
+ TypestylesStylesheet,
42
+ createTypestylesLayout,
43
+ useServerInsertedHTML,
44
+ useTypestyles
45
+ };
package/dist/index.cjs CHANGED
@@ -33,29 +33,55 @@ __export(index_exports, {
33
33
  TypestylesStylesheet: () => TypestylesStylesheet,
34
34
  collectStylesFromComponent: () => collectStylesFromComponent,
35
35
  createTypestylesLayout: () => createTypestylesLayout,
36
- getRegisteredCss: () => import_server.getRegisteredCss
36
+ getRegisteredCss: () => import_server.getRegisteredCss,
37
+ getTypestylesMetadata: () => getTypestylesMetadata,
38
+ useServerInsertedHTML: () => import_react.useServerInsertedHTML,
39
+ useTypestyles: () => useTypestyles
37
40
  });
38
41
  module.exports = __toCommonJS(index_exports);
39
- var import_react = require("react");
42
+
43
+ // src/server.ts
40
44
  var import_server = require("typestyles/server");
45
+ async function collectStylesFromComponent(component) {
46
+ const { renderToString } = await import("react-dom/server");
47
+ const { css } = (0, import_server.collectStyles)(() => renderToString(component));
48
+ return css;
49
+ }
50
+ async function getTypestylesMetadata(component) {
51
+ return collectStylesFromComponent(component);
52
+ }
53
+
54
+ // src/client.tsx
55
+ var import_react = require("react");
56
+ var import_server2 = require("typestyles/server");
41
57
  var import_jsx_runtime = require("react/jsx-runtime");
58
+ function useTypestyles() {
59
+ return (0, import_react.useSyncExternalStore)(
60
+ () => () => {
61
+ },
62
+ () => (0, import_server2.getRegisteredCss)(),
63
+ () => ""
64
+ );
65
+ }
42
66
  function TypestylesStylesheet({ children }) {
43
67
  const [css, setCss] = (0, import_react.useState)("");
68
+ (0, import_react.useServerInsertedHTML)(() => {
69
+ const registeredCss = (0, import_server2.getRegisteredCss)();
70
+ if (registeredCss) {
71
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: registeredCss } });
72
+ }
73
+ return null;
74
+ });
44
75
  (0, import_react.useEffect)(() => {
45
- setCss((0, import_server.getRegisteredCss)());
76
+ setCss((0, import_server2.getRegisteredCss)());
46
77
  }, []);
47
78
  if (children) {
48
79
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
49
80
  children,
50
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: css } })
81
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles-client", dangerouslySetInnerHTML: { __html: css } })
51
82
  ] });
52
83
  }
53
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: css } });
54
- }
55
- async function collectStylesFromComponent(component) {
56
- const { renderToString } = await import("react-dom/server");
57
- const { css } = (0, import_server.collectStyles)(() => renderToString(component));
58
- return css;
84
+ return null;
59
85
  }
60
86
  function createTypestylesLayout(layout) {
61
87
  return function TypestylesLayout(props) {
@@ -67,5 +93,8 @@ function createTypestylesLayout(layout) {
67
93
  TypestylesStylesheet,
68
94
  collectStylesFromComponent,
69
95
  createTypestylesLayout,
70
- getRegisteredCss
96
+ getRegisteredCss,
97
+ getTypestylesMetadata,
98
+ useServerInsertedHTML,
99
+ useTypestyles
71
100
  });