Skip to content

Commit 3375bd4

Browse files
committed
feat: affix support cssvar
1 parent a0c9369 commit 3375bd4

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

components/affix/index.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ import useStyle from './style';
2727
function getDefaultTarget() {
2828
return typeof window !== 'undefined' ? window : null;
2929
}
30-
enum AffixStatus {
31-
None,
32-
Prepare,
33-
}
30+
const AFFIX_STATUS_NONE = 0;
31+
const AFFIX_STATUS_PREPARE = 1;
32+
33+
type AffixStatus = typeof AFFIX_STATUS_NONE | typeof AFFIX_STATUS_PREPARE;
34+
3435
export interface AffixState {
3536
affixStyle?: CSSProperties;
3637
placeholderStyle?: CSSProperties;
@@ -82,7 +83,7 @@ const Affix = defineComponent({
8283
const state = reactive({
8384
affixStyle: undefined,
8485
placeholderStyle: undefined,
85-
status: AffixStatus.None,
86+
status: AFFIX_STATUS_NONE,
8687
lastAffix: false,
8788
prevTarget: null,
8889
timeout: null,
@@ -98,7 +99,12 @@ const Affix = defineComponent({
9899
const measure = () => {
99100
const { status, lastAffix } = state;
100101
const { target } = props;
101-
if (status !== AffixStatus.Prepare || !fixedNode.value || !placeholderNode.value || !target) {
102+
if (
103+
status !== AFFIX_STATUS_PREPARE ||
104+
!fixedNode.value ||
105+
!placeholderNode.value ||
106+
!target
107+
) {
102108
return;
103109
}
104110

@@ -108,7 +114,7 @@ const Affix = defineComponent({
108114
}
109115

110116
const newState = {
111-
status: AffixStatus.None,
117+
status: AFFIX_STATUS_NONE,
112118
} as AffixState;
113119
const placeholderRect = getTargetRect(placeholderNode.value as HTMLElement);
114120

@@ -172,7 +178,7 @@ const Affix = defineComponent({
172178
};
173179
const prepareMeasure = () => {
174180
Object.assign(state, {
175-
status: AffixStatus.Prepare,
181+
status: AFFIX_STATUS_PREPARE,
176182
affixStyle: undefined,
177183
placeholderStyle: undefined,
178184
});
@@ -253,12 +259,13 @@ const Affix = defineComponent({
253259
});
254260

255261
const { prefixCls } = useConfigInject('affix', props);
256-
const [wrapSSR, hashId] = useStyle(prefixCls);
262+
const [wrapSSR, hashId, cssVarCls] = useStyle(prefixCls);
257263
return () => {
258264
const { affixStyle, placeholderStyle, status } = state;
259265
const className = classNames({
260266
[prefixCls.value]: affixStyle,
261267
[hashId.value]: true,
268+
[cssVarCls.value]: true,
262269
});
263270
const restProps = omit(props, [
264271
'prefixCls',

components/affix/style/index.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import type { CSSObject } from '../../_util/cssinjs';
2-
import type { FullToken, GenerateStyle } from '../../theme/internal';
3-
import { genComponentStyleHook, mergeToken } from '../../theme/internal';
2+
import { FullToken, GenerateStyle, genStyleHooks, GetDefaultToken } from '../../theme/internal';
43

5-
export interface ComponentToken {}
4+
export interface ComponentToken {
5+
/**
6+
* @desc 弹出层的 z-index
7+
* @descEN z-index of popup
8+
*/
9+
zIndexPopup: number;
10+
}
611

712
interface AffixToken extends FullToken<'Affix'> {
8-
zIndexPopup: number;
13+
//
914
}
1015

1116
// ============================== Shared ==============================
1217
const genSharedAffixStyle: GenerateStyle<AffixToken> = (token): CSSObject => {
1318
const { componentCls } = token;
14-
1519
return {
1620
[componentCls]: {
1721
position: 'fixed',
@@ -20,10 +24,9 @@ const genSharedAffixStyle: GenerateStyle<AffixToken> = (token): CSSObject => {
2024
};
2125
};
2226

23-
// ============================== Export ==============================
24-
export default genComponentStyleHook('Affix', token => {
25-
const affixToken = mergeToken<AffixToken>(token, {
26-
zIndexPopup: token.zIndexBase + 10,
27-
});
28-
return [genSharedAffixStyle(affixToken)];
27+
export const prepareComponentToken: GetDefaultToken<'Affix'> = token => ({
28+
zIndexPopup: token.zIndexBase + 10,
2929
});
30+
31+
// ============================== Export ==============================
32+
export default genStyleHooks('Affix', genSharedAffixStyle, prepareComponentToken);

components/affix/utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ export function getTargetRect(target: BindElement): DOMRect {
99
: ({ top: 0, bottom: window.innerHeight } as DOMRect);
1010
}
1111

12-
export function getFixedTop(placeholderRect: DOMRect, targetRect: DOMRect, offsetTop: number) {
13-
if (offsetTop !== undefined && targetRect.top > placeholderRect.top - offsetTop) {
12+
export function getFixedTop(placeholderRect: DOMRect, targetRect: DOMRect, offsetTop?: number) {
13+
if (
14+
offsetTop !== undefined &&
15+
Math.round(targetRect.top) > Math.round(placeholderRect.top) - offsetTop
16+
) {
1417
return `${offsetTop + targetRect.top}px`;
1518
}
1619
return undefined;
@@ -19,17 +22,20 @@ export function getFixedTop(placeholderRect: DOMRect, targetRect: DOMRect, offse
1922
export function getFixedBottom(
2023
placeholderRect: DOMRect,
2124
targetRect: DOMRect,
22-
offsetBottom: number,
25+
offsetBottom?: number,
2326
) {
24-
if (offsetBottom !== undefined && targetRect.bottom < placeholderRect.bottom + offsetBottom) {
27+
if (
28+
offsetBottom !== undefined &&
29+
Math.round(targetRect.bottom) < Math.round(placeholderRect.bottom) + offsetBottom
30+
) {
2531
const targetBottomOffset = window.innerHeight - targetRect.bottom;
2632
return `${offsetBottom + targetBottomOffset}px`;
2733
}
2834
return undefined;
2935
}
3036

3137
// ======================== Observer ========================
32-
const TRIGGER_EVENTS = [
38+
const TRIGGER_EVENTS: (keyof WindowEventMap)[] = [
3339
'resize',
3440
'scroll',
3541
'touchstart',

0 commit comments

Comments
 (0)