File size: 3,904 Bytes
1e92f2d |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import * as React from 'react';
import classNames from 'classnames';
import omit from 'rc-util/lib/omit';
import { ConfigContext } from '../config-provider';
import { useComponentConfig } from '../config-provider/context';
import { LayoutContext } from './context';
import useHasSider from './hooks/useHasSider';
import useStyle from './style';
export interface GeneratorProps {
suffixCls?: string;
tagName: 'header' | 'footer' | 'main' | 'div';
displayName: string;
}
export interface BasicProps extends React.HTMLAttributes<HTMLDivElement> {
prefixCls?: string;
suffixCls?: string;
rootClassName?: string;
hasSider?: boolean;
}
interface BasicPropsWithTagName extends BasicProps {
tagName: 'header' | 'footer' | 'main' | 'div';
}
function generator({ suffixCls, tagName, displayName }: GeneratorProps) {
return (BasicComponent: any) => {
const Adapter = React.forwardRef<HTMLElement, BasicProps>((props, ref) => (
<BasicComponent ref={ref} suffixCls={suffixCls} tagName={tagName} {...props} />
));
if (process.env.NODE_ENV !== 'production') {
Adapter.displayName = displayName;
}
return Adapter;
};
}
const Basic = React.forwardRef<HTMLDivElement, BasicPropsWithTagName>((props, ref) => {
const {
prefixCls: customizePrefixCls,
suffixCls,
className,
tagName: TagName,
...others
} = props;
const { getPrefixCls } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('layout', customizePrefixCls);
const [wrapSSR, hashId, cssVarCls] = useStyle(prefixCls);
const prefixWithSuffixCls = suffixCls ? `${prefixCls}-${suffixCls}` : prefixCls;
return wrapSSR(
<TagName
className={classNames(
customizePrefixCls || prefixWithSuffixCls,
className,
hashId,
cssVarCls,
)}
ref={ref}
{...others}
/>,
);
});
const BasicLayout = React.forwardRef<HTMLDivElement, BasicPropsWithTagName>((props, ref) => {
const { direction } = React.useContext(ConfigContext);
const [siders, setSiders] = React.useState<string[]>([]);
const {
prefixCls: customizePrefixCls,
className,
rootClassName,
children,
hasSider,
tagName: Tag,
style,
...others
} = props;
const passedProps = omit(others, ['suffixCls']);
const {
getPrefixCls,
className: contextClassName,
style: contextStyle,
} = useComponentConfig('layout');
const prefixCls = getPrefixCls('layout', customizePrefixCls);
const mergedHasSider = useHasSider(siders, children, hasSider);
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
const classString = classNames(
prefixCls,
{
[`${prefixCls}-has-sider`]: mergedHasSider,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
contextClassName,
className,
rootClassName,
hashId,
cssVarCls,
);
const contextValue = React.useMemo(
() => ({
siderHook: {
addSider: (id: string) => {
setSiders((prev) => [...prev, id]);
},
removeSider: (id: string) => {
setSiders((prev) => prev.filter((currentId) => currentId !== id));
},
},
}),
[],
);
return wrapCSSVar(
<LayoutContext.Provider value={contextValue}>
<Tag ref={ref} className={classString} style={{ ...contextStyle, ...style }} {...passedProps}>
{children}
</Tag>
</LayoutContext.Provider>,
);
});
const Layout = generator({
tagName: 'div',
displayName: 'Layout',
})(BasicLayout);
const Header = generator({
suffixCls: 'header',
tagName: 'header',
displayName: 'Header',
})(Basic);
const Footer = generator({
suffixCls: 'footer',
tagName: 'footer',
displayName: 'Footer',
})(Basic);
const Content = generator({
suffixCls: 'content',
tagName: 'main',
displayName: 'Content',
})(Basic);
export { Content, Footer, Header };
export default Layout;
|