Lumen library
Part of Lumen
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
ref of an element whose panel is removed.text and button elements do.Functions
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:121Applies 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
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
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
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
nilwhy the template did not compile
Returns
- Map the stored template
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
nilprops of the element;keytells elements of a list apart between renders,styleis its style,refreceives the panel - children List optional, defaults to
nilchildren: elements, strings, numbers or lists of these
Returns
- Map the element
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
Returns the definition of an intrinsic element.
Parameters
- name String tag name
Returns
- Map the definition, or nil if there is no such element
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
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
nillist to add the children to
Returns
- List the flattened children
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
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
Checks whether a value is an element.
Parameters
- value Any
Returns
- Boolean
Checks whether an element is a fragment.
Parameters
- element Map
Returns
- Boolean
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
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
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
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
nilbackground to draw instead of the one of the style
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
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
Clears the ref of an element whose panel is removed.
Parameters
- _panel Panel
- props Map the last props of the element
Mounts the component of a template into a panel.
Parameters
- id String template ID
- props Map optional, defaults to
nilprops 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
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
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
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
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
nilvalues the effect depends on; nil runs it after every render
Computes a value and keeps it until one of the dependencies changes.
Parameters
- compute Function returns the value
- deps List optional, defaults to
nilvalues the result depends on; nil computes it on every render
Returns
- Any the value
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
nilthe value ofcurrenton the first render
Returns
- Map the table, with the value in
current
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