Skip to content

Commit

Permalink
feat: pagecontainer scoll # into view
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Apr 30, 2020
1 parent 8cb87bf commit b9817bd
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion ui/blocks/src/PageContainer/PageContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react/display-name */
/** @jsx jsx */
import { FC } from 'react';
import { FC, useEffect } from 'react';
import { jsx, Box, Theme } from 'theme-ui';
import { MDXProvider, MDXProviderComponents } from '@mdx-js/react';
import { StoryStore } from '@component-controls/store';
Expand Down Expand Up @@ -58,6 +58,32 @@ export const PageContainer: FC<PageContainerProps> = ({
options,
components = {},
}) => {
let scrollId: string | undefined;
try {
const pageURL =
(window.location != window.parent.location && window.parent.location
? window.parent.location.href
: document.location.href) || '';
const url = new URL(pageURL);
scrollId = url.hash ? url.hash.substring(1) : undefined;
} catch (err) {}

useEffect(() => {
console.log(scrollId);
if (scrollId) {
const element = document.getElementById(scrollId);
if (element) {
// Introducing a delay to ensure scrolling works when it's a full refresh.
setTimeout(() => {
element.scrollIntoView({
behavior: 'smooth',
block: 'start',
inline: 'nearest',
});
}, 100);
}
}
}, [scrollId]);
return (
<ThemeProvider theme={theme} dark={dark}>
<Box
Expand Down

0 comments on commit b9817bd

Please sign in to comment.