Input

Rust UI component that displays an input field that allows the user to enter text.

input
  • Rust UI Icons - CopyCopy Demo

Input Demo

use leptos::prelude::*;

use crate::components::ui::input::Input;

// TODO Fix 🐛 Input type="number" can take "e" as a valid input

#[component]
pub fn DemoInput() -> impl IntoView {
    view! {
        <div class="space-y-4 w-full max-w-lg">
            <h2 class="text-2xl font-bold">Input Demo</h2>

            <Input placeholder="Default input" />
            <Input r#type="email" placeholder="Email input" />
            <Input r#type="password" placeholder="Password input" />
            <Input class="border-2 border-purple-500 focus:border-purple-700" placeholder="Custom styled input" />
            <Input r#type="number" placeholder="Number input" />
        </div>
    }
}

Demos

1. Input Copy

  • Rust UI Icons - CopyCopy Demo
use icons::{Check, Copy};
use leptos::prelude::*;

use crate::components::hooks::use_copy_clipboard::use_copy_clipboard;
use crate::components::ui::button::{Button, ButtonVariant};
use crate::components::ui::input::Input;

#[component]
pub fn DemoInputCopy() -> impl IntoView {
    let url = RwSignal::new("https://rust-ui.com/docs/components/input");
    let (copy_to_clipboard, copied) = use_copy_clipboard(None);

    let handle_copy = move |_| {
        copy_to_clipboard(url.get());
    };

    view! {
        <div class="flex gap-2">
            <Input value_str=Some(url.read_only()) readonly=true class="flex-1" />

            <Button variant=ButtonVariant::Outline on:click=handle_copy>
                {move || { if copied.get() { view! { <Check /> }.into_any() } else { view! { <Copy /> }.into_any() } }}
            </Button>
        </div>
    }
}

Installation

You can run either of the following commands:

# cargo install ui-cli --force
ui add demo_input
ui add input

Update the imports to match your project setup.

Copy and paste the following code into your project:

components/ui/input.rs

use leptos::prelude::*;
use leptos_ui::input;

mod components {
    use super::*;

    input! {Input,
        "file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input flex h-9 w-full min-w-0 rounded-md border bg-transparent px-3 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
        "focus-visible:border-ring focus-visible:ring-ring/50",
        "focus-visible:ring-2", // TODO. Port tw_merge to Tailwind V4.
     // "focus-visible:ring-[3px]", // TODO. Port tw_merge to Tailwind V4.
        "aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
        //
        // Added (TW V4)
        "read-only:bg-muted",
    }
}

pub use components::*;

Update the imports to match your project setup.

Usage

use crate::components::ui::input::Input;
<Input placeholder="Enter text..." />