ant-design/components/back-top/style/index.ts

159 lines
4.3 KiB
TypeScript
Raw Normal View History

import { unit } from '@ant-design/cssinjs';
import type { CSSObject } from '@ant-design/cssinjs';
import { resetComponent } from '../../style';
import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';
import { genStyleHooks, mergeToken } from '../../theme/internal';
/** Component only token. Which will handle additional calculation of alias token */
2022-05-17 21:57:22 +08:00
export interface ComponentToken {
/**
* @desc z-index
* @descEN z-index of popup
*/
2022-05-17 21:57:22 +08:00
zIndexPopup: number;
}
type BackTopToken = FullToken<'BackTop'> & {
/**
* @desc BackTop
* @descEN Background color of BackTop
*/
backTopBackground: string;
/**
* @desc BackTop
* @descEN Text color of BackTop
*/
backTopColor: string;
/**
* @desc BackTop
* @descEN Hover background color of BackTop
*/
backTopHoverBackground: string;
/**
* @desc BackTop
* @descEN Font size of BackTop
*/
2022-05-17 21:57:22 +08:00
backTopFontSize: number;
/**
* @desc BackTop
* @descEN Size of BackTop
*/
2022-05-17 21:57:22 +08:00
backTopSize: number;
// Position
/**
* @desc BackTop
* @descEN Bottom offset of BackTop
*/
backTopBlockEnd: number | string;
/**
* @desc BackTop
* @descEN Right offset of BackTop
*/
backTopInlineEnd: number | string;
/**
* @desc BackTop
* @descEN Right offset of BackTop on medium screens
*/
backTopInlineEndMD: number | string;
/**
* @desc BackTop
* @descEN Right offset of BackTop on small screens
*/
backTopInlineEndXS: number | string;
};
// ============================== Shared ==============================
const genSharedBackTopStyle: GenerateStyle<BackTopToken, CSSObject> = (token): CSSObject => {
2022-05-17 21:57:22 +08:00
const { componentCls, backTopFontSize, backTopSize, zIndexPopup } = token;
return {
[componentCls]: {
...resetComponent(token),
position: 'fixed',
2022-05-17 21:57:22 +08:00
insetInlineEnd: token.backTopInlineEnd,
insetBlockEnd: token.backTopBlockEnd,
zIndex: zIndexPopup,
width: 40,
height: 40,
cursor: 'pointer',
'&:empty': {
display: 'none',
},
[`${componentCls}-content`]: {
2022-05-17 21:57:22 +08:00
width: backTopSize,
height: backTopSize,
overflow: 'hidden',
color: token.backTopColor,
textAlign: 'center',
backgroundColor: token.backTopBackground,
2022-05-17 21:57:22 +08:00
borderRadius: backTopSize,
transition: `all ${token.motionDurationMid}`,
'&:hover': {
backgroundColor: token.backTopHoverBackground,
transition: `all ${token.motionDurationMid}`,
},
},
// change to .backtop .backtop-icon
[`${componentCls}-icon`]: {
2022-05-17 21:57:22 +08:00
fontSize: backTopFontSize,
lineHeight: unit(backTopSize),
},
},
};
};
const genMediaBackTopStyle: GenerateStyle<BackTopToken> = (token): CSSObject => {
const { componentCls, screenMD, screenXS, backTopInlineEndMD, backTopInlineEndXS } = token;
return {
[`@media (max-width: ${unit(screenMD)})`]: {
[componentCls]: {
insetInlineEnd: backTopInlineEndMD,
},
},
[`@media (max-width: ${unit(screenXS)})`]: {
[componentCls]: {
insetInlineEnd: backTopInlineEndXS,
},
},
};
};
export const prepareComponentToken: GetDefaultToken<'BackTop'> = (token) => ({
zIndexPopup: token.zIndexBase + 10,
});
// ============================== Export ==============================
export default genStyleHooks(
'BackTop',
(token) => {
2022-05-17 21:57:22 +08:00
const {
fontSizeHeading3,
2022-08-04 16:16:50 +08:00
colorTextDescription,
2022-05-17 21:57:22 +08:00
colorTextLightSolid,
colorText,
controlHeightLG,
calc,
2022-05-17 21:57:22 +08:00
} = token;
const backTopToken = mergeToken<BackTopToken>(token, {
2022-08-04 16:16:50 +08:00
backTopBackground: colorTextDescription,
2022-05-17 21:57:22 +08:00
backTopColor: colorTextLightSolid,
backTopHoverBackground: colorText,
backTopFontSize: fontSizeHeading3,
backTopSize: controlHeightLG,
backTopBlockEnd: calc(controlHeightLG).mul(1.25).equal(),
backTopInlineEnd: calc(controlHeightLG).mul(2.5).equal(),
backTopInlineEndMD: calc(controlHeightLG).mul(1.5).equal(),
backTopInlineEndXS: calc(controlHeightLG).mul(0.5).equal(),
});
return [genSharedBackTopStyle(backTopToken), genMediaBackTopStyle(backTopToken)];
},
prepareComponentToken,
);