← All Workshops

MudEngine Part 5: Polished UI with dioxus-components

Step 11 / 13

Style the command input

The command input currently uses a plain <input> with a CSS class. Let us swap it for the Input component from the library.

mud-engine/src/components/app.rs
use crate::components::input::Input;

// inside App's rsx!, replace the existing <input>:

Input {
    style: "width: 100%;",
    placeholder: "Type a command and press Enter...",
    value: input,
    oninput: move |e: FormEvent| input.set(e.value()),
    onkeydown: move |e: KeyboardEvent| {
        if e.key() == Key::Enter {
            let cmd = input.read().trim().to_lowercase();
            if cmd.is_empty() { return; }
            input.write().clear();
            match cmd.as_str() {
                "look" | "l" => feedback.write().clear(),
                "north" | "n" | "south" | "s"
                | "east" | "e" | "west" | "w" => {
                    let dir = match cmd.as_str() {
                        "north" | "n" => Direction::North,
                        "south" | "s" => Direction::South,
                        "east" | "e" => Direction::East,
                        "west" | "w" => Direction::West,
                        _ => unreachable!(),
                    };
                    if world.write().go(dir) {
                        feedback.write().clear();
                    } else {
                        feedback.set(
                            format!("You cannot go {dir} from here."),
                        );
                    }
                }
                "help" | "?" => {
                    feedback.set(
                        "Commands: look/l | north/n south/s east/e west/w | help/? | quit/exit"
                            .into(),
                    );
                }
                "quit" | "exit" => {
                    feedback.set("Farewell, adventurer!".into());
                }
                _ => {
                    feedback.set(
                        "Unknown command. Type 'help' or '?' for a list."
                            .into(),
                    );
                }
            }
        }
    },
}
💡 Input props

The Input component accepts all the standard HTML input attributes — value, placeholder, r#type, disabled, etc. — through its extended attributes system. The oninput and onkeydown props are typed event handlers that work exactly like the native Dioxus events.

Step 11 / 13