Overview
Svelte Atoms Core is inspired by chemistry concepts, providing two fundamental building blocks that form the foundation of all UI components: Atoms and Bonds.The chemistry metaphor reflects how components are structured: atoms are the smallest functional units, while bonds connect and coordinate multiple atoms together.
Atoms
What is an Atom?
An Atom is the smallest, reusable UI component with a single responsibility. Each atom represents a fundamental unit of UI functionality and focuses on doing one thing exceptionally well. Key Characteristics:- Single Responsibility: Each atom has one clear purpose
- Composable: Atoms can be combined to create complex components
- Consistent Base: All atoms inherit from the base
atom.sveltecomponent - Headless by Default: No opinionated styling, bring your own CSS
- Button
- Input
- Badge
- Avatar
- Label
- Icon
Atom Variants
All atoms are rendered internally using the baseatom.svelte component, which provides three rendering variants:
- Html
- Svg
- MathML
Renders HTML elements using the
HtmlAtom component. This is the most common variant for building standard UI components.Core Atom Props
Every atom inherits these core props from the base atom component:Element Control
Lifecycle Hooks
Animation Hooks
Using the as Prop
The as prop enables dynamic element transformation, allowing you to render any HTML element while maintaining the atom’s behavior:
Using the base Prop
The base prop enables powerful component composition patterns:
Bonds
What is a Bond?
A Bond is a communication mechanism that connects multiple atoms together. Bonds manage shared state and enable coordination between connected atoms and their descendant components. Key Characteristics:- State Management: Maintains internal state accessible to all bonded atoms
- Communication: Enables atoms to communicate and coordinate behavior
- Scoped Access: Bond state is accessible to bonded atoms and their descendants
- Context-Based: Uses Svelte context for state sharing
Bond Architecture
Bonds are implemented using a two-class pattern:Bond Implementation Pattern
Bonds are typically created in*-root.svelte components:
Accessing Bond State
Descendant components can access the bond through context:Real-World Example: Accordion
Here’s how atoms and bonds work together in the Accordion component:How the Accordion Bond Works
How the Accordion Bond Works
- Root Component: Creates
AccordionBondand shares it via context - Item Components: Access the bond to check if they’re open
- Header Components: Use bond methods to toggle item state
- Indicator Components: React to bond state to show open/closed
- State Synchronization: All components stay in sync through the bond
Benefits of Atoms and Bonds
Flexibility
Atoms
Mix and match atoms to create custom component combinations without modifying core code
Bonds
Connect any atoms together with shared state management for coordinated behavior
Reusability
Use the same atoms in different contexts and compositions:Consistency
Shared base ensures uniform behavior across all composed components:- Lifecycle hooks (
onmount,ondestroy) - Animation hooks (
initial,enter,exit,animate) - Element transformation (
asprop) - TailwindCSS integration
- Base composition (
baseprop)
Next Steps
Composition Patterns
Learn how to compose atoms into powerful UI components
Styling
Explore styling approaches and TailwindCSS integration
Accessibility
Discover built-in accessibility features
Animations
Master animation lifecycle hooks