by 2389-research
此技能将混乱的 CSS(来自内联样式和重复的实用类)转换为有组织的语义化组件。它提取模式、添加暗色模式支持、创建测试,并通过 Tailwind 组合确保可维护的样式。
1. 打开 Claude 聊天界面
2. 点击下方 "📋 复制" 按钮
3. 粘贴到 Claude 聊天框中并发送
4. 输入 "使用 css-development-refactor 技能" 开始使用
=== css-development-refactor 技能 === 作者: 2389-research 描述: 此技能将混乱的 CSS(来自内联样式和重复的实用类)转换为有组织的语义化组件。它提取模式、添加暗色模式支持、创建测试,并通过 Tailwind 组合确保可维护的样式。 使用方法: 1. 调用技能: "使用 css-development-refactor 技能" 2. 提供相关信息: 根据技能要求提供必要参数 3. 查看结果: 技能会返回处理结果 示例: "使用 css-development-refactor 技能,帮我分析一下这段代码"
这种方法适用于所有 Claude 用户,不需要安装额外工具。
coding
safe
Transforms existing CSS into semantic component patterns:
@apply compositionsThis is a sub-skill of css-development - typically invoked automatically via the main skill.
Use when:
This skill refactors toward patterns documented in the main css-development skill:
Semantic naming: .button-primary not .btn-blue
Tailwind composition: Use @apply to compose utilities
Dark mode: Include dark: variants
Composition first: Reuse existing classes before creating new
Test coverage: Static CSS + component rendering tests
When this skill is invoked, create a TodoWrite checklist and refactor systematically.
"I'm using the css-development:refactor skill to transform this CSS into semantic component patterns."
Use the TodoWrite tool:
Refactoring CSS:
- [ ] Analyze existing CSS (identify what needs refactoring)
- [ ] Find repeated patterns (look for duplicated utility combinations)
- [ ] Check existing components (see if patterns already exist)
- [ ] Extract to semantic classes (create new classes using @apply)
- [ ] Include dark mode (add dark: variants to new classes)
- [ ] Update markup (replace inline/utility classes with semantic names)
- [ ] Add tests (write static CSS and rendering tests)
- [ ] Document components (add usage comments)
- [ ] Verify behavior unchanged (ensure visual output matches original)
Action: Read and understand the CSS that needs refactoring
Look for:
Example patterns to refactor:
// Inline styles
<button style={{ background: 'indigo', padding: '1.5rem 2rem' }}>Click</button>
// Repeated utilities in markup
<button class="bg-indigo-500 hover:bg-indigo-700 px-6 py-3 rounded-lg text-white">
Click me
</button>
<button class="bg-indigo-500 hover:bg-indigo-700 px-6 py-3 rounded-lg text-white">
Submit
</button>
// Non-semantic CSS
.btn-blue {
background: blue;
padding: 12px 24px;
}
Capture:
Mark as completed when analysis is done.
Action: Identify duplicated utility combinations that should become semantic classes
Use Grep tool to search for repeated patterns:
# Search for common utility combinations
grep -r "bg-indigo-500 hover:bg-indigo-700 px-6 py-3" .
grep -r "rounded-lg shadow-md p-6" .
Categorize patterns:
For each pattern:
Mark as completed when patterns are cataloged.
Action: Read styles/components.css to see if patterns already exist
Before creating new classes, check:
Example:
Pattern found: bg-indigo-500 hover:bg-indigo-700 px-6 py-3 rounded-lg text-white
Check: Does .button-primary already exist? YES
Solution: Use .button-primary instead of creating new class
Decision for each pattern:
Mark as completed when reuse opportunities are identified.
Action: Create new semantic classes in styles/components.css for patterns that need extraction
For each pattern being extracted:
@applyExample extraction:
Before (in markup):
<button class="bg-indigo-500 hover:bg-indigo-700 px-6 py-3 rounded-lg text-white transition-all duration-200">
Click me
</button>
After (in components.css):
/* Primary button - Main call-to-action button with hover states
Usage: <button className="button-primary">Click me</button> */
.button-primary {
@apply bg-indigo-500 hover:bg-indigo-700 dark:bg-indigo-600 dark:hover:bg-indigo-800;
@apply px-6 py-3 rounded-lg text-white;
@apply transition-all duration-200;
}
Use Edit tool to add each new class to components.css
Mark as completed when all semantic classes are created.
Action: Ensure all new/refactored classes have dark: variants
For each class created in Step 4:
dark: variants for backgroundsdark: variants for text colorsdark: variants for bordersPattern:
.component {
@apply bg-white dark:bg-gray-800;
@apply text-gray-900 dark:text-white;
@apply border-gray-200 dark:border-gray-700;
}
Mark as completed when dark mode coverage is added.
Action: Replace inline styles and utility classes with semantic class names
For each file using the old pattern:
Example:
Before:
<button class="bg-indigo-500 hover:bg-indigo-700 px-6 py-3 rounded-lg text-white">
Click me
</button>
After:
<button class="button-primary">
Click me
</button>
Handle custom classes:
<!-- If there were additional custom classes, preserve them -->
<button class="button-primary w-full mt-4">
Click me
</button>
Track changes:
Mark as completed when all markup is updated.
Action: Add test coverage for refactored components
Static CSS test in styles/__tests__/components.test.ts:
it('should have button-primary class', () => {
const content = readFileSync('styles/components.css', 'utf-8');
expect(content).toContain('.button-primary');
});
it('should have dark mode variants in button-primary', () => {
const content = readFileSync('styles/components.css', 'utf-8');
expect(content).toContain('dark:bg-indigo');
});
Component rendering test (if applicable):
it('applies button-primary class after refactor', () => {
render(<Button variant="primary">Click</Button>);
expect(screen.getByRole('button')).toHaveClass('button-primary');
});
Run tests to ensure they pass:
npm test
Mark as completed when tests are added and passing.
Action: Ensure all refactored classes have documentation
Documentation includes:
Example:
/* Primary button - Main CTA button (refactored from inline utilities)
Usage: <button className="button-primary">Click me</button>
Replaces: bg-indigo-500 hover:bg-indigo-700 px-6 py-3 rounded-lg text-white */
.button-primary {
@apply bg-indigo-500 hover:bg-indigo-700 dark:bg-indigo-600 dark:hover:bg-indigo-800;
@apply px-6 py-3 rounded-lg text-white;
@apply transition-all duration-200;
}
Mark as completed when documentation is added.
Action: Ensure visual output and behavior match the original
Verification steps:
Run tests (if project has them):
npm test
Visual inspection (if possible):
Check for regressions:
If behavior changed:
Behavior must be preserved - refactoring should be visually neutral
Mark as completed when behavior is verified unchanged.
When all checklist items are completed:
## CSS Refactoring Summary
### Changes Made
**Semantic classes created:**
- `.button-primary` (extracted from 8 instances across 5 files)
- `.card` (extracted from 12 instances across 7 files)
- `.badge-success` (extracted from 4 instances across 3 files)
**Files modified:**
- `styles/components.css` (+45 lines, 3 new classes)
- `components/Button.tsx` (replaced utilities with .button-primary)
- `components/Card.tsx` (replaced utilities with .card)
- `components/Badge.tsx` (replaced utilities with .badge-success)
- `styles/__tests__/components.test.ts` (+12 lines, 3 new tests)
**Dark mode support:**
- ✅ All refactored classes include dark: variants
- ✅ Tested in both light and dark mode
**Test coverage:**
- ✅ Static CSS tests added for all new classes
- ✅ Component rendering tests updated
- ✅ All tests passing
**Behavior verification:**
- ✅ Visual output matches original
- ✅ No console errors
- ✅ Interactive states work correctly
### Impact
**Code reduction:**
- Removed 247 lines of repeated utility classes from markup
- Added 45 lines of semantic CSS
- Net reduction: 202 lines
**Maintainability:**
- Styling centralized in components.css
- Changes now made in one place instead of many
- Consistent component appearance
**Dark mode:**
- Added dark mode support that didn't exist before
- All components now work in light and dark themes
Suggest next steps:
Offer validation: "Would you like me to validate the refactored CSS using the css-development:validate skill?"
Mark as completed when summary is presented.
View Count
0
Download Count
0
Favorite Count
0
Quality Score
70