Lumen builds Derma interfaces from templates. A template is a .lumen file in the views/lumen folder of Flux, of the schema or of a plugin: Lua code with markup in it that returns a component, a function that takes a table of props and returns a tree of elements (see Lumen.Compiler for the markup). The server compiles the templates to Lua and sends them to the clients, where Lumen.component gives the component of a template by its ID, the file name without the extension, and Lumen.render mounts it into a panel:

-- views/cl_help.lua
function PANEL:Init()
  self.root = Lumen.render('help', { pages = Flux.Help:get_pages() }, self)
end

function PANEL:rebuild()
  self.root:update()
end

Elements are mapped to real vgui panels by the reconciler (Lumen.mount, Lumen.Reconciler): intrinsic elements such as view, text, button and scroll become Lumen's own panels, image, input, checkbox, combo, slider, avatar and model become the stock Derma controls, and the registry in Lumen.register_element takes more. Components keep state with Lumen.use_state and friends, and view lays its children out with flexbox (Lumen.Layout) from the style prop (Lumen.Style).

This file holds the registry of templates. Lumen.define stores a compiled template under its ID, on the server when the lumen pipeline reads the file and on the client when the generated files are loaded; Lumen.load compiles and runs a template from a string, which is handy in the console and in tests.

Summary

Functions

Returns every stored template.
Applies the props that every element takes to its panel: the style, the visibility, the tooltip, the ref, mouse input for press handlers, and the position and the size of a panel at the root of a tree, which is docked to fill its container unless its style sizes it.
Compiles a template to Lua code without raising errors.
Returns what the template with the given ID returns, usually its component.
Stores a compiled template under an ID.
Creates an element.
Turns compiled template code into the value the template returns: its component, usually.
Returns the definition of an intrinsic element.
Returns a stored template.
Flattens a list of children into the list that elements carry: nested lists are unrolled, elements, strings and numbers are kept, and nil and booleans become false.
Generates the Lua code that gives a template to the clients.
Reads a template file, compiles it and stores it under the given ID.
Checks whether a value is an element.
Checks whether an element is a fragment.
Compiles and runs a template given as a string, and returns its component.
Calls a function for every item of a list and collects what it returns, leaving out nil and false.
Mounts an element into a panel and returns the root of the tree.
Draws the background and the border that a resolved style asks for.
Merges tables of props into one, later tables overriding earlier ones.
Registers an intrinsic element.
Clears the ref of an element whose panel is removed.
Mounts the component of a template into a panel.
Returns what a template returns, for templates that use other templates.
Compares two lists shallowly, as the dependencies of effects are compared.
Joins the text children of an element into one string, as the text and button elements do.
Runs a function after the component has been mounted or updated.
Computes a value and keeps it until one of the dependencies changes.
Gives the component a table that stays the same between renders, to keep anything in without rendering again when it changes.
Gives the component a value that it can change.

Functions

Lumen.all_templates()

shared packages/lumen/lib/sh_lumen.lua:141

Returns every stored template.

Returns

  • Map templates keyed by ID

Lumen.apply_props(panel, props, old_props, def, is_root)

client packages/lumen/lib/cl_elements.lua:121

Applies the props that every element takes to its panel: the style, the visibility, the tooltip, the ref, mouse input for press handlers, and the position and the size of a panel at the root of a tree, which is docked to fill its container unless its style sizes it.

Parameters

  • panel Panel
  • props Map
  • old_props Map the props of the last update, nil on the first
  • def Map the definition of the element
  • is_root Boolean whether the panel is at the top of its tree, outside of any Lumen container

Lumen.compile(source, name)

shared packages/lumen/lib/sh_lumen.lua:55

Compiles a template to Lua code without raising errors.

Parameters

  • source String template source
  • name String optional, defaults to 'lumen' name used in error messages

Returns

  • String Lua code, or nil if the template is malformed
  • String the error message in that case

Lumen.component(id)

shared packages/lumen/lib/sh_lumen.lua:150

Returns what the template with the given ID returns, usually its component. The template is evaluated the first time it is asked for and the result is kept. A template that did not compile or that fails to run is reported in the console once and gives nil.

Parameters

  • id String template ID: the file name without the extension

Returns

  • Function/Map the component, or nil if there is no such template or it is broken

Lumen.define(id, path, code, err)

shared packages/lumen/lib/sh_lumen.lua:119

Stores a compiled template under an ID. Replaces the template that has the same ID and forgets its component, so that the next Lumen.component call evaluates the new code.

Parameters

  • id String template ID
  • path String path of the template file, used in error messages
  • code String compiled Lua code, nil if the template did not compile
  • err String optional, defaults to nil why the template did not compile

Returns

  • Map the stored template

Lumen.element(kind, props, children)

client packages/lumen/lib/cl_element.lua:56

Creates an element. Children given here are flattened and stored as props.children.

Parameters

  • kind String/Function/Map the type of the element: the name of an intrinsic element or of a template, a component function or Lumen.Fragment
  • props Map optional, defaults to nil props of the element; key tells elements of a list apart between renders, style is its style, ref receives the panel
  • children List optional, defaults to nil children: elements, strings, numbers or lists of these

Returns

  • Map the element

Lumen.evaluate(code, name)

shared packages/lumen/lib/sh_lumen.lua:71

Turns compiled template code into the value the template returns: its component, usually. The code is run with the hooks use_state, use_effect, use_ref and use_memo in scope.

Parameters

  • code String Lua code, as returned by Lumen.compile
  • name String optional, defaults to 'lumen' name used in error messages

Returns

  • Any what the template returns, or nil if it does not compile or fails to run
  • String the error message in that case

Lumen.find_element(name)

client packages/lumen/lib/cl_elements.lua:72

Returns the definition of an intrinsic element.

Parameters

  • name String tag name

Returns

  • Map the definition, or nil if there is no such element

Lumen.find_template(id)

shared packages/lumen/lib/sh_lumen.lua:135

Returns a stored template.

Parameters

  • id String template ID

Returns

  • Map the template: id, path, code and error, or nil if there is no such template

Lumen.flatten(children, into)

client packages/lumen/lib/cl_element.lua:25

Flattens a list of children into the list that elements carry: nested lists are unrolled, elements, strings and numbers are kept, and nil and booleans become false. The false entries keep the children after a {condition and <element/>} at the same positions whether or not the condition holds, so that they are matched between renders without keys.

Parameters

  • children List children as given in markup or code
  • into List optional, defaults to nil list to add the children to

Returns

  • List the flattened children

Lumen.generate_client_code(template)

server packages/lumen/lib/sh_lumen.lua:299

Generates the Lua code that gives a template to the clients. The clients include the generated files before Lumen itself loads, so the code only fills Flux.lumen_sources, which Lumen reads once it is there.

Parameters

  • template Map a stored template

Returns

  • String Lua code

Lumen.include_file(id, file_name)

server packages/lumen/lib/sh_lumen.lua:254

Reads a template file, compiles it and stores it under the given ID. Templates whose compilation fails are stored with their error, so that the clients can report it.

Parameters

  • id String template ID
  • file_name String path of the file relative to the Lua search path, such as 'flux/packages/active_ui/views/lumen/help.lumen'

Returns

  • Map the stored template, or nil if the file could not be read

Lumen.is_element(value)

client packages/lumen/lib/cl_element.lua:76

Checks whether a value is an element.

Parameters

  • value Any

Returns

  • Boolean

Lumen.is_fragment(element)

client packages/lumen/lib/cl_element.lua:83

Checks whether an element is a fragment.

Parameters

  • element Map

Returns

  • Boolean

Lumen.load(source, name)

shared packages/lumen/lib/sh_lumen.lua:104

Compiles and runs a template given as a string, and returns its component. Templates in files go through Lumen.component instead; this is for the console and for tests.

local Greeting = Lumen.load([[
  return function(props)
    return <text>Hello, {props.name}!</text>
  end
]])

Lumen.mount(Lumen.element(Greeting, { name = 'John' }), parent)

Parameters

  • source String template source
  • name String optional, defaults to 'lumen' name used in error messages

Returns

  • Any what the template returns, or nil if it is malformed or fails to run
  • String the error message in that case

Lumen.map(list, callback)

shared packages/lumen/lib/sh_lumen.lua:231

Calls a function for every item of a list and collects what it returns, leaving out nil and false. This is the way to render a list of elements inside of markup.

<view>
  {Lumen.map(props.items, function(item, index)
    return <text key={item.id}>{index..'. '..item.name}</text>
  end)}
</view>

Parameters

  • list List items to go through; nothing happens if it is not a table
  • callback Function receives the item and its index, returns what to collect

Returns

  • List what the callback returned, in order

Lumen.mount(element, container)

client packages/lumen/lib/cl_reconciler.lua:552

Mounts an element into a panel and returns the root of the tree.

local Greeting = Lumen.load([[
  return function(props) return <text>Hello, {props.name}!</text> end
]])

self.root = Lumen.mount(Lumen.element(Greeting, { name = 'John' }), self)

Parameters

  • element Map the element at the top of the tree
  • container Panel the panel to mount into

Returns

  • LumenRoot the root, or nil if the container is not valid

Lumen.paint_box(panel, w, h, background)

client packages/lumen/views/cl_view.lua:52

Draws the background and the border that a resolved style asks for.

Parameters

  • panel Panel a panel with a lumen_style
  • w Number panel width
  • h Number panel height
  • background Color optional, defaults to nil background to draw instead of the one of the style

Lumen.props(...)

shared packages/lumen/lib/sh_lumen.lua:203

Merges tables of props into one, later tables overriding earlier ones. The compiler calls it for elements with {...spread} attributes; nil arguments are skipped.

Parameters

  • ... Vararg Map tables of props

Returns

  • Map

Lumen.register_element(name, def)

client packages/lumen/lib/cl_elements.lua:62

Registers an intrinsic element.

Lumen.register_element('progress', {
  class = 'DProgress',
  style = { height = 16 },
  update = function(panel, props, old_props)
    panel:SetFraction(props.value or 0)
  end
})

Parameters

  • name String tag name, lower case
  • def Map the definition: class (String vgui class to create), style (Map default style), children (Boolean whether the element lays out child elements), container (Function(panel) returns the panel that children are created in, the panel itself if missing), mount (Function(panel, props) called once after the panel has been created, the place to wire callbacks), update (Function(panel, props, old_props) called after mounting and on every update, old_props being nil the first time), mouse (Boolean the panel takes mouse input even without press handlers)

Returns

  • Map the definition

Lumen.release_ref(_panel, props)

client packages/lumen/lib/cl_elements.lua:106

Clears the ref of an element whose panel is removed.

Parameters

  • _panel Panel
  • props Map the last props of the element

Lumen.render(id, props, container)

client packages/lumen/lib/cl_reconciler.lua:572

Mounts the component of a template into a panel.

Parameters

  • id String template ID
  • props Map optional, defaults to nil props for the component
  • container Panel the panel to mount into

Returns

  • LumenRoot the root, or nil if there is no such template or the container is not valid

Lumen.require(id)

shared packages/lumen/lib/sh_lumen.lua:195

Returns what a template returns, for templates that use other templates. The same as Lumen.component.

local Card = Lumen.require('card')

return function(props)
  return <Card title={props.title} />
end

Parameters

  • id String template ID

Returns

  • Function/Map the component, or nil if there is no such template or it is broken

Lumen.shallow_equal(a, b)

client packages/lumen/lib/cl_element.lua:118

Compares two lists shallowly, as the dependencies of effects are compared.

Parameters

  • a List
  • b List

Returns

  • Boolean true if both have the same values at the same positions

Lumen.text_of(props)

client packages/lumen/lib/cl_element.lua:92

Joins the text children of an element into one string, as the text and button elements do. Numbers are converted, elements and other values are skipped. The text prop, if the element has one, takes the place of the children.

Parameters

  • props Map props of the element

Returns

  • String

Lumen.use_effect(effect, deps)

client packages/lumen/lib/cl_state.lua:122

Runs a function after the component has been mounted or updated. With dependencies the function only runs when one of them has changed since the last render; with an empty list it runs once, after the first render. The function may return a function that cleans up, which runs before the next run and when the component is unmounted.

use_effect(function()
  local timer_name = 'lumen_clock_'..tostring(ref.current)

  timer.Create(timer_name, 1, 0, function() set_time(os.time()) end)

  return function()
    timer.Remove(timer_name)
  end
end, {})

Parameters

  • effect Function the effect, may return a cleanup function
  • deps List optional, defaults to nil values the effect depends on; nil runs it after every render

Lumen.use_memo(compute, deps)

client packages/lumen/lib/cl_state.lua:154

Computes a value and keeps it until one of the dependencies changes.

Parameters

  • compute Function returns the value
  • deps List optional, defaults to nil values the result depends on; nil computes it on every render

Returns

  • Any the value

Lumen.use_ref(initial)

client packages/lumen/lib/cl_state.lua:140

Gives the component a table that stays the same between renders, to keep anything in without rendering again when it changes. Passed as the ref prop of an element, it receives the panel of the element in current.

Parameters

  • initial Any optional, defaults to nil the value of current on the first render

Returns

  • Map the table, with the value in current

Lumen.use_state(initial)

client packages/lumen/lib/cl_state.lua:78

Gives the component a value that it can change. Changing it renders the component again on the next frame, unless the new value is the same as the old one.

Parameters

  • initial Any the value on the first render; a function is called for it

Returns

  • Any the current value
  • Function sets the value: give it the new value, or a function that receives the current value and returns the new one

Defined in