Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support theme toggle in navbar. #499

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion layouts/partials/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
{{- range .Site.Menus.main -}}
{{- if eq .Params.type "search" -}}
{{- partial "search.html" (dict "params" .Params) -}}
{{- else if eq .Params.type "theme-toggle" -}}
{{- partial "theme-toggle.html" (dict "navbar" true "hideLabel" (.Params.hideLabel | default true)) -}}
{{- else -}}
{{- $link := .URL -}}
{{- $external := strings.HasPrefix $link "http" -}}
Expand Down Expand Up @@ -67,4 +69,4 @@
{{- partial "utils/icon.html" (dict "name" "hamburger-menu" "attributes" "height=24") -}}
</button>
</nav>
</div>
</div>
13 changes: 10 additions & 3 deletions layouts/partials/theme-toggle.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{{- $navbar := .navbar | default false -}}
{{- $height_px := 12 -}}
{{- $height_tw := "hx-text-xs" -}}
{{- if $navbar -}}
{{- $height_px = 24 -}}
{{- $height_tw = "hx-text-sm" -}}
{{- end -}}
Comment on lines +2 to +7
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious where are 12 and 24 coming from?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question! I've been offline for a few weeks, and honestly, I'd have to say it is likely a best estimate based on testing that made things look "right."

If those values can be computed from other inputs in the theme configuration, I'm certainly willing to explore that. Do you have any guidance on how we might calculate a sane value for this?

{{- $hideLabel := .hideLabel | default false -}}

{{- $changeTheme := (T "changeTheme") | default "Change theme" -}}
Expand All @@ -8,14 +15,14 @@
<button
title="{{ $changeTheme }}"
data-theme="light"
class="theme-toggle hx-group hx-h-7 hx-rounded-md hx-px-2 hx-text-left hx-text-xs hx-font-medium hx-text-gray-600 hx-transition-colors dark:hx-text-gray-400 hover:hx-bg-gray-100 hover:hx-text-gray-900 dark:hover:hx-bg-primary-100/5 dark:hover:hx-text-gray-50"
class="theme-toggle hx-group hx-h-7 hx-rounded-md hx-px-2 hx-text-left {{ $height_tw }} hx-font-medium hx-text-gray-600 hx-transition-colors dark:hx-text-gray-400 hover:hx-bg-gray-100 hover:hx-text-gray-900 dark:hover:hx-bg-primary-100/5 dark:hover:hx-text-gray-50"
type="button"
aria-label="{{ $changeTheme }}"
>
<div class="hx-flex hx-items-center hx-gap-2 hx-capitalize">
{{- partial "utils/icon.html" (dict "name" "sun" "attributes" "height=12 class=\"group-data-[theme=light]:hx-hidden\"") -}}
{{- partial "utils/icon.html" (dict "name" "sun" "attributes" (printf "height=%d class=\"group-data-[theme=light]:hx-hidden\"" $height_px)) -}}
{{- if not $hideLabel }}<span class="group-data-[theme=light]:hx-hidden">{{ $light }}</span>{{ end -}}
{{- partial "utils/icon.html" (dict "name" "moon" "attributes" "height=12 class=\"group-data-[theme=dark]:hx-hidden\"") -}}
{{- partial "utils/icon.html" (dict "name" "moon" "attributes" (printf "height=%d class=\"group-data-[theme=dark]:hx-hidden\"" $height_px)) -}}
{{- if not $hideLabel }}<span class="group-data-[theme=dark]:hx-hidden">{{ $dark }}</span>{{ end -}}
</div>
</button>