2022-05-09 22:20:07 +08:00
|
|
|
import type { CSSObject } from '@ant-design/cssinjs';
|
2023-05-18 13:53:51 +08:00
|
|
|
import { resetComponent } from '../../style';
|
2022-11-23 20:22:38 +08:00
|
|
|
import type { FullToken, GenerateStyle } from '../../theme/internal';
|
|
|
|
import { genComponentStyleHook, 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-05-18 13:53:51 +08:00
|
|
|
outline: `${token.lineWidth}px dashed ${token.starColor}`,
|
|
|
|
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',
|
|
|
|
|
2022-04-08 11:10:46 +08:00
|
|
|
[token.iconCls]: {
|
2022-03-10 10:47:33 +08:00
|
|
|
verticalAlign: 'middle',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
'&-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,
|
2022-03-10 10:47:33 +08:00
|
|
|
lineHeight: 'unset',
|
|
|
|
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),
|
|
|
|
|
|
|
|
// text styles
|
2022-04-08 11:10:46 +08:00
|
|
|
[`+ ${componentCls}-text`]: {
|
2022-03-10 10:47:33 +08:00
|
|
|
display: 'inline-block',
|
|
|
|
marginInlineStart: token.marginXS,
|
|
|
|
fontSize: token.fontSize,
|
|
|
|
},
|
|
|
|
|
|
|
|
// rtl styles
|
|
|
|
...genRateRtlStyle(token),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// ============================== Export ==============================
|
2023-05-18 13:53:51 +08:00
|
|
|
export default genComponentStyleHook(
|
|
|
|
'Rate',
|
|
|
|
(token) => {
|
|
|
|
const rateToken = mergeToken<RateToken>(token, {});
|
|
|
|
return [genRateStyle(rateToken)];
|
|
|
|
},
|
|
|
|
(token) => ({
|
|
|
|
starColor: token.yellow6,
|
|
|
|
starSize: token.controlHeightLG * 0.5,
|
|
|
|
starHoverScale: 'scale(1.1)',
|
|
|
|
starBg: token.colorFillContent,
|
|
|
|
}),
|
|
|
|
);
|