Skip to content

Commit

Permalink
Merge pull request #1114 from tomrule007/bugfix/codepen-mdx-errors
Browse files Browse the repository at this point in the history
Bugfix/codepen mdx errors
  • Loading branch information
tomrule007 committed Sep 9, 2021
2 parents 6105698 + cf5b4b8 commit 8e0c173
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 122 deletions.
63 changes: 63 additions & 0 deletions components/mdx/CodePen.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react'
import { render } from '@testing-library/react'
import '@testing-library/jest-dom'
import CodePen from './CodePen'
describe('CodePen component', () => {
test('should render a single iframe element', () => {
const { container } = render(
<CodePen id="SOME_TEST" height="500" defaultTab="js" showResult={true} />
)
expect(container.children.length).toBe(1)
expect(container.children[0].tagName).toBe('IFRAME')
})

test('should render correct src value containing prop values', () => {
const { container } = render(
<CodePen
id="SOME_TEST"
height="1000"
defaultTab="html"
showResult={false}
theme="dark"
/>
)
expect(container.children[0]).toHaveAttribute(
'src',
'https://codepen.io/c0d3codepen/embed/SOME_TEST?theme-id=dark&default-tab=html'
)
})

test("should render default height and style of 300 and {width: '100%'}", () => {
const { container } = render(<CodePen id="SOME_TEST" defaultTab="html" />)
const iframe = container.children[0]
expect(iframe).toHaveAttribute('style', 'width: 100%;')
expect(iframe).toHaveAttribute('height', '300')
})

test('should render light theme and result tab only by default', () => {
const { container } = render(<CodePen id="SOME_TEST" />)
expect(container.children[0]).toHaveAttribute(
'src',
'https://codepen.io/c0d3codepen/embed/SOME_TEST?theme-id=light&default-tab=result'
)
})

test('should render selected tab, "html", and include result tab by default', () => {
const { container } = render(<CodePen id="SOME_TEST" defaultTab="html" />)
expect(container.children[0]).toHaveAttribute(
'src',
'https://codepen.io/c0d3codepen/embed/SOME_TEST?theme-id=light&default-tab=html%2Cresult'
)
})
test('should match screenshot', () => {
const { container } = render(
<CodePen
id="SOME_OTHER_TEST"
height={3001}
style={{ maxWidth: '700px' }}
defaultTab="css"
/>
)
expect(container).toMatchSnapshot()
})
})
43 changes: 43 additions & 0 deletions components/mdx/CodePen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react'
import CSS from 'csstype'

type Props = {
id: string
style?: CSS.Properties
height?: number
defaultTab?: 'js' | 'css' | 'html' | 'result'
showResult?: boolean
theme?: 'light' | 'dark'
}

const CodePen: React.FC<Props> = ({
id,
height = 300,
style = { width: '100%' },
defaultTab = 'result',
showResult = true,
theme = 'light'
}) => {
// User can select one of 3 tabs for left hand side: js / css / html
// or pick 'result' to display full width result. selecting 'result' will
// override 'showResults = false' as you must display some tab.
// showResults true will display results on right hand side.
const tabString =
defaultTab === 'result'
? 'result'
: `${defaultTab}${showResult ? '%2Cresult' : ''}` // %2C is URL encoded comma character

return (
<iframe
height={height}
style={style}
scrolling="no"
src={`https://codepen.io/c0d3codepen/embed/${id}?theme-id=${theme}&default-tab=${tabString}`}
frameBorder="no"
loading="lazy"
allowFullScreen={true}
/>
)
}

export default CodePen
15 changes: 15 additions & 0 deletions components/mdx/__snapshots__/CodePen.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CodePen component should match screenshot 1`] = `
<div>
<iframe
allowfullscreen=""
frameborder="no"
height="3001"
loading="lazy"
scrolling="no"
src="https://codepen.io/c0d3codepen/embed/SOME_OTHER_TEST?theme-id=light&default-tab=css%2Cresult"
style="max-width: 700px;"
/>
</div>
`;
129 changes: 7 additions & 122 deletions content/lessons/js4/sublesson/css.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,7 @@ language that helps us make websites beautiful by changing the color, position,
and other properties of browser elements. CSS stands for Cascading Style Sheets.
Here's an example:

<div style={{ maxWidth: 'max-content' }}>
<p
class="codepen"
data-height="265"
data-theme-id="light"
data-default-tab="result"
data-user="c0d3codepen"
data-slug-hash="oNZpNKe"
data-pen-title="hw"
>
<span>
See the Pen <a href="https://codepen.io/c0d3codepen/pen/oNZpNKe">hw</a> by
c0d3 (<a href="https://codepen.io/c0d3codepen">@c0d3codepen</a>) on{' '}
<a href="https://codepen.io">CodePen</a>.
</span>
</p>
<script
async
src="https://cpwebassets.codepen.io/assets/embed/ei.js"
></script>
</div>

<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>
<CodePen id="oNZpNKe" height="265" style={{ maxWidth: 'max-content' }} />

Fortunately, CSS is a really simple language. You only need to know 3 things.

Expand Down Expand Up @@ -531,21 +509,7 @@ below are a few more common properties that can add cool effects to your pages.

## background and border-radius

<p
class="codepen"
data-height="265"
data-theme-id="light"
data-default-tab="css,result"
data-user="c0d3codepen"
data-slug-hash="MWprWLg"
data-pen-title="radius"
>
<span>
See the Pen <a href="https://codepen.io/c0d3codepen/pen/MWprWLg">radius</a>{' '}
by c0d3 (<a href="https://codepen.io/c0d3codepen">@c0d3codepen</a>) on{' '}
<a href="https://codepen.io">CodePen</a>.
</span>
</p>
<CodePen id="MWprWLg" height="265" defaultTab="css" />

- Change the value of `background` to
`url([https://placebear.com/50/50](https://placekitten.com/200/300))` and see
Expand Down Expand Up @@ -596,24 +560,7 @@ create all kinds of interesting layouts.
We'll start with 4 divs with no special positioning, which we'll adjust in the
following examples.

<p
class="codepen"
data-height="562"
data-theme-id="light"
data-default-tab="js,result"
data-user="c0d3codepen"
data-slug-hash="dyvJZEW"
style={{ height: '562px' }}
data-pen-title="position"
>
<span>
See the Pen{' '}
<a href="https://codepen.io/c0d3codepen/pen/dyvJZEW">position</a> by c0d3 (
<a href="https://codepen.io/c0d3codepen">@c0d3codepen</a>) on{' '}
<a href="https://codepen.io">CodePen</a>.
</span>
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>
<CodePen id="dyvJZEW" height="562" defaultTab="js" />

The code on the left will render the above UI.

Expand All @@ -634,22 +581,7 @@ up, we use a negative value for `top`.)
We can also specify a `position` property with the value `fixed` to adjust the
element.

<p
class="codepen"
data-height="265"
data-theme-id="light"
data-default-tab="css,result"
data-user="c0d3codepen"
data-slug-hash="OJpzPXr"
data-pen-title="fixed"
>
<span>
See the Pen <a href="https://codepen.io/c0d3codepen/pen/OJpzPXr">fixed</a>{' '}
by c0d3 (<a href="https://codepen.io/c0d3codepen">@c0d3codepen</a>) on{' '}
<a href="https://codepen.io">CodePen</a>.
</span>
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>
<CodePen id="OJpzPXr" height="265" defaultTab="css" />

Notice that the `.div3` element **no longer occupies its default position**.
Unlike when we used `relative`, the element does not take up any space in its
Expand Down Expand Up @@ -700,22 +632,7 @@ property to help the computer determine how the elements are ordered. An element
with a higher `z-index` will always be drawn above an element with a lower
z-index.

<p
class="codepen"
data-height="265"
data-theme-id="light"
data-default-tab="css,result"
data-user="c0d3codepen"
data-slug-hash="rNypaGj"
data-pen-title="z-index"
>
<span>
See the Pen <a href="https://codepen.io/c0d3codepen/pen/rNypaGj">z-index</a>{' '}
by c0d3 (<a href="https://codepen.io/c0d3codepen">@c0d3codepen</a>) on{' '}
<a href="https://codepen.io">CodePen</a>.
</span>
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>
<CodePen id="rNypaGj" height="265" defaultTab="css" />

# Box Model

Expand Down Expand Up @@ -851,23 +768,7 @@ property.
To understand how this property works, we can use `div`s to create 4 boxes side
by side:

<p
class="codepen"
data-height="265"
data-theme-id="light"
data-default-tab="css,result"
data-user="c0d3codepen"
data-slug-hash="QWpawoN"
data-pen-title="abcd"
>
<span>
See the Pen <a href="https://codepen.io/c0d3codepen/pen/QWpawoN">abcd</a> by
c0d3 (<a href="https://codepen.io/c0d3codepen">@c0d3codepen</a>) on{' '}
<a href="https://codepen.io">CodePen</a>.
</span>
</p>

<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>
<CodePen id="QWpawoN" height="265" defaultTab="css" />

Try adding the HTML and the rest of the necessary CSS for the above layout, and
then change the `display` to `inline` and `block` to see how the boxes change.
Expand Down Expand Up @@ -910,23 +811,7 @@ This code has a `div` element that has three child `h1` elements. Since each
`h1` is a block element, it takes 100% of its parent's width, looking like the
following:

<p
class="codepen"
data-height="265"
data-theme-id="light"
data-default-tab="html,result"
data-user="c0d3codepen"
data-slug-hash="XWMVbNq"
data-pen-title="flexbox-justify"
>
<span>
See the Pen{' '}
<a href="https://codepen.io/c0d3codepen/pen/XWMVbNq">flexbox-justify</a> by
c0d3 (<a href="https://codepen.io/c0d3codepen">@c0d3codepen</a>) on{' '}
<a href="https://codepen.io">CodePen</a>.
</span>
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>>
<CodePen id="XWMVbNq" height="265" defaultTab="html" />

What if we wanted the 3 headers to be displayed side-by-side and _together_ fill
up 100% of the space available to them (meaning they would resize when the
Expand Down
2 changes: 2 additions & 0 deletions helpers/mdxComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ModalImage from '../components/mdx/ModalImage'
import styles from '../scss/mdx.module.scss'
import Spoiler from '../components/mdx/Spoiler'
import ChallengeBar from '../components/mdx/ChallengBar'
import CodePen from '../components/mdx/CodePen'
const TwoColumns: React.FC = ({ children }) => {
return <div className={`${styles['MDX_twoColumns']}`}>{children}</div>
}
Expand All @@ -20,6 +21,7 @@ const MDXcomponents = {
Spoiler: Spoiler,
twoColumns: TwoColumns,
ChallengeBar: ChallengeBar,
CodePen: CodePen,
h1: (props: any) => <h1 className={`${styles['MDX_h1']}`} {...props} />,
h2: (props: any) => <h2 className={`${styles['MDX_h2']}`} {...props} />,
h3: (props: any) => <h3 className={`${styles['MDX_h3']}`} {...props} />,
Expand Down

1 comment on commit 8e0c173

@vercel
Copy link

@vercel vercel bot commented on 8e0c173 Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.