#114: anchor links support

This commit is contained in:
SleepWalker 2016-05-31 07:50:22 +03:00
parent cbf0fc979e
commit 6ea62bca08
3 changed files with 26 additions and 10 deletions

View File

@ -38,7 +38,7 @@ userFactory(store)
ReactDOM.render( ReactDOM.render(
<ReduxProvider store={store}> <ReduxProvider store={store}>
<IntlProvider> <IntlProvider>
<Router history={browserHistory}> <Router history={browserHistory} onUpdate={restoreScroll}>
{routesFactory(store)} {routesFactory(store)}
</Router> </Router>
</IntlProvider> </IntlProvider>
@ -49,6 +49,23 @@ userFactory(store)
document.getElementById('loader').classList.remove('is-active'); document.getElementById('loader').classList.remove('is-active');
}); });
/**
* Scrolls to page's top or #anchor link, if any
*/
function restoreScroll() {
const {hash} = location;
// Push onto callback queue so it runs after the DOM is updated
setTimeout(() => {
const id = hash.replace('#', '');
const el = id ? document.getElementById(id) : null;
if (el) {
el.scrollIntoView();
} else {
window.scrollTo(0, 0);
}
}, 100);
}
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
// some shortcuts for testing on localhost // some shortcuts for testing on localhost

View File

@ -59,8 +59,9 @@ export default function RulesPage() {
return ( return (
<div> <div>
<div className={styles.rules}> <div className={styles.rules}>
{rules.map((block, key) => ( {rules.map((block, sectionIndex) => (
<div className={styles.rulesSection} key={key}> <div className={styles.rulesSection} key={sectionIndex}>
<span id={`rule-${sectionIndex + 1}`} />
<h2 className={styles.rulesSectionTitle}>{block.title}</h2> <h2 className={styles.rulesSectionTitle}>{block.title}</h2>
<div className={styles.rulesBody}> <div className={styles.rulesBody}>
@ -70,8 +71,11 @@ export default function RulesPage() {
</div> </div>
) : ''} ) : ''}
<ol className={styles.rulesList}> <ol className={styles.rulesList}>
{block.items.map((item, key) => ( {block.items.map((item, ruleIndex) => (
<li className={styles.rulesItem} key={key}>{item}</li> <li className={styles.rulesItem} key={ruleIndex}>
<span id={`rule-${sectionIndex + 1}-${ruleIndex + 1}`} />
{item}
</li>
))} ))}
</ol> </ol>
</div> </div>

View File

@ -26,11 +26,6 @@ import Finish from 'components/auth/finish/Finish';
import authFlow from 'services/authFlow'; import authFlow from 'services/authFlow';
browserHistory.listen(() => {
// scroll to top on route transition
document.body.scrollTop = document.documentElement.scrollTop = 0;
});
export default function routesFactory(store) { export default function routesFactory(store) {
authFlow.setStore(store); authFlow.setStore(store);