1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| import React from 'React'; import { ThemeProvider, Css, useTheme } from '@emotion/React';
switch (variant) { case 'light': return theme.colors[color] + '20'; return shadeColor(theme.colors[color], -20); case 'hover': return shadeColor(theme.colors[color], -10); default: return theme.colors[color]; } },
${mobile && `@media (max-width: ${theme.breakpoints.sm}) { ${mobile} }`} ${tablet && `@media (min-width: ${theme.breakpoints.sm}) and (max-width: ${theme.breakpoints.lg}) { ${tablet} }`} ${desktop && `@media (min-width: ${theme.breakpoints.lg}) { ${desktop} }`} `,
// 混合工具 blendColors: (color1, color2, ratio = 0.5) => (theme) => blend( theme.colors[color1], theme.colors[color2], ratio ) };
// 颜色处理工具 function shadeColor(color, percent) { const f = parseInt(color.slice(1), 16); const t = percent < 0 ? 0 : 255; const p = percent < 0 ? percent * -1 : percent; const R = f >> 16; const G = (f & 0x0000ff) >> 8; const B = f & 0x0000ff;
return `#${( 0x1000000 + (Math.round((t - R) * p) + R) * 0x10000 + (Math.round((t - G) * p) + G) * 0x100 + (Math.round((t - B) * p) + B) ) .toString(16) .slice(1)}`; }
function blend(color1, color2, ratio) { const r1 = parseInt(color1.slice(1, 3), 16); const g1 = parseInt(color1.slice(3, 5), 16); const b1 = parseInt(color1.slice(5, 7), 16);
const r2 = parseInt(color2.slice(1, 3), 16); const g2 = parseInt(color2.slice(3, 5), 16); const b2 = parseInt(color2.slice(5, 7), 16);
const r = Math.round(r1 * (1 - ratio) + r2 * ratio); const g = Math.round(g1 * (1 - ratio) + g2 * ratio); const b = Math.round(b1 * (1 - ratio) + b2 * ratio);
return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`; }
function AdvancedThemedComponent() { const theme = useTheme();
const advancedStyles = Css` background: linear-gradient(135deg, ${themeUtils.getVariantColor('primary', 'light')(theme)}, ${themeUtils.getVariantColor('secondary', 'light')(theme)}); border: 2px solid ${themeUtils.getVariantColor('primary')(theme)}; border-radius: 8px; padding: ${theme.spacing.lg}; margin: ${theme.spacing.md};
${themeUtils.responsive( `padding: ${theme.spacing.sm}; font-size: ${theme.typography.small};`, `padding: ${theme.spacing.md}; font-size: ${theme.typography.body};`, `padding: ${theme.spacing.lg}; font-size: ${theme.typography.body};` )(theme)}
transition: all 0.3s ease;
&:hover { transform: translateY(-2px); box-shadow: 0 4px 16px ${themeUtils.getVariantColor('primary', 'light')(theme)}; } `;
return ( <div Css={advancedStyles}> <h3>Advanced Themed Component</h3> <p>This component demonstrates advanced theme usage with utility functions.</p> </div> ); }
|