本节将详细介绍 Tailwind CSS 的最新特性及其实际应用,帮助开发者更好地利用这些新功能。

容器查询(Container Queries)

基础配置

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. extend: {
  5. containers: {
  6. 'sm': '320px',
  7. 'md': '768px',
  8. 'lg': '1024px',
  9. },
  10. },
  11. },
  12. }

使用示例

  1. // components/AdaptiveCard.tsx
  2. const AdaptiveCard = () => {
  3. return (
  4. <div className="@container">
  5. <div className="
  6. grid gap-4
  7. @sm:grid-cols-1
  8. @md:grid-cols-2
  9. @lg:grid-cols-3
  10. ">
  11. <div className="
  12. p-4 rounded-lg shadow
  13. @sm:flex-col
  14. @md:flex-row
  15. @lg:flex-col
  16. ">
  17. <img
  18. src="/image.jpg"
  19. alt="Feature"
  20. className="
  21. w-full rounded
  22. @md:w-1/3
  23. @lg:w-full
  24. "
  25. />
  26. <div className="
  27. mt-4
  28. @md:mt-0 @md:ml-4
  29. @lg:mt-4 @lg:ml-0
  30. ">
  31. <h3 className="
  32. text-lg font-medium
  33. @md:text-xl
  34. @lg:text-2xl
  35. ">
  36. 特性标题
  37. </h3>
  38. <p className="mt-2 text-gray-600">
  39. 特性描述内容
  40. </p>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. );
  46. };

任意值支持

动态值使用

  1. // components/DynamicStyles.tsx
  2. interface StyleProps {
  3. width?: string;
  4. height?: string;
  5. color?: string;
  6. }
  7. const DynamicStyle: React.FC<StyleProps> = ({
  8. width = '100px',
  9. height = '100px',
  10. color = '#000000'
  11. }) => {
  12. return (
  13. <div className={`
  14. [width:${width}]
  15. [height:${height}]
  16. [background-color:${color}]
  17. rounded-lg
  18. transition-all
  19. duration-300
  20. `}>
  21. 动态样式元素
  22. </div>
  23. );
  24. };
  25. // 使用示例
  26. <DynamicStyle
  27. width="200px"
  28. height="150px"
  29. color="#3B82F6"
  30. />

CSS变量集成

  1. // styles/theme.css
  2. :root {
  3. --brand-hue: 215;
  4. --brand-saturation: 90%;
  5. --brand-lightness: 50%;
  6. --brand-color: hsl(
  7. var(--brand-hue)
  8. var(--brand-saturation)
  9. var(--brand-lightness)
  10. );
  11. }
  12. // components/ThemeAware.tsx
  13. const ThemeAware = () => {
  14. return (
  15. <div className="
  16. [--brand-hue:215]
  17. [--brand-saturation:90%]
  18. [--brand-lightness:50%]
  19. bg-[color:var(--brand-color)]
  20. text-white
  21. p-4
  22. rounded-lg
  23. ">
  24. 主题感知组件
  25. </div>
  26. );
  27. };

多重样式变体

复杂状态处理

  1. // components/ComplexButton.tsx
  2. interface ComplexButtonProps {
  3. variant?: 'primary' | 'secondary';
  4. size?: 'sm' | 'md' | 'lg';
  5. isLoading?: boolean;
  6. isDisabled?: boolean;
  7. }
  8. const ComplexButton: React.FC<ComplexButtonProps> = ({
  9. variant = 'primary',
  10. size = 'md',
  11. isLoading = false,
  12. isDisabled = false,
  13. children
  14. }) => {
  15. return (
  16. <button
  17. disabled={isDisabled || isLoading}
  18. className={`
  19. inline-flex items-center justify-center
  20. rounded-lg font-medium
  21. transition-colors duration-200
  22. focus:outline-none focus:ring-2 focus:ring-offset-2
  23. ${variant === 'primary' ? `
  24. bg-blue-500
  25. hover:bg-blue-600
  26. focus:ring-blue-500
  27. text-white
  28. disabled:bg-blue-300
  29. ` : `
  30. bg-gray-100
  31. hover:bg-gray-200
  32. focus:ring-gray-500
  33. text-gray-900
  34. disabled:bg-gray-50
  35. `}
  36. ${size === 'sm' ? `
  37. px-3 py-1.5
  38. text-sm
  39. ` : size === 'md' ? `
  40. px-4 py-2
  41. text-base
  42. ` : `
  43. px-6 py-3
  44. text-lg
  45. `}
  46. ${isLoading ? `
  47. relative
  48. !text-transparent
  49. pointer-events-none
  50. after:content-['']
  51. after:absolute
  52. after:w-5
  53. after:h-5
  54. after:border-2
  55. after:border-white
  56. after:border-t-transparent
  57. after:rounded-full
  58. after:animate-spin
  59. ` : ''}
  60. ${isDisabled ? `
  61. cursor-not-allowed
  62. opacity-50
  63. ` : ''}
  64. `}
  65. >
  66. {children}
  67. </button>
  68. );
  69. };

文件范围应用

模块化样式

  1. // styles/typography.css
  2. @layer base {
  3. @scope {
  4. :scope {
  5. font-family: Inter var, sans-serif;
  6. line-height: 1.5;
  7. }
  8. h1 {
  9. @apply text-4xl font-bold text-gray-900;
  10. }
  11. h2 {
  12. @apply text-3xl font-bold text-gray-900;
  13. }
  14. p {
  15. @apply text-base text-gray-600;
  16. }
  17. a {
  18. @apply text-blue-600 hover:text-blue-700
  19. underline transition-colors;
  20. }
  21. }
  22. }
  23. // components/Article.tsx
  24. const Article = () => {
  25. return (
  26. <article className="prose">
  27. <h1>文章标题</h1>
  28. <p>文章内容...</p>
  29. <a href="#">了解更多</a>
  30. </article>
  31. );
  32. };

Just-in-Time 引擎增强

动态类生成

  1. // utils/classNames.ts
  2. export const generateUtilities = (properties: Record<string, string>) => {
  3. return Object.entries(properties)
  4. .map(([key, value]) => `[${key}:${value}]`)
  5. .join(' ');
  6. };
  7. // 使用示例
  8. const styles = generateUtilities({
  9. '--gradient-from': '#3B82F6',
  10. '--gradient-to': '#10B981',
  11. 'background-image': 'linear-gradient(var(--gradient-from), var(--gradient-to))'
  12. });
  13. // components/GradientCard.tsx
  14. const GradientCard = () => {
  15. return (
  16. <div className={`
  17. p-6 rounded-lg text-white
  18. ${styles}
  19. `}>
  20. 渐变卡片
  21. </div>
  22. );
  23. };

性能优化

选择器优化

  1. // components/OptimizedList.tsx
  2. const OptimizedList = () => {
  3. return (
  4. <ul className="space-y-4">
  5. {items.map((item, index) => (
  6. <li
  7. key={item.id}
  8. className={`
  9. p-4 rounded-lg
  10. transition-colors
  11. hover:bg-gray-50
  12. ${index === 0 && 'rounded-t-lg'}
  13. ${index === items.length - 1 && 'rounded-b-lg'}
  14. `}
  15. >
  16. {item.content}
  17. </li>
  18. ))}
  19. </ul>
  20. );
  21. };

构建优化

  1. // tailwind.config.js
  2. module.exports = {
  3. mode: 'jit',
  4. purge: {
  5. content: [
  6. './src/**/*.{js,jsx,ts,tsx}',
  7. './public/index.html'
  8. ],
  9. options: {
  10. safelist: [
  11. 'bg-blue-500',
  12. 'text-xl',
  13. 'p-4'
  14. ]
  15. }
  16. },
  17. future: {
  18. removeDeprecatedGapUtilities: true,
  19. purgeLayersByDefault: true,
  20. defaultLineHeights: true,
  21. standardFontWeights: true
  22. }
  23. };

最佳实践

  1. 新特性应用

    • 合理使用容器查询
    • 灵活运用任意值
    • 优化选择器性能
  2. 开发建议

    • 保持代码整洁
    • 模块化管理
    • 持续优化更新
  3. 性能考虑

    • 减少选择器复杂度
    • 优化构建配置
    • 监控样式体积
  4. 维护策略

    • 版本迭代计划
    • 文档同步更新
    • 性能持续优化