refactor: affix support cssinjs

pull/6213/head
tangjinzhou 2023-01-25 16:16:25 +08:00
parent 1f33babc89
commit 46862d749b
6 changed files with 47 additions and 16 deletions

View File

@ -23,7 +23,7 @@ import {
} from './utils'; } from './utils';
import useConfigInject from '../_util/hooks/useConfigInject'; import useConfigInject from '../_util/hooks/useConfigInject';
import omit from '../_util/omit'; import omit from '../_util/omit';
import useStyle from './style';
function getDefaultTarget() { function getDefaultTarget() {
return typeof window !== 'undefined' ? window : null; return typeof window !== 'undefined' ? window : null;
} }
@ -74,8 +74,9 @@ export type AffixInstance = ComponentPublicInstance<AffixProps, AffixExpose>;
const Affix = defineComponent({ const Affix = defineComponent({
compatConfig: { MODE: 3 }, compatConfig: { MODE: 3 },
name: 'AAffix', name: 'AAffix',
inheritAttrs: false,
props: affixProps(), props: affixProps(),
setup(props, { slots, emit, expose }) { setup(props, { slots, emit, expose, attrs }) {
const placeholderNode = ref(); const placeholderNode = ref();
const fixedNode = ref(); const fixedNode = ref();
const state = reactive({ const state = reactive({
@ -113,6 +114,15 @@ const Affix = defineComponent({
const placeholderReact = getTargetRect(placeholderNode.value as HTMLElement); const placeholderReact = getTargetRect(placeholderNode.value as HTMLElement);
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop.value); const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop.value);
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom.value); const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom.value);
if (
placeholderReact.top === 0 &&
placeholderReact.left === 0 &&
placeholderReact.width === 0 &&
placeholderReact.height === 0
) {
return;
}
if (fixedTop !== undefined) { if (fixedTop !== undefined) {
newState.affixStyle = { newState.affixStyle = {
position: 'fixed', position: 'fixed',
@ -228,11 +238,12 @@ const Affix = defineComponent({
}); });
const { prefixCls } = useConfigInject('affix', props); const { prefixCls } = useConfigInject('affix', props);
const [wrapSSR, hashId] = useStyle(prefixCls);
return () => { return () => {
const { affixStyle, placeholderStyle } = state; const { affixStyle, placeholderStyle } = state;
const className = classNames({ const className = classNames({
[prefixCls.value]: affixStyle, [prefixCls.value]: affixStyle,
[hashId.value]: true,
}); });
const restProps = omit(props, [ const restProps = omit(props, [
'prefixCls', 'prefixCls',
@ -242,14 +253,15 @@ const Affix = defineComponent({
'onChange', 'onChange',
'onTestUpdatePosition', 'onTestUpdatePosition',
]); ]);
return ( return wrapSSR(
<ResizeObserver onResize={updatePosition}> <ResizeObserver onResize={updatePosition}>
<div {...restProps} style={placeholderStyle} ref={placeholderNode}> <div {...restProps} {...attrs} ref={placeholderNode}>
{affixStyle && <div style={placeholderStyle} aria-hidden="true" />}
<div class={className} ref={fixedNode} style={affixStyle}> <div class={className} ref={fixedNode} style={affixStyle}>
{slots.default?.()} {slots.default?.()}
</div> </div>
</div> </div>
</ResizeObserver> </ResizeObserver>,
); );
}; };
}, },

View File

@ -1,6 +0,0 @@
@import '../../style/themes/index';
.@{ant-prefix}-affix {
position: fixed;
z-index: @zindex-affix;
}

View File

@ -0,0 +1,27 @@
import type { CSSObject } from '../../_util/cssinjs';
import type { FullToken, GenerateStyle } from '../../theme/internal';
import { genComponentStyleHook, mergeToken } from '../../theme/internal';
interface AffixToken extends FullToken<'Affix'> {
zIndexPopup: number;
}
// ============================== Shared ==============================
const genSharedAffixStyle: GenerateStyle<AffixToken> = (token): CSSObject => {
const { componentCls } = token;
return {
[componentCls]: {
position: 'fixed',
zIndex: token.zIndexPopup,
},
};
};
// ============================== Export ==============================
export default genComponentStyleHook('Affix', token => {
const affixToken = mergeToken<AffixToken>(token, {
zIndexPopup: token.zIndexBase + 10,
});
return [genSharedAffixStyle(affixToken)];
});

View File

@ -1,2 +0,0 @@
import '../../style/index.less';
import './index.less';

View File

@ -55,7 +55,7 @@ export function getObserverEntities() {
export function addObserveTarget<T>(target: HTMLElement | Window | null, affix: T): void { export function addObserveTarget<T>(target: HTMLElement | Window | null, affix: T): void {
if (!target) return; if (!target) return;
let entity: ObserverEntity | undefined = observerEntities.find(item => item.target === target); let entity = observerEntities.find(item => item.target === target);
if (entity) { if (entity) {
entity.affixList.push(affix); entity.affixList.push(affix);

View File

@ -26,7 +26,7 @@ import './spin/style';
import './select/style'; import './select/style';
import './switch/style'; import './switch/style';
import './auto-complete/style'; import './auto-complete/style';
import './affix/style'; // import './affix/style';
import './cascader/style'; import './cascader/style';
import './back-top/style'; import './back-top/style';
import './modal/style'; import './modal/style';