Overview
BindJS is built around a few core concepts that make it powerful and easy to use. Understanding these concepts will help you build better components and make the most of the framework.Components
Components are the building blocks of your UI. Every BindJS component is defined using thedefineComponent function and consists of three main parts: metadata, properties, and a body function.
Basic Component Structure
Component Metadata
Metadata describes your component and helps organize it in the component library:Component Properties
Properties define the configurable inputs for your component. BindJS supports various property types: String Properties:Component Body
The body function receives props and children, and returns the component’s UI:Modifiers
Modifiers allow you to customize the appearance and behavior of components. They’re chainable methods that return a modified version of the component.Styling Modifiers
Layout Modifiers
Interactive Modifiers
Common Modifiers
.padding()- Add space around content.background()- Set background color or component.foregroundStyle()- Set text/icon color.font()- Set text font.frame()- Set size constraints.cornerRadius()- Round corners.shadow()- Add drop shadow.opacity()- Set transparency
State Management
BindJS provides hooks for managing component state, similar to React hooks.useState Hook
TheuseState hook lets you add state to your components:
useStore Hook
For shared state across components, useuseStore:
State Best Practices
- Keep state minimal - Only store what you need
- Derive values - Calculate derived values in the body, don’t store them
- Use appropriate hooks -
useStatefor local state,useStorefor shared state - Initialize with defaults - Always provide initial values
Layout Components
BindJS provides three main layout components for organizing your UI.VStack - Vertical Stack
Arranges children vertically:spacing- Space between childrenalignment- Horizontal alignment (leading,center,trailing)
HStack - Horizontal Stack
Arranges children horizontally:spacing- Space between childrenalignment- Vertical alignment (top,center,bottom)
ZStack - Overlay Stack
Layers children on top of each other:alignment- Where to align children (topLeading,center,bottomTrailing, etc.)
Layout Best Practices
- Use the right stack - VStack for vertical, HStack for horizontal, ZStack for overlays
- Set spacing - Consistent spacing improves visual hierarchy
- Use Spacer() - Push content to edges when needed
- Nest stacks - Combine stacks for complex layouts
Children
Components can accept children, which are passed as the second argument to the body function:Using Children
Conditional Children
Previews
Previews show different states and configurations of your component:Preview Best Practices
- Show different states - Empty, loading, error, success
- Demonstrate variations - Different colors, sizes, configurations
- Use meaningful names - Clear
.previewName()descriptions - Keep realistic - Use representative content
Next Steps
Now that you understand the core concepts, you’re ready to:- Build Your First Component: Follow a step-by-step tutorial
- Explore Components: See all available components
- Learn Modifiers: Discover all modifiers
- Read the LLM Guide: Comprehensive reference
