ant-design/components/layout/layout.tsx
MadCcc 1ac0bcaa60
Some checks are pending
Publish Any Commit / build (push) Waiting to run
✅ test v6 / lint (push) Waiting to run
✅ test v6 / test-react-legacy (18, 1/2) (push) Waiting to run
✅ test v6 / test-react-legacy (18, 2/2) (push) Waiting to run
✅ test v6 / test-node (push) Waiting to run
✅ test v6 / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test v6 / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test v6 / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test v6 / test-coverage (push) Blocked by required conditions
✅ test v6 / build (push) Waiting to run
✅ test v6 / test lib/es module (es, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (es, 2/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
feat: use cssVar by default (#52671)
* feat: use cssVar by default

* chore: update deps

* chore: update snapshot

* chore: update snapshot

* chore: update snapshot

* chore: test

* chore: update

* chore: update scripts

* chore: UPDATE TEST

* feat: add root

* chore: update snapshot

* chore: fix test case

* chore: fix cycle deps

* feat: rm legacy css variables configuration

* chore: update test case

* chore: update

* chore: fix test

* chore: update overrides

* chore: bump cssinjs

* chore: add test case
2025-02-24 15:35:29 +08:00

158 lines
3.8 KiB
TypeScript

import * as React from 'react';
import omit from '@rc-component/util/lib/omit';
import classNames from 'classnames';
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 [hashId] = useStyle(prefixCls);
const prefixWithSuffixCls = suffixCls ? `${prefixCls}-${suffixCls}` : prefixCls;
return (
<TagName
className={classNames(
customizePrefixCls || prefixWithSuffixCls,
className,
hashId,
)}
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 [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 (
<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;