Styling Guide
Learn how to customize the appearance of @c15t/react components through our flexible theming system.
Our theming system provides a powerful way to customize the appearance of @c15t/react components while maintaining their functionality and accessibility. This guide will walk you through the different approaches to styling and help you understand how our theming system works.
Understanding the Theming System
Think of our theming system as a set of style instructions that cascade through your components, similar to CSS but with more structure and type safety. Each component has specific customizable areas, called theme keys, that you can target for styling.
For example, when you want to style a Cookie Banner, you might want to customize:
- The banner's container
- The title and description text
- The action buttons
- The overlay background
Our theming system makes this straightforward while ensuring you can't accidentally target non-existent elements.
Basic Theme Structure
Let's start with a simple example of customizing a Cookie Banner:
Every theme key follows a predictable pattern:
- The component name, which will be banner, widget, dialog.
- The element path (e.g., 'header.title' or 'footer.accept-button')
- Optional state variations (e.g., 'switch.thumb.checked')
Style Values
Our theming system supports several ways to define styles:
String Classes
The simplest approach is using class names:
Style Objects
For inline styles, use a style object:
CSS Variables
CSS Variables allow you to customize the component using the component's CSS variables.
Working with Component Hierarchies
@c15t/react components often contain nested elements. Understanding this hierarchy helps you style effectively:
Each level in the hierarchy can be styled independently, giving you fine-grained control over the appearance.
Using Theme Context
For consistent styling across your application, you can use theme context:
Disabling Default Styles
Sometimes you might want to start from scratch. Use the noStyle
prop to remove all default styling:
Type-Safe Styling
Our theming system includes TypeScript support to help prevent errors:
Responsive Design
You can use media queries and responsive classes just like you would in regular CSS:
Common Patterns
Here are some effective patterns for common styling needs:
Creating a Clean, Modern Look
Building Dark Mode Support
Best Practices
-
Maintain Accessibility
- Ensure sufficient color contrast
- Keep interactive elements visually distinct
- Test your styles with screen readers
-
Consider Responsive Design
- Start with mobile-first styles
- Use flexible units (rem, em) over fixed pixels
- Test across different screen sizes
-
Performance
- Group related styles together
- Avoid unnecessary style overrides
- Use CSS classes over inline styles when possible
-
Maintainability
- Keep theme configurations organized
- Document custom styles
- Use consistent naming conventions
Troubleshooting
If your styles aren't applying as expected, check these common issues:
-
Style Precedence
- Component-level styles take precedence over theme context
- More specific selectors take precedence over general ones
-
Class Name Conflicts
- Use unique class names to avoid conflicts
- Consider using CSS modules or similar solutions
-
Theme Key Accuracy
- Verify theme keys match exactly
- Check for typos in dot notation paths