Overview
FormEngine Core provides multiple mechanisms for styling custom components:- CSS Classes: Styles are passed through the
classNameprop - Inline Styles: Dynamic styles are passed through the
styleprop cssannotation: Structured CSS property definitions that FormEngine Core can apply to components- Component Wrapping: Understanding how FormEngine Core structures components for styling
Basic Styling with CSS Classes
The fundamental approach to styling in FormEngine Core is accepting aclassName prop and passing it to the rendered element. FormEngine
Core automatically generates and passes CSS class names to your components.
Live Example
live
className prop. Your component should
merge these with its own base classes.
Understanding Component Wrapping
FormEngine Core applies styles through a structured component hierarchy. Understanding this structure helps you design components that work well with the styling system:- Wrapper styles affect positioning, sizing, padding, and layout
- Component styles affect the actual component’s visual appearance
- Container components receive all styles directly through the
classNameprop
className prop.
Using the css Annotation for Structured Styling
The css method allows you to define structured CSS properties that FormEngine Core can apply to your components. This creates a type-safe interface for styling configuration.Basic css Annotation Usage
css become part of the component’s type definition, allowing FormEngine Core to apply these styles
appropriately.
Live Example
live
Available CSS Property Types
FormEngine Core provides several type helpers for defining CSS properties: | Type | Description | Example | |----------------------------------------------------------------------|------------------------------------------------|---------------------------------------------| | color | Color values (hex, rgb, rgba, hsl, hsla) |color.default('#ff0000') |
| size | CSS size values (px, em,
rem, %, vw, vh, etc.) | size.default('16px') |
| number | Numeric values
| number.default(1) |
| oneOf | Enumerated values
| oneOf('solid', 'dashed').default('solid') |
| cssSize | Special size type with
validation | cssSize.setup({default: '100%'}) |
Complete Example: Styled Button Component
Live Example
live
Advanced Styling Patterns
Reusable Style Objects
Create reusable style objects for consistency across your component library:commonStyles.ts
InfoCard.tsx
Conditional Styling Based on Props
Combine component props with CSS properties for dynamic styling behavior:Inline Styles
In addition to CSS classes, FormEngine Core can also pass styles through thestyle prop. This is useful for:
- Components that don’t support
classNameprop - Dynamic styling that needs runtime calculation
- Situations where both
classNameandstyleprops are needed
Using Inline Styles
FormEngine Core can pass styles through thestyle prop when configured appropriately. Your
component should accept both className and style props:
Live Example
live
Combining ClassName and Inline Styles
For maximum flexibility, design your components to handle both styling approaches:Live Example
live
Best Practices
1. Always Accept className and style Props
Design your components to accept both styling mechanisms:2. Use Structured CSS Properties
Prefer css for type-safe styling definitions:3. Group Related Styles
Organize CSS properties logically for better maintainability:4. Set Sensible Defaults
Provide appropriate default values for CSS properties:5. Use Type-Safe Enums
UseoneOf for properties with limited valid values:
6. Test with Different Styling Approaches
Test your components with various styling configurations to ensure robustness.Real-World Example: Complete Styled Card Component
Summary
Styling custom components in FormEngine Core involves several key concepts:- CSS Classes: The primary mechanism for styling, passed via the
classNameprop - Inline Styles: Additional styling through the
styleprop for dynamic or component-specific needs - The
cssannotation: Structured, type-safe CSS property definitions that integrate with FormEngine Core - Component Structure: Understanding wrapper vs. component styling for proper implementation
- Always design components to accept
classNameand optionallystyleprops - The
cssannotation for structured, maintainable styling definitions - Provide sensible defaults for CSS properties
- Use type-safe enums (
oneOf) for properties with limited valid values - Test components with various styling approaches