bevy_bsml
bevy_bsml is a UI library built on top of bevy's official bevy_ui library to help easily create and reuse styled UI components.
bevy_bsml is very much inspired by svelte and tailwindcss. So, if you're a web dev, you may feel right at home, with components and inline styling.
To see the basic usages of bevy_bsml, check out examples:
What is BSML?
BSML stands for Bevy's Simple Markup Language, or BS Markup Language; feel free to pick the one you like more.
Why not HTML or XML?
Because the angle bracketed markup languages don't work well with rust macros, and (...)
and {...}
work naturally.
BSML Overview
1. Tag is inside parenthesis (MyComponent)
, followed by content nested in braces { ... }
.
bsml!
is same as
You can have multiple elements inside braces:
bsml!
is same as
hello world
nested text
2. There can be only one root element.
Valid cases:
bsml!
bsml!
bsml!
bsml!
Invalid:
bsml!
bsml!
As you can see, if element doesn't have any nested elements, braces are optional.
So, if you have a UI Component MenuItem
, which already has content layed out, you can skip braces when using the component.
;
bsml!
;
bsml!
3. Building Blocks
Currently, bsml has 3 base elements: node, text, and slot.
node is like div
in html; you can have nested elements in it, or just use one without nested elements just to style it.
bsml!
text is an element for displaying text. This element is different than others.
You must follow up the tag with braces, where its content is a string with optional arguments, exactly like arguments of format!(...)
.
;
bsml!
If it is a component which has fields, you can also use fields as arguments:
bsml!
limitations:
- you cannot have arguments in strings:
- valid:
(text) { "index: {}, name: {}", i, name }
- invalid:
(text) { "index: {i}, name: {name}" }
- valid:
- you can only have component fields as arguments, no expressions. This may change in the future.
- valid:
(text) { "index: {}, name: {}", i, name }
- invalid:
(text) { "index: {}, name: {}", "0".parse::<u8>().unwrap(), "hello world" }
- valid:
slot is an element used in components to let the users of the component fill in content.
Nested elements in braces of slot
element is the default content if not supplied by user.
;
bsml!
;
bsml!
4. Spawning a UI element
You can directly spawn a UI element without defining a component.
use ;
commands.spawn_bsml
You can also spawn the UI component:
use ;
bsml!;
commands.spawn_bsml;
5. Defining a Reusable Component
You can also define UI component that can be reused by other elements, by specifying component struct type followed by semicolon.
;
bsml!
bsml!
commands.spawn_bsml;
To Be Continued
I'll finish the rest of documentation when I have time. In the mean time, check the examples to see how things work!