|
| 1 | +import type { CSSObject } from '../../_util/cssinjs'; |
| 2 | +import type { FullToken, GenerateStyle } from '../../theme/internal'; |
| 3 | +import { genComponentStyleHook, mergeToken } from '../../theme/internal'; |
| 4 | +import { resetComponent } from '../../_style'; |
| 5 | + |
| 6 | +export type ComponentToken = {}; |
| 7 | + |
| 8 | +interface RateToken extends FullToken<'Rate'> { |
| 9 | + rateStarColor: string; |
| 10 | + rateStarSize: number; |
| 11 | + rateStarHoverScale: CSSObject['transform']; |
| 12 | + defaultColor: string; |
| 13 | +} |
| 14 | + |
| 15 | +const genRateStarStyle: GenerateStyle<RateToken, CSSObject> = token => { |
| 16 | + const { componentCls } = token; |
| 17 | + |
| 18 | + return { |
| 19 | + [`${componentCls}-star`]: { |
| 20 | + position: 'relative', |
| 21 | + display: 'inline-block', |
| 22 | + color: 'inherit', |
| 23 | + cursor: 'pointer', |
| 24 | + |
| 25 | + '&:not(:last-child)': { |
| 26 | + marginInlineEnd: token.marginXS, |
| 27 | + }, |
| 28 | + |
| 29 | + '> div': { |
| 30 | + transition: `all ${token.motionDurationMid}, outline 0s`, |
| 31 | + |
| 32 | + '&:hover': { |
| 33 | + transform: token.rateStarHoverScale, |
| 34 | + }, |
| 35 | + |
| 36 | + '&:focus': { |
| 37 | + outline: 0, |
| 38 | + }, |
| 39 | + |
| 40 | + '&:focus-visible': { |
| 41 | + outline: `${token.lineWidth}px dashed ${token.rateStarColor}`, |
| 42 | + transform: token.rateStarHoverScale, |
| 43 | + }, |
| 44 | + }, |
| 45 | + |
| 46 | + '&-first, &-second': { |
| 47 | + color: token.defaultColor, |
| 48 | + transition: `all ${token.motionDurationMid}`, |
| 49 | + userSelect: 'none', |
| 50 | + |
| 51 | + [token.iconCls]: { |
| 52 | + verticalAlign: 'middle', |
| 53 | + }, |
| 54 | + }, |
| 55 | + |
| 56 | + '&-first': { |
| 57 | + position: 'absolute', |
| 58 | + top: 0, |
| 59 | + insetInlineStart: 0, |
| 60 | + width: '50%', |
| 61 | + height: '100%', |
| 62 | + overflow: 'hidden', |
| 63 | + opacity: 0, |
| 64 | + }, |
| 65 | + |
| 66 | + [`&-half ${componentCls}-star-first, &-half ${componentCls}-star-second`]: { |
| 67 | + opacity: 1, |
| 68 | + }, |
| 69 | + |
| 70 | + [`&-half ${componentCls}-star-first, &-full ${componentCls}-star-second`]: { |
| 71 | + color: 'inherit', |
| 72 | + }, |
| 73 | + }, |
| 74 | + }; |
| 75 | +}; |
| 76 | + |
| 77 | +const genRateRtlStyle = (token: RateToken): CSSObject => ({ |
| 78 | + [`&-rtl${token.componentCls}`]: { |
| 79 | + direction: 'rtl', |
| 80 | + }, |
| 81 | +}); |
| 82 | + |
| 83 | +const genRateStyle: GenerateStyle<RateToken> = token => { |
| 84 | + const { componentCls } = token; |
| 85 | + |
| 86 | + return { |
| 87 | + [componentCls]: { |
| 88 | + ...resetComponent(token), |
| 89 | + |
| 90 | + display: 'inline-block', |
| 91 | + margin: 0, |
| 92 | + padding: 0, |
| 93 | + color: token.rateStarColor, |
| 94 | + fontSize: token.rateStarSize, |
| 95 | + lineHeight: 'unset', |
| 96 | + listStyle: 'none', |
| 97 | + outline: 'none', |
| 98 | + |
| 99 | + // disable styles |
| 100 | + [`&-disabled${componentCls} ${componentCls}-star`]: { |
| 101 | + cursor: 'default', |
| 102 | + |
| 103 | + '&:hover': { |
| 104 | + transform: 'scale(1)', |
| 105 | + }, |
| 106 | + }, |
| 107 | + |
| 108 | + // star styles |
| 109 | + ...genRateStarStyle(token), |
| 110 | + |
| 111 | + // text styles |
| 112 | + [`+ ${componentCls}-text`]: { |
| 113 | + display: 'inline-block', |
| 114 | + marginInlineStart: token.marginXS, |
| 115 | + fontSize: token.fontSize, |
| 116 | + }, |
| 117 | + |
| 118 | + // rtl styles |
| 119 | + ...genRateRtlStyle(token), |
| 120 | + }, |
| 121 | + }; |
| 122 | +}; |
| 123 | + |
| 124 | +// ============================== Export ============================== |
| 125 | +export default genComponentStyleHook('Rate', token => { |
| 126 | + const { colorFillContent } = token; |
| 127 | + |
| 128 | + const rateToken = mergeToken<RateToken>(token, { |
| 129 | + rateStarColor: token['yellow-6'], |
| 130 | + rateStarSize: token.controlHeightLG * 0.5, |
| 131 | + rateStarHoverScale: 'scale(1.1)', |
| 132 | + defaultColor: colorFillContent, |
| 133 | + }); |
| 134 | + return [genRateStyle(rateToken)]; |
| 135 | +}); |
0 commit comments