From 29e8ea2a5d649a74d708fb86277db042cf0587f8 Mon Sep 17 00:00:00 2001 From: indaco Date: Tue, 30 Apr 2024 12:00:57 +0200 Subject: [PATCH] refactor: new gropdown.Item api --- _examples/a-h-templ/home.templ | 29 ++++++++++++++++++--- _examples/custom-animations/home.templ | 29 ++++++++++++++++++--- _examples/custom-button-icon/home.templ | 29 ++++++++++++++++++--- _examples/go-html-template/demo.templ | 29 ++++++++++++++++++--- _examples/icon-only-button/home.templ | 29 ++++++++++++++++++--- _examples/positioning/home.templ | 29 ++++++++++++++++++--- _examples/theming/home.templ | 29 ++++++++++++++++++--- gropdown-button.templ | 13 +--------- gropdown-button_templ.go | 21 ++++----------- gropdown-content-item.templ | 12 ++++----- gropdown-content-item_templ.go | 12 ++++----- types.go | 14 ++++++++-- utils.go | 34 +++++++++++++++++++++++++ 13 files changed, 239 insertions(+), 70 deletions(-) diff --git a/_examples/a-h-templ/home.templ b/_examples/a-h-templ/home.templ index d33acaa..7bedfa9 100644 --- a/_examples/a-h-templ/home.templ +++ b/_examples/a-h-templ/home.templ @@ -47,12 +47,33 @@ templ HomePage() { @gropdown.Root("demo") { @gropdown.Button("Menu") @gropdown.Content() { - @gropdown.Item(gropdown.DropdownItem{Label: "Profile", Href: "/profile", Icon: profileIcon}) - @gropdown.Item(gropdown.DropdownItem{Label: "Settings", Href: "/settings", Icon: settingsIcon}) + @gropdown.Item("Profile", + gropdown.ItemOptions{ + Href: "/profile", + Icon: profileIcon, + }, + ) + @gropdown.Item("Settings", + gropdown.ItemOptions{ + Href: "/settings", + Icon: settingsIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "GitHub", Href: "https://github.com", External: true, Icon: globeIcon}) + @gropdown.Item("GitHub", + gropdown.ItemOptions{ + Href: "https://github.com", + External: true, + Icon: globeIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "Button", Icon: clickIcon, Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}}) + @gropdown.Item("Button", + gropdown.ItemOptions{ + Icon: clickIcon, + Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}, + }, + ) } } diff --git a/_examples/custom-animations/home.templ b/_examples/custom-animations/home.templ index 4819f45..fa29390 100644 --- a/_examples/custom-animations/home.templ +++ b/_examples/custom-animations/home.templ @@ -60,12 +60,33 @@ templ HomePage() { @gropdown.Root("demo") { @gropdown.Button("Menu") @gropdown.Content() { - @gropdown.Item(gropdown.DropdownItem{Label: "Profile", Href: "/profile", Icon: profileIcon}) - @gropdown.Item(gropdown.DropdownItem{Label: "Settings", Href: "/settings", Icon: settingsIcon}) + @gropdown.Item("Profile", + gropdown.ItemOptions{ + Href: "/profile", + Icon: profileIcon, + }, + ) + @gropdown.Item("Settings", + gropdown.ItemOptions{ + Href: "/settings", + Icon: settingsIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "GitHub", Href: "https://github.com", External: true, Icon: globeIcon}) + @gropdown.Item("GitHub", + gropdown.ItemOptions{ + Href: "https://github.com", + External: true, + Icon: globeIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "Button", Icon: clickIcon, Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}}) + @gropdown.Item("Button", + gropdown.ItemOptions{ + Icon: clickIcon, + Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}, + }, + ) } } diff --git a/_examples/custom-button-icon/home.templ b/_examples/custom-button-icon/home.templ index 3cfa1b6..dd8b623 100644 --- a/_examples/custom-button-icon/home.templ +++ b/_examples/custom-button-icon/home.templ @@ -51,12 +51,33 @@ templ HomePage() { @gropdown.Root("demo") { @gropdown.Button("Menu", gropdown.Icon(buttonIcon)) @gropdown.Content() { - @gropdown.Item(gropdown.DropdownItem{Label: "Profile", Href: "/profile", Icon: profileIcon}) - @gropdown.Item(gropdown.DropdownItem{Label: "Settings", Href: "/settings", Icon: settingsIcon}) + @gropdown.Item("Profile", + gropdown.ItemOptions{ + Href: "/profile", + Icon: profileIcon, + }, + ) + @gropdown.Item("Settings", + gropdown.ItemOptions{ + Href: "/settings", + Icon: settingsIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "GitHub", Href: "https://github.com", External: true, Icon: globeIcon}) + @gropdown.Item("GitHub", + gropdown.ItemOptions{ + Href: "https://github.com", + External: true, + Icon: globeIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "Button", Icon: clickIcon, Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}}) + @gropdown.Item("Button", + gropdown.ItemOptions{ + Icon: clickIcon, + Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}, + }, + ) } } diff --git a/_examples/go-html-template/demo.templ b/_examples/go-html-template/demo.templ index 8697132..6a78363 100644 --- a/_examples/go-html-template/demo.templ +++ b/_examples/go-html-template/demo.templ @@ -26,12 +26,33 @@ templ GropdownDemo() { @gropdown.Root("demo") { @gropdown.Button("Menu") @gropdown.Content() { - @gropdown.Item(gropdown.DropdownItem{Label: "Profile", Href: "/profile", Icon: profileIcon}) - @gropdown.Item(gropdown.DropdownItem{Label: "Settings", Href: "/settings", Icon: settingsIcon}) + @gropdown.Item("Profile", + gropdown.ItemOptions{ + Href: "/profile", + Icon: profileIcon, + }, + ) + @gropdown.Item("Settings", + gropdown.ItemOptions{ + Href: "/settings", + Icon: settingsIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "GitHub", Href: "https://github.com", External: true, Icon: globeIcon}) + @gropdown.Item("GitHub", + gropdown.ItemOptions{ + Href: "https://github.com", + External: true, + Icon: globeIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "Button", Icon: clickIcon, Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}}) + @gropdown.Item("Button", + gropdown.ItemOptions{ + Icon: clickIcon, + Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}, + }, + ) } } } diff --git a/_examples/icon-only-button/home.templ b/_examples/icon-only-button/home.templ index 4e9dd2f..263a8b3 100644 --- a/_examples/icon-only-button/home.templ +++ b/_examples/icon-only-button/home.templ @@ -47,12 +47,33 @@ templ HomePage() { @gropdown.Root("demo") { @gropdown.Button("") @gropdown.Content() { - @gropdown.Item(gropdown.DropdownItem{Label: "Profile", Href: "/profile", Icon: profileIcon}) - @gropdown.Item(gropdown.DropdownItem{Label: "Settings", Href: "/settings", Icon: settingsIcon}) + @gropdown.Item("Profile", + gropdown.ItemOptions{ + Href: "/profile", + Icon: profileIcon, + }, + ) + @gropdown.Item("Settings", + gropdown.ItemOptions{ + Href: "/settings", + Icon: settingsIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "GitHub", Href: "https://github.com", External: true, Icon: globeIcon}) + @gropdown.Item("GitHub", + gropdown.ItemOptions{ + Href: "https://github.com", + External: true, + Icon: globeIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "Button", Icon: clickIcon, Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}}) + @gropdown.Item("Button", + gropdown.ItemOptions{ + Icon: clickIcon, + Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}, + }, + ) } } diff --git a/_examples/positioning/home.templ b/_examples/positioning/home.templ index f436881..1df9c47 100644 --- a/_examples/positioning/home.templ +++ b/_examples/positioning/home.templ @@ -47,12 +47,33 @@ templ HomePage() { @gropdown.Root("demo") { @gropdown.Button("Menu") @gropdown.Content() { - @gropdown.Item(gropdown.DropdownItem{Label: "Profile", Href: "/profile", Icon: profileIcon}) - @gropdown.Item(gropdown.DropdownItem{Label: "Settings", Href: "/settings", Icon: settingsIcon}) + @gropdown.Item("Profile", + gropdown.ItemOptions{ + Href: "/profile", + Icon: profileIcon, + }, + ) + @gropdown.Item("Settings", + gropdown.ItemOptions{ + Href: "/settings", + Icon: settingsIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "GitHub", Href: "https://github.com", External: true, Icon: globeIcon}) + @gropdown.Item("GitHub", + gropdown.ItemOptions{ + Href: "https://github.com", + External: true, + Icon: globeIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "Button", Icon: clickIcon, Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}}) + @gropdown.Item("Button", + gropdown.ItemOptions{ + Icon: clickIcon, + Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}, + }, + ) } } diff --git a/_examples/theming/home.templ b/_examples/theming/home.templ index 0ebe9d7..2a60e9e 100644 --- a/_examples/theming/home.templ +++ b/_examples/theming/home.templ @@ -51,12 +51,33 @@ templ HomePage() { @gropdown.Root("demo") { @gropdown.Button("Menu") @gropdown.Content() { - @gropdown.Item(gropdown.DropdownItem{Label: "Profile", Href: "/profile", Icon: profileIcon}) - @gropdown.Item(gropdown.DropdownItem{Label: "Settings", Href: "/settings", Icon: settingsIcon}) + @gropdown.Item("Profile", + gropdown.ItemOptions{ + Href: "/profile", + Icon: profileIcon, + }, + ) + @gropdown.Item("Settings", + gropdown.ItemOptions{ + Href: "/settings", + Icon: settingsIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "GitHub", Href: "https://github.com", External: true, Icon: globeIcon}) + @gropdown.Item("GitHub", + gropdown.ItemOptions{ + Href: "https://github.com", + External: true, + Icon: globeIcon, + }, + ) @gropdown.Divider() - @gropdown.Item(gropdown.DropdownItem{Label: "Button", Icon: clickIcon, Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}}) + @gropdown.Item("Button", + gropdown.ItemOptions{ + Icon: clickIcon, + Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"}, + }, + ) } } diff --git a/gropdown-button.templ b/gropdown-button.templ index 367546e..c5e2a53 100644 --- a/gropdown-button.templ +++ b/gropdown-button.templ @@ -5,19 +5,8 @@ const defaultButtonIcon = ` ` -// getIcon If icons are provided, use the first one; otherwise, empty ButtonIcon struct. -func getIcon(icons []*ButtonIcon) *ButtonIcon { - var icon *ButtonIcon - if len(icons) > 0 { - icon = icons[0] - } else { - icon = &ButtonIcon{} - } - return icon -} - templ Button(label string, icons ...*ButtonIcon) { - @renderButton(label, getIcon(icons)) + @renderButton(label, getButtonIcon(icons)) } templ renderButton(label string, icon *ButtonIcon) { diff --git a/gropdown-button_templ.go b/gropdown-button_templ.go index 12bbf58..60870c7 100644 --- a/gropdown-button_templ.go +++ b/gropdown-button_templ.go @@ -15,17 +15,6 @@ const defaultButtonIcon = ` ` -// getIcon If icons are provided, use the first one; otherwise, empty ButtonIcon struct. -func getIcon(icons []*ButtonIcon) *ButtonIcon { - var icon *ButtonIcon - if len(icons) > 0 { - icon = icons[0] - } else { - icon = &ButtonIcon{} - } - return icon -} - func Button(label string, icons ...*ButtonIcon) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) @@ -39,7 +28,7 @@ func Button(label string, icons ...*ButtonIcon) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = renderButton(label, getIcon(icons)).Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = renderButton(label, getButtonIcon(icons)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -75,7 +64,7 @@ func renderButton(label string, icon *ButtonIcon) templ.Component { var templ_7745c5c3_Var4 string templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(buttonId(label)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `gropdown-button.templ`, Line: 25, Col: 22} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `gropdown-button.templ`, Line: 14, Col: 22} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { @@ -101,7 +90,7 @@ func renderButton(label string, icon *ButtonIcon) templ.Component { var templ_7745c5c3_Var6 string templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(label) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `gropdown-button.templ`, Line: 27, Col: 20} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `gropdown-button.templ`, Line: 16, Col: 20} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) if templ_7745c5c3_Err != nil { @@ -114,7 +103,7 @@ func renderButton(label string, icon *ButtonIcon) templ.Component { var templ_7745c5c3_Var7 string templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(menuId(label)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `gropdown-button.templ`, Line: 30, Col: 31} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `gropdown-button.templ`, Line: 19, Col: 31} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) if templ_7745c5c3_Err != nil { @@ -128,7 +117,7 @@ func renderButton(label string, icon *ButtonIcon) templ.Component { var templ_7745c5c3_Var8 string templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(label) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `gropdown-button.templ`, Line: 33, Col: 10} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `gropdown-button.templ`, Line: 22, Col: 10} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) if templ_7745c5c3_Err != nil { diff --git a/gropdown-content-item.templ b/gropdown-content-item.templ index 94bae4c..8b34dc7 100644 --- a/gropdown-content-item.templ +++ b/gropdown-content-item.templ @@ -2,17 +2,17 @@ package gropdown const defaultExternalLinkIcon = `` -templ Item(item DropdownItem) { +templ Item(label string, opts ...ItemOptions) {
  • - if item.Href != "" { - @linkItem(item) + if buildItemOptions(label, opts).Href != "" { + @linkItem(buildItemOptions(label, opts)) } else { - @buttonItem(item) + @buttonItem(buildItemOptions(label, opts)) }
  • } -templ linkItem(item DropdownItem) { +templ linkItem(item *DropdownItem) { } -templ buttonItem(item DropdownItem) { +templ buttonItem(item *DropdownItem) {