-
Notifications
You must be signed in to change notification settings - Fork 50.2k
[Gatsby Docs Update] Initial version of new sidebar #10707
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
Closed
flarnie
wants to merge
4
commits into
facebook:gatsby
from
flarnie:responsiveSidebarGatsbyDocsUpdate
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
www/src/components/StickyResponsiveSidebar/StickyResponsiveSidebar.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /** | ||
| * Copyright 2015-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| * | ||
| * @emails react-core | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| import Container from 'components/Container'; | ||
| import {Component, React} from 'react'; | ||
| import isItemActive from 'utils/isItemActive'; | ||
| import {Sticky} from 'react-sticky'; | ||
| import Sidebar from 'templates/components/Sidebar'; | ||
| import {colors, media} from 'theme'; | ||
|
|
||
| // TODO: memoize to save doing O(n) on the active section items + subitems every | ||
| // time. | ||
| function findActiveItemTitle(location, defaultActiveSection) { | ||
| const {items} = defaultActiveSection; | ||
| for (let i = 0, len = items.length; i < len; i++) { | ||
| const item = items[i]; | ||
| if (isItemActive(location, item)) { | ||
| return item.title; | ||
| } else if (item.subitems && item.subitems.length) { | ||
| const {subitems} = item; | ||
| for (let j = 0, len2 = subitems.length; j < len2; j++) { | ||
| const subitem = subitems[j]; | ||
| if (isItemActive(location, subitem)) { | ||
| return subitem.title; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // If nothing else is found, warn and default to section title | ||
| console.warn('No active item title found in <StickyResponsiveSidebar>'); | ||
| return defaultActiveSection.title; | ||
| } | ||
|
|
||
| class StickyResponsiveSidebar extends Component { | ||
| constructor(props, context) { | ||
| super(props, context); | ||
|
|
||
| this.state = { | ||
| activeSection: props.defaultActiveSection, | ||
| open: false, | ||
| }; | ||
| this.toggleOpen = this._toggleOpen.bind(this); | ||
| } | ||
|
|
||
| _toggleOpen() { | ||
| this.setState({open: !this.state.open}); | ||
| } | ||
|
|
||
| render() { | ||
| const {defaultActiveSection, location} = this.props; | ||
| console.log('the defaultActiveSection is ;', defaultActiveSection); | ||
| console.log('this.props are ', this.props); | ||
| const smallScreenSidebarStyles = { | ||
| // TODO: animate height instead of using display: none? | ||
| // Changing height may be better a11y too | ||
| display: this.state.open ? 'block' : 'none', | ||
| top: 0, | ||
| left: 0, | ||
| bottom: 0, | ||
| right: 0, | ||
| position: 'fixed', | ||
| backgroundColor: colors.white, | ||
| }; | ||
|
|
||
| const smallScreenBottomBarStyles = { | ||
| display: 'block', | ||
| }; | ||
|
|
||
| const bottomBarText = this.state.open | ||
| ? 'Close' | ||
| : findActiveItemTitle(location, defaultActiveSection); | ||
|
|
||
| return ( | ||
| <div> | ||
| <Sticky> | ||
| {({style}) => { | ||
| return ( | ||
| <div | ||
| css={{ | ||
| [media.lessThan('small')]: smallScreenSidebarStyles, | ||
| [media.greaterThan('small')]: style, | ||
| }}> | ||
| <div | ||
| css={{ | ||
| marginTop: 60, | ||
|
|
||
| [media.lessThan('small')]: { | ||
| marginTop: 40, | ||
| }, | ||
|
|
||
| [media.between('medium', 'large')]: { | ||
| marginTop: 50, | ||
| }, | ||
| }}> | ||
| <Sidebar {...this.props} /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }} | ||
| </Sticky> | ||
| <div | ||
| css={{ | ||
| backgroundColor: colors.darker, | ||
| bottom: 0, | ||
| color: colors.brand, | ||
| display: 'none', // gets overriden at small screen sizes | ||
| left: 0, | ||
| cursor: 'pointer', | ||
| position: 'fixed', | ||
| right: 0, | ||
| width: '100%', | ||
| zIndex: 1, | ||
| [media.lessThan('small')]: smallScreenBottomBarStyles, | ||
| }} | ||
| onClick={this.toggleOpen}> | ||
| <Container> | ||
| <div | ||
| css={{ | ||
| display: 'flex', | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| height: 60, | ||
| [media.between('medium', 'large')]: { | ||
| height: 50, | ||
| }, | ||
| [media.lessThan('small')]: { | ||
| height: 40, | ||
| }, | ||
| }}> | ||
| {bottomBarText} | ||
| </div> | ||
| </Container> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default StickyResponsiveSidebar; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * Copyright 2015-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| * | ||
| * @emails react-core | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| import slugify from 'utils/slugify'; | ||
|
|
||
| const toAnchor = (href = '') => { | ||
| const index = href.indexOf('#'); | ||
| return index >= 0 ? href.substr(index) : ''; | ||
| }; | ||
|
|
||
| // TODO Account for redirect_from URLs somehow; they currently won't match. | ||
|
|
||
| const isItemActive = (location, item) => { | ||
| if (location == null) { | ||
| return false; // Production build of Gatsby is eval'ed in Node | ||
| } else if (location.hash) { | ||
| if (item.href) { | ||
| return location.hash === toAnchor(item.href); | ||
| } | ||
| } else if (item.id.includes('html')) { | ||
| return location.pathname.includes(item.id); | ||
| } else { | ||
| return location.pathname.includes(slugify(item.id)); | ||
| } | ||
| }; | ||
|
|
||
| export default isItemActive; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line causes errors for me.
yarn buildfails with:yarn devshows a runtime redbox with:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that I happened to have this page open when running
yarn dev:http://localhost:8000/blog/2013/06/12/community-roundup.html