bevy_react_core 0.7.0

The core bridge of bevy-react (drive bevy_ui from React over an embedded V8 runtime). Apps depend on the `bevy-react` crate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! The JSX typing, generated from the element registry: the core elements
//! ([`CORE_ELEMENTS`](crate::elements::CORE_ELEMENTS)) into the `bevy-react`
//! package's committed `js/src/generated/elements.ts` (by the
//! `core_element_ts_is_current` test), an app's own elements (a feature
//! crate's included) into its `bevy.ts` as a `declare module "bevy-react"`
//! augmentation of `BevyIntrinsicElements`
//! ([`render_app_element_augmentation`]).
//!
//! Each element gets one props interface (`Bevy` + PascalCase kind +
//! `Props`) composed of the hand-written group interfaces of its
//! [`Common`] groups (the package's `jsx.d.ts`), `style` for a styled
//! element, its typed attributes (the codec's TS type; required ones without
//! `?`), its events (`on<Name>` taking the payload), its `ref` handle type,
//! and `children`.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write as _;

use super::{Common, Element, handler_prop};
use crate::ext::TextRole;
use crate::ts_codegen::{json_key, type_idents};

/// The props interface name of an element kind: `Bevy` + PascalCase(kind) +
/// `Props` (`editableText` → `BevyEditableTextProps`).
pub(crate) fn props_interface(kind: &str) -> String {
    let mut chars = kind.chars();
    let pascal = match chars.next() {
        Some(first) => format!("{}{}", first.to_uppercase(), chars.as_str()),
        None => String::new(),
    };
    format!("Bevy{pascal}Props")
}

/// Whether `element` is a JSX intrinsic. A text span is not: it is the
/// nested form of its block (`<text>` inside `<text>`), a wire kind the
/// reconciler picks itself.
pub(crate) fn is_intrinsic(element: &Element) -> bool {
    element.flags.text != TextRole::Span
}

/// Whether `element` takes a `style`: every element with a box, plus a text
/// span (its glyph style).
fn styled(element: &Element) -> bool {
    !element.flags.node_less || element.flags.text != TextRole::None
}

/// The hand-written group interfaces (`jsx.d.ts`) `element`'s props extend,
/// one per [`Common`] group.
fn groups(element: &Element) -> Vec<&'static str> {
    let common = element.common;
    let mut out = vec![if common.contains(Common::IDENTITY) {
        "BevyAttributes"
    } else {
        "BevyKeyProps"
    }];
    for (group, name) in [
        (Common::VARIANTS, "BevyVariantProps"),
        (Common::POINTER, "BevyPointerProps"),
        (Common::SCROLL, "BevyScrollProps"),
        (Common::WHEEL, "BevyWheelProps"),
    ] {
        if common.contains(group) {
            out.push(name);
        }
    }
    out
}

/// The named types every props interface may reference beyond its own
/// fields: `BevyStyle`, the group interfaces, and React's `ReactNode`/`Ref`.
const REACT_TYPES: &[&str] = &["ReactNode", "Ref"];

/// Render `element`'s props interface; the ts-rs declarations its attribute
/// and payload types need land in `decls`, the named types it references in
/// `refs`.
pub(crate) fn render_props(
    element: &Element,
    decls: &mut BTreeMap<String, String>,
    refs: &mut BTreeSet<String>,
) -> String {
    let groups = groups(element);
    refs.extend(groups.iter().map(|g| (*g).to_owned()));
    let mut out = format!(
        "export interface {} extends {} {{\n",
        props_interface(element.name),
        groups.join(", ")
    );
    let mut field = |out: &mut String, key: &str, optional: bool, ts: &str| {
        refs.extend(type_idents(ts));
        let q = if optional { "?" } else { "" };
        writeln!(out, "  {}{q}: {ts};", json_key(key)).unwrap();
    };
    if styled(element) {
        field(&mut out, "style", true, "BevyStyle");
    }
    for attr in element.attrs.iter().filter(|a| a.is_typed()) {
        attr.ts_decls(decls);
        let required = element
            .required
            .iter()
            .any(|r| super::attribute::same_attr(*r, *attr));
        field(&mut out, attr.name(), !required, &attr.ts_type());
    }
    for event in element.events {
        let prop = handler_prop(event.name());
        let ts = if event.is_unit() {
            "() => void".to_owned()
        } else {
            event.ts_decls(decls);
            format!("(payload: {}) => void", event.ts_payload())
        };
        field(&mut out, &prop, true, &ts);
    }
    if let Some(handle) = element.ts_ref {
        field(&mut out, "ref", true, &format!("Ref<{handle}>"));
    }
    field(&mut out, "children", true, "ReactNode");
    out.push_str("}\n");
    out
}

/// The `BevyIntrinsicElements` rows of `elements` (the JSX intrinsics among
/// them, sorted by name), one per line at `indent`.
fn intrinsic_rows(elements: &[&Element], indent: &str) -> String {
    let mut rows: Vec<(&str, String)> = elements
        .iter()
        .filter(|e| is_intrinsic(e))
        .map(|e| (e.name, props_interface(e.name)))
        .collect();
    rows.sort();
    let mut out = String::new();
    for (name, iface) in rows {
        writeln!(out, "{indent}{}: {iface};", json_key(name)).unwrap();
    }
    out
}

/// Every props interface of `elements` (sorted by kind), with the
/// declarations and references they need.
fn render_all_props(
    elements: &[&Element],
    decls: &mut BTreeMap<String, String>,
    refs: &mut BTreeSet<String>,
) -> String {
    let mut sorted: Vec<&Element> = elements
        .iter()
        .copied()
        .filter(|e| is_intrinsic(e))
        .collect();
    sorted.sort_by_key(|e| e.name);
    let mut out = String::new();
    for element in sorted {
        out.push('\n');
        out.push_str(&render_props(element, decls, refs));
    }
    out
}

/// The named types `refs` holds that `decls` does not declare, split into
/// React's own and the rest.
fn imports(
    refs: &BTreeSet<String>,
    decls: &BTreeMap<String, String>,
) -> (Vec<String>, Vec<String>) {
    let (react, rest): (Vec<String>, Vec<String>) = refs
        .iter()
        .filter(|r| !decls.contains_key(*r))
        .cloned()
        .partition(|r| REACT_TYPES.contains(&r.as_str()));
    (react, rest)
}

/// What an app's generated `bevy.ts` needs for its own elements (a feature
/// crate's included): the props interfaces and the augmentation block, plus
/// the type imports they need (React's, and the `bevy-react` package's named
/// types). Empty when the app registered no element beyond the core's.
pub(crate) struct AppElementTs {
    /// `import type { … } from "react";` / `"bevy-react"` lines.
    pub(crate) imports: String,
    /// The props interfaces and the `declare module "bevy-react"` block.
    pub(crate) body: String,
}

/// Render an app's own elements for its `bevy.ts`; value/payload types'
/// declarations land in `decls` (the exporter declares them with the rest).
pub(crate) fn render_app_element_augmentation(
    elements: &[&Element],
    decls: &mut BTreeMap<String, String>,
) -> AppElementTs {
    if !elements.iter().any(|e| is_intrinsic(e)) {
        return AppElementTs {
            imports: String::new(),
            body: String::new(),
        };
    }
    let mut refs = BTreeSet::new();
    let props = render_all_props(elements, decls, &mut refs);
    let (react, package) = imports(&refs, decls);
    let mut imports = String::new();
    if !react.is_empty() {
        writeln!(
            imports,
            "import type {{ {} }} from \"react\";",
            react.join(", ")
        )
        .unwrap();
    }
    if !package.is_empty() {
        writeln!(
            imports,
            "import type {{ {} }} from \"bevy-react\";",
            package.join(", ")
        )
        .unwrap();
    }
    let mut body = String::from("/** The app's own elements' props (`app.add_react_element`). */");
    body.push_str(&props);
    body.push_str(
        "\n/** The app's own elements (`app.add_react_element`). Augments the\n\
         \x20*  `BevyIntrinsicElements` interface in the `bevy-react` package, so JSX\n\
         \x20*  types them. */\n\
         declare module \"bevy-react\" {\n\
         \x20 interface BevyIntrinsicElements {\n",
    );
    body.push_str(&intrinsic_rows(elements, "    "));
    body.push_str("  }\n}\n\n");
    AppElementTs { imports, body }
}

/// The core generator: it runs from its test (`npm run elements:generate`
/// rewrites the committed file), never at runtime.
#[cfg(test)]
mod tests {
    use super::*;

    /// The package module (relative to `js/src/generated/`) each named type
    /// the core elements reference is imported from; every other named type
    /// lives in `jsx.d.ts`.
    const TYPE_MODULES: &[(&str, &str)] = &[
        ("Animatable", "../animated"),
        ("BevyCanvasElement", "../canvas"),
        ("CanvasPainter", "../canvas"),
        ("DrawCmd", "../canvas"),
        ("BevyStyle", "./style"),
        ("ReactNode", "react"),
        ("Ref", "react"),
    ];
    const DEFAULT_TYPE_MODULE: &str = "../jsx";

    /// The committed core element typing, relative to the workspace root.
    const CORE_ELEMENT_TS_PATH: &str = "js/src/generated/elements.ts";

    /// The `bevy-react` package's element typing: one props interface per core
    /// element and `BevyIntrinsicElements`, with the imports they need.
    fn render_core_element_ts(elements: &[&Element]) -> String {
        let mut decls = BTreeMap::new();
        let mut refs = BTreeSet::new();
        let props = render_all_props(elements, &mut decls, &mut refs);
        let mut by_module: BTreeMap<&str, BTreeSet<&str>> = BTreeMap::new();
        for name in refs.iter().filter(|r| !decls.contains_key(*r)) {
            let module = TYPE_MODULES
                .iter()
                .find(|(n, _)| n == name)
                .map_or(DEFAULT_TYPE_MODULE, |(_, m)| m);
            by_module.entry(module).or_default().insert(name);
        }
        let mut out = String::from(
            "// @generated by bevy-react from the core element registry (`CORE_ELEMENTS`) —\n\
             // do not edit by hand. Regenerate with `npm run elements:generate`.\n\n",
        );
        for (module, names) in &by_module {
            let names: Vec<&str> = names.iter().copied().collect();
            writeln!(
                out,
                "import type {{ {} }} from {module:?};",
                names.join(", ")
            )
            .unwrap();
        }
        for decl in decls.values() {
            write!(out, "\nexport {decl}\n").unwrap();
        }
        out.push_str(&props);
        out.push_str(
            "\n/** Every host element JSX knows, name → props. An app's own elements\n\
             \x20*  (`app.add_react_element`, a feature crate's included) augment this\n\
             \x20*  interface from its generated `bevy.ts`. */\n\
             export interface BevyIntrinsicElements {\n",
        );
        out.push_str(&intrinsic_rows(elements, "  "));
        out.push_str("}\n");
        out
    }

    /// The committed core element typing matches the registry. Regenerate
    /// with `UPDATE_ELEMENT_TS=1` (`npm run elements:generate`).
    #[test]
    fn core_element_ts_is_current() {
        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../..")
            .join(CORE_ELEMENT_TS_PATH);
        let rendered = render_core_element_ts(crate::elements::CORE_ELEMENTS);
        if std::env::var_os("UPDATE_ELEMENT_TS").is_some() {
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(&path, &rendered).unwrap();
            return;
        }
        let committed = std::fs::read_to_string(&path).unwrap_or_default();
        assert!(
            committed == rendered,
            "{CORE_ELEMENT_TS_PATH} is stale — regenerate with `npm run elements:generate`"
        );
    }

    #[test]
    fn props_interface_names() {
        assert_eq!(props_interface("node"), "BevyNodeProps");
        assert_eq!(props_interface("editableText"), "BevyEditableTextProps");
        assert_eq!(props_interface("svg"), "BevySvgProps");
    }

    mod synthetic {
        use crate::element::{Attribute, Common, Element, ElementEvent};
        use crate::ext::ElementFlags;
        use crate::style::Codec;

        #[derive(Debug, Clone, serde::Serialize, ts_rs::TS)]
        pub struct DialPayload {
            pub turns: f32,
        }

        pub static LEVEL: Attribute<f32> = Attribute::new("level");
        pub static LABEL: Attribute<String> =
            Attribute::with_codec("label", Codec::serde_as("Animatable<Color>"));
        pub static WIRE: Attribute<f32> = Attribute {
            typed: false,
            ..Attribute::new("wire")
        };
        pub static TURN: ElementEvent<DialPayload> = ElementEvent::new("turn");
        pub static PING: ElementEvent<()> = ElementEvent::new("ping");

        pub static DIAL: Element = Element {
            attrs: &[&LEVEL, &LABEL, &WIRE],
            required: &[&LEVEL],
            common: Common::IDENTITY.with(Common::POINTER),
            events: &[&TURN, &PING],
            ..Element::new("dial")
        };

        pub static DOT: Element = Element {
            flags: ElementFlags::NODE_LESS,
            common: Common::POINTER,
            ..Element::new("dot")
        };
    }

    /// An app's own elements render into `bevy.ts`: props interfaces (groups,
    /// required/optional attributes, untyped ones left out, events with their
    /// payloads, no `style` on a node-less element), the
    /// `BevyIntrinsicElements` augmentation, and the imports they need.
    #[test]
    fn app_elements_augment_intrinsics() {
        let mut decls = BTreeMap::new();
        let ts = render_app_element_augmentation(&[&synthetic::DIAL, &synthetic::DOT], &mut decls);
        let body = &ts.body;
        assert!(
            body.contains(
                "export interface BevyDialProps extends BevyAttributes, BevyPointerProps {\n\
                 \x20 style?: BevyStyle;\n\
                 \x20 level: number;\n\
                 \x20 label?: Animatable<Color>;\n\
                 \x20 onTurn?: (payload: DialPayload) => void;\n\
                 \x20 onPing?: () => void;\n\
                 \x20 children?: ReactNode;\n}"
            ),
            "{body}"
        );
        assert!(!body.contains("wire"), "{body}");
        assert!(
            body.contains(
                "export interface BevyDotProps extends BevyKeyProps, BevyPointerProps {\n\
                 \x20 children?: ReactNode;\n}"
            ),
            "{body}"
        );
        assert!(
            body.contains(
                "declare module \"bevy-react\" {\n  interface BevyIntrinsicElements {\n    \
                 dial: BevyDialProps;\n    dot: BevyDotProps;\n  }\n}"
            ),
            "{body}"
        );
        assert!(decls.contains_key("DialPayload"), "{decls:?}");
        assert!(
            ts.imports
                .contains("import type { ReactNode } from \"react\";"),
            "{}",
            ts.imports
        );
        assert!(
            ts.imports.contains(
                "import type { Animatable, BevyAttributes, BevyKeyProps, BevyPointerProps, \
                 BevyStyle, Color } from \"bevy-react\";"
            ),
            "{}",
            ts.imports
        );
    }

    #[test]
    fn no_app_elements_render_nothing() {
        let mut decls = BTreeMap::new();
        let ts = render_app_element_augmentation(&[], &mut decls);
        assert!(ts.body.is_empty() && ts.imports.is_empty());
    }
}