theme: add a theme selector #178

This commit is contained in:
Sören Weber 2022-02-13 01:53:23 +01:00
parent a32646b5cb
commit 8c1a7304f9
No known key found for this signature in database
GPG key ID: 07D17FF580AE7589
11 changed files with 96 additions and 21 deletions

View file

@ -36,7 +36,7 @@ title = "Hugo Relearn Documentation"
disableLandingPageButton = true disableLandingPageButton = true
disableMermaid = false disableMermaid = false
titleSeparator = "::" titleSeparator = "::"
themeVariant = "relearn-light" themeVariant = [ "relearn-light", "relearn-dark", "learn", "blue", "green", "red" ]
disableSeoHiddenPages = true disableSeoHiddenPages = true
[outputs] [outputs]

View file

@ -60,7 +60,7 @@ Note that some of these parameters are explained in details in other sections of
# Order sections in menu by "weight" or "title". Default to "weight"; # Order sections in menu by "weight" or "title". Default to "weight";
# this can be overridden in the pages frontmatter # this can be overridden in the pages frontmatter
ordersectionsby = "weight" ordersectionsby = "weight"
# Change default color scheme with a variant one. Can be "red", "blue", "green". # Change default color scheme with a variant one. Can be "red", "blue", "green" or an array like [ "blue", "green" ].
themeVariant = "" themeVariant = ""
# Provide a list of custom css files to load relative from the `static/` folder in the site root. # Provide a list of custom css files to load relative from the `static/` folder in the site root.
custom_css = ["css/foo.css", "css/bar.css"] custom_css = ["css/foo.css", "css/bar.css"]

View file

@ -53,7 +53,7 @@ The Relearn theme let you choose between some predefined color scheme variants,
```toml ```toml
[params] [params]
# Change default color scheme with a variant one. Can be empty, "red", "blue", "green". # Change default color scheme with a variant one. Can be empty, "red", "blue", "green" or an array like [ "blue", "green" ].
themeVariant = "" themeVariant = ""
``` ```
@ -63,7 +63,7 @@ The Relearn theme let you choose between some predefined color scheme variants,
```toml ```toml
[params] [params]
# Change default color scheme with a variant one. Can be empty, "red", "blue", "green". # Change default color scheme with a variant one. Can be empty, "red", "blue", "green" or an array like [ "blue", "green" ].
themeVariant = "red" themeVariant = "red"
``` ```
@ -73,7 +73,7 @@ The Relearn theme let you choose between some predefined color scheme variants,
```toml ```toml
[params] [params]
# Change default color scheme with a variant one. Can be empty, "red", "blue", "green". # Change default color scheme with a variant one. Can be empty, "red", "blue", "green" or an array like [ "blue", "green" ].
themeVariant = "blue" themeVariant = "blue"
``` ```
@ -83,7 +83,7 @@ The Relearn theme let you choose between some predefined color scheme variants,
```toml ```toml
[params] [params]
# Change default color scheme with a variant one. Can be empty, "red", "blue", "green". # Change default color scheme with a variant one. Can be empty, "red", "blue", "green" or an array like [ "blue", "green" ].
themeVariant = "green" themeVariant = "green"
``` ```
@ -137,6 +137,17 @@ Then, set the `themeVariant` value with the name of your custom theme file. That
```toml ```toml
[params] [params]
# Change default color scheme with a variant one. Can be "red", "blue", "green". # Change default color scheme with a variant one. Can be "red", "blue", "green" or an array like [ "blue", "green" ].
themeVariant = "mine" themeVariant = "mine"
``` ```
### Multiple variants
You can also set multiple variants. In this case, the first variant is the default choosen on first view and a theme selector will be shown in the menu footer.
```toml
[params]
# Change default color scheme with a variant one. Can be empty, "red", "blue", "green" or an array like [ "blue", "green" ].
themeVariant = [ "red", "blue ]
```

View file

@ -20,6 +20,8 @@ This document shows you what's new in the latest release. For a detailed list of
This change was made to allow your own menu footer to be placed right after the so called prefooter that comes with the theme (containing the language switch and *Clear history* functionality). This change was made to allow your own menu footer to be placed right after the so called prefooter that comes with the theme (containing the language switch and *Clear history* functionality).
- **New**: You can also set multiple variants. In this case, the first variant is the default choosen on first view and a theme selector will be shown in the menu footer. See the [documentation]({{%relref "basics/customization/#multiple-variants" %}}) for configuration.
## 2.9.0 ## 2.9.0
- **Breaking**: This release removes the themes implementation of `ref`/`relref` in favor for Hugos standard implementation. This is because of inconsistencies with the themes implementation. In advantage, your project becomes standard complient and exchanging this theme in your project to some other theme will be effortless. - **Breaking**: This release removes the themes implementation of `ref`/`relref` in favor for Hugos standard implementation. This is because of inconsistencies with the themes implementation. In advantage, your project becomes standard complient and exchanging this theme in your project to some other theme will be effortless.

View file

@ -23,7 +23,7 @@
{{- partial "custom-header.html" . }} {{- partial "custom-header.html" . }}
</head> </head>
<body class="" data-url="/"> <body class="" data-url="/">
<div id="body" style="margin-left:0px;"> <div id="body default-animation" style="margin-left:0px;">
<div id="overlay"></div> <div id="overlay"></div>
<div id="chapter"> <div id="chapter">
<main id="body-inner"> <main id="body-inner">

View file

@ -8,10 +8,10 @@
{{- $showPrevNext := (and (not .Params.disableNextPrev) (not .Site.Params.disableNextPrev)) }} {{- $showPrevNext := (and (not .Params.disableNextPrev) (not .Site.Params.disableNextPrev)) }}
{{- if $showPrevNext }} {{- if $showPrevNext }}
{{- with ($.Scratch.Get "relearnPrevPage") }} {{- with ($.Scratch.Get "relearnPrevPage") }}
<a class="nav nav-prev" href="{{.RelPermalink}}" title="{{.Title}}"><i class="fa fa-chevron-left"></i></a> <a class="nav nav-prev default-animation" href="{{.RelPermalink}}" title="{{.Title}}"><i class="fa fa-chevron-left"></i></a>
{{- end}} {{- end}}
{{- with ($.Scratch.Get "relearnNextPage") }} {{- with ($.Scratch.Get "relearnNextPage") }}
<a class="nav nav-next" href="{{.RelPermalink}}" title="{{.Title}}"><i class="fa fa-chevron-right"></i></a> <a class="nav nav-next default-animation" href="{{.RelPermalink}}" title="{{.Title}}"><i class="fa fa-chevron-right"></i></a>
{{- end }} {{- end }}
{{- end }} {{- end }}
</div> </div>

View file

@ -26,7 +26,7 @@
</head> </head>
<body class="" data-url="{{ .RelPermalink }}"> <body class="" data-url="{{ .RelPermalink }}">
{{- partial "menu.html" . }} {{- partial "menu.html" . }}
<div id="body"> <div id="body" class="default-animation">
<div id="overlay"></div> <div id="overlay"></div>
<div class="padding highlightable"> <div class="padding highlightable">
{{- if not .IsHome }} {{- if not .IsHome }}

View file

@ -6,7 +6,7 @@
var root_url="/"; var root_url="/";
var baseUri=root_url.replace(/\/$/, ''); var baseUri=root_url.replace(/\/$/, '');
</script> </script>
<nav id="sidebar" class="{{ if $.Site.Params.showVisitedLinks }}showVisitedLinks{{ end }}"> <nav id="sidebar" class="default-animation {{ if $.Site.Params.showVisitedLinks }}showVisitedLinks{{ end }}">
{{- $currentNode := . }} {{- $currentNode := . }}
{{- $showvisitedlinks := .Site.Params.showVisitedLinks }} {{- $showvisitedlinks := .Site.Params.showVisitedLinks }}
<div id="header-wrapper"> <div id="header-wrapper">
@ -54,12 +54,14 @@
{{- end }} {{- end }}
<div class="footermargin"></div> <div class="footermargin"></div>
{{- $showlangswitch := and .Site.IsMultiLingual (not .Site.Params.disableLanguageSwitchingButton) }} {{- $showlangswitch := and .Site.IsMultiLingual (not .Site.Params.disableLanguageSwitchingButton) }}
{{- $themevariants := slice | append .Site.Params.themeVariant }}
{{- $showthemeswitch := gt (int (len $themevariants)) 1 }}
{{- $footer := partial "menu-footer.html" . }} {{- $footer := partial "menu-footer.html" . }}
{{- $showfooter := not (eq 0 (int (len $footer | plainify))) }} {{- $showfooter := not (eq 0 (int (len $footer | plainify))) }}
{{- if or $showlangswitch $showvisitedlinks $showfooter }} {{- if or $showlangswitch $showvisitedlinks $showfooter }}
<hr/> <hr/>
{{- end }} {{- end }}
{{- if or $showlangswitch $showvisitedlinks }} {{- if or $showlangswitch $showthemeswitch $showvisitedlinks }}
<div id="prefooter"> <div id="prefooter">
<ul> <ul>
{{- if $showlangswitch }} {{- if $showlangswitch }}
@ -87,6 +89,28 @@
</a> </a>
</li> </li>
{{- end }} {{- end }}
{{- if $showthemeswitch }}
<li>
<a class="padding select-container">
<i class="fas fa-paint-brush fa-fw"></i>
<div class="select-style">
<select id="select-theme" onchange="changeTheme( this.value );">
{{- $firsttheme := true }}
{{- range $themevariants }}
{{- $themevariant := . }}
{{- $themename := replaceRE "[-_]+" " " $themevariant }}
{{- if $firsttheme }}
{{- $firsttheme = false }}
<option id="{{ $themevariant }}" value="{{ $themevariant }}" selected>{{ $themename }}</option>
{{- else }}
<option id="{{ $themevariant }}" value="{{ $themevariant }}">{{ $themename }}</option>
{{- end }}
{{- end }}
</select>
</div>
</a>
</li>
{{- end }}
{{- if $showvisitedlinks }} {{- if $showvisitedlinks }}
<li><a class="padding" href="#" data-clear-history-toggle=""><i class="fas fa-history fa-fw"></i> {{ T "Clear-History" }}</a></li> <li><a class="padding" href="#" data-clear-history-toggle=""><i class="fas fa-history fa-fw"></i> {{ T "Clear-History" }}</a></li>
{{- end }} {{- end }}

View file

@ -5,8 +5,9 @@
<link href="{{"css/perfect-scrollbar.min.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet"> <link href="{{"css/perfect-scrollbar.min.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
<link href="{{"css/auto-complete.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet"> <link href="{{"css/auto-complete.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
<link href="{{"css/theme.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet"> <link href="{{"css/theme.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
{{- with .Site.Params.themeVariant }} {{- $themevariants := slice | append .Site.Params.themeVariant }}
<link href="{{(printf "css/theme-%s.css" .) | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet"> {{- with index $themevariants 0 }}
<link id="variant-style" href="{{(printf "css/theme-%s.css" .) | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
<link href="{{"css/variant.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet"> <link href="{{"css/variant.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
{{- end }} {{- end }}
<link href="{{"css/print.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet" media="print"> <link href="{{"css/print.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet" media="print">

View file

@ -78,12 +78,7 @@ th {
font-weight: 600; font-weight: 600;
} }
.default-animation, .default-animation{
#sidebar,
#sidebar ul,
#body,
#body .padding,
#body .nav {
-webkit-transition: all 0.5s ease; -webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease; -moz-transition: all 0.5s ease;
transition: all 0.5s ease; transition: all 0.5s ease;
@ -1419,6 +1414,10 @@ h6 a {
cursor: pointer; cursor: pointer;
} }
.select-style #select-theme {
text-transform: capitalize;
}
@media only all and (max-width: 47.938em) { @media only all and (max-width: 47.938em) {
#breadcrumbs .links, #top-github-link-text { #breadcrumbs .links, #top-github-link-text {
display: none; display: none;

View file

@ -1,3 +1,41 @@
var theme = window.localStorage.getItem( 'theme' );
changeTheme( theme, true );
function changeTheme( theme, noanimation ){
if( !theme ){
return;
}
var link = document.querySelector( '#variant-style' );
var old_path = link.getAttribute( 'href' );
var new_path = old_path.replace( /^(.*\/theme-).*?(\.css.*)$/, '$1' + theme + '$2' );
if( old_path != new_path ){
var animations = document.querySelectorAll( '.default-animation' );
if( noanimation ){
animations.forEach( function( e ){
e.classList.remove( 'default-animation' );
});
}
window.localStorage.setItem( 'theme', theme );
link.setAttribute( 'href', new_path );
var select = document.querySelector( '#select-theme' );
select.value = theme;
// we have to wait to reset the animation style
if( noanimation ){
setTimeout( function(){
animations.forEach( function( e ){
e.classList.add( 'default-animation' );
});
}, 525 );
}
// remove selection, because if some uses an arrow navigation
// by pressing the left or right cursor key, we will automatically
// select a different style
if( document.activeElement ){
document.activeElement.blur();
}
}
}
// Scrollbar Width function // Scrollbar Width function
function getScrollBarWidth() { function getScrollBarWidth() {
var inner = document.createElement('p'); var inner = document.createElement('p');