React bindings for Decentraland's Entity Component System (ECS), providing a declarative way to build UIs in Decentraland scenes using React's component model and JSX syntax.
- Flexbox-based Layout: Implements a subset of CSS Flexbox for powerful and intuitive UI layouts
- React Components: Familiar React-like component API for building UIs
- Type Safety: Full TypeScript support with proper type definitions
- Event Handling: Support for mouse events and user interactions
- Performance: Optimized reconciliation for minimal runtime overhead
- Theme Support: Built-in light and dark theme system with context-based switching
All components in @dcl/react-ecs must:
- Return JSX.Elements for consistency and composability
- Support theme integration through context
- Be reusable and composable with other components
npm install @dcl/react-ecsimport { ReactEcs, UiEntity, Label, Button } from '@dcl/react-ecs'
export function MyUI() {
return (
<UiEntity
uiTransform={{
width: 300,
height: 100,
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
}}
>
<Label value="Hello Decentraland!" />
<Button value="Click Me!" onMouseDown={() => console.log('clicked!')} />
</UiEntity>
)
}The package includes a theme system for consistent UI styling:
import { ReactEcs, ThemeProvider, Button } from '@dcl/react-ecs'
// Wrap your UI with ThemeProvider
export function MyUI() {
return (
<ThemeProvider>
<Button value="Theme-Aware Button" />
</ThemeProvider>
)
}
// Use themes in custom components
function CustomComponent() {
const { theme, toggleTheme } = React.useContext(ThemeContext)
return (
<UiEntity
uiBackground={{
color: theme === 'light' ? '#FFFFFF' : '#000000'
}}
onMouseDown={toggleTheme}
/>
)
}- UiEntity: Base component for UI elements
- Label: Text display component
- Button: Interactive button component
- Input: Text input field
- Dropdown: Selection dropdown menu
The layout system is based on Flexbox and supports the following properties:
display: 'flex'flexDirection: 'row' | 'column'justifyContent: 'flex-start' | 'center' | 'flex-end' | 'space-between' | 'space-around'alignItems: 'flex-start' | 'center' | 'flex-end' | 'stretch'width,heightmargin,paddingpositionType: 'absolute' | 'relative'
Components support the following mouse events:
<Button onMouseDown={() => {}} onMouseUp={() => {}} onMouseEnter={() => {}} onMouseLeave={() => {}} />The package implements a custom React reconciler that bridges React's component model with Decentraland's ECS. It:
- Translates JSX into ECS entities and components
- Manages component lifecycle and updates
- Handles event delegation and bubbling
- Provides a performant update mechanism
For more details about the UI system architecture and design decisions:
- ADR-124: Implementing Flexbox-based UI
- ADR-125: User Interface Components
- ADR-237: SDK 7 Custom UI Components
Apache 2.0