Skip to content

Commit ce9985f

Browse files
committed
fix(renderer): render the root component correctly
1 parent 0665bb3 commit ce9985f

File tree

5 files changed

+218
-158
lines changed

5 files changed

+218
-158
lines changed

docs/pages/docs/component.mdx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,19 @@ local signal = n.create_signal({
455455
value = 1,
456456
})
457457

458-
local body = counter({
459-
border_label = {
460-
text = "Counter",
461-
align = "center",
462-
},
463-
autofocus = true,
464-
value = signal.value,
465-
on_change = function(value)
466-
signal.value = value
467-
end,
468-
})
458+
local body = function()
459+
return counter({
460+
border_label = {
461+
text = "Counter",
462+
align = "center",
463+
},
464+
autofocus = true,
465+
value = signal.value,
466+
on_change = function(value)
467+
signal.value = value
468+
end,
469+
})
470+
end
469471

470472
renderer:render(body)
471473
```

docs/pages/docs/getting-started.mdx

Lines changed: 87 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -39,72 +39,76 @@ local renderer = n.create_renderer({
3939

4040
Next, let's define the `body` variable that contains a two-row layout. In the first row, we have a text input field, followed by a small gap and a button labeled `Send`. The second row has a paragraph element that displays the text `nui.components`.
4141

42-
```lua showLineNumbers {8-29}
42+
```lua showLineNumbers {8-31}
4343
local n = require("nui-components")
4444

4545
local renderer = n.create_renderer({
4646
width = 60,
4747
height = 8,
4848
})
4949

50-
local body = n.rows(
51-
n.columns(
52-
{ flex = 0 },
53-
n.text_input({
54-
autofocus = true,
55-
flex = 1,
56-
max_lines = 1,
57-
}),
58-
n.gap(1),
59-
n.button({
60-
label = "Send",
61-
padding = {
62-
top = 1,
63-
},
50+
local body = function()
51+
return n.rows(
52+
n.columns(
53+
{ flex = 0 },
54+
n.text_input({
55+
autofocus = true,
56+
flex = 1,
57+
max_lines = 1,
58+
}),
59+
n.gap(1),
60+
n.button({
61+
label = "Send",
62+
padding = {
63+
top = 1,
64+
},
65+
})
66+
),
67+
n.paragraph({
68+
lines = "nui.components",
69+
align = "center",
70+
is_focusable = false,
6471
})
65-
),
66-
n.paragraph({
67-
lines = "nui.components",
68-
align = "center",
69-
is_focusable = false,
70-
})
71-
)
72+
)
73+
end
7274
```
7375

7476
The text input field has some custom settings. It will automatically gain focus when the renderer is mounted, it will take up all the available space in its row and it will only allow one line of text to be entered. The paragraph component will be centered horizontally, and it won't be focusable.
7577

7678
Finally, we can pass the `body` layout to the `renderer` function, which renders the defined layout.
7779

78-
```lua showLineNumbers {31}
80+
```lua showLineNumbers {33}
7981
local n = require("nui-components")
8082

8183
local renderer = n.create_renderer({
8284
width = 60,
8385
height = 4,
8486
})
8587

86-
local body = n.rows(
87-
n.columns(
88-
{ flex = 0 },
89-
n.text_input({
90-
autofocus = true,
91-
flex = 1,
92-
max_lines = 1,
93-
}),
94-
n.gap(1),
95-
n.button({
96-
label = "Send",
97-
padding = {
98-
top = 1,
99-
},
88+
local body = function()
89+
return n.rows(
90+
n.columns(
91+
{ flex = 0 },
92+
n.text_input({
93+
autofocus = true,
94+
flex = 1,
95+
max_lines = 1,
96+
}),
97+
n.gap(1),
98+
n.button({
99+
label = "Send",
100+
padding = {
101+
top = 1,
102+
},
103+
})
104+
),
105+
n.paragraph({
106+
lines = "nui.components",
107+
align = "center",
108+
is_focusable = false,
100109
})
101-
),
102-
n.paragraph({
103-
lines = "nui.components",
104-
align = "center",
105-
is_focusable = false,
106-
})
107-
)
110+
)
111+
end
108112

109113
renderer:render(body)
110114
```
@@ -116,7 +120,7 @@ Here's the result:
116120

117121
Now, let's make the UI implementation more reactive! Check out [this page](/docs/signal) to discover the power of `Signals`.
118122

119-
```lua showLineNumbers {6-9,15,26-34,36-40,43}
123+
```lua showLineNumbers {6-9,16,27-35,37-41,44}
120124
local renderer = n.create_renderer({
121125
width = 60,
122126
height = 8,
@@ -127,43 +131,45 @@ local signal = n.create_signal({
127131
text = "nui.components",
128132
})
129133

130-
local body = n.rows(
131-
n.columns(
132-
{ flex = 0 },
133-
n.text_input({
134-
id = "text-input",
135-
autofocus = true,
136-
flex = 1,
137-
max_lines = 1,
138-
}),
139-
n.gap(1),
140-
n.button({
141-
label = "Send",
142-
padding = {
143-
top = 1,
144-
},
145-
on_press = function()
146-
signal.is_loading = true
147-
148-
vim.defer_fn(function()
149-
local ref = renderer:get_component_by_id("text-input")
150-
signal.is_loading = false
151-
signal.text = ref:get_current_value()
152-
end, 2000)
153-
end,
154-
}),
155-
n.spinner({
156-
is_loading = signal.is_loading,
157-
padding = { top = 1, left = 1 },
158-
hidden = signal.is_loading:negate(),
134+
local body = function()
135+
return n.rows(
136+
n.columns(
137+
{ flex = 0 },
138+
n.text_input({
139+
id = "text-input",
140+
autofocus = true,
141+
flex = 1,
142+
max_lines = 1,
143+
}),
144+
n.gap(1),
145+
n.button({
146+
label = "Send",
147+
padding = {
148+
top = 1,
149+
},
150+
on_press = function()
151+
signal.is_loading = true
152+
153+
vim.defer_fn(function()
154+
local ref = renderer:get_component_by_id("text-input")
155+
signal.is_loading = false
156+
signal.text = ref:get_current_value()
157+
end, 2000)
158+
end,
159+
}),
160+
n.spinner({
161+
is_loading = signal.is_loading,
162+
padding = { top = 1, left = 1 },
163+
hidden = signal.is_loading:negate(),
164+
})
165+
),
166+
n.paragraph({
167+
lines = signal.text,
168+
align = "center",
169+
is_focusable = false,
159170
})
160-
),
161-
n.paragraph({
162-
lines = signal.text,
163-
align = "center",
164-
is_focusable = false,
165-
})
166-
)
171+
)
172+
end
167173

168174
renderer:render(body)
169175
```

docs/pages/docs/renderer.mdx

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Callout } from 'nextra/components';
12
import { Property } from '../../components/Property'
23
import { Method } from '../../components/Method'
34

@@ -19,7 +20,9 @@ local subscription = signal:observe(function(previous_state, current_state)
1920
-- call side effects
2021
end)
2122

22-
local body = n.rows(…)
23+
local body = function()
24+
return n.rows(…)
25+
end
2326

2427
renderer:add_mappings({
2528
{
@@ -80,7 +83,7 @@ For further information, please refer to the documentation of [`nui.nvim`](https
8083
For further information, please refer to the documentation of [`nui.nvim`](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/layout#relative).
8184
</Property>
8285

83-
#### `keymap`
86+
#### keymap
8487

8588
> A table containing a default global key bindings for the renderer.
8689
@@ -120,10 +123,47 @@ For further information, please refer to the documentation of [`nui.nvim`](https
120123
<Method
121124
name="render"
122125
args={[
123-
['component', 'Component'],
126+
['render_fn', 'fun(): Component'],
124127
]}
125128
/>
126129

130+
<Callout type="info">
131+
If the `render` method is called in the same function as the root component definition, `Component` can be passed instead of `fun(): Component`.
132+
</Callout>
133+
134+
Examples:
135+
136+
```lua
137+
local n = require("nui-components")
138+
139+
local renderer = n.create_renderer({
140+
width = 100,
141+
height = 24,
142+
})
143+
144+
local body = function()
145+
return n.rows(…)
146+
end
147+
148+
vim.keymap.set("n", "<leader>i", function()
149+
renderer:render(body)
150+
end, { noremap = true, desc = "" })
151+
```
152+
153+
```lua
154+
local n = require("nui-components")
155+
156+
local renderer = n.create_renderer({
157+
width = 100,
158+
height = 24,
159+
})
160+
161+
vim.keymap.set("n", "<leader>i", function()
162+
local body = n.rows(…)
163+
renderer:render(body)
164+
end, { noremap = true, desc = "" })
165+
```
166+
127167
#### schedule
128168

129169
> Schedules a callback to be executed after the current update cycle has finished.

0 commit comments

Comments
 (0)