Skip to content

Commit

Permalink
feat: add mechanism to hide content on embedded pages
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlight-komorebi committed Apr 12, 2021
1 parent 428f201 commit dbcedd2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/components/EmbedContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

const EmbedContext = React.createContext();

export default EmbedContext;
14 changes: 14 additions & 0 deletions src/components/HideWhenEmbedded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import EmbedContext from './EmbedContext';

const HideWhenEmbedded = ({ children }) => {
const embedContext = React.useContext(EmbedContext);

if (embedContext && embedContext.isEmbedded) {
return null;
}

return children;
};

export default HideWhenEmbedded;
2 changes: 2 additions & 0 deletions src/components/MDXContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Tutorial from './Tutorial';
import TutorialStep from './TutorialStep';
import TutorialSection from './TutorialSection';
import Project from './Project';
import HideWhenEmbedded from './HideWhenEmbedded';

const components = {
// Remove these when all step/steps components have been updated to the new
Expand All @@ -28,6 +29,7 @@ const components = {
/>
),
iframe: Iframe,
HideWhenEmbedded,
};

const MDXContainer = ({ className, children }) => {
Expand Down
5 changes: 3 additions & 2 deletions src/templates/embedPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import React from 'react';
import { graphql } from 'gatsby';
import PropTypes from 'prop-types';
import PageLayout from '../components/PageLayout';
import EmbedContext from '../components/EmbedContext';

const EmbedPage = ({ data }) => {
const { mdx } = data;
const { frontmatter, body } = mdx;
const { title } = frontmatter;

return (
<>
<EmbedContext.Provider value={{ isEmbedded: true }}>
<h1>{title}</h1>
<PageLayout.MarkdownContent>{body}</PageLayout.MarkdownContent>
</>
</EmbedContext.Provider>
);
};

Expand Down

0 comments on commit dbcedd2

Please sign in to comment.