2022-05-09 22:20:07 +08:00
|
|
|
import type { CSSObject } from '@ant-design/cssinjs';
|
2024-04-08 14:04:08 +08:00
|
|
|
import { unit } from '@ant-design/cssinjs';
|
|
|
|
|
2023-05-18 13:53:51 +08:00
|
|
|
import { resetComponent } from '../../style';
|
2023-11-13 15:33:55 +08:00
|
|
|
import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';
|
2023-11-29 17:23:45 +08:00
|
|
|
import { genStyleHooks, mergeToken } from '../../theme/internal';
|
2022-04-08 11:10:46 +08:00
|
|
|
|
2023-05-18 13:53:51 +08:00
|
|
|
export type ComponentToken = {
|
2023-07-05 17:49:59 +08:00
|
|
|
/**
|
|
|
|
* @desc 星星颜色
|
|
|
|
* @descEN Star color
|
|
|
|
*/
|
2023-05-18 13:53:51 +08:00
|
|
|
starColor: string;
|
2023-07-05 17:49:59 +08:00
|
|
|
/**
|
|
|
|
* @desc 星星大小
|
|
|
|
* @descEN Star size
|
|
|
|
*/
|
2023-05-18 13:53:51 +08:00
|
|
|
starSize: number;
|
2023-07-05 17:49:59 +08:00
|
|
|
/**
|
|
|
|
* @desc 星星悬浮时的缩放
|
|
|
|
* @descEN Scale of star when hover
|
|
|
|
*/
|
2023-05-18 13:53:51 +08:00
|
|
|
starHoverScale: CSSObject['transform'];
|
2023-07-05 17:49:59 +08:00
|
|
|
/**
|
|
|
|
* @desc 星星背景色
|
|
|
|
* @descEN Star background color
|
|
|
|
*/
|
2023-05-18 13:53:51 +08:00
|
|
|
starBg: string;
|
|
|
|
};
|
2022-06-17 18:47:47 +08:00
|
|
|
|
2023-05-18 13:53:51 +08:00
|
|
|
interface RateToken extends FullToken<'Rate'> {}
|
2022-03-10 10:47:33 +08:00
|
|
|
|
2022-11-17 23:31:41 +08:00
|
|
|
const genRateStarStyle: GenerateStyle<RateToken, CSSObject> = (token) => {
|
2022-04-08 11:10:46 +08:00
|
|
|
const { componentCls } = token;
|
2022-03-10 10:47:33 +08:00
|
|
|
|
|
|
|
return {
|
2022-04-08 11:10:46 +08:00
|
|
|
[`${componentCls}-star`]: {
|
2022-03-10 10:47:33 +08:00
|
|
|
position: 'relative',
|
|
|
|
display: 'inline-block',
|
|
|
|
color: 'inherit',
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
|
|
|
'&:not(:last-child)': {
|
|
|
|
marginInlineEnd: token.marginXS,
|
|
|
|
},
|
|
|
|
|
|
|
|
'> div': {
|
2022-11-17 23:31:41 +08:00
|
|
|
transition: `all ${token.motionDurationMid}, outline 0s`,
|
2022-03-10 10:47:33 +08:00
|
|
|
|
|
|
|
'&:hover': {
|
2023-05-18 13:53:51 +08:00
|
|
|
transform: token.starHoverScale,
|
2022-03-10 10:47:33 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'&:focus': {
|
|
|
|
outline: 0,
|
|
|
|
},
|
|
|
|
|
|
|
|
'&:focus-visible': {
|
2023-11-13 15:33:55 +08:00
|
|
|
outline: `${unit(token.lineWidth)} dashed ${token.starColor}`,
|
2023-05-18 13:53:51 +08:00
|
|
|
transform: token.starHoverScale,
|
2022-03-10 10:47:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
'&-first, &-second': {
|
2023-05-18 13:53:51 +08:00
|
|
|
color: token.starBg,
|
2022-11-17 23:31:41 +08:00
|
|
|
transition: `all ${token.motionDurationMid}`,
|
2022-03-10 10:47:33 +08:00
|
|
|
userSelect: 'none',
|
|
|
|
},
|
|
|
|
|
|
|
|
'&-first': {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
insetInlineStart: 0,
|
|
|
|
width: '50%',
|
|
|
|
height: '100%',
|
|
|
|
overflow: 'hidden',
|
|
|
|
opacity: 0,
|
|
|
|
},
|
|
|
|
|
2022-04-08 11:10:46 +08:00
|
|
|
[`&-half ${componentCls}-star-first, &-half ${componentCls}-star-second`]: {
|
2022-03-10 10:47:33 +08:00
|
|
|
opacity: 1,
|
|
|
|
},
|
|
|
|
|
2022-04-08 11:10:46 +08:00
|
|
|
[`&-half ${componentCls}-star-first, &-full ${componentCls}-star-second`]: {
|
2022-03-10 10:47:33 +08:00
|
|
|
color: 'inherit',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const genRateRtlStyle = (token: RateToken): CSSObject => ({
|
2022-04-08 11:10:46 +08:00
|
|
|
[`&-rtl${token.componentCls}`]: {
|
2022-03-10 10:47:33 +08:00
|
|
|
direction: 'rtl',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-11-17 23:31:41 +08:00
|
|
|
const genRateStyle: GenerateStyle<RateToken> = (token) => {
|
2022-04-08 11:10:46 +08:00
|
|
|
const { componentCls } = token;
|
2022-03-10 10:47:33 +08:00
|
|
|
|
|
|
|
return {
|
2022-04-08 11:10:46 +08:00
|
|
|
[componentCls]: {
|
2022-03-10 10:47:33 +08:00
|
|
|
...resetComponent(token),
|
|
|
|
|
|
|
|
display: 'inline-block',
|
|
|
|
margin: 0,
|
|
|
|
padding: 0,
|
2023-05-18 13:53:51 +08:00
|
|
|
color: token.starColor,
|
|
|
|
fontSize: token.starSize,
|
2023-09-14 16:32:54 +08:00
|
|
|
lineHeight: 1,
|
2022-03-10 10:47:33 +08:00
|
|
|
listStyle: 'none',
|
|
|
|
outline: 'none',
|
|
|
|
|
|
|
|
// disable styles
|
2022-04-08 11:10:46 +08:00
|
|
|
[`&-disabled${componentCls} ${componentCls}-star`]: {
|
2022-03-10 10:47:33 +08:00
|
|
|
cursor: 'default',
|
|
|
|
|
2023-02-21 10:25:46 +08:00
|
|
|
'> div:hover': {
|
2022-03-10 10:47:33 +08:00
|
|
|
transform: 'scale(1)',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// star styles
|
|
|
|
...genRateStarStyle(token),
|
|
|
|
|
|
|
|
// rtl styles
|
|
|
|
...genRateRtlStyle(token),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// ============================== Export ==============================
|
2023-11-13 15:33:55 +08:00
|
|
|
export const prepareComponentToken: GetDefaultToken<'Rate'> = (token) => ({
|
|
|
|
starColor: token.yellow6,
|
|
|
|
starSize: token.controlHeightLG * 0.5,
|
|
|
|
starHoverScale: 'scale(1.1)',
|
|
|
|
starBg: token.colorFillContent,
|
|
|
|
});
|
|
|
|
|
2023-11-29 17:23:45 +08:00
|
|
|
export default genStyleHooks(
|
2023-05-18 13:53:51 +08:00
|
|
|
'Rate',
|
|
|
|
(token) => {
|
|
|
|
const rateToken = mergeToken<RateToken>(token, {});
|
|
|
|
return [genRateStyle(rateToken)];
|
|
|
|
},
|
2023-11-13 15:33:55 +08:00
|
|
|
prepareComponentToken,
|
2023-05-18 13:53:51 +08:00
|
|
|
);
|