2023-10-16 14:49:01 +08:00
|
|
|
import * as React from 'react';
|
2016-12-30 21:41:28 +08:00
|
|
|
import classNames from 'classnames';
|
2023-02-28 16:26:21 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2023-10-16 14:49:01 +08:00
|
|
|
|
2020-11-06 10:26:45 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-10-16 14:49:01 +08:00
|
|
|
import { LayoutContext } from './context';
|
|
|
|
import useHasSider from './hooks/useHasSider';
|
2022-04-22 15:00:18 +08:00
|
|
|
import useStyle from './style';
|
2016-12-30 21:41:28 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
export interface GeneratorProps {
|
2023-02-27 15:35:22 +08:00
|
|
|
suffixCls?: string;
|
2023-07-24 15:38:55 +08:00
|
|
|
tagName: 'header' | 'footer' | 'main' | 'div';
|
2020-01-28 15:03:04 +08:00
|
|
|
displayName: string;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2018-03-06 15:51:06 +08:00
|
|
|
export interface BasicProps extends React.HTMLAttributes<HTMLDivElement> {
|
2016-12-30 21:41:28 +08:00
|
|
|
prefixCls?: string;
|
2023-02-27 15:35:22 +08:00
|
|
|
suffixCls?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2018-01-12 20:44:35 +08:00
|
|
|
hasSider?: boolean;
|
2019-03-05 14:39:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
interface BasicPropsWithTagName extends BasicProps {
|
2023-07-24 15:38:55 +08:00
|
|
|
tagName: 'header' | 'footer' | 'main' | 'div';
|
2016-12-30 21:41:28 +08:00
|
|
|
}
|
|
|
|
|
2020-01-28 15:03:04 +08:00
|
|
|
function generator({ suffixCls, tagName, displayName }: GeneratorProps) {
|
2019-08-06 14:40:17 +08:00
|
|
|
return (BasicComponent: any) => {
|
2023-02-27 15:35:22 +08:00
|
|
|
const Adapter = React.forwardRef<HTMLElement, BasicProps>((props, ref) => (
|
|
|
|
<BasicComponent ref={ref} suffixCls={suffixCls} tagName={tagName} {...props} />
|
|
|
|
));
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Adapter.displayName = displayName;
|
|
|
|
}
|
2020-11-06 10:26:45 +08:00
|
|
|
return Adapter;
|
2016-12-30 21:41:28 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-24 15:38:55 +08:00
|
|
|
const Basic = React.forwardRef<HTMLDivElement, BasicPropsWithTagName>((props, ref) => {
|
2023-02-27 15:35:22 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
suffixCls,
|
|
|
|
className,
|
|
|
|
tagName: TagName,
|
|
|
|
...others
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('layout', customizePrefixCls);
|
|
|
|
|
2023-07-11 10:44:18 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2023-02-27 15:35:22 +08:00
|
|
|
|
|
|
|
const prefixWithSuffixCls = suffixCls ? `${prefixCls}-${suffixCls}` : prefixCls;
|
2022-04-22 15:00:18 +08:00
|
|
|
|
2023-02-27 15:35:22 +08:00
|
|
|
return wrapSSR(
|
|
|
|
<TagName
|
|
|
|
className={classNames(customizePrefixCls || prefixWithSuffixCls, className, hashId)}
|
|
|
|
ref={ref}
|
|
|
|
{...others}
|
|
|
|
/>,
|
|
|
|
);
|
2022-03-22 16:52:20 +08:00
|
|
|
});
|
2017-11-09 20:04:34 +08:00
|
|
|
|
2023-07-24 15:38:55 +08:00
|
|
|
const BasicLayout = React.forwardRef<HTMLDivElement, BasicPropsWithTagName>((props, ref) => {
|
2020-11-06 10:26:45 +08:00
|
|
|
const { direction } = React.useContext(ConfigContext);
|
2018-12-12 03:40:36 +08:00
|
|
|
|
2020-11-06 10:26:45 +08:00
|
|
|
const [siders, setSiders] = React.useState<string[]>([]);
|
2017-11-09 20:04:34 +08:00
|
|
|
|
2023-01-20 11:03:50 +08:00
|
|
|
const {
|
2023-02-27 15:35:22 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2023-01-20 11:03:50 +08:00
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
children,
|
|
|
|
hasSider,
|
|
|
|
tagName: Tag,
|
2023-06-28 13:51:24 +08:00
|
|
|
style,
|
2023-01-20 11:03:50 +08:00
|
|
|
...others
|
|
|
|
} = props;
|
2023-02-27 15:35:22 +08:00
|
|
|
|
2023-02-28 16:26:21 +08:00
|
|
|
const passedProps = omit(others, ['suffixCls']);
|
|
|
|
|
2023-06-28 13:51:24 +08:00
|
|
|
const { getPrefixCls, layout } = React.useContext(ConfigContext);
|
2023-02-27 15:35:22 +08:00
|
|
|
const prefixCls = getPrefixCls('layout', customizePrefixCls);
|
|
|
|
|
2023-10-16 14:49:01 +08:00
|
|
|
const mergedHasSider = useHasSider(siders, children, hasSider);
|
|
|
|
|
2023-07-11 10:44:18 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2020-11-06 10:26:45 +08:00
|
|
|
const classString = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
2023-10-16 14:49:01 +08:00
|
|
|
[`${prefixCls}-has-sider`]: mergedHasSider,
|
2020-11-06 10:26:45 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
2023-06-28 13:51:24 +08:00
|
|
|
layout?.className,
|
2020-11-06 10:26:45 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-04-22 15:00:18 +08:00
|
|
|
hashId,
|
2020-11-06 10:26:45 +08:00
|
|
|
);
|
|
|
|
|
2021-11-26 12:18:21 +08:00
|
|
|
const contextValue = React.useMemo(
|
|
|
|
() => ({
|
|
|
|
siderHook: {
|
|
|
|
addSider: (id: string) => {
|
2022-11-19 13:47:33 +08:00
|
|
|
setSiders((prev) => [...prev, id]);
|
2021-11-26 12:18:21 +08:00
|
|
|
},
|
|
|
|
removeSider: (id: string) => {
|
2022-11-19 13:47:33 +08:00
|
|
|
setSiders((prev) => prev.filter((currentId) => currentId !== id));
|
2020-11-20 08:56:12 +08:00
|
|
|
},
|
2021-11-26 12:18:21 +08:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
2022-04-22 15:00:18 +08:00
|
|
|
return wrapSSR(
|
2021-11-26 12:18:21 +08:00
|
|
|
<LayoutContext.Provider value={contextValue}>
|
2023-06-28 13:51:24 +08:00
|
|
|
<Tag
|
|
|
|
ref={ref}
|
|
|
|
className={classString}
|
|
|
|
style={{ ...layout?.style, ...style }}
|
|
|
|
{...passedProps}
|
|
|
|
>
|
2020-11-06 10:26:45 +08:00
|
|
|
{children}
|
|
|
|
</Tag>
|
2022-04-22 15:00:18 +08:00
|
|
|
</LayoutContext.Provider>,
|
2020-11-06 10:26:45 +08:00
|
|
|
);
|
2022-03-22 16:52:20 +08:00
|
|
|
});
|
2016-12-30 21:41:28 +08:00
|
|
|
|
2020-11-06 10:26:45 +08:00
|
|
|
const Layout = generator({
|
2023-07-24 15:38:55 +08:00
|
|
|
tagName: 'div',
|
2020-01-28 15:03:04 +08:00
|
|
|
displayName: 'Layout',
|
2017-11-09 20:04:34 +08:00
|
|
|
})(BasicLayout);
|
2016-12-30 21:41:28 +08:00
|
|
|
|
|
|
|
const Header = generator({
|
2023-02-27 15:35:22 +08:00
|
|
|
suffixCls: 'header',
|
2019-02-14 17:51:52 +08:00
|
|
|
tagName: 'header',
|
2020-01-28 15:03:04 +08:00
|
|
|
displayName: 'Header',
|
2016-12-30 21:41:28 +08:00
|
|
|
})(Basic);
|
|
|
|
|
|
|
|
const Footer = generator({
|
2023-02-27 15:35:22 +08:00
|
|
|
suffixCls: 'footer',
|
2019-02-14 17:51:52 +08:00
|
|
|
tagName: 'footer',
|
2020-01-28 15:03:04 +08:00
|
|
|
displayName: 'Footer',
|
2016-12-30 21:41:28 +08:00
|
|
|
})(Basic);
|
|
|
|
|
|
|
|
const Content = generator({
|
2023-02-27 15:35:22 +08:00
|
|
|
suffixCls: 'content',
|
2019-02-14 17:51:52 +08:00
|
|
|
tagName: 'main',
|
2020-01-28 15:03:04 +08:00
|
|
|
displayName: 'Content',
|
2016-12-30 21:41:28 +08:00
|
|
|
})(Basic);
|
|
|
|
|
2023-06-28 13:51:24 +08:00
|
|
|
export { Content, Footer, Header };
|
2016-12-30 21:41:28 +08:00
|
|
|
|
|
|
|
export default Layout;
|