Styling
RefaceComposer provides CSS-in-JS styling through the Styled Plugin.
Basic Usage
Simple Styled Component
example.typescripttypescript
1import { styled } from "@reface/plugins/styled";23const Button = styled.button`4 & {5 background: blue;6 color: white;7 padding: 8px 16px;8 }910 &:hover {11 background: darkblue;12 }13`;1415// Usage16<Button>Click me</Button>;
Component Extension
example.typescripttypescript
1const BaseButton = styled.button`2 & {3 padding: 8px 16px;4 border: none;5 border-radius: 4px;6 }7`;89const PrimaryButton = styled(BaseButton)`10 & {11 background: blue;12 color: white;13 }14`;1516const SecondaryButton = styled(BaseButton)`17 & {18 background: gray;19 color: white;20 }21`;
CSS Features
Nested Selectors
example.typescripttypescript
1const Card = styled.div`2 & {3 padding: 16px;4 border: 1px solid #eee;5 }67 & h2 {8 margin: 0;9 color: #333;10 }1112 & p {13 color: #666;14 }1516 & .footer {17 margin-top: 16px;18 border-top: 1px solid #eee;19 }20`;
Pseudo Classes
example.typescripttypescript
1const Input = styled.input`2 & {3 border: 1px solid #ccc;4 padding: 8px;5 }67 &:focus {8 border-color: blue;9 outline: none;10 }1112 &::placeholder {13 color: #999;14 }1516 &:disabled {17 background: #f5f5f5;18 }19`;
Media Queries
example.typescripttypescript
1const Container = styled.div`2 & {3 width: 100%;4 padding: 16px;5 }67 @media (min-width: 768px) {8 & {9 width: 750px;10 margin: 0 auto;11 }12 }1314 @media (min-width: 1200px) {15 & {16 width: 1170px;17 }18 }19`;
Implementation Details
Class Generation
example.typescripttypescript
1// Unique class names are automatically generated2const Button = styled.button`...`;3// Renders as: <button class="s1a">...</button>45const Card = styled.div`...`;6// Renders as: <div class="s1b">...</div>
Style Collection
example.typescripttypescript
1// Styles are automatically collected and added to <style> tag2const composer = new RefaceComposer();3composer.use(new StyledPlugin());45const html = composer.render(6 <div>7 <Button>Click me</Button>8 <Card>Content</Card>9 </div>,10);1112// Renders as:13// <div>14// <button class="s1a">Click me</button>15// <div class="s1b">Content</div>16// </div>17// <style>18// .s1a { ... }19// .s1b { ... }20// </style>
CSS Parser
Replaces
&
with generated class nameHandles nested selectors
Processes media queries
Maintains selector specificity
Best Practices
Component Design
Keep styles component-scoped
Use semantic class names
Follow BEM-like nesting
Avoid deep nesting
Performance
Reuse base components
Minimize style rules
Use efficient selectors
Share common styles
Maintainability
Clear style structure
Consistent patterns
Documentation
Type safety
Browser Support
Cross-browser testing
Fallback styles
Progressive enhancement
Vendor prefixes