htmgo/htmgo-site/partials/navbar.go

172 lines
4.1 KiB
Go
Raw Normal View History

2024-09-20 16:45:23 +00:00
package partials
2024-09-21 03:59:07 +00:00
import (
2024-09-28 02:29:53 +00:00
"fmt"
2024-09-21 03:59:07 +00:00
"github.com/maddalax/htmgo/framework/h"
2024-09-27 02:15:04 +00:00
"htmgo-site/internal/httpjson"
"time"
2024-09-21 03:59:07 +00:00
)
2024-09-20 16:45:23 +00:00
type NavItem struct {
Name string
Url string
}
2024-09-28 02:29:53 +00:00
var navItems = []NavItem{
{Name: "Docs", Url: "/docs"},
{Name: "Examples", Url: "/examples"},
}
2024-09-20 20:15:12 +00:00
func ToggleNavbar(ctx *h.RequestContext) *h.Partial {
2024-09-21 16:52:56 +00:00
return h.SwapManyPartial(
ctx,
2024-09-27 02:15:04 +00:00
MobileNav(ctx, h.GetQueryParam(ctx, "expanded") == "true"),
2024-09-20 20:15:12 +00:00
)
}
2024-09-20 16:45:23 +00:00
2024-09-28 02:41:32 +00:00
var CachedStar = h.CachedT(time.Minute*15, func(t *h.RequestContext) *h.Element {
return Star(t)
})
2024-09-27 02:15:04 +00:00
func Star(ctx *h.RequestContext) *h.Element {
2024-09-26 22:00:30 +00:00
2024-09-27 02:15:04 +00:00
type Repo struct {
StarCount int `json:"stargazers_count"`
}
2024-09-28 02:29:53 +00:00
fmt.Printf("making github star request\n")
count := 0
response, err := httpjson.Get[Repo]("https://api.github.com/repos/maddalax/htmgo")
if err == nil && response != nil {
count = response.StarCount
}
2024-09-27 02:15:04 +00:00
return h.A(
h.Href("https://github.com/maddalax/htmgo"),
h.Target("_blank"),
h.Class("inline-flex items-center rounded overflow-hidden shadow-sm"),
h.Div(
h.Class("flex items-center px-2 py-1 bg-gray-800 text-white text-sm font-semibold hover:bg-gray-700 transition"),
h.Svg(
2024-09-27 02:18:58 +00:00
h.Class("w-4 h-4 -mt-0.5 mr-0.5 stroke-current text-white"),
2024-09-27 02:15:04 +00:00
h.Attribute("xmlns", "http://www.w3.org/2000/svg"),
h.Attribute("viewBox", "0 0 24 24"),
2024-09-27 02:18:58 +00:00
h.Attribute("fill", "none"), // No fill
h.Attribute("stroke", "currentColor"), // Apply stroke
h.Attribute("stroke-width", "2"), // Stroke width
2024-09-27 02:15:04 +00:00
h.Path(
h.D("M12 17.27l5.18 3.05-1.64-5.68 4.46-3.87-5.88-.5L12 3.5l-2.12 6.77-5.88.5 4.46 3.87-1.64 5.68L12 17.27z"),
),
),
h.Text("Star"),
),
h.If(count > 0, h.Div(
h.Class("flex items-center px-3 py-1 bg-black text-white text-sm font-semibold"),
h.Pf("%d", count),
)),
2024-09-24 18:23:38 +00:00
)
2024-09-20 20:15:12 +00:00
}
2024-09-20 16:45:23 +00:00
2024-09-27 02:15:04 +00:00
func NavBar(ctx *h.RequestContext, expanded bool) *h.Element {
2024-09-24 19:51:46 +00:00
prelease := h.A(h.Class("bg-yellow-200 text-yellow-800 text-center p-2 flex items-center justify-center"),
h.Href("https://github.com/maddalax/htmgo/issues"),
h.Attribute("target", "_blank"),
h.Text("htmgo is in alpha release. Please report any issues on GitHub."),
2024-09-20 18:25:14 +00:00
)
2024-09-20 20:15:12 +00:00
desktopNav := h.Nav(
h.Class("hidden sm:block bg-neutral-100 border border-b-slate-300 p-4 md:p-3"),
h.Div(
2024-09-24 18:16:07 +00:00
h.Class("max-w-[95%] md:max-w-3xl px-4 mx-auto"),
2024-09-20 20:15:12 +00:00
h.Div(
h.Class("flex justify-between items-center"),
h.Div(
h.Class("flex items-center"),
h.A(
h.Class("text-2xl"),
h.Href("/"),
h.Text("htmgo"),
)),
h.Div(
h.Class("flex gap-4 items-center"),
h.List(navItems, func(item NavItem, index int) *h.Element {
return h.Div(
h.Class("flex items-center"),
h.A(
h.Class(""),
h.Href(item.Url),
h.Text(item.Name),
),
)
}),
2024-09-28 02:29:53 +00:00
CachedStar(ctx),
2024-09-20 20:15:12 +00:00
),
),
),
)
2024-09-20 18:25:14 +00:00
return h.Div(
2024-09-20 20:15:12 +00:00
h.Id("navbar"),
2024-09-20 18:25:14 +00:00
prelease,
2024-09-27 02:15:04 +00:00
MobileNav(ctx, expanded),
2024-09-20 20:15:12 +00:00
desktopNav,
)
}
2024-09-27 02:15:04 +00:00
func MobileNav(ctx *h.RequestContext, expanded bool) *h.Element {
2024-09-20 20:15:12 +00:00
return h.Nav(
h.Id("mobile-nav"),
h.Class("block sm:hidden bg-neutral-100 border border-b-slate-300 p-4 md:p-3"),
h.Div(
h.Class("max-w-[95%] md:max-w-prose mx-auto"),
2024-09-20 16:45:23 +00:00
h.Div(
2024-09-20 20:15:12 +00:00
h.Class("flex justify-between items-center"),
2024-09-20 16:45:23 +00:00
h.Div(
2024-09-20 20:15:12 +00:00
h.Class("flex items-center"),
h.A(
2024-09-21 03:59:07 +00:00
h.Boost(),
2024-09-20 20:15:12 +00:00
h.Class("text-2xl"),
h.Href("/"),
h.Text("htmgo"),
)),
h.Div(
2024-09-26 22:00:30 +00:00
h.Class("flex items-center gap-3"),
2024-09-28 02:29:53 +00:00
h.Div(h.Class("mt-1"), CachedStar(ctx)),
2024-09-20 20:15:12 +00:00
h.Button(
2024-09-21 03:59:07 +00:00
h.Boost(),
h.GetPartialWithQs(
ToggleNavbar,
h.NewQs("expanded", h.Ternary(expanded, "false", "true"), "test", "true"),
"click",
),
h.AttributePairs(
"class", "text-2xl",
"aria-expanded", h.Ternary(expanded, "true", "false"),
),
2024-09-20 20:15:12 +00:00
h.Class("text-2xl"),
2024-09-26 22:00:30 +00:00
h.Text("☰"),
2024-09-20 18:25:14 +00:00
),
2024-09-20 16:45:23 +00:00
),
),
),
2024-09-20 20:15:12 +00:00
h.If(expanded, h.Div(
h.Class("mt-2 ml-2 flex flex-col gap-2"),
h.List(navItems, func(item NavItem, index int) *h.Element {
return h.Div(
h.Class("flex items-center"),
h.A(
2024-09-21 03:59:07 +00:00
h.Boost(),
2024-09-20 20:15:12 +00:00
h.Class(""),
h.Href(item.Url),
h.Text(item.Name),
),
)
}),
)),
2024-09-20 16:45:23 +00:00
)
}