1#![cfg_attr(not(feature = "std"), no_std)]
2#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3#![warn(missing_docs)]
4
5extern crate alloc;
28
29use alloc::borrow::Cow;
30
31use fun_html::Attribute;
32
33pub fn hx_boost(value: bool) -> Attribute {
35 Attribute::new("hx-boost", boolean(value))
36}
37
38pub fn hx_get(path: impl Into<Cow<'static, str>>) -> Attribute {
40 Attribute::new("hx-get", path)
41}
42
43pub fn hx_post(path: impl Into<Cow<'static, str>>) -> Attribute {
45 Attribute::new("hx-post", path)
46}
47
48pub fn hx_put(path: impl Into<Cow<'static, str>>) -> Attribute {
50 Attribute::new("hx-put", path)
51}
52
53pub fn hx_patch(path: impl Into<Cow<'static, str>>) -> Attribute {
55 Attribute::new("hx-patch", path)
56}
57
58pub fn hx_delete(path: impl Into<Cow<'static, str>>) -> Attribute {
60 Attribute::new("hx-delete", path)
61}
62
63pub fn hx_trigger(path: impl Into<Cow<'static, str>>) -> Attribute {
65 Attribute::new("hx-trigger", path)
66}
67
68pub fn hx_select(path: impl Into<Cow<'static, str>>) -> Attribute {
70 Attribute::new("hx-select", path)
71}
72
73pub fn hx_target(path: impl Into<Cow<'static, str>>) -> Attribute {
75 Attribute::new("hx-target", path)
76}
77
78pub fn hx_swap(path: impl Into<Cow<'static, str>>) -> Attribute {
80 Attribute::new("hx-swap", path)
81}
82
83pub fn hx_swap_inner_html() -> Attribute {
87 hx_swap("innerHTML")
88}
89
90pub fn hx_swap_outer_html() -> Attribute {
94 hx_swap("outerHTML")
95}
96
97pub fn hx_swap_text_content() -> Attribute {
101 hx_swap("textContent")
102}
103
104pub fn hx_swap_before_begin() -> Attribute {
108 hx_swap("beforebegin")
109}
110
111pub fn hx_swap_after_begin() -> Attribute {
115 hx_swap("afterbegin")
116}
117
118pub fn hx_swap_before_end() -> Attribute {
122 hx_swap("beforeend")
123}
124
125pub fn hx_swap_after_end() -> Attribute {
129 hx_swap("afterend")
130}
131
132pub fn hx_swap_delete() -> Attribute {
136 hx_swap("delete")
137}
138
139pub fn hx_swap_none() -> Attribute {
143 hx_swap("none")
144}
145
146pub fn hx_push_url(value: bool) -> Attribute {
148 hx_push_url_str(boolean(value))
149}
150
151pub fn hx_push_url_str(url: impl Into<Cow<'static, str>>) -> Attribute {
153 Attribute::new("hx-push-url", url)
154}
155
156pub fn hx_confirm(value: impl Into<Cow<'static, str>>) -> Attribute {
158 Attribute::new("hx-confirm", value)
159}
160
161pub fn hx_swap_oob() -> Attribute {
165 hx_swap_oob_swap("true")
166}
167
168pub fn hx_swap_oob_swap(value: impl Into<Cow<'static, str>>) -> Attribute {
170 Attribute::new("hx-swap-oob", value)
171}
172
173pub fn hx_on(event: &'static str, action: impl Into<Cow<'static, str>>) -> Attribute {
175 Attribute::new_unsafe_name(alloc::format!("hx-on:{event}"), action)
176}
177
178pub fn hx_on_htmx_before_request(action: impl Into<Cow<'static, str>>) -> Attribute {
180 Attribute::new("hx-on::before-request", action)
181}
182
183pub fn hx_on_htmx_after_request(action: impl Into<Cow<'static, str>>) -> Attribute {
185 Attribute::new("hx-on::after-request", action)
186}
187
188pub fn hx_vals(values: impl Into<Cow<'static, str>>) -> Attribute {
190 Attribute::new("hx-vals", values)
191}
192
193pub fn hx_disinherit(attributes: impl IntoIterator<Item = &'static str>) -> Attribute {
195 let mut iter = attributes.into_iter();
196 let mut string = iter
197 .next()
198 .map(alloc::string::String::from)
199 .unwrap_or_default();
200 for value in iter {
201 string.push(' ');
202 string.push_str(value);
203 }
204 Attribute::new("hx-disinherit", string)
205}
206
207pub fn hx_disinherit_all() -> Attribute {
209 Attribute::new("hx-disinherit", "*")
210}
211
212#[cfg(feature = "serde_json")]
220pub fn hx_vals_serde(values: &impl serde::Serialize) -> Attribute {
221 hx_vals(serde_json::to_string(values).expect("hx-vals values should not fail to serialize"))
222}
223
224fn boolean(value: bool) -> &'static str {
225 if value {
226 "true"
227 } else {
228 "false"
229 }
230}