Skip to content

Commit d39c0fb

Browse files
committed
favicon: add light & dark option for OS's prefered color scheme matcornic#549
1 parent cba1d38 commit d39c0fb

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

exampleSite/content/basics/customization/_index.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The size of the logo will adapt automatically
4343

4444
If your favicon is a SVG, PNG or ICO, just drop off your image in your local `static/images/` folder and name it `favicon.svg`, `favicon.png` or `favicon.ico` respectively.
4545

46-
Alternatively, if you want your site to use light & dark theme SVG favicons that follow the OS' (and in some cases, the browser's) color scheme, add the image files to your local `static/images/` folder and name them `favicon-light.svg` and `favicon-dark.svg` respectively.
46+
Additionally, if you want your site to use light & dark theme favicons that follow the OS' (and in some cases, the browser's) color scheme, add the image files to your local `static/images/` folder and name them eg. `favicon-light.svg` and/or `favicon-dark.svg` respectively corresponding to your file format. In case one of the files is missing, the theme falls back to eg. `favicon.svg` for the missing file. All supplied favicons must be of the same file format.
4747

4848
{{% notice warning %}}
4949
IE and old browser versions do not support [media queries](https://caniuse.com/css-media-interaction), which are necessary for the light & dark theme favicon option.

exampleSite/content/basics/migration/_index.en.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ This document shows you what's new in the latest release. For a detailed list of
2424

2525
Additionally the `name` parameter was renamed to `title`. You don't need to change anything yet as the old name will be used as a fallback but you will get deprecation warnings while executing Hugo.
2626

27+
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The theme now supports [separate favicons]({{%relref "basics/customization/#change-the-favicon" %}}) for light & dark mode optionally.
28+
2729
---
2830

2931
## 5.15.0 (2023-05-29)

layouts/partials/favicon.html

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,32 @@
11
{{- $assetBusting := not .Site.Params.disableAssetsBusting }}
22
{{- $faviconMatch := false }}
3-
{{- $svg := dict "ext" "svg" "type" "image/svg+xml" }}
4-
{{- $png := dict "ext" "png" "type" "image/png" }}
5-
{{- $ico := dict "ext" "ico" "type" "image/x-icon" }}
3+
{{- $svg := dict "ext" "svg" "type" "type=\"image/svg+xml\"" }}
4+
{{- $png := dict "ext" "png" "type" "type=\"image/png\"" }}
5+
{{- $ico := dict "ext" "ico" "type" "type=\"image/x-icon\" sizes=\"any\"" }}
66
{{- $faviconTypes := slice $svg $png $ico }}
7-
{{- range $faviconTypes }}
8-
{{- if and
9-
(eq $faviconMatch false)
10-
(or
11-
(fileExists (printf "/static/images/favicon-light.%s" .ext))
12-
(fileExists (printf "/static/images/favicon-dark.%s"))
13-
)
14-
}}
15-
{{- $faviconMatch = true }}
16-
{{$c:=""}}{{/*
7+
{{- $faviconNames := slice "favicon" "logo" }}
8+
{{- range $faviconNames }}
9+
{{- $faviconName := . }}
10+
{{- range $faviconTypes }}
11+
{{- if fileExists (printf "/static/images/%s.%s" $faviconName .ext) }}
12+
{{- $faviconMatch = true }}
13+
<link href="{{ printf "images/%s.%s" $faviconName .ext | relURL }}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="icon" {{ .type | safeHTMLAttr }}>
14+
{{- end }}
15+
{{- $c:="" }}{{/*
1716
Warning: IE and old browser versions do not support media queries necessary for the light & dark theme option.
1817
If you have requirements to support IE and/or older browser versions, use one of the other options.
1918
Reference: https://caniuse.com/css-media-interaction
2019
*/}}
21-
{{- if (fileExists (printf "/static/images/favicon-light.%s" .ext)) }}
22-
<link href="{{ printf "images/favicon-light.%s" .ext | relURL }}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="icon" type="{{ .type }}" media="(prefers-color-scheme: light)">
20+
{{- if (fileExists (printf "/static/images/%s-light.%s" $faviconName .ext)) }}
21+
{{- $faviconMatch = true }}
22+
<link href="{{ printf "images/%s-light.%s" $faviconName .ext | relURL }}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="icon" {{ .type | safeHTMLAttr }} media="(prefers-color-scheme: light)">
2323
{{- end }}
24-
{{- if (fileExists (printf "/static/images/favicon-dark.%s" .ext)) }}
25-
<link href="{{ printf "images/favicon-dark.%s" .ext | relURL }}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="icon" type="{{ .type }}" media="(prefers-color-scheme: dark)">
24+
{{- if (fileExists (printf "/static/images/%s-dark.%s" $faviconName .ext)) }}
25+
{{- $faviconMatch = true }}
26+
<link href="{{ printf "images/%s-dark.%s" $faviconName .ext | relURL }}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="icon" {{ .type | safeHTMLAttr }} media="(prefers-color-scheme: dark)">
2627
{{- end }}
2728
{{- end }}
28-
{{- end }}
29-
{{- range $faviconTypes }}
30-
{{- if and
31-
(eq $faviconMatch false)
32-
(fileExists (printf "/static/images/favicon.%s" .ext))
33-
}}
34-
{{- $faviconMatch = true }}
35-
<link href="{{ printf "images/favicon.%s" .ext | relURL }}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="icon" type="{{ .type }}">
36-
{{- end }}
37-
{{- end }}
38-
{{- range $faviconTypes }}
39-
{{- if and
40-
(eq $faviconMatch false)
41-
(fileExists (printf "/static/images/logo.%s" .ext))
42-
}}
43-
{{- $faviconMatch := true }}
44-
<link href="{{ printf "images/logo.%s" .ext | relURL }}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="icon" type="{{ .type }}">
29+
{{- if $faviconMatch }}
30+
{{- break }}
4531
{{- end }}
4632
{{- end }}

0 commit comments

Comments
 (0)