Skip to content

Commit

Permalink
searchbutton can be deactive to not react to clicks
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasHiltl committed Nov 23, 2024
1 parent d733cc7 commit f91dfbd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 19 deletions.
16 changes: 11 additions & 5 deletions components/search.templ
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,29 @@ templ emptySearchResults(query string) {
}

type SearchButtonArgs struct {
Show bool
HasMetaKey bool
Active bool // whether the click should open the search modal
}

templ SearchButton(args SearchButtonArgs) {
@searchDialog(args)
if args.Active {
@searchDialog(args)
}
<button
class="o-flex o-items-center o-gap-2 o-py-1 o-px-1 sm:o-px-2 o-rounded
sm:o-text-black/40 sm:hover:o-text-black/60 hover:o-bg-black/5
dark:o-text-white/50 dark:hover:o-text-white/70 dark:o-bg-white/5 hover:dark:o-bg-white/5"
hx-on::trigger="toggleSearchDialog()"
hx-trigger="click, keyup[ctrlKey&&key=='k'] from:body, keydown[metaKey&&key=='k'] from:body"
if args.Active {
hx-on::trigger="toggleSearchDialog()"
hx-trigger="click, keyup[ctrlKey&&key=='k'] from:body, keydown[metaKey&&key=='k'] from:body"
}
>
@icons.Search(16, 16)
<p class="o-text-sm o-leading-none o-hidden sm:o-block">Search...</p>
@searchKbd(args.HasMetaKey)
</button>
<script>
if args.Active {
<script>
function toggleSearchDialog() {
const dialog = document.getElementById('search-dialog')
if (!dialog) return
Expand All @@ -101,6 +106,7 @@ templ SearchButton(args SearchButtonArgs) {
}
}
</script>
}
}

templ searchDialog(args SearchButtonArgs) {
Expand Down
30 changes: 24 additions & 6 deletions components/search_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/handler/web/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ func (r *renderer) RenderChangelog(ctx context.Context, w io.Writer, args Render
RSSArgs: components.RSSArgs{
FeedURL: args.FeedURL,
},
ShowSearchButton: args.CL.Searchable,
SearchButtonArgs: components.SearchButtonArgs{
Show: args.CL.Searchable,
Active: true,
HasMetaKey: args.HasMetaKey,
},
ChangelogContainerArgs: components.ChangelogContainerArgs{
Expand Down Expand Up @@ -138,8 +139,9 @@ func (r *renderer) RenderDetails(ctx context.Context, w io.Writer, args RenderDe
RSSArgs: components.RSSArgs{
FeedURL: args.FeedURL,
},
ShowSearchButton: args.CL.Searchable,
SearchButtonArgs: components.SearchButtonArgs{
Show: args.CL.Searchable,
Active: true,
HasMetaKey: args.HasMetaKey,
},
MainArgs: layout.MainArgs{
Expand Down
3 changes: 2 additions & 1 deletion internal/handler/web/views/details.templ
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type DetailsArgs struct {
Prev components.ArticleArgs
Next components.ArticleArgs
components.FooterArgs
ShowSearchButton bool
components.SearchButtonArgs
}

Expand All @@ -30,7 +31,7 @@ templ Details(arg DetailsArgs) {
@components.Navbar() {
@components.LogoImg(arg.Logo)
@components.NavbarActions() {
if arg.SearchButtonArgs.Show {
if arg.ShowSearchButton {
@components.SearchButton(arg.SearchButtonArgs)
}
@components.RSS(arg.RSSArgs)
Expand Down
7 changes: 4 additions & 3 deletions internal/handler/web/views/details_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/handler/web/views/index.templ
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type IndexArgs struct {
components.HeaderArgs
components.ArticleListArgs
components.FooterArgs
ShowSearchButton bool
components.SearchButtonArgs
}

Expand All @@ -28,7 +29,7 @@ templ Index(arg IndexArgs) {
@components.Navbar() {
@components.LogoImg(arg.Logo)
@components.NavbarActions() {
if arg.SearchButtonArgs.Show {
if arg.ShowSearchButton {
@components.SearchButton(arg.SearchButtonArgs)
}
@components.RSS(arg.RSSArgs)
Expand Down
3 changes: 2 additions & 1 deletion internal/handler/web/views/index_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f91dfbd

Please sign in to comment.