Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract style node to support Next.js _document.tsx render style tags #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/hooks/useStyleRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,25 @@ export function extractStyle(cache: Cache) {

return styleText;
}

export function extractStyleNode(cache: Cache) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

逻辑是不是可以合并到 extractStyle 一起?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一开始我是想放一起的,直接让extraStyle 返回一个对象,让用户想获取那个就哪个,但是怕对用户来说有break change,所以重启炉灶

const styleKeys = Array.from(cache.cache.keys()).filter((key) =>
key.startsWith('style%'),
);

return styleKeys.map((key) => {
const [styleStr, tokenKey, styleId]: [string, string, string] =
cache.cache.get(key)![1];

return (
<style
key={key}
{...{
[ATTR_TOKEN]: tokenKey,
[ATTR_MARK]: styleId,
}}
dangerouslySetInnerHTML={{ __html: styleStr }}
/>
);
});
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import useCacheToken from './hooks/useCacheToken';
import type { CSSInterpolation, CSSObject } from './hooks/useStyleRegister';
import useStyleRegister, { extractStyle } from './hooks/useStyleRegister';
import useStyleRegister, {
extractStyle,
extractStyleNode,
} from './hooks/useStyleRegister';
import Keyframes from './Keyframes';
import type { Linter } from './linters';
import { logicalPropertiesLinter } from './linters';
Expand All @@ -19,6 +22,7 @@ export {
StyleProvider,
Keyframes,
extractStyle,
extractStyleNode,

// Transformer
legacyLogicalPropertiesTransformer,
Expand Down
42 changes: 34 additions & 8 deletions tests/server.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { render } from '@testing-library/react';
import * as React from 'react';
import { renderToString } from 'react-dom/server';
import { render } from '@testing-library/react';
import type { CSSInterpolation } from '../src';
import {
createCache,
extractStyle,
extractStyleNode,
StyleProvider,
Theme,
useCacheToken,
useStyleRegister,
StyleProvider,
extractStyle,
createCache,
} from '../src';
import type { CSSInterpolation } from '../src';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import canUseDom from 'rc-util/lib/Dom/canUseDom';
import classNames from 'classnames';
import {
ATTR_MARK,
CSS_IN_JS_INSTANCE,
CSS_IN_JS_INSTANCE_ID,
ATTR_MARK,
} from '../src/StyleContext';
import classNames from 'classnames';

interface DesignToken {
primaryColor: string;
Expand Down Expand Up @@ -156,6 +156,32 @@ describe('SSR', () => {
expect(errorSpy).not.toHaveBeenCalled();
});

it('ssr extract style node', () => {
// >>> SSR
const cache = createCache();

const html = renderToString(
<StyleProvider cache={cache}>
<IdHolder />
<Box>
<IdHolder />
</Box>
<IdHolder />
</StyleProvider>,
);

const styleNode = extractStyleNode(cache);
const styleText = extractStyle(cache);

const style = renderToString(<>{styleNode}</>);

expect(html).toEqual(
'<div id=":R1:" class="id">:R1:</div><div class="box"><div id=":Ra:" class="id">:Ra:</div></div><div id=":R3:" class="id">:R3:</div>',
);

expect(style).toEqual(styleText);
});

it('default hashPriority', () => {
// >>> SSR
const cache = createCache();
Expand Down