Skip to content

Commit

Permalink
Table of Contents toggle (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Jan 25, 2021
1 parent 05927a4 commit 4558d3c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
48 changes: 37 additions & 11 deletions client/message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import OverlayTrigger from 'react-bootstrap/OverlayTrigger'
import Tooltip from 'react-bootstrap/Tooltip'
import {useTracker} from 'meteor/react-meteor-data'
import Blaze from 'meteor/gadicc:blaze-react-component'
import {useMediaQuery} from 'react-responsive'
import useEventListener from '@use-it/event-listener'

import {Credits} from './layout.coffee'
Expand Down Expand Up @@ -207,6 +208,17 @@ Message = React.memo ({message}) ->
undefined
, [message.title]

## Display table of contents according to whether screen is at least
## Bootstrap 3's "sm" size, but allow user to override.
isScreenSm = useMediaQuery query: '(min-width: 768px)'
[toc, setToc] = useState()
useLayoutEffect ->
setToc isScreenSm
, [isScreenSm]
onToc = (e) ->
e.preventDefault()
setToc not toc

## Set sticky column height to remaining height of screen:
## 100vh when stuck, and less when header is (partly) visible.
stickyRef = useRef()
Expand All @@ -215,22 +227,36 @@ Message = React.memo ({message}) ->
stickyRef.current.style.height = "calc(100vh - #{rect.y}px)" if rect.height
undefined
, 100), []
useEffect stickyHeight, [] # initialize
useEffect stickyHeight, [toc] # initialize
useEventListener 'resize', stickyHeight
useEventListener 'scroll', stickyHeight

<div className="row">
<div className="col-sm-9" role="main">
<MaybeRootHeader message={message}/>
<Submessage message={message}/>
<MessageInfoBoxes message={message}/>
<Credits/>
<>
<div className="hidden-print text-right toc-toggle">
<a href="#" onClick={onToc}>
{if toc
<span className="fas fa-caret-down"/>
else
<span className="fas fa-caret-right"/>
}
{' Table of Contents'}
</a>
</div>
<div className="col-sm-3 hidden-print hidden-xs sticky-top"
role="complementary" ref={stickyRef}>
<TableOfContentsID messageID={message._id}/>
<div className="row">
<div className={if toc then "col-xs-9" else "col-xs-12"} role="main">
<MaybeRootHeader message={message}/>
<Submessage message={message}/>
<MessageInfoBoxes message={message}/>
<Credits/>
</div>
{if toc
<div className="col-xs-3 hidden-print sticky-top"
role="complementary" ref={stickyRef}>
<TableOfContentsID messageID={message._id}/>
</div>
}
</div>
</div>
</>
Message.displayName = 'Message'

MessageInfoBoxes = React.memo ({message}) ->
Expand Down
3 changes: 3 additions & 0 deletions client/message.styl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ bootstrap-3-button(bg-color) // based on mixins/buttons.less

// TableOfContents

.toc-toggle
margin-top: -19.39px // rendered height, putting exactly into margin

nav.contents
padding-bottom: 5px
height: 100% // match parent height set in JavaScript
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"react-bootstrap-typeahead": "^5.1.4",
"react-dom": "^17.0.1",
"react-intersection-observer": "^8.31.0",
"react-responsive": "^8.2.0",
"roman-numeral": "^0.2.6",
"sanitize-html": "^1.27.5"
},
Expand Down

0 comments on commit 4558d3c

Please sign in to comment.