-
Notifications
You must be signed in to change notification settings - Fork 115
/
ServerStyles.tsx
67 lines (57 loc) · 1.75 KB
/
ServerStyles.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React from 'react';
import '../../global';
import useBaseUrl from '@docusaurus/useBaseUrl';
import { AppStore, Redoc, RedocRawOptions } from 'redoc';
// eslint-disable-next-line import/no-extraneous-dependencies
import { renderToString } from 'react-dom/server';
import { ServerStyleSheet } from 'styled-components';
import postcss from 'postcss';
import prefixer from 'postcss-prefix-selector';
const prefixCssSelectors = function (css: string, className: string): string {
const processor = postcss().use(
prefixer({
prefix: className,
}),
);
return processor.process(css).css;
};
const renderCss = function (store: AppStore): string {
const styleSheet = new ServerStyleSheet();
renderToString(
styleSheet.collectStyles(React.createElement(Redoc, { store })),
);
return String(styleSheet.instance);
};
const LIGHT_MODE_PREFIX = "html:not([data-theme='dark'])";
const DARK_MODE_PREFIX = "html([data-theme='dark'])";
export function ServerStyles({
specProps,
lightThemeOptions,
darkThemeOptions,
}: {
specProps: SpecProps;
lightThemeOptions: RedocRawOptions;
darkThemeOptions: RedocRawOptions;
}) {
const fullUrl = useBaseUrl(specProps.url, { absolute: true });
const lightCss = prefixCssSelectors(
renderCss(new AppStore(specProps.spec, fullUrl, lightThemeOptions)),
LIGHT_MODE_PREFIX,
);
const darkCss = prefixCssSelectors(
renderCss(new AppStore(specProps.spec, fullUrl, darkThemeOptions)),
DARK_MODE_PREFIX,
);
return (
<div className="redocusaurus-styles">
<style
key="light-mode-styles"
dangerouslySetInnerHTML={{ __html: lightCss }}
/>
<style
key="dark-mode-styles"
dangerouslySetInnerHTML={{ __html: darkCss }}
/>
</div>
);
}