diff --git a/components/_util/theme/interface.ts b/components/_util/theme/interface.ts index 98cd0e36d6..2334986815 100644 --- a/components/_util/theme/interface.ts +++ b/components/_util/theme/interface.ts @@ -192,6 +192,7 @@ export interface AliasToken extends DerivativeToken { componentBackgroundDisabled: string; zIndexDropdown: number; + zIndexAffix: number; boxShadow?: string; diff --git a/components/_util/theme/util/alias.ts b/components/_util/theme/util/alias.ts index 519c0f9f55..b52c3a78e7 100644 --- a/components/_util/theme/util/alias.ts +++ b/components/_util/theme/util/alias.ts @@ -149,6 +149,7 @@ export default function formatToken(derivativeToken: RawMergedToken): AliasToken durationFast: '0.1s', zIndexDropdown: 1050, + zIndexAffix: 10, }; return aliasToken; diff --git a/components/affix/index.tsx b/components/affix/index.tsx index 1be9f0f545..b54374d4d5 100644 --- a/components/affix/index.tsx +++ b/components/affix/index.tsx @@ -12,6 +12,7 @@ import { getFixedTop, getFixedBottom, } from './utils'; +import useStyle from './style'; function getDefaultTarget() { return typeof window !== 'undefined' ? window : null; @@ -35,6 +36,7 @@ export interface AffixProps { export interface InternalAffixProps extends AffixProps { affixPrefixCls: string; + rootClassName: string; } enum AffixStatus { @@ -255,8 +257,9 @@ class Affix extends React.Component { // =================== Render =================== render() { const { affixStyle, placeholderStyle } = this.state; - const { affixPrefixCls, children } = this.props; + const { affixPrefixCls, rootClassName, children } = this.props; const className = classNames({ + [rootClassName]: !!affixStyle, [affixPrefixCls]: !!affixStyle, }); @@ -267,6 +270,7 @@ class Affix extends React.Component { 'target', 'onChange', 'affixPrefixCls', + 'rootClassName', ]); // Omit this since `onTestUpdatePosition` only works on test. if (process.env.NODE_ENV === 'test') { @@ -299,16 +303,18 @@ class Affix extends React.Component { const AffixFC = React.forwardRef((props, ref) => { const { prefixCls: customizePrefixCls } = props; const { getPrefixCls } = React.useContext(ConfigContext); - const affixPrefixCls = getPrefixCls('affix', customizePrefixCls); + const [wrapSSR, hashId] = useStyle(affixPrefixCls); + const AffixProps: InternalAffixProps = { ...props, affixPrefixCls, + rootClassName: hashId, }; - return ; + return wrapSSR(); }); if (process.env.NODE_ENV !== 'production') { diff --git a/components/affix/style/index.less b/components/affix/style/index.less index 3762903fcc..c8cf8011b5 100644 --- a/components/affix/style/index.less +++ b/components/affix/style/index.less @@ -1,6 +1,6 @@ -@import '../../style/themes/index'; +// @import '../../style/themes/index'; -.@{ant-prefix}-affix { - position: fixed; - z-index: @zindex-affix; -} +// .@{ant-prefix}-affix { +// position: fixed; +// z-index: @zindex-affix; +// } diff --git a/components/affix/style/index.tsx b/components/affix/style/index.tsx index 3a3ab0de59..e4c7224709 100644 --- a/components/affix/style/index.tsx +++ b/components/affix/style/index.tsx @@ -1,2 +1,43 @@ -import '../../style/index.less'; -import './index.less'; +// deps-lint-skip-all +import { CSSObject } from '@ant-design/cssinjs'; +import { + DerivativeToken, + useStyleRegister, + useToken, + UseComponentStyleResult, + GenerateStyle, +} from '../../_util/theme'; + +interface AffixToken extends DerivativeToken { + affixCls: string; +} + +// ============================== Shared ============================== +const genSharedAffixStyle: GenerateStyle = (token): CSSObject => { + const { affixCls } = token; + + return { + [affixCls]: { + position: 'fixed', + zIndex: token.zIndexAffix, + }, + }; +}; + +// ============================== Export ============================== +export default function useStyle(prefixCls: string): UseComponentStyleResult { + const [theme, token, hashId] = useToken(); + + const affixToken: AffixToken = { + ...token, + + affixCls: `.${prefixCls}`, + }; + + return [ + useStyleRegister({ theme, token, hashId, path: [prefixCls] }, () => [ + genSharedAffixStyle(affixToken), + ]), + hashId, + ]; +}