本节将介绍如何参与 Tailwind CSS 社区生态建设,包括插件开发、组件贡献、文档建设等方面。

插件开发

插件基础结构

  1. // plugins/myPlugin.js
  2. const plugin = require('tailwindcss/plugin')
  3. module.exports = plugin(function({ addUtilities, addComponents, theme }) {
  4. // 添加工具类
  5. addUtilities({
  6. '.custom-utility': {
  7. padding: theme('spacing.4'),
  8. backgroundColor: theme('colors.blue.500'),
  9. color: theme('colors.white'),
  10. borderRadius: theme('borderRadius.lg')
  11. }
  12. })
  13. // 添加组件
  14. addComponents({
  15. '.custom-component': {
  16. backgroundColor: theme('colors.white'),
  17. borderRadius: theme('borderRadius.lg'),
  18. padding: theme('spacing.6'),
  19. boxShadow: theme('boxShadow.lg')
  20. }
  21. })
  22. }, {
  23. // 配置项
  24. theme: {
  25. extend: {
  26. colors: {
  27. custom: {
  28. 100: '#f0f9ff',
  29. 500: '#0ea5e9',
  30. 900: '#0c4a6e'
  31. }
  32. }
  33. }
  34. }
  35. })

插件发布流程

  1. // package.json
  2. {
  3. "name": "tailwindcss-custom-plugin",
  4. "version": "1.0.0",
  5. "main": "index.js",
  6. "files": [
  7. "index.js",
  8. "styles.css"
  9. ],
  10. "peerDependencies": {
  11. "tailwindcss": "^3.0.0"
  12. },
  13. "scripts": {
  14. "build": "postcss styles.css -o dist/styles.css",
  15. "test": "jest",
  16. "prepublishOnly": "npm run build && npm test"
  17. }
  18. }

组件贡献

组件开发规范

  1. // components/CustomButton.tsx
  2. interface CustomButtonProps {
  3. variant?: 'primary' | 'secondary';
  4. size?: 'sm' | 'md' | 'lg';
  5. className?: string;
  6. children: React.ReactNode;
  7. }
  8. const CustomButton: React.FC<CustomButtonProps> = ({
  9. variant = 'primary',
  10. size = 'md',
  11. className = '',
  12. children
  13. }) => {
  14. const baseStyles = 'inline-flex items-center justify-center rounded-lg font-medium transition-colors'
  15. const variantStyles = {
  16. primary: 'bg-blue-500 text-white hover:bg-blue-600',
  17. secondary: 'bg-gray-100 text-gray-900 hover:bg-gray-200'
  18. }
  19. const sizeStyles = {
  20. sm: 'px-3 py-1.5 text-sm',
  21. md: 'px-4 py-2 text-base',
  22. lg: 'px-6 py-3 text-lg'
  23. }
  24. return (
  25. <button
  26. className={[
  27. baseStyles,
  28. variantStyles[variant],
  29. sizeStyles[size],
  30. className
  31. ].join(' ')}
  32. >
  33. {children}
  34. </button>
  35. )
  36. }
  37. export default CustomButton

组件文档规范

  1. # CustomButton
  2. 可定制的按钮组件,支持多种样式变体和尺寸。
  3. ## 安装
  4. npm install @your-org/custom-button
  5. ## 使用
  6. import CustomButton from '@your-org/custom-button'
  7. function App() {
  8. return (
  9. <CustomButton
  10. variant="primary"
  11. size="md"
  12. className="my-custom-class"
  13. >
  14. 点击我
  15. </CustomButton>
  16. )
  17. }
  18. ## Props
  19. | 属性 | 类型 | 默认值 | 说明 |
  20. |----------|----------|-----------|----------|
  21. | variant | string | 'primary' | 按钮变体 |
  22. | size | string | 'md' | 按钮尺寸 |
  23. | className| string | '' | 自定义类名 |
  24. | children | ReactNode| - | 按钮内容 |

文档建设

文档站点结构

  1. // docs/components/Layout.tsx
  2. const Layout: React.FC = ({ children }) => {
  3. return (
  4. <div className="min-h-screen bg-white">
  5. {/* 导航栏 */}
  6. <nav className="bg-white shadow-sm">
  7. <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
  8. <div className="flex justify-between h-16">
  9. <div className="flex">
  10. <div className="flex-shrink-0 flex items-center">
  11. <img
  12. className="h-8 w-auto"
  13. src="/logo.svg"
  14. alt="Logo"
  15. />
  16. </div>
  17. <div className="hidden sm:ml-6 sm:flex sm:space-x-8">
  18. <a
  19. href="#"
  20. className="border-blue-500 text-gray-900 inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium"
  21. >
  22. 文档
  23. </a>
  24. {/* 更多导航项 */}
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. </nav>
  30. {/* 主要内容 */}
  31. <div className="py-10">
  32. <main>
  33. <div className="max-w-7xl mx-auto sm:px-6 lg:px-8">
  34. {children}
  35. </div>
  36. </main>
  37. </div>
  38. </div>
  39. )
  40. }

MDX 集成示例

  1. // docs/pages/components/button.mdx
  2. import { CustomButton } from '@/components'
  3. # 按钮组件
  4. 按钮用于触发操作或事件。
  5. ## 基础用法
  6. <CustomButton>默认按钮</CustomButton>
  7. <CustomButton>默认按钮</CustomButton>

变体

  1. <div className="space-x-4">
  2. <CustomButton variant="primary">主要按钮</CustomButton>
  3. <CustomButton variant="secondary">次要按钮</CustomButton>
  4. </div>
  5. <CustomButton variant="primary">主要按钮</CustomButton>
  6. <CustomButton variant="secondary">次要按钮</CustomButton>
  7. ## API
  8. | 参数 | 说明 | 类型 | 默认值 |
  9. |----------|---------|---------|----------|
  10. | variant | 变体样式 | string | 'primary'|
  11. | size | 尺寸 | string | 'md' |

社区互动

Issue 模板

  1. # Issue 模板
  2. ## 问题类型
  3. - [ ] Bug 报告
  4. - [ ] 功能请求
  5. - [ ] 文档改进
  6. - [ ] 其他
  7. ## 问题描述
  8. [详细描述你的问题]
  9. ## 复现步骤
  10. 1. [第一步]
  11. 2. [第二步]
  12. 3. [更多步骤...]
  13. ## 期望行为
  14. [描述你期望看到的结果]
  15. ## 实际行为
  16. [描述实际发生的情况]
  17. ## 环境信息
  18. - Tailwind CSS 版本:
  19. - Node.js 版本:
  20. - 浏览器版本:
  21. - 操作系统:
  22. ## 其他信息
  23. [其他相关信息]

Pull Request 模板

  1. # Pull Request 模板
  2. ## 变更说明
  3. [描述这个 PR 做了什么]
  4. ## 变更类型
  5. - [ ] Bug 修复
  6. - [ ] 新功能
  7. - [ ] 代码优化
  8. - [ ] 文档更新
  9. - [ ] 其他
  10. ## 检查清单
  11. - [ ] 代码遵循项目规范
  12. - [ ] 添加了必要的测试
  13. - [ ] 更新了相关文档
  14. - [ ] 所有测试通过
  15. - [ ] 变更已经在本地测试
  16. ## 关联 Issue
  17. Fixes #[issue number]
  18. ## 截图(如果适用)
  19. [添加截图]
  20. ## 其他说明
  21. [其他需要说明的内容]

最佳实践

  1. 插件开发

    • 遵循命名规范
    • 完善的文档
    • 充分的测试
  2. 组件贡献

    • 组件职责单一
    • 接口设计合理
    • 样式可定制
  3. 文档建设

    • 结构清晰
    • 示例完整
    • 及时更新
  4. 社区参与

    • 积极回应问题
    • 遵循贡献规范
    • 维护友好氛围