2020-12-10 04:00:23 -08:00
|
|
|
// Package components provides high-level components and helpers that are composed of low-level elements and attributes.
|
2020-10-29 04:03:43 -07:00
|
|
|
package components
|
|
|
|
|
|
|
|
import (
|
2022-11-03 03:32:16 -07:00
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2020-10-29 04:03:43 -07:00
|
|
|
g "github.com/maragudk/gomponents"
|
2020-12-10 04:00:23 -08:00
|
|
|
. "github.com/maragudk/gomponents/html"
|
2020-10-29 04:03:43 -07:00
|
|
|
)
|
|
|
|
|
2020-12-10 04:00:23 -08:00
|
|
|
// HTML5Props for HTML5.
|
2020-10-29 04:03:43 -07:00
|
|
|
// Title is set no matter what, Description and Language elements only if the strings are non-empty.
|
2020-12-10 04:00:23 -08:00
|
|
|
type HTML5Props struct {
|
2020-10-29 04:03:43 -07:00
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Language string
|
|
|
|
Head []g.Node
|
|
|
|
Body []g.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTML5 document template.
|
2021-01-07 01:20:03 -08:00
|
|
|
func HTML5(p HTML5Props) g.Node {
|
2020-12-10 04:13:10 -08:00
|
|
|
return Doctype(
|
2021-04-28 01:42:49 -07:00
|
|
|
HTML(g.If(p.Language != "", Lang(p.Language)),
|
2020-12-10 04:00:23 -08:00
|
|
|
Head(
|
|
|
|
Meta(Charset("utf-8")),
|
|
|
|
Meta(Name("viewport"), Content("width=device-width, initial-scale=1")),
|
2021-05-05 00:03:16 -07:00
|
|
|
TitleEl(g.Text(p.Title)),
|
2021-04-28 01:42:49 -07:00
|
|
|
g.If(p.Description != "", Meta(Name("description"), Content(p.Description))),
|
2020-10-29 04:03:43 -07:00
|
|
|
g.Group(p.Head),
|
|
|
|
),
|
2020-12-10 04:00:23 -08:00
|
|
|
Body(g.Group(p.Body)),
|
2020-10-29 04:03:43 -07:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2022-11-03 03:32:16 -07:00
|
|
|
|
|
|
|
// Classes is a map of strings to booleans, which Renders to an attribute with name "class".
|
|
|
|
// The attribute value is a sorted, space-separated string of all the map keys,
|
|
|
|
// for which the corresponding map value is true.
|
|
|
|
type Classes map[string]bool
|
|
|
|
|
|
|
|
func (c Classes) Render(w io.Writer) error {
|
|
|
|
var included []string
|
|
|
|
for c, include := range c {
|
|
|
|
if include {
|
|
|
|
included = append(included, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(included)
|
|
|
|
return Class(strings.Join(included, " ")).Render(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Classes) Type() g.NodeType {
|
|
|
|
return g.AttributeType
|
|
|
|
}
|
|
|
|
|
|
|
|
// String satisfies fmt.Stringer.
|
|
|
|
func (c Classes) String() string {
|
|
|
|
var b strings.Builder
|
|
|
|
_ = c.Render(&b)
|
|
|
|
return b.String()
|
|
|
|
}
|