Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Htmx v1.9.6 #3

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/HelloWorld/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ let handleHtml : HttpHandler =
Templates.html5 "en"
[
Elem.link [ Attr.href "style.css"; Attr.rel "stylesheet" ]
Elem.script [ Attr.src "https://unpkg.com/htmx.org@1.8.0"] []
Elem.script [ Attr.src Script.src ] []
]
[
Elem.h1 [] [
Expand Down
24 changes: 16 additions & 8 deletions src/Falco.Htmx/Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type HtmxRequestHeaders =
/// The id of the triggered element if it exists
HxTrigger : string option }

/// Value for the HX-Trigger Response Header
type HxTriggerResponse =
| Events of string list
| DetailedEvents of (string * obj) list

[<RequireQualifiedAccess>]
module Request =
let getHtmxHeaders (ctx : HttpContext) : HtmxRequestHeaders =
Expand Down Expand Up @@ -100,17 +105,15 @@ module Response =
Response.withHeaders [ "HX-Retarget", TargetOption.AsString option ]

// Allows you to trigger client side events, see the documentation for more info
let withHxTrigger<'T>(event: string, detail : 'T option) : HttpResponseModifier =
let withHxTrigger<'T>(triggerResponse: HxTriggerResponse) : HttpResponseModifier =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

let headerValue =
match detail with
| Some detail' ->
[ event, detail ]
match triggerResponse with
| Events events -> events |> String.concat ", "
| DetailedEvents events ->
events
|> Map.ofList
|> fun x -> JsonSerializer.Serialize(x, Json.defaultSerializerOptions)

| None ->
event

Response.withHeaders [ "HX-Trigger", headerValue ]

// Allows you to trigger client side events, see the documentation for more info
Expand All @@ -119,4 +122,9 @@ module Response =

// Allows you to trigger client side events, see the documentation for more info
let withHxTriggerAfterSwap : HttpResponseModifier =
Response.withHeaders [ "HX-Trigger-After-Swap", _trueValue ]
Response.withHeaders [ "HX-Trigger-After-Swap", _trueValue ]

/// A CSS selector that allows you to choose which part of the response is used to be swapped in.
/// see the documentation for more info
let withHxReselect (selector: string) : HttpResponseModifier =
Response.withHeaders [ "HX-Reselect", selector ]
4 changes: 4 additions & 0 deletions src/Falco.Htmx/Htmx.fs
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,7 @@ type DisinheritOption =
| AllAttributes -> "*"
| ExcludeAttributes [] -> "*"
| ExcludeAttributes names -> String.Join(" ", names)

module Script =
/// The script of the version of HTMX this library is written for.
let src = "https://unpkg.com/htmx.org@1.9.6"
15 changes: 14 additions & 1 deletion src/Falco.Htmx/Hx.fs
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,17 @@ let sync (targetOption : TargetOption, syncOption : SyncOption option) =
| Some sync' -> String.Concat([ target'; ":"; SyncOption.AsString sync' ])
| None -> target'

Attr.create "hx-sync" attrValue
Attr.create "hx-sync" attrValue

/// Whether to force elements to validate themselves before a request
let validate (value: bool) = Attr.create "hx-validate" (string value)

/// Whether to save sensitive data to the history cache
let history (value: bool) = Attr.create "hx-history" (string value)

/// Handle any event with a script inline
let on eventName = Attr.create $"hx-on:{eventName}"

/// adds the `disabled` attribute to the specified elements while a request is in flight
let disabledElement (targetOption: TargetOption) =
Attr.create "hx-disabled-elt" (TargetOption.AsString targetOption)
Loading