@typestyles/next 0.0.0-unstable.015bb9921923
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 +307 -0
- package/dist/build.cjs +89 -0
- package/dist/build.js +63 -0
- package/dist/client.cjs +72 -0
- package/dist/client.js +45 -0
- package/dist/index.cjs +100 -0
- package/dist/index.js +57 -0
- package/dist/server.cjs +52 -0
- package/dist/server.js +15 -0
- package/package.json +88 -0
- package/src/build.d.ts +27 -0
- package/src/build.test.ts +58 -0
- package/src/build.ts +112 -0
- package/src/client.d.ts +37 -0
- package/src/client.tsx +143 -0
- package/src/index.d.ts +15 -0
- package/src/index.tsx +27 -0
- package/src/react-augmentation.d.ts +13 -0
- package/src/server.d.ts +7 -0
- package/src/server.ts +55 -0
package/src/server.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-safe utilities for Next.js integration.
|
|
3
|
+
* These functions can be used in Server Components without adding "use client".
|
|
4
|
+
*/
|
|
5
|
+
import { collectStyles, getRegisteredCss } from 'typestyles/server';
|
|
6
|
+
|
|
7
|
+
export { getRegisteredCss };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Collect styles from a React component and return them as a string.
|
|
11
|
+
* Useful for server components or when you need more control.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* // app/styles.ts
|
|
16
|
+
* import { collectStylesFromComponent } from '@typestyles/next/server';
|
|
17
|
+
* import { YourApp } from './YourApp';
|
|
18
|
+
*
|
|
19
|
+
* export async function getStyles() {
|
|
20
|
+
* return collectStylesFromComponent(<YourApp />);
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export async function collectStylesFromComponent(component: React.ReactElement): Promise<string> {
|
|
25
|
+
const { renderToString } = await import('react-dom/server');
|
|
26
|
+
const { css } = collectStyles(() => renderToString(component));
|
|
27
|
+
return css;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Collect CSS by rendering a React element on the server (alias for {@link collectStylesFromComponent}).
|
|
32
|
+
* Use when you need styles for a specific subtree; inject the returned string in `<head>` / layout as needed.
|
|
33
|
+
*
|
|
34
|
+
* Next.js `metadata` / `generateMetadata` does not support injecting arbitrary `<style>` payloads; prefer
|
|
35
|
+
* {@link getRegisteredCss} in root `layout.tsx` or pipe this string into your own head/streaming pipeline.
|
|
36
|
+
*
|
|
37
|
+
* Note: This only captures CSS registered during that render.
|
|
38
|
+
* For dynamic styles that may be registered client-side, use TypestylesStylesheet.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```tsx
|
|
42
|
+
* // app/some-route/head-styles.tsx (Server Component)
|
|
43
|
+
* import { getTypestylesMetadata } from '@typestyles/next/server';
|
|
44
|
+
* import { ProductPage } from './ProductPage';
|
|
45
|
+
*
|
|
46
|
+
* export async function ProductPageStyles() {
|
|
47
|
+
* const css = await getTypestylesMetadata(<ProductPage />);
|
|
48
|
+
* if (!css) return null;
|
|
49
|
+
* return <style id="typestyles-product" dangerouslySetInnerHTML={{ __html: css }} />;
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export async function getTypestylesMetadata(component: React.ReactElement): Promise<string> {
|
|
54
|
+
return collectStylesFromComponent(component);
|
|
55
|
+
}
|