CopyButton

A copy button that briefly shows the copied text above itself, so you see exactly what landed on the clipboard.

Preview

Loading GPUI

Props

Install

cargo add evilbuttons

Usage

use evilbuttons::CopyButton;
use gpui::{div, prelude::*, Context, IntoElement, Render, Window};

struct ButtonDemo;

impl Render for ButtonDemo {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div().child(
            CopyButton::new("install", "cargo add evilbuttons")
                .on_copy(|text, _, _| println!("Copied {text}")),
        )
    }
}

Variants

Custom labels

Name the action after what it copies.

Loading GPUI
CopyButton::new("clone", "git clone https://github.com/radiumcoders/Evil-Buttons-gpui.git")
    .copy_label("Copy clone URL")
    .copied_label("Clone URL copied")

Secret value

Turn off show_value when the text shouldn't be flashed on screen, like an API key.

Loading GPUI
CopyButton::new("api-key", "sk_live_51Hx9example")
    .copy_label("Copy API key")
    .copied_label("API key copied")
    .show_value(false)
    .timeout(Duration::from_millis(3000))

Props

CopyButton is a GPUI RenderOnce builder; its state is keyed by id.

MethodTypeDefaultDescription
new(id, value)impl Into<ElementId>, impl Into<SharedString>-Unique id and the text written to the clipboard.
timeout(d)Duration1500msHow long before the button returns to idle.
copy_label(s)impl Into<SharedString>"Copy"Label shown before copying.
copied_label(s)impl Into<SharedString>"Copied"Label shown after copying.
show_value(bool)booltrueShow the copied text in a small chip above the button after copying.
value_font(family)impl Into<SharedString>-Font family for the chip, e.g. your monospace font.
chip_colors(bg, fg)impl Into<Hsla>, impl Into<Hsla>#fafafa, #0a0a0aChip background and text colors.
on_copy(f)Fn(&SharedString, &mut Window, &mut App)-Fired with the copied text after writing it.
disabled(bool)boolfalseDims the button and ignores input.

Good to know

  • Uses GPUI's clipboard, which maps to navigator.clipboard in the browser.
  • Never changes width between states.

Search docs

Jump to a button documentation page