Skip to content

Instantly share code, notes, and snippets.

@corlaez
Last active February 20, 2024 18:04
Show Gist options
  • Save corlaez/79b9a68a3f6c0a8f94058650f07c2af7 to your computer and use it in GitHub Desktop.
Save corlaez/79b9a68a3f6c0a8f94058650f07c2af7 to your computer and use it in GitHub Desktop.
html dsl htmx extensions
import kotlinx.html.HTMLTag
// Core Attributes
fun HTMLTag.hxGet(value: String) { attributes["hx-get"] = value }
fun HTMLTag.hxPost(value: String) { attributes["hx-post"] = value }
fun HTMLTag.hxOn(eventIncludingColons: String, value: String) { attributes["hx-on$eventIncludingColons"] = value }
fun HTMLTag.hxPushUrl(value: String) { attributes["hx-push-url"] = value }
fun HTMLTag.hxSelect(value: String) { attributes["hx-select"] = value }
fun HTMLTag.hxSelectOob(value: String) { attributes["hx-select-oob"] = value }
fun HTMLTag.hxSwapInnerHtml(modifiers: String = "") { attributes["hx-swap"] = "innerHTML${modifiers}" }
fun HTMLTag.hxSwapInnerHtml2(modifiers: String = "") { attributes["hx-swap"] = "innerHTML${modifiers}" }
fun HTMLTag.hxSwapOuterHtml(modifiers: String = "") { attributes["hx-swap"] = "outerHTML${modifiers}" }
fun HTMLTag.hxSwapBeforeBegin(modifiers: String = "") { attributes["hx-swap"] = "beforebegin${modifiers}" }
fun HTMLTag.hxSwapAfterBegin(modifiers: String = "") { attributes["hx-swap"] = "afterbegin${modifiers}" }
fun HTMLTag.hxSwapBeforeEnd(modifiers: String = "") { attributes["hx-swap"] = "beforeend${modifiers}" }
fun HTMLTag.hxSwapAfterEnd(modifiers: String = "") { attributes["hx-swap"] = "afterend${modifiers}" }
fun HTMLTag.hxSwapDelete(modifiers: String = "") { attributes["hx-swap"] = "delete${modifiers}" }
fun HTMLTag.hxSwapNone(modifiers: String = "") { attributes["hx-swap"] = "none${modifiers}" }
fun HTMLTag.hxSwapOob(value: String) { attributes["hx-swap-oob"] = value }
fun HTMLTag.hxTarget(value: String) { attributes["hx-target"] = value }
fun HTMLTag.hxVals(value: String) { attributes["hx-vals"] = value }
fun HTMLTag.hxTrigger(s: String) { attributes["hx-trigger"] = s }
// Additional Attributes
fun HTMLTag.hxBoost() { attributes["hx-boost"] = "hx-boost" }
fun HTMLTag.hxConfirm(value: String) { attributes["hx-confirm"] = value }
fun HTMLTag.hxDelete(value: String) { attributes["hx-delete"] = value }
fun HTMLTag.hxDisable() { attributes["hx-disable"] = "hx-disable" }
fun HTMLTag.hxDisabledElt(value: String) { attributes["hx-disabled-elt"] = value }
fun HTMLTag.hxDisinherit(value: String) { attributes["hx-disinherit"] = value }
fun HTMLTag.hxEncoding(value: String) { attributes["hx-encoding"] = value }
fun HTMLTag.hxExt(value: String) { attributes["hx-ext"] = value }
fun HTMLTag.hxHeaders(value: String) { attributes["hx-headers"] = value }
fun HTMLTag.hxHistory(historyEnabled: Boolean) { attributes["hx-history"] = "$historyEnabled" }
fun HTMLTag.hxHistoryElt() { attributes["hx-history-elt"] = "hx-history-elt" }
fun HTMLTag.hxInclude(value: String) { attributes["hx-include"] = value }
fun HTMLTag.hxIndicator(value: String) { attributes["hx-indicator"] = value }
fun HTMLTag.hxParams(value: String) { attributes["hx-params"] = value }
fun HTMLTag.hxPatch(value: String) { attributes["hx-patch"] = value }
fun HTMLTag.hxPreserve() { attributes["hx-preserve"] = "hx-preserve" }
fun HTMLTag.hxPrompt(value: String) { attributes["hx-prompt"] = value }
fun HTMLTag.hxPut(value: String) { attributes["hx-put"] = value }
fun HTMLTag.hxReplaceUrl(value: String) { attributes["hx-replace-url"] = value }
fun HTMLTag.hxReplaceUrl(flag: Boolean = true) { attributes["hx-replace-url"] = "$flag" }
fun HTMLTag.hxRequest(value: String) { attributes["hx-request"] = value }
// hx-sse has been moved to an extension
fun HTMLTag.hxSync(value: String) { attributes["hx-sync"] = value }
fun HTMLTag.hxValidate() { attributes["hx-validate"] = "true" }// forces validation of this element outside forms
// hx-vars is deprecated
// hx-ws has been moved to an extension
object HxCssClasses {
const val htmxAdded = "htmx-added"// Applied to a new piece of content before it is swapped, removed after it is settled.
const val htmxindicator = "htmx-indicator"// A dynamically generated class that will toggle visible (opacity:1) when a htmx-request class is present
const val htmxrequest = "htmx-request"// Applied to either the element or the element specified with hx-indicator while a request is ongoing
const val htmxsettling = "htmx-settling"// Applied to a target after content is swapped, removed after it is settled. The duration can be modified via hx-swap.
const val htmxswapping = "htmx-swapping"// Applied to a target before any content is swapped, removed after it is swapped. The duration can be modified via hx-swap.
}
object HxRequestHeader {
const val HxRequestHeaderBoosted = "HX-Boosted"// Boolean indicates that the request is via an element using hx-boost
const val HxRequestHeaderCurrentURL = "HX-Current-URL"// String the current URL of the browser
const val HxRequestHeaderHistoryRestoreRequest = "HX-History-Restore-Request"// Boolean “true” if the request is for history restoration after a miss in the local history cache
const val HxRequestHeaderPrompt = "HX-Prompt"// String the user response to an hx-prompt
const val HxRequest = "HX-Request"// Boolean always “true”
const val HxRequestHeaderTarget = "HX-Target"// String
const val HxRequestHeaderTriggerName = "HX-Trigger-Name"// String the name of the triggered element if it exists
const val HxRequestHeaderTrigger = "HX-Trigger"// String the id of the triggered element if it exists
}
object HxResponseHeader {
const val HXLocation = "HX-Location" // Allows you to do a client-side redirect that does not do a full page reload
const val HXPushUrl = "HX-Push-Url" // pushes a new url into the history stack
const val HXRedirect = "HX-Redirect" // can be used to do a client-side redirect to a new location
const val HXRefresh = "HX-Refresh" // if set to "true" the client side will do a full refresh of the page
const val HXReplaceUrl = "HX-Replace-Url" // replaces the current URL in the location bar
const val HXReswap = "HX-Reswap" // Allows you to specify how the response will be swapped. See hx-swap for possible values
const val HXRetarget = "HX-Retarget" // A CSS selector that updates the target of the content update to a different element on the page
const val HXReselect = "HX-Reselect" // A CSS selector that allows you to choose which part of the response is used to be swapped in. Overrides an existing hx-select on the triggering element
const val HXTrigger = "HX-Trigger" // allows you to trigger client side events, see the documentation for more info
const val HXTriggerAfterSettle = "HX-Trigger-After-Settle" // allows you to trigger client side events, see the documentation for more info
const val HXTriggerAfterSwap = "HX-Trigger-After-Swap" // allows you to trigger client side events, see the documentation for more info
}
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment