Skip to content

Commit

Permalink
feat: create a PageLayout component
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jul 30, 2020
1 parent 2880c29 commit 2e6f488
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/components/PageLayout/Content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import styled from '@emotion/styled';

const Content = styled.div`
grid-area: content;
`;

export default Content;
39 changes: 39 additions & 0 deletions src/components/PageLayout/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/core';

const Header = ({ title, children }) => (
<header
css={css`
grid-area: page-header;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid var(--divider-color);
@media screen and (max-width: 1080px) {
flex-direction: column;
align-items: flex-start;
}
`}
>
<h1
css={css`
font-family: var(--secondary-font-family);
font-size: 2.5rem;
font-weight: normal;
margin-bottom: 0;
`}
>
{title}
</h1>
{children}
</header>
);

Header.propTypes = {
children: PropTypes.node,
title: PropTypes.string.isRequired,
};

export default Header;
8 changes: 8 additions & 0 deletions src/components/PageLayout/MarkdownContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import styled from '@emotion/styled';
import MDXContainer from '../MDXContainer';

const MarkdownContent = styled(MDXContainer)`
grid-area: content;
`;

export default MarkdownContent;
77 changes: 77 additions & 0 deletions src/components/PageLayout/PageLayout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/core';
import { graphql, useStaticQuery } from 'gatsby';
import Content from './Content';
import Header from './Header';
import MarkdownContent from './MarkdownContent';
import RelatedContent from './RelatedContent';

const TYPES = {
SINGLE_COLUMN: 'SINGLE_COLUMN',
RELATED_CONTENT: 'RELATED_CONTENT',
};

const LAYOUTS = {
[TYPES.RELATED_CONTENT]: css`
grid-template-areas:
'page-header page-header'
'content related-content';
grid-template-columns: minmax(0, 1fr) auto;
grid-gap: 2rem;
@media (max-width: 1240px) {
grid-template-areas:
'page-header'
'content';
grid-template-columns: minmax(0, 1fr);
}
`,
[TYPES.SINGLE_COLUMN]: css`
grid-template-areas:
'page-header'
'content';
grid-template-columns: minmax(0, 1fr);
`,
};

const PageLayout = ({ children, type }) => {
const {
site: { layout },
} = useStaticQuery(graphql`
query {
site {
layout {
contentPadding
}
}
}
`);

return (
<div
css={css`
display: grid;
grid-gap: ${layout.contentPadding};
${LAYOUTS[type]};
`}
>
{children}
</div>
);
};

PageLayout.propTypes = {
children: PropTypes.node,
type: PropTypes.oneOf(Object.values(TYPES)).isRequired,
};

PageLayout.TYPE = TYPES;

PageLayout.Content = Content;
PageLayout.Header = Header;
PageLayout.MarkdownContent = MarkdownContent;
PageLayout.RelatedContent = RelatedContent;

export default PageLayout;
108 changes: 108 additions & 0 deletions src/components/PageLayout/RelatedContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/core';
import { graphql, useStaticQuery } from 'gatsby';
import { Button, ExternalLink, Icon } from '@newrelic/gatsby-theme-newrelic';
import usePageContext from '../../hooks/usePageContext';

const RelatedContent = ({ page }) => {
const { site } = useStaticQuery(graphql`
query {
site {
siteMetadata {
repository
}
}
}
`);
const { fileRelativePath } = usePageContext();

const {
fields: { gitAuthorTime },
} = page;

const {
siteMetadata: { repository },
} = site;

return (
<aside
css={css`
grid-area: related-content;
position: sticky;
top: calc(var(--global-header-height) + 2rem);
align-self: start;
@media (max-width: 1240px) {
display: none;
}
`}
>
<h4>Contribute</h4>
<Button
as={ExternalLink}
href={`${repository}/issues/new/choose`}
css={css`
margin-right: 0.5rem;
`}
variant={Button.VARIANT.PRIMARY}
size={Button.SIZE.SMALL}
>
<Icon
name={Icon.TYPE.GITHUB}
css={css`
margin-right: 0.5rem;
`}
/>
File an issue
</Button>
<Button
as={ExternalLink}
href={`${repository}/tree/main/${fileRelativePath}`}
variant={Button.VARIANT.NORMAL}
size={Button.SIZE.SMALL}
>
<Icon
name={Icon.TYPE.EDIT}
css={css`
margin-right: 0.5rem;
`}
/>
Edit this page
</Button>

<div
css={css`
font-size: 0.875rem;
font-style: italic;
margin-top: 1rem;
color: var(--color-neutrals-500);
.dark-mode & {
color: var(--color-dark-500);
}
`}
>
{`Page last modified on ${gitAuthorTime}`}
</div>
</aside>
);
};

RelatedContent.propTypes = {
page: PropTypes.shape({
fields: PropTypes.shape({
gitAuthorTime: PropTypes.string,
}),
}),
};

export const query = graphql`
fragment RelatedContent_page on Mdx {
fields {
gitAuthorTime(formatString: "MMMM DD, YYYY")
}
}
`;

export default RelatedContent;
1 change: 1 addition & 0 deletions src/components/PageLayout/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './PageLayout';

0 comments on commit 2e6f488

Please sign in to comment.