Skip to main content

logicaffeine_web/ui/components/
guide_snippet.rs

1//! LOGOS quoted in the Syntax Guide's prose.
2//!
3//! A snippet is read-only — the prose quotes it as written — but it is still LOGOS, so it is read
4//! the way the guide's examples are: the shared editor with the language server on, its words
5//! coloured and explained by the same hover. A squiggle there would mean the guide quotes broken
6//! code, which `tests/guide_language_server.rs` forbids.
7
8use dioxus::prelude::*;
9
10use super::code_editor::Language;
11use super::inline_editor::{InlineEditor, InlineEditorStyle};
12
13#[component]
14pub fn GuideSnippet(code: String, language: Language) -> Element {
15    let mut copied = use_signal(|| false);
16    let copy = {
17        let code = code.clone();
18        move |_| {
19            #[cfg(target_arch = "wasm32")]
20            if let Some(window) = web_sys::window() {
21                let _ = window.navigator().clipboard().write_text(&code);
22                copied.set(true);
23                spawn(async move {
24                    gloo_timers::future::TimeoutFuture::new(1200).await;
25                    copied.set(false);
26                });
27            }
28            #[cfg(not(target_arch = "wasm32"))]
29            {
30                let _ = &code;
31                copied.set(true);
32            }
33        }
34    };
35
36    rsx! {
37        InlineEditorStyle {}
38        div { class: "guide-snippet",
39            button { class: "guide-code-copy", onclick: copy, if copied() { "Copied!" } else { "Copy" } }
40            InlineEditor { value: code, on_change: move |_| {}, language, readonly: true }
41        }
42    }
43}