Skip to content

Commit

Permalink
feat(design): ssr support (#1603)
Browse files Browse the repository at this point in the history
  • Loading branch information
jikkai committed Mar 15, 2024
1 parent ab76ebd commit cdb7d49
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -25,7 +25,7 @@
"scripts": {
"prepare": "husky install",
"pre-commit": "lint-staged",
"dev:demo": "turbo dev:demo",
"dev": "turbo dev:demo",
"lint:types": "turbo lint:types",
"test": "turbo test -- --passWithNoTests",
"coverage": "turbo coverage -- --passWithNoTests",
Expand Down
Expand Up @@ -15,19 +15,20 @@
*/

import React, { createContext } from 'react';
import canUseDom from 'rc-util/lib/Dom/canUseDom';

import type { ILocale } from '../../locale';
import { enUS } from '../../locale';

export interface IConfigProviderProps {
children: React.ReactNode;
locale: ILocale;
mountContainer: HTMLElement;
mountContainer: HTMLElement | null;
}

export const ConfigContext = createContext<Omit<IConfigProviderProps, 'children'>>({
locale: enUS,
mountContainer: document.body,
mountContainer: canUseDom() ? document.body : null,
});

export function ConfigProvider(props: IConfigProviderProps) {
Expand Down
2 changes: 1 addition & 1 deletion packages/design/src/components/dialog/Dialog.tsx
Expand Up @@ -176,7 +176,7 @@ export function Dialog(props: IDialogProps) {
: modal;
};

return (
return mountContainer && (
<RcDialog
width={width}
prefixCls={styles.dialog}
Expand Down
2 changes: 1 addition & 1 deletion packages/design/src/components/dropdown/Dropdown.tsx
Expand Up @@ -84,7 +84,7 @@ export function Dropdown(props: IDropdownProps) {

const { mountContainer } = useContext(ConfigContext);

return (
return mountContainer && (
<RcDropdown
{...props}
overlayClassName={className}
Expand Down
2 changes: 1 addition & 1 deletion packages/design/src/components/menu/Menu.tsx
Expand Up @@ -24,7 +24,7 @@ import styles from './index.module.less';
export function Menu(props: MenuProps) {
const { mountContainer } = useContext(ConfigContext);

return React.cloneElement(<RcMenu prefixCls={styles.menu} getPopupContainer={() => mountContainer} />, {
return mountContainer && React.cloneElement(<RcMenu prefixCls={styles.menu} getPopupContainer={() => mountContainer} />, {
...props,
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/design/src/components/select/Select.tsx
Expand Up @@ -61,7 +61,7 @@ export function Select(props: ISelectProps) {

const { mountContainer } = useContext(ConfigContext);

return (
return mountContainer && (
<RcSelect
mode={mode}
prefixCls={styles.select}
Expand Down
2 changes: 1 addition & 1 deletion packages/design/src/components/tooltip/Tooltip.tsx
Expand Up @@ -42,7 +42,7 @@ export const Tooltip = forwardRef((props: ITooltipProps, ref: Ref<TooltipRef>) =

const { mountContainer } = useContext(ConfigContext);

return (
return mountContainer && (
<RcTooltip
visible={visible}
ref={ref}
Expand Down
4 changes: 4 additions & 0 deletions packages/design/src/themes/theme.ts
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import canUseDom from 'rc-util/lib/Dom/canUseDom';

import styles from './theme-root.module.less';

function convertToDashCase(input: string): string {
Expand Down Expand Up @@ -41,6 +43,8 @@ class Theme {
private _themeRootName = styles.theme;

constructor() {
if (!canUseDom()) return;

const $style = document.createElement('style');
$style.id = this._themeRootName;
document.head.appendChild($style);
Expand Down
3 changes: 0 additions & 3 deletions packages/docs/vite.config.ts
Expand Up @@ -4,7 +4,4 @@ import pkg from './package.json';
export default ({ mode }) => createViteConfig({}, {
mode,
pkg,
features: {
dom: true, // FIXME: should not use dom here
},
});
2 changes: 1 addition & 1 deletion packages/facade/vite.config.ts
Expand Up @@ -8,7 +8,7 @@ export default ({ mode }) => createViteConfig({
deps: {
inline: ['vitest-canvas-mock'],
},
// For this config, check https://github.com/vitest-dev/vitest/issues/740
// For this config, check https://github.com/vitest-dev/vitest/issues/740
threads: false,
environmentOptions: {
jsdom: {
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/components/notification/Notification.tsx
Expand Up @@ -89,6 +89,8 @@ export const PureContent = (props: INotificationMethodOptions) => {
export function Notification() {
const { mountContainer } = useContext(ConfigContext);

if (!mountContainer) return <></>;

const [api, contextHolder] = useNotification({
prefixCls: styles.notification,
maxCount: 3,
Expand Down

0 comments on commit cdb7d49

Please sign in to comment.