fun_htmx/
lib.rs

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
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(missing_docs)]

//! This crate provides a collection of HTMX attributes for [`fun-html`](https://github.com/jcornaz/fun-html/)
//!
//! # Example
//!
//! ```
//! use fun_html::{elt::{script_empty, button, text}, attr::src};
//! use fun_htmx::{hx_get, hx_swap_outer_html};
//!
//! let quick_start = [
//!    script_empty([src("https://unpkg.com/htmx.org@2.0.3")]),
//!    button(
//!      [hx_get("/clicked"), hx_swap_outer_html()],
//!      text("Click Me")
//!    ),
//! ];
//! ```

extern crate alloc;

use alloc::borrow::Cow;

use fun_html::Attribute;

/// [`hx-boost`](https://htmx.org/attributes/hx-boost/) attribute
pub fn hx_boost(value: bool) -> Attribute {
    Attribute::new("hx-boost", boolean(value))
}

/// [`hx-get`](https://htmx.org/attributes/hx-get/) attribute
pub fn hx_get(path: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-get", path)
}

/// [`hx-post`](https://htmx.org/attributes/hx-post/) attribute
pub fn hx_post(path: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-post", path)
}

/// [`hx-put`](https://htmx.org/attributes/hx-put/) attribute
pub fn hx_put(path: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-put", path)
}

/// [`hx-patch`](https://htmx.org/attributes/hx-patch/) attribute
pub fn hx_patch(path: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-patch", path)
}

/// [`hx-delete`](https://htmx.org/attributes/hx-delete/) attribute
pub fn hx_delete(path: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-delete", path)
}

/// [`hx-trigger`](https://htmx.org/attributes/hx-trigger/) attribute
pub fn hx_trigger(path: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-trigger", path)
}

/// [`hx-select`](https://htmx.org/attributes/hx-select/) attribute
pub fn hx_select(path: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-select", path)
}

/// [`hx-target`](https://htmx.org/attributes/hx-target/) attribute
pub fn hx_target(path: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-target", path)
}

/// [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap(path: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-swap", path)
}

/// `hx-swap="innerHTML"`
///
/// See [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap_inner_html() -> Attribute {
    hx_swap("innerHTML")
}

/// `hx-swap="outerHTML"`
///
/// See [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap_outer_html() -> Attribute {
    hx_swap("outerHTML")
}

/// `hx-swap="textContent"`
///
/// See [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap_text_content() -> Attribute {
    hx_swap("textContent")
}

/// `hx-swap="beforebegin"`
///
/// See [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap_before_begin() -> Attribute {
    hx_swap("beforebegin")
}

/// `hx-swap="afterbegin"`
///
/// See [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap_after_begin() -> Attribute {
    hx_swap("afterbegin")
}

/// `hx-swap="beforeend"`
///
/// See [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap_before_end() -> Attribute {
    hx_swap("beforeend")
}

/// `hx-swap="afterend"`
///
/// See [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap_after_end() -> Attribute {
    hx_swap("afterend")
}

/// `hx-swap="delete"`
///
/// See [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap_delete() -> Attribute {
    hx_swap("delete")
}

/// `hx-swap="none"`
///
/// See [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap_none() -> Attribute {
    hx_swap("none")
}

/// [`hx-push-url`](https://htmx.org/attributes/hx-push-url/) attribute using a boolean
pub fn hx_push_url(value: bool) -> Attribute {
    hx_push_url_str(boolean(value))
}

/// [`hx-push-url`](https://htmx.org/attributes/hx-push-url/) attribute using an URL
pub fn hx_push_url_str(url: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-push-url", url)
}

/// `hx-swap-oob="true"
///
/// See [`hx-swap`](https://htmx.org/attributes/hx-swap/) attribute
pub fn hx_swap_oob() -> Attribute {
    hx_swap_oob_swap("true")
}

/// [`hx-swap-oob`](https://htmx.org/attributes/hx-swap-oob/) attribute
pub fn hx_swap_oob_swap(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-swap-oob", value)
}

/// [`hx-on*`](https://htmx.org/attributes/hx-on/) attributes
pub fn hx_on(event: &'static str, action: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new_unsafe_name(alloc::format!("hx-on:{event}"), action)
}

/// [`hx-on:htmx:before-request`](https://htmx.org/attributes/hx-on/) attribute
pub fn hx_on_htmx_before_request(action: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-on::before-request", action)
}

/// [`hx-on:htmx:after-request`](https://htmx.org/attributes/hx-on/) attribute
pub fn hx_on_htmx_after_request(action: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("hx-on::after-request", action)
}

fn boolean(value: bool) -> &'static str {
    if value {
        "true"
    } else {
        "false"
    }
}