Skip to content

Commit

Permalink
🎉 Feat: add reading progress bar support (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Sep 9, 2022
1 parent 0659a17 commit 995f955
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
> This version fixes several bugs, adds a few new features and SEO optimizations, and refactors part of the project structure and code.
> Snapshot: <>
- :tada: Feat: add reading progress bar support ([#191](https://github.com/hugo-fixit/FixIt/issues/191))
- :sparkles: Feat: add `raw` shortcode
- :sparkles: Feat(menu): add params: `icon`, `type` for menu items
- :sparkles: Feat: add custom aside template in post page ([#172](https://github.com/hugo-fixit/FixIt/issues/172))
Expand Down
1 change: 1 addition & 0 deletions assets/css/_partials/_widgets.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import '_widgets/cookieconsent';
@import '_widgets/fixed-button';
@import '_widgets/github-corner';
@import '_widgets/reading-progress';
@import '_widgets/typeit';
11 changes: 11 additions & 0 deletions assets/css/_partials/_widgets/_reading-progress.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.reading-progress-bar {
--progress: 0;
width: var(--progress);
background-color: var(--bg-progress, $info);
z-index: 150;
position: fixed;

[data-theme="dark"] & {
background-color: var(--bg-progress-dark, darken($info, 5%));
}
}
2 changes: 1 addition & 1 deletion assets/js/theme.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/theme.min.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,17 @@
# Gravatar host, default: "www.gravatar.com"
host = "www.gravatar.com" # "cn.gravatar.com", "gravatar.loli.net"
style = "" # "", mp, identicon, monsterid, wavatar, retro, blank, robohash
# FixIt 0.2.16 | NEW Reading progress bar
[params.readingProgress]
enable = false
# Available values: ["left", "right"]
start = "left"
# Available values: ["top", "bottom"]
position = "top"
reversed = false
light = ""
dark = ""
height = "2px"
# FixIt 0.2.15 | NEW Developer options
[params.dev]
enable = false
Expand Down
2 changes: 1 addition & 1 deletion docs
13 changes: 13 additions & 0 deletions layouts/partials/widgets.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@

{{- /* Search mask */ -}}
<div id="mask"></div>

{{- /* Reading progress bar */ -}}
{{- $readingProgress := .Site.Params.readingProgress -}}
{{- if $readingProgress.enable -}}
{{- with $readingProgress -}}
<div class="reading-progress-bar" style="
{{- if .Reversed -}}width: calc(100% - var(--progress));{{- end -}}
{{- if .Light -}}--bg-progress: {{ .Light }};{{- end -}}
{{- if .Dark -}}--bg-progress-dark: {{ .Dark }};{{- end -}}
{{- .Start | default "left" -}}: 0;
{{- .Position | default "top" -}}: 0;height: {{ .Height | default "2px" }};"></div>
{{- end -}}
{{- end -}}
</div>
6 changes: 6 additions & 0 deletions src/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ class FixIt {
this.util.scrollIntoView('body');
});
const $fixedButtons = document.querySelector('.fixed-buttons');
const $readingProgressBar = document.querySelector('.reading-progress-bar');
const ACCURACY = 20, MINIMUM = 100;
window.addEventListener('scroll', (event) => {
if (this.disableScrollEvent) {
Expand Down Expand Up @@ -1053,6 +1054,11 @@ class FixIt {
$fixedButtons.classList.contains('animate__fadeOut') && $fixedButtons.classList.add('d-none');
});
}
if ($readingProgressBar) {
const contentHeight = document.body.scrollHeight - window.innerHeight;
const scrollPercent = contentHeight > 0 ? Math.min(100 * window.scrollY / contentHeight, 100) : 0;
$readingProgressBar.style.setProperty('--progress', `${scrollPercent.toFixed(2)}%`);
}
for (let event of this.scrollEventSet) {
event();
}
Expand Down

0 comments on commit 995f955

Please sign in to comment.