@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/src/index.tsx CHANGED
@@ -1,106 +1,27 @@
1
- import React, { useEffect, useState } from 'react';
2
- import { collectStyles, getRegisteredCss } from 'typestyles/server';
3
-
4
- export { getRegisteredCss };
5
-
6
- /**
7
- * Props for the TypestylesStylesheet component.
8
- */
9
- export interface TypestylesStylesheetProps {
10
- /**
11
- * The children to render (styles will be collected from these).
12
- * If not provided, only the existing registered CSS will be included.
13
- */
14
- children?: React.ReactNode;
15
- }
16
-
17
1
  /**
18
- * A React component that renders typestyles CSS.
19
- *
20
- * For App Router: Use this in your root layout to collect and render styles.
2
+ * @typestyles/next - Next.js integration for typestyles
21
3
  *
22
- * @example
23
- * ```tsx
24
- * // app/layout.tsx
25
- * import { TypestylesStylesheet } from '@typestyles/next';
4
+ * This package provides two entry points:
26
5
  *
27
- * export default function RootLayout({ children }) {
28
- * return (
29
- * <html>
30
- * <head>
31
- * <TypestylesStylesheet />
32
- * </head>
33
- * <body>{children}</body>
34
- * </html>
35
- * );
36
- * }
37
- * ```
38
- */
39
- export function TypestylesStylesheet({ children }: TypestylesStylesheetProps) {
40
- const [css, setCss] = useState('');
41
-
42
- useEffect(() => {
43
- // On client: get any CSS that was registered before hydration
44
- setCss(getRegisteredCss());
45
- }, []);
46
-
47
- if (children) {
48
- return (
49
- <>
50
- {children}
51
- <style id="typestyles" dangerouslySetInnerHTML={{ __html: css }} />
52
- </>
53
- );
54
- }
55
-
56
- return <style id="typestyles" dangerouslySetInnerHTML={{ __html: css }} />;
57
- }
58
-
59
- /**
60
- * Collect styles from a React component and return them as a string.
61
- * Useful for server components or when you need more control.
6
+ * 1. Main entry (this file) - Re-exports everything
7
+ * 2. `/server` - Server-safe utilities (getRegisteredCss, collectStylesFromComponent, getTypestylesMetadata)
8
+ * 3. `/client` - Client components (TypestylesStylesheet, useTypestyles, useServerInsertedHTML)
62
9
  *
63
- * @example
64
- * ```tsx
65
- * // app/styles.ts
66
- * import { collectStylesFromComponent } from '@typestyles/next';
67
- * import { YourApp } from './YourApp';
10
+ * For App Router, prefer using the client entry for components:
11
+ * import { TypestylesStylesheet } from '@typestyles/next/client';
68
12
  *
69
- * export async function getStyles() {
70
- * return collectStylesFromComponent(<YourApp />);
71
- * }
72
- * ```
13
+ * Or use server utilities for SSR:
14
+ * import { getRegisteredCss } from '@typestyles/next/server';
73
15
  */
74
- export async function collectStylesFromComponent(component: React.ReactElement): Promise<string> {
75
- const { renderToString } = await import('react-dom/server');
76
- const { css } = collectStyles(() => renderToString(component));
77
- return css;
78
- }
79
16
 
80
- /**
81
- * Create a Next.js layout export that includes typestyles.
82
- *
83
- * @example
84
- * ```tsx
85
- * // app/layout.tsx
86
- * import { createTypestylesLayout } from '@typestyles/next';
87
- *
88
- * export default createTypestylesLayout(function RootLayout({ children }) {
89
- * return (
90
- * <html>
91
- * <head>
92
- * <title>My App</title>
93
- * </head>
94
- * <body>{children}</body>
95
- * </html>
96
- * );
97
- * });
98
- * ```
99
- */
100
- export function createTypestylesLayout<P extends { children?: React.ReactNode }>(
101
- layout: (props: P) => React.ReactNode,
102
- ): (props: P) => React.ReactNode {
103
- return function TypestylesLayout(props: P) {
104
- return <TypestylesStylesheet>{layout(props)}</TypestylesStylesheet>;
105
- };
106
- }
17
+ // Re-export server-safe utilities
18
+ export { getRegisteredCss, collectStylesFromComponent, getTypestylesMetadata } from './server';
19
+
20
+ // Re-export client components and hooks
21
+ export {
22
+ TypestylesStylesheet,
23
+ useTypestyles,
24
+ useServerInsertedHTML,
25
+ createTypestylesLayout,
26
+ type TypestylesStylesheetProps,
27
+ } from './client';
@@ -0,0 +1,13 @@
1
+ /**
2
+ * `useServerInsertedHTML` exists at runtime in Next.js App Router / React 18.3+ client builds,
3
+ * but some `@types/react` releases omit it. Augment `react` so local `tsc` and tooling match runtime.
4
+ *
5
+ * @see https://react.dev/reference/react-dom/server/useServerInsertedHTML
6
+ */
7
+ import type { ReactNode } from 'react';
8
+
9
+ declare module 'react' {
10
+ export function useServerInsertedHTML(callback: () => ReactNode | null): void;
11
+ }
12
+
13
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { ReactElement } from 'react';
2
+
3
+ export { getRegisteredCss } from 'typestyles/server';
4
+
5
+ export declare function collectStylesFromComponent(component: ReactElement): Promise<string>;
6
+
7
+ export declare function getTypestylesMetadata(component: ReactElement): Promise<string>;
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
+ }