1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! Component Area Hook - Access to component's render area
//!
//! This module provides a professional context-based API for accessing the current
//! component's render area (Rect). The renderer provides the area via context,
//! and components can consume it using `use_area()`.
//!
//! # Architecture
//!
//! - Renderer provides area via `use_context_provider`
//! - Components consume area via `use_area()` (which calls `use_context`)
//! - Clean separation of concerns following SOLID principles
//!
//! # Use Cases
//!
//! - Layout calculations
//! - Responsive design decisions
//! - Scroll calculations
//! - Mouse event handling within component bounds
use crateuse_context;
use Rect;
use Deref;
/// Context type for component render area
///
/// This is provided by the renderer and consumed by components via `use_area()`.
///
/// Implements `Deref<Target = Rect>` so you can access Rect methods directly:
///
/// ```rust,ignore
/// use reratui::prelude::*;
/// use ratatui::layout::Margin;
///
/// let area = use_area();
/// let width = area.width; // Direct access to Rect fields
/// let height = area.height;
/// let inner = area.inner(&Margin::new(1, 1)); // Call Rect methods
/// ```
;
/// Hook to access the current component's render area
///
/// This hook retrieves the component's render area from the context provided
/// by the renderer. The area represents the Rect that the component is being
/// rendered into.
///
/// # Panics
///
/// Panics if called outside of a component render context where the area
/// has been provided by the renderer.
///
/// # Use Cases
///
/// - **Layout calculations**: Determine available space for child components
/// - **Responsive design**: Adjust rendering based on available space
/// - **Positioning**: Calculate absolute positions within the component
/// - **Scroll handling**: Track viewport dimensions for scrollable content
/// - **Mouse events**: Determine if mouse events are within component bounds
///
/// # Examples
///
/// ## Basic Usage - Direct Field Access
///
/// ```rust,ignore
/// use reratui::prelude::*;
/// #[component]
/// fn MyComponent() -> Element {
/// let area = use_area();
///
/// // Direct access to Rect fields via Deref
/// println!("Width: {}, Height: {}", area.width, area.height);
/// println!("Position: ({}, {})", area.x, area.y);
///
/// rsx! {
/// <Block title="My Component">
/// <Paragraph>
/// {format!("Size: {}x{}", area.width, area.height)}
/// </Paragraph>
/// </Block>
/// }
/// }
/// ```
///
/// ## Responsive Layout
///
/// ```rust,ignore
/// use reratui::prelude::*;
/// #[component]
/// fn AdaptiveLayout() -> Element {
/// let area = use_area();
///
/// // Direct field access - no .0 needed!
/// let layout_mode = if area.width < 80 {
/// "compact"
/// } else {
/// "wide"
/// };
///
/// rsx! {
/// <Block title={format!("Layout: {}", layout_mode)}>
/// <Paragraph>
/// {format!("Adaptive content")}
/// </Paragraph>
/// </Block>
/// }
/// }
/// ```
///
/// ## Scroll Viewport
///
/// ```rust,ignore
/// use reratui::prelude::*;
/// #[component]
/// fn ScrollableList() -> Element {
/// let area = use_area();
/// let (scroll_offset, set_scroll) = use_state(|| 0u16);
///
/// // Calculate visible items based on area height
/// let visible_count = area.height.saturating_sub(2);
///
/// rsx! {
/// <Block title="Scrollable List">
/// <Paragraph>
/// {format!("Showing {} items", visible_count)}
/// </Paragraph>
/// </Block>
/// }
/// }
/// ```
///
/// ## Using Rect Methods
///
/// ```rust,ignore
/// use reratui::prelude::*;
/// use ratatui::layout::Margin;
/// #[component]
/// fn LayoutComponent() -> Element {
/// let area = use_area();
///
/// // Call Rect methods directly
/// let inner = area.inner(&Margin::new(1, 1));
/// let is_empty = area.is_empty();
/// let area_size = area.area();
///
/// rsx! {
/// <Block title="Layout">
/// <Paragraph>
/// {format!("Inner: {}x{}", inner.width, inner.height)}
/// </Paragraph>
/// </Block>
/// }
/// }
/// ```