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

Add scroll visible util #197

Merged
merged 5 commits into from
Jul 24, 2020
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ ignoreHiddenElements: false,
headingObjectCallback: null,
// Set the base path, useful if you use a `base` tag in `head`.
basePath: '',
// Only takes affect when `tocSelector` is scrolling,
// keep the toc scroll position in sync with the content.
disableTocScrollSync: false,
```


Expand Down
2 changes: 1 addition & 1 deletion src/components/Template/TryIt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class TryIt extends React.Component {
backupHtml = backupHtml && backupHtml.innerHTML

return (
<div className={`try-it-container transition--300 fixed w-60 ma2 z-3 bottom-0 right-0 ${this.state.open ? 'is-open' : 'is-closed'}`}>
<div className={`try-it-container transition--300 fixed w-20 ma2 z-3 bottom-0 right-0 ${this.state.open ? 'is-open' : 'is-closed'}`}>
<div className='cb pb2'>
<button
className='button bn f6 link br1 ph3 pv2 mb2 dib white bg-dark-gray fr'
Expand Down
5 changes: 4 additions & 1 deletion src/js/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,8 @@ module.exports = {
// function (object, HTMLElement) => object | void
headingObjectCallback: null,
// Set the base path, useful if you use a `base` tag in `head`.
basePath: ''
basePath: '',
// Only takes affect when `tocSelector` is scrolling,
// keep the toc scroll position in sync with the content.
disableTocScrollSync: false,
}
2 changes: 2 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

var BuildHtml = require('./build-html.js')
var ParseContent = require('./parse-content.js')
var updateTocScroll = require('./update-toc-scroll.js')
// Keep these variables at top scope once options are passed in.
var buildHtml
var parseContent
Expand Down Expand Up @@ -161,6 +162,7 @@
// Update Sidebar and bind listeners.
this._scrollListener = throttle(function (e) {
buildHtml.updateToc(headingsArray)
!options.disableTocScrollSync && updateTocScroll(options)
var isTop = e && e.target && e.target.scrollingElement && e.target.scrollingElement.scrollTop === 0
if ((e && (e.eventPhase === 0 || e.currentTarget === null)) || isTop) {
buildHtml.updateToc(headingsArray)
Expand Down
9 changes: 9 additions & 0 deletions src/js/update-toc-scroll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function updateTocScroll(options) {
var toc = document.querySelector(options.tocSelector)
if (toc && toc.scrollHeight > toc.clientHeight) {
var activeItem = toc.querySelector('.' + options.activeListItemClass)
if (activeItem) {
toc.scrollTop = activeItem.offsetTop
}
}
}
26 changes: 14 additions & 12 deletions src/utils/make-ids.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
var content = document.querySelector('.js-toc-content')
var headings = content.querySelectorAll('h1, h2, h3, h4, h5, h6, h7')
var headingMap = {}
function makeIds () {
var content = document.querySelector('.js-toc-content')
var headings = content.querySelectorAll('h1, h2, h3, h4, h5, h6, h7')
var headingMap = {}

Array.prototype.forEach.call(headings, function (heading) {
var id = heading.id ? heading.id : heading.textContent.trim().toLowerCase()
Array.prototype.forEach.call(headings, function (heading) {
var id = heading.id ? heading.id : heading.textContent.trim().toLowerCase()
.split(' ').join('-').replace(/[!@#$%^&*():]/ig, '').replace(/\//ig, '-')
headingMap[id] = !isNaN(headingMap[id]) ? ++headingMap[id] : 0
if (headingMap[id]) {
heading.id = id + '-' + headingMap[id]
} else {
heading.id = id
}
})
headingMap[id] = !isNaN(headingMap[id]) ? ++headingMap[id] : 0
if (headingMap[id]) {
heading.id = id + '-' + headingMap[id]
} else {
heading.id = id
}
})
}