Skip to content

Commit

Permalink
Update for v2 beta 1
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljsummers committed Mar 19, 2024
1 parent bdb7255 commit 4f6bb83
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 47 deletions.
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ 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...

```fsharp
// "partial" and "full" are handlers that return the contents;
// "view" can be whatever your view engine needs for the body of the page
let result view : HttpHandler =
// "partial" and "full" are handlers that return the contents;
// "view" can be whatever your view engine needs for the body of the page
let result view : HttpHandler =
fun next ctx ->
match ctx.Request.IsHtmx && not ctx.Request.IsHtmxRefresh with
| true -> partial view
| false -> full view
if ctx.Request.IsHtmx && not ctx.Request.IsHtmxRefresh then
partial view
else
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.

```fsharp
let theHandler : HttpHandler =
let theHandler : HttpHandler =
fun next ctx ->
// some interesting stuff
withHxRedirect "/the-new-url" >=> Successful.OK
// some interesting stuff
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.
Expand All @@ -48,19 +49,21 @@ 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:

```fsharp
let autoload =
div [ _hxGet "/lazy-load-data"; _hxTrigger "load" ] [ str "Loading..." ]
let autoload =
div [ _hxGet "/lazy-load-data"; _hxTrigger HxTrigger.Load ] [
str "Loading..."
]
```

_(As `hx-boost="true"` is the usual desire for boosting, `_hxBoost` implies true. To disable it for an element, use `_hxNoBoost` instead.)_

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
let shiftClick =
let shiftClick =
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.
Expand Down
2 changes: 1 addition & 1 deletion src/Common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

**htmx version: 2.0.0-alpha2**
**htmx version: 2.0.0-beta1**
6 changes: 3 additions & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionSuffix>alpha2</VersionSuffix>
<PackageReleaseNotes>Update script tags to pull htmx 2.0.0-alpha2; remove deprecated _hxOn attribute</PackageReleaseNotes>
<VersionSuffix>beta1</VersionSuffix>
<PackageReleaseNotes>Update script tags to pull htmx 2.0.0-beta1</PackageReleaseNotes>
<Authors>danieljsummers</Authors>
<Company>Bit Badger Solutions</Company>
<PackageProjectUrl>https://github.com/bit-badger/Giraffe.Htmx</PackageProjectUrl>
Expand All @@ -13,6 +13,6 @@
<RepositoryType>Git</RepositoryType>
<Copyright>MIT License</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>Giraffe htmx alpha</PackageTags>
<PackageTags>Giraffe htmx beta</PackageTags>
</PropertyGroup>
</Project>
18 changes: 9 additions & 9 deletions src/Htmx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

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-alpha2**
**htmx version: 2.0.0-beta1**

_Note that htmx 2.0 is an ALPHA 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._
_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

Expand All @@ -16,20 +16,20 @@ _Note that htmx 2.0 is an ALPHA release. The [migration guide](https://v2-0v2-0.
To obtain a request header, using the `IHeaderDictionary` extension properties:

```fsharp
let myHandler : HttpHander =
let myHandler : HttpHander =
fun next ctx ->
match ctx.HxPrompt with
| Some prompt -> ... // do something with the text the user provided
| None -> ... // no text provided
match ctx.HxPrompt with
| Some prompt -> ... // do something with the text the user provided
| None -> ... // no text provided
```

To set a response header:

```fsharp
let myHandler : HttpHander =
let myHandler : HttpHander =
fun next ctx ->
// some meaningful work
withHxPushUrl "/some/new/url" >=> [other handlers]
// some meaningful work
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.
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/ViewEngine.fs
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,14 @@ let script =
let html = RenderView.AsString.htmlNode Script.minified
Expect.equal
html
"""<script src="https://unpkg.com/[email protected]alpha2" integrity="sha384-291pOdLqD0RmU6d5pyz/xmHmZZPhEv465X4AJDWvc8n0KrWrD65qaf2fk0xmsrJe" crossorigin="anonymous"></script>"""
"""<script src="https://unpkg.com/[email protected]beta1" integrity="sha384-T4bn1KcRXtR+kI4M+oOdhVp039Q5Oyq59dsDYJuSBCuHDbLA4iBpCSYaEKmGK5AU" crossorigin="anonymous"></script>"""
"Minified script tag is incorrect"
}
test "unminified succeeds" {
let html = RenderView.AsString.htmlNode Script.unminified
Expect.equal
html
"""<script src="https://unpkg.com/[email protected]alpha2/dist/htmx.js" integrity="sha384-4Owd2qj8qFNBZ8QiR2ItYnNuMGgpOxaDT1uuRJXziuB1lvvKGYA/Lxy/oVM6q3h1" crossorigin="anonymous"></script>"""
"""<script src="https://unpkg.com/[email protected]beta1/dist/htmx.js" integrity="sha384-LT44Cr6uyeKreQr6d0kPh+o3cuLJ3Q+ysV1YzehXD6D9pdpF/7DMiUDx7hrlPiNi" crossorigin="anonymous"></script>"""
"Unminified script tag is incorrect"
}
]
Expand Down
8 changes: 4 additions & 4 deletions src/ViewEngine.Htmx/Htmx.fs
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,14 @@ module Script =

/// Script tag to load the minified version from unpkg.com
let minified =
script [ _src "https://unpkg.com/[email protected]alpha2"
_integrity "sha384-291pOdLqD0RmU6d5pyz/xmHmZZPhEv465X4AJDWvc8n0KrWrD65qaf2fk0xmsrJe"
script [ _src "https://unpkg.com/[email protected]beta1"
_integrity "sha384-T4bn1KcRXtR+kI4M+oOdhVp039Q5Oyq59dsDYJuSBCuHDbLA4iBpCSYaEKmGK5AU"
_crossorigin "anonymous" ] []

/// Script tag to load the unminified version from unpkg.com
let unminified =
script [ _src "https://unpkg.com/[email protected]alpha2/dist/htmx.js"
_integrity "sha384-4Owd2qj8qFNBZ8QiR2ItYnNuMGgpOxaDT1uuRJXziuB1lvvKGYA/Lxy/oVM6q3h1"
script [ _src "https://unpkg.com/[email protected]beta1/dist/htmx.js"
_integrity "sha384-LT44Cr6uyeKreQr6d0kPh+o3cuLJ3Q+ysV1YzehXD6D9pdpF/7DMiUDx7hrlPiNi"
_crossorigin "anonymous" ] []


Expand Down
28 changes: 14 additions & 14 deletions src/ViewEngine.Htmx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

This package enables [htmx](https://htmx.org) support within the [Giraffe](https://giraffe.wiki) view engine.

**htmx version: 2.0.0-alpha2**
**htmx version: 2.0.0-beta1**

_Note that this is an ALPHA release of htmx 2.0; see [the migration guide](https://v2-0v2-0.htmx.org/migration-guide-htmx-1/) for changes_
_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

Expand All @@ -16,7 +16,7 @@ _Note that this is an ALPHA 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:

```fsharp
let autoload =
let autoload =
div [ _hxGet "/this/data"; _hxTrigger HxTrigger.Load ] [ str "Loading..." ]
```

Expand Down Expand Up @@ -45,19 +45,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.

```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:

```fsharp
HxTrigger.Click
|> HxTrigger.Filter.Shift
|> HxTrigger.FromDocument
|> HxTrigger.Delay "3s"
|> _hxTrigger
// or
(HxTrigger.Filter.Shift >> HxTrigger.FromDocument >> HxTrigger.Delay "3s") HxTrigger.Click
|> _hxTrigger
HxTrigger.Click
|> HxTrigger.Filter.Shift
|> HxTrigger.FromDocument
|> HxTrigger.Delay "3s"
|> _hxTrigger
// or
(HxTrigger.Filter.Shift >> HxTrigger.FromDocument >> HxTrigger.Delay "3s") HxTrigger.Click
|> _hxTrigger
```

0 comments on commit 4f6bb83

Please sign in to comment.