Compare commits

..

No commits in common. "7f9b3a62349e969716ca84c37fc1d87153c10da0" and "1ea05b79ed78fdee705ba8dba21e40fb889ebc5f" have entirely different histories.

7 changed files with 66 additions and 55 deletions

View File

@ -21,23 +21,22 @@ In addition to the regular HTTP request payloads, htmx sets [one or more headers
A server may want to respond to a request that originated from htmx differently than a regular request. One way htmx can provide the same feel as a Single Page Application (SPA) is by swapping out the `body` content (or an element within it) instead of reloading the entire page. In this case, the developer can provide a partial layout to be used for these responses, while returning the full page for regular requests. The `IsHtmx` property makes this easy... A server may want to respond to a request that originated from htmx differently than a regular request. One way htmx can provide the same feel as a Single Page Application (SPA) is by swapping out the `body` content (or an element within it) instead of reloading the entire page. In this case, the developer can provide a partial layout to be used for these responses, while returning the full page for regular requests. The `IsHtmx` property makes this easy...
```fsharp ```fsharp
// "partial" and "full" are handlers that return the contents; // "partial" and "full" are handlers that return the contents;
// "view" can be whatever your view engine needs for the body of the page // "view" can be whatever your view engine needs for the body of the page
let result view : HttpHandler = let result view : HttpHandler =
fun next ctx -> fun next ctx ->
if ctx.Request.IsHtmx && not ctx.Request.IsHtmxRefresh then match ctx.Request.IsHtmx && not ctx.Request.IsHtmxRefresh with
partial view | true -> partial view
else | false -> full view
full view
``` ```
htmx also utilizes [response headers](https://htmx.org/docs/#response_headers) to affect client-side behavior. For each of these, this library provides `HttpHandler`s that can be chained along with the response. As an example, if the server returns a redirect response (301, 302, 303, 307), the `XMLHttpRequest` handler on the client will follow the redirection before htmx can do anything with it. To redirect to a new page, you would return an OK (200) response with an `HX-Redirect` header set in the response. htmx also utilizes [response headers](https://htmx.org/docs/#response_headers) to affect client-side behavior. For each of these, this library provides `HttpHandler`s that can be chained along with the response. As an example, if the server returns a redirect response (301, 302, 303, 307), the `XMLHttpRequest` handler on the client will follow the redirection before htmx can do anything with it. To redirect to a new page, you would return an OK (200) response with an `HX-Redirect` header set in the response.
```fsharp ```fsharp
let theHandler : HttpHandler = let theHandler : HttpHandler =
fun next ctx -> fun next ctx ->
// some interesting stuff // some interesting stuff
withHxRedirect "/the-new-url" >=> Successful.OK withHxRedirect "/the-new-url" >=> Successful.OK
``` ```
Of note is that the `HX-Trigger` headers can take either one or more events. For a single event with no parameters, use `withHxTrigger`; for a single event with parameters, or multiple events, use `withHxTriggerMany`. Both these have `AfterSettle` and `AfterSwap` versions as well. Of note is that the `HX-Trigger` headers can take either one or more events. For a single event with no parameters, use `withHxTrigger`; for a single event with parameters, or multiple events, use `withHxTriggerMany`. Both these have `AfterSettle` and `AfterSwap` versions as well.
@ -49,10 +48,8 @@ As htmx uses [attributes](https://htmx.org/docs/#attributes) to extend HTML, the
As an example, creating a `div` that loads data once the HTML is rendered: As an example, creating a `div` that loads data once the HTML is rendered:
```fsharp ```fsharp
let autoload = let autoload =
div [ _hxGet "/lazy-load-data"; _hxTrigger HxTrigger.Load ] [ div [ _hxGet "/lazy-load-data"; _hxTrigger "load" ] [ str "Loading..." ]
str "Loading..."
]
``` ```
_(As `hx-boost="true"` is the usual desire for boosting, `_hxBoost` implies true. To disable it for an element, use `_hxNoBoost` instead.)_ _(As `hx-boost="true"` is the usual desire for boosting, `_hxBoost` implies true. To disable it for an element, use `_hxNoBoost` instead.)_
@ -60,10 +57,10 @@ _(As `hx-boost="true"` is the usual desire for boosting, `_hxBoost` implies true
Some attributes have known values, such as `hx-trigger` and `hx-swap`; for these, there are modules with those values. For example, `HxTrigger.Load` could be used in the example above, to ensure that the known values are spelled correctly. `hx-trigger` can also take modifiers, such as an action that only responds to `Ctrl`+click. The `HxTrigger` module has a `Filter` submodule to assist with defining these actions. Some attributes have known values, such as `hx-trigger` and `hx-swap`; for these, there are modules with those values. For example, `HxTrigger.Load` could be used in the example above, to ensure that the known values are spelled correctly. `hx-trigger` can also take modifiers, such as an action that only responds to `Ctrl`+click. The `HxTrigger` module has a `Filter` submodule to assist with defining these actions.
```fsharp ```fsharp
let shiftClick = let shiftClick =
p [ _hxGet = "/something"; _hxTrigger (HxTrigger.Filter.Shift HxTrigger.Click) ] [ p [ _hxGet = "/something"; _hxTrigger (HxTrigger.Filter.Shift HxTrigger.Click) ] [
str "hold down Shift and click me" str "hold down Shift and click me"
] ]
``` ```
If you want to load htmx from unpkg, `Htmx.Script.minified` or `Htmx.Script.unminified` can be used to load the script in your HTML trees. If you want to load htmx from unpkg, `Htmx.Script.minified` or `Htmx.Script.unminified` can be used to load the script in your HTML trees.

View File

@ -2,4 +2,4 @@
This package contains common code shared between [`Giraffe.Htmx`](https://www.nuget.org/packages/Giraffe.Htmx) and [`Giraffe.ViewEngine.Htmx`](https://www.nuget.org/packages/Giraffe.ViewEngine.Htmx), and will be automatically installed when you install either one. This package contains common code shared between [`Giraffe.Htmx`](https://www.nuget.org/packages/Giraffe.Htmx) and [`Giraffe.ViewEngine.Htmx`](https://www.nuget.org/packages/Giraffe.ViewEngine.Htmx), and will be automatically installed when you install either one.
**htmx version: 2.0.0-beta1** **htmx version: 1.9.11**

View File

@ -2,9 +2,8 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks> <TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<VersionPrefix>2.0.0</VersionPrefix> <VersionPrefix>1.9.11</VersionPrefix>
<VersionSuffix>beta1</VersionSuffix> <PackageReleaseNotes>Update script tags to pull htmx 1.9.11</PackageReleaseNotes>
<PackageReleaseNotes>Update script tags to pull htmx 2.0.0-beta1</PackageReleaseNotes>
<Authors>danieljsummers</Authors> <Authors>danieljsummers</Authors>
<Company>Bit Badger Solutions</Company> <Company>Bit Badger Solutions</Company>
<PackageProjectUrl>https://github.com/bit-badger/Giraffe.Htmx</PackageProjectUrl> <PackageProjectUrl>https://github.com/bit-badger/Giraffe.Htmx</PackageProjectUrl>
@ -13,6 +12,6 @@
<RepositoryType>Git</RepositoryType> <RepositoryType>Git</RepositoryType>
<Copyright>MIT License</Copyright> <Copyright>MIT License</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>Giraffe htmx beta</PackageTags> <PackageTags>Giraffe htmx</PackageTags>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -2,13 +2,11 @@
This package enables server-side support for [htmx](https://htmx.org) within [Giraffe](https://giraffe.wiki) and ASP.NET's `HttpContext`. This package enables server-side support for [htmx](https://htmx.org) within [Giraffe](https://giraffe.wiki) and ASP.NET's `HttpContext`.
**htmx version: 2.0.0-beta1** **htmx version: 1.9.11**
_Note that htmx 2.0 is a BETA release. The [migration guide](https://v2-0v2-0.htmx.org/migration-guide-htmx-1/) does not currently specify any request or response header changes. This means that, as of this release, there are no required code changes in moving to this major version._
### Setup ### Setup
1. Install the package (must use `--Prerelease` flag). 1. Install the package.
2. Prior to using the request header extension properties or the header-setting `HttpHandler`s, `open Giraffe.Htmx`. 2. Prior to using the request header extension properties or the header-setting `HttpHandler`s, `open Giraffe.Htmx`.
### Use ### Use
@ -16,20 +14,20 @@ _Note that htmx 2.0 is a BETA release. The [migration guide](https://v2-0v2-0.ht
To obtain a request header, using the `IHeaderDictionary` extension properties: To obtain a request header, using the `IHeaderDictionary` extension properties:
```fsharp ```fsharp
let myHandler : HttpHander = let myHandler : HttpHander =
fun next ctx -> fun next ctx ->
match ctx.HxPrompt with match ctx.HxPrompt with
| Some prompt -> ... // do something with the text the user provided | Some prompt -> ... // do something with the text the user provided
| None -> ... // no text provided | None -> ... // no text provided
``` ```
To set a response header: To set a response header:
```fsharp ```fsharp
let myHandler : HttpHander = let myHandler : HttpHander =
fun next ctx -> fun next ctx ->
// some meaningful work // some meaningful work
withHxPushUrl "/some/new/url" >=> [other handlers] withHxPushUrl "/some/new/url" >=> [other handlers]
``` ```
The `HxSwap` module has constants to use for the `HX-Reswap` header. These may be extended with settle, show, and other qualifiers; see the htmx documentation for the `hx-swap` attribute for more information. The `HxSwap` module has constants to use for the `HX-Reswap` header. These may be extended with settle, show, and other qualifiers; see the htmx documentation for the `hx-swap` attribute for more information.

View File

@ -820,14 +820,14 @@ let script =
let html = RenderView.AsString.htmlNode Script.minified let html = RenderView.AsString.htmlNode Script.minified
Expect.equal Expect.equal
html html
"""<script src="https://unpkg.com/htmx.org@2.0.0-beta1" integrity="sha384-T4bn1KcRXtR+kI4M+oOdhVp039Q5Oyq59dsDYJuSBCuHDbLA4iBpCSYaEKmGK5AU" crossorigin="anonymous"></script>""" """<script src="https://unpkg.com/htmx.org@1.9.11" integrity="sha384-0gxUXCCR8yv9FM2b+U3FDbsKthCI66oH5IA9fHppQq9DDMHuMauqq1ZHBpJxQ0J0" crossorigin="anonymous"></script>"""
"Minified script tag is incorrect" "Minified script tag is incorrect"
} }
test "unminified succeeds" { test "unminified succeeds" {
let html = RenderView.AsString.htmlNode Script.unminified let html = RenderView.AsString.htmlNode Script.unminified
Expect.equal Expect.equal
html html
"""<script src="https://unpkg.com/htmx.org@2.0.0-beta1/dist/htmx.js" integrity="sha384-LT44Cr6uyeKreQr6d0kPh+o3cuLJ3Q+ysV1YzehXD6D9pdpF/7DMiUDx7hrlPiNi" crossorigin="anonymous"></script>""" """<script src="https://unpkg.com/htmx.org@1.9.11/dist/htmx.js" integrity="sha384-l9bYT9SL4CAW0Hl7pAOpfRc18mys1b0wK4U8UtGnWOxPVbVMgrOdB+jyz/WY8Jue" crossorigin="anonymous"></script>"""
"Unminified script tag is incorrect" "Unminified script tag is incorrect"
} }
] ]
@ -979,6 +979,18 @@ let renderFragment =
] ]
] ]
#nowarn "44"
/// Tests for the HtmxAttrs module
let deprecatedAttributes =
testList "Deprecated Attributes" [
test "_hxOn succeeds" {
let newLine = "\n"
strong [ _hxOn "submit: alert('oops')\nclick: alert('howdy!')" ] []
|> shouldRender $"""<strong hx-on="submit: alert(&#39;oops&#39;){newLine}click: alert(&#39;howdy!&#39;)"></strong>"""
}
]
/// All tests in this module /// All tests in this module
let allTests = let allTests =
testList "ViewEngine.Htmx" [ testList "ViewEngine.Htmx" [
@ -992,4 +1004,5 @@ let allTests =
attributes attributes
script script
renderFragment renderFragment
deprecatedAttributes
] ]

View File

@ -317,6 +317,8 @@ module HxVals =
/// Create values from a list of key/value pairs /// Create values from a list of key/value pairs
let From = toJson let From = toJson
open System
/// Attributes and flags for htmx /// Attributes and flags for htmx
[<AutoOpen>] [<AutoOpen>]
module HtmxAttrs = module HtmxAttrs =
@ -366,6 +368,10 @@ module HtmxAttrs =
/// Overrides a previous `hx-boost` /// Overrides a previous `hx-boost`
let _hxNoBoost = attr "hx-boost" "false" let _hxNoBoost = attr "hx-boost" "false"
/// Attach an event handler for DOM or htmx events
[<Obsolete "This will be removed in htmx 2; use _hxOnEvent or _hxOnHxEvent instead">]
let _hxOn = attr "hx-on"
/// Attach an event handler for DOM events /// Attach an event handler for DOM events
let _hxOnEvent evtName = let _hxOnEvent evtName =
attr $"hx-on:%s{evtName}" attr $"hx-on:%s{evtName}"
@ -444,14 +450,14 @@ module Script =
/// Script tag to load the minified version from unpkg.com /// Script tag to load the minified version from unpkg.com
let minified = let minified =
script [ _src "https://unpkg.com/htmx.org@2.0.0-beta1" script [ _src "https://unpkg.com/htmx.org@1.9.11"
_integrity "sha384-T4bn1KcRXtR+kI4M+oOdhVp039Q5Oyq59dsDYJuSBCuHDbLA4iBpCSYaEKmGK5AU" _integrity "sha384-0gxUXCCR8yv9FM2b+U3FDbsKthCI66oH5IA9fHppQq9DDMHuMauqq1ZHBpJxQ0J0"
_crossorigin "anonymous" ] [] _crossorigin "anonymous" ] []
/// Script tag to load the unminified version from unpkg.com /// Script tag to load the unminified version from unpkg.com
let unminified = let unminified =
script [ _src "https://unpkg.com/htmx.org@2.0.0-beta1/dist/htmx.js" script [ _src "https://unpkg.com/htmx.org@1.9.11/dist/htmx.js"
_integrity "sha384-LT44Cr6uyeKreQr6d0kPh+o3cuLJ3Q+ysV1YzehXD6D9pdpF/7DMiUDx7hrlPiNi" _integrity "sha384-l9bYT9SL4CAW0Hl7pAOpfRc18mys1b0wK4U8UtGnWOxPVbVMgrOdB+jyz/WY8Jue"
_crossorigin "anonymous" ] [] _crossorigin "anonymous" ] []

View File

@ -2,13 +2,11 @@
This package enables [htmx](https://htmx.org) support within the [Giraffe](https://giraffe.wiki) view engine. This package enables [htmx](https://htmx.org) support within the [Giraffe](https://giraffe.wiki) view engine.
**htmx version: 2.0.0-beta1** **htmx version: 1.9.11**
_Note that this is a BETA release of htmx 2.0; see [the migration guide](https://v2-0v2-0.htmx.org/migration-guide-htmx-1/) for changes_
### Setup ### Setup
1. Install the package (must use `--Prelease` flag). 1. Install the package.
2. Prior to using the attribute or support modules, `open Giraffe.ViewEngine.Htmx`. 2. Prior to using the attribute or support modules, `open Giraffe.ViewEngine.Htmx`.
### Use ### Use
@ -16,7 +14,7 @@ _Note that this is a BETA release of htmx 2.0; see [the migration guide](https:/
Following Giraffe View Engine's lead, there are a set of attribute functions for htmx; for many of the attributes, there are also helper modules to assist with typing the values. The example below utilizes both: Following Giraffe View Engine's lead, there are a set of attribute functions for htmx; for many of the attributes, there are also helper modules to assist with typing the values. The example below utilizes both:
```fsharp ```fsharp
let autoload = let autoload =
div [ _hxGet "/this/data"; _hxTrigger HxTrigger.Load ] [ str "Loading..." ] div [ _hxGet "/this/data"; _hxTrigger HxTrigger.Load ] [ str "Loading..." ]
``` ```
@ -45,19 +43,19 @@ The support modules contain named properties for known values (as illustrated wi
- `HxRequest` has a `Configure` function, which takes a list of strings; the other functions in the module allow for configuring the request. - `HxRequest` has a `Configure` function, which takes a list of strings; the other functions in the module allow for configuring the request.
```fsharp ```fsharp
HxRequest.Configure [ HxRequest.Timeout 500 ] |> _hxRequest HxRequest.Configure [ HxRequest.Timeout 500 ] |> _hxRequest
``` ```
- `HxTrigger` is _(by far)_ the most complex of these modules. Most uses won't need that complexity; however, complex triggers can be defined by piping into or composing with other functions. For example, to define an event that responds to a shift-click anywhere on the document, with a delay of 3 seconds before firing: - `HxTrigger` is _(by far)_ the most complex of these modules. Most uses won't need that complexity; however, complex triggers can be defined by piping into or composing with other functions. For example, to define an event that responds to a shift-click anywhere on the document, with a delay of 3 seconds before firing:
```fsharp ```fsharp
HxTrigger.Click HxTrigger.Click
|> HxTrigger.Filter.Shift |> HxTrigger.Filter.Shift
|> HxTrigger.FromDocument |> HxTrigger.FromDocument
|> HxTrigger.Delay "3s" |> HxTrigger.Delay "3s"
|> _hxTrigger |> _hxTrigger
// or // or
(HxTrigger.Filter.Shift >> HxTrigger.FromDocument >> HxTrigger.Delay "3s") HxTrigger.Click (HxTrigger.Filter.Shift >> HxTrigger.FromDocument >> HxTrigger.Delay "3s") HxTrigger.Click
|> _hxTrigger |> _hxTrigger
``` ```