Documentation
Renderer

Renderer

Renderer is a key element in building UI with NuiComponents. Renderer takes responsibility for rendering, layout, focus management, and keybindings for the components it contains. Developers can create complex interfaces by nesting components within each other, and can also reuse them with different configurations.

Usage Example

local renderer = n.create_renderer({
  width = 80,
  height = 40,
})
 
local signal = n.create_signal(…)
 
local subscription = signal:observe(function(previous_state, current_state)
  -- call side effects
end)
 
local body = function()
  return n.rows(…)
end
 
renderer:add_mappings({
  {
    mode = { "n", "i" },
    from = "<leader>c",
    to = function()
      renderer:close()
    end,
  },
})
 
renderer:on_unmount(function()
  subscription:unsubscribe()
end)
 
renderer:render(body)

Properties

width

Refers to the number of characters that can be displayed horizontally by the renderer.

Type
number

height

Refers to the number of lines that can be displayed vertically by the renderer.

Type
number

position

Sets the window position.

Default value
50%
Type
string | number | table

For further information, please refer to the documentation of nui.nvim (opens in a new tab).

relative

Sets the window layout to "floating" (NuiComponents doesn't support split layouts at the moment).

Default value
editor
Type
string | table

For further information, please refer to the documentation of nui.nvim (opens in a new tab).

keymap

A table containing a default global key bindings for the renderer.

Default value
{
  close = "<Esc>",
  focus_next = "<Tab>",
  focus_prev = "<S-Tab>",
  focus_left = nil,
  focus_right = nil,
  focus_up = nil,
  focus_down = nil
}
Type
table

on_mount

Registers a callback that will be executed when the component tree is mounted for the first time.

Default value
fn.ignore
Type
function

on_unmount

Registers a callback function to be executed when the component tree is unmounted (i.e., the window is closed).

Default value
fn.ignore
Type
function

Methods

render

Renders the component tree.

:render(render_fn)
Parameters
render_fnfun(): Component

If the render method is called in the same function as the root component definition, Component can be passed instead of fun(): Component.

Examples:

local n = require("nui-components")
 
local renderer = n.create_renderer({
  width = 100,
  height = 24,
})
 
local body = function()
  return n.rows(…)
end
 
vim.keymap.set("n", "<leader>i", function()
  renderer:render(body)
end, { noremap = true, desc = "" })
local n = require("nui-components")
 
local renderer = n.create_renderer({
  width = 100,
  height = 24,
})
 
vim.keymap.set("n", "<leader>i", function()
  local body = n.rows(…)
  renderer:render(body)
end, { noremap = true, desc = "" })

schedule

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

:schedule(schedule_fn)
Parameters
schedule_fnfun(): nil

redraw

Forces an immediate redraw of the component tree.

:redraw()

focus

Sets the focus to the current renderer window.

:focus()

close

Closes the current renderer window.

:close()

add_mappings

Adds additional keyboard shortcuts for the component tree.

:add_mappings(mappings)
Parameters
mappingsMapping[]

Where Mapping is:

{
  mode = string[] | string, -- "n", "i", "v"
  key = string,
  handler = function
}

set_size

Changes the size of the renderer window.

:set_size(size)
Parameters
sizeSize

Where Size is:

{
  width = number,
  height = number
}

get_size

Returns the current size of the renderer window.

:get_size()
Returns
Size

get_component_by_id

Searches the component tree for a component with the specified id and returns it if found.

:get_component_by_id(id)
Parameters
idstring
Returns
Component | nil

get_component_by_direction

Searches the component tree for a focusable component in the specified direction, starting from the focused component, or the specified from component if provided.

:get_component_by_direction(direction, from)
Parameters
direction"left" | "right" | "up" | "down"
fromComponent | nil
Returns
Component | nil

get_origin_winid

Returns the original window id when the component tree was first rendered.

:get_origin_winid()
Returns
number

get_last_focused_component

Returns the last focused component within the component tree.

:get_last_focused_component()
Returns
Component

get_focusable_components

Returns a list of all focusable components within the tree.

:get_focusable_components()
Returns
Component[]

get_component_tree

Returns the entire internal tree used by the component

:get_component_tree()
Returns
Component[]

on_mount

Registers a callback function to be executed when the component tree is first mounted.

:on_mount(mount_fn)
Parameters
mount_fnfun(): nil

on_unmount

Registers a function to be called when the component tree is unmounted.

:on_unmount(unmount_fn)
Parameters
unmount_fnfun(): nil