Building New Components
When building new components with the Coinbase Design System (CDS), it's important to adhere to the principles of consistency, reusability, and accessibility. This guide provides a structured approach to creating custom components that seamlessly integrate with CDS while maintaining the flexibility to address unique application requirements.
Before You Begin
Before building a new component, consider:
- Reusability: Could the new component be reused across your application or even other projects?
- Existing Components: Does an existing CDS component already fulfill your requirements? Consider extending or customizing existing components before creating new ones.
- Design Alignment: Ensure the new component adheres to Coinbase’s design guidelines and branding.
Core Principles
Consistency
Use CDS’s theming, styling, and layout primitives to ensure the new component integrates seamlessly with the rest of your application.
Accessibility
All components must meet accessibility standards, including proper keyboard navigation, focus management, and ARIA attributes where applicable.
Performance
Ensure components are optimized for performance, especially when handling state or rendering large datasets.
Steps to Build a New Component
1. Define the Component API
Start by defining the props and API for your new component. Ensure the API:
- Is intuitive and easy to use.
- Leverages existing CDS props, such as
StyleProps
and responsive design support. - Includes clear default values for commonly used props.
Example:
2. Choose a Base Component
CDS provides flexible primitives like Box
, HStack
, and VStack
that can be used as the foundation for building new components. Start with these components to leverage built-in theming and styling capabilities.
Example:
3. Use StyleProps for Customization
Enable developers to customize your component by supporting CDS StyleProps
. This makes the component flexible and ensures it integrates well with the rest of the system.
Example:
4. Implement Theming
Ensure the component supports theming by using CDS theme variables. For example, use color.textPrimary
or space.3
instead of hardcoding values.
Example:
5. Add Accessibility Features
Use ARIA attributes, keyboard navigation, and focus management to make your component accessible.
Example:
6. Test Your Component
Thoroughly test your component in the following areas:
- Visual Tests: Check theming, responsive behavior, and consistency.
- Functional Tests: Validate interactivity, state management, and accessibility.
- Performance Tests: Ensure the component performs well in various scenarios, such as large datasets.
7. Document Your Component
Provide clear documentation, including:
- Usage Examples: Show how to use the component with different props.
- API Reference: Document all props and their types.
- Customization: Highlight ways to customize the component with
StyleProps
or themes.
Example: Custom Card Component
Here’s a complete example of a custom Card
component:
Best Practices
- Reuse CDS Primitives: Start with
Box
,HStack
, orVStack
to save development time. - Follow Design Guidelines: Ensure your component aligns with Coinbase’s design principles.
- Test Accessibility: Use tools like Axe or Lighthouse to validate accessibility.
- Document Clearly: Make it easy for other developers to understand and use your component.
Next Steps
- Component Library: Explore the full set of CDS components.
- Styling: Learn how to use
StyleProps
and responsive design. - Theming: Understand how to integrate theme variables into your components.
By following these guidelines, you can build robust, reusable components that integrate seamlessly with the Coinbase Design System!