From b12942fb32082729e862eb53b8c3962534561e83 Mon Sep 17 00:00:00 2001 From: Markus Wüstenberg Date: Thu, 3 Nov 2022 11:32:16 +0100 Subject: Delete low-value helper components (#115) I'd rather reserve the package for components that have proven repeatedly useful, like `Classes` and `HTML5`.--- components/attributes.go | 37 ------------------------- components/attributes_test.go | 39 -------------------------- components/components.go | 64 +++++++++++++++++++++++++++++++++++++++++++ components/components_test.go | 62 +++++++++++++++++++++++++++++++++++++++++ components/documents.go | 33 ---------------------- components/documents_test.go | 32 ---------------------- components/elements.go | 18 ------------ components/elements_test.go | 30 -------------------- 8 files changed, 126 insertions(+), 189 deletions(-) delete mode 100644 components/attributes.go delete mode 100644 components/attributes_test.go create mode 100644 components/components.go create mode 100644 components/components_test.go delete mode 100644 components/documents.go delete mode 100644 components/documents_test.go delete mode 100644 components/elements.go delete mode 100644 components/elements_test.go (limited to 'components') diff --git a/components/attributes.go b/components/attributes.go deleted file mode 100644 index dc7ef9b..0000000 --- a/components/attributes.go +++ /dev/null @@ -1,37 +0,0 @@ -package components - -import ( - "io" - "sort" - "strings" - - g "github.com/maragudk/gomponents" - "github.com/maragudk/gomponents/html" -) - -// 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 html.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() -} diff --git a/components/attributes_test.go b/components/attributes_test.go deleted file mode 100644 index dcfbad2..0000000 --- a/components/attributes_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package components_test - -import ( - "os" - "testing" - - g "github.com/maragudk/gomponents" - c "github.com/maragudk/gomponents/components" - "github.com/maragudk/gomponents/internal/assert" -) - -func TestClasses(t *testing.T) { - t.Run("given a map, returns sorted keys from the map with value true", func(t *testing.T) { - assert.Equal(t, ` class="boheme-hat hat partyhat"`, c.Classes{ - "boheme-hat": true, - "hat": true, - "partyhat": true, - "turtlehat": false, - }) - }) - - t.Run("renders as attribute in an element", func(t *testing.T) { - e := g.El("div", c.Classes{"hat": true}) - assert.Equal(t, `
`, e) - }) - - t.Run("also works with fmt", func(t *testing.T) { - a := c.Classes{"hat": true} - if a.String() != ` class="hat"` { - t.FailNow() - } - }) -} - -func ExampleClasses() { - e := g.El("div", c.Classes{"party-hat": true, "boring-hat": false}) - _ = e.Render(os.Stdout) - // Output: -} diff --git a/components/components.go b/components/components.go new file mode 100644 index 0000000..379d18d --- /dev/null +++ b/components/components.go @@ -0,0 +1,64 @@ +// Package components provides high-level components and helpers that are composed of low-level elements and attributes. +package components + +import ( + "io" + "sort" + "strings" + + g "github.com/maragudk/gomponents" + . "github.com/maragudk/gomponents/html" +) + +// HTML5Props for HTML5. +// Title is set no matter what, Description and Language elements only if the strings are non-empty. +type HTML5Props struct { + Title string + Description string + Language string + Head []g.Node + Body []g.Node +} + +// HTML5 document template. +func HTML5(p HTML5Props) g.Node { + return Doctype( + HTML(g.If(p.Language != "", Lang(p.Language)), + Head( + Meta(Charset("utf-8")), + Meta(Name("viewport"), Content("width=device-width, initial-scale=1")), + TitleEl(g.Text(p.Title)), + g.If(p.Description != "", Meta(Name("description"), Content(p.Description))), + g.Group(p.Head), + ), + Body(g.Group(p.Body)), + ), + ) +} + +// 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() +} diff --git a/components/components_test.go b/components/components_test.go new file mode 100644 index 0000000..6420cab --- /dev/null +++ b/components/components_test.go @@ -0,0 +1,62 @@ +package components_test + +import ( + "os" + "testing" + + g "github.com/maragudk/gomponents" + . "github.com/maragudk/gomponents/components" + . "github.com/maragudk/gomponents/html" + "github.com/maragudk/gomponents/internal/assert" +) + +func TestHTML5(t *testing.T) { + t.Run("returns an html5 document template", func(t *testing.T) { + e := HTML5(HTML5Props{ + Title: "Hat", + Description: "Love hats.", + Language: "en", + Head: []g.Node{Link(Rel("stylesheet"), Href("/hat.css"))}, + Body: []g.Node{Div()}, + }) + + assert.Equal(t, `