From dabb1a7cc27a358ee7e14ee9085308f61dca83ae Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Thu, 11 Jun 2020 12:11:28 -0700 Subject: [PATCH] feat: Better handling of text followed by code snippets by creating sections at each code snippet point --- src/components/SideBySide.js | 36 ++++++++++++++++----------- src/components/SideBySide.module.scss | 36 +++++++-------------------- 2 files changed, 31 insertions(+), 41 deletions(-) diff --git a/src/components/SideBySide.js b/src/components/SideBySide.js index 19c076694..f60443e0a 100644 --- a/src/components/SideBySide.js +++ b/src/components/SideBySide.js @@ -1,18 +1,31 @@ -import React from 'react'; +import React, { Children, Fragment } from 'react'; import PropTypes from 'prop-types'; import cx from 'classnames'; - import styles from './SideBySide.module.scss'; +import splitUsing from '../utils/splitUsing'; +import splitWhen from '../utils/splitWhen'; + +const isMdxType = (child, type) => child?.props?.mdxType === type; -const SideBySide = ({ className, children, type, dir }) => { - const childObjects = React.Children.toArray(children); - const side = childObjects.find((child) => child?.props?.mdxType === type); - const rest = childObjects.filter((child) => child !== side); +const SideBySide = ({ className, children, type }) => { + const childObjects = Children.toArray(children); + const rendersRightColumn = childObjects.some((child) => + isMdxType(child, type) + ); + const sections = splitUsing(childObjects, (child) => + isMdxType(child, type) + ).map((section) => splitWhen(section, (child) => isMdxType(child, type))); return ( -
-
{rest}
- {side &&
{side}
} +
+ {sections.map(([left, right], idx) => ( + +
+ {left} +
+ {rendersRightColumn &&
{right}
} +
+ ))}
); }; @@ -21,11 +34,6 @@ SideBySide.propTypes = { children: PropTypes.node.isRequired, type: PropTypes.string.isRequired, className: PropTypes.string, - dir: PropTypes.oneOf(['right', 'left']), -}; - -SideBySide.defaultProps = { - dir: 'right', }; export default SideBySide; diff --git a/src/components/SideBySide.module.scss b/src/components/SideBySide.module.scss index f79337163..84ce66400 100644 --- a/src/components/SideBySide.module.scss +++ b/src/components/SideBySide.module.scss @@ -1,32 +1,14 @@ .container { - display: flex; - - > div { - flex: 1; - } - - &.right { - flex-direction: row; - - > div:nth-child(2) { - margin-left: 1rem; - } - } - - &.left { - flex-direction: row-reverse; - - > div:nth-child(2) { - margin-right: 1rem; - } - } + display: grid; + grid-template-columns: 50% 50%; + grid-gap: 1rem; @media (max-width: 760px) { - display: block; - - &.left > div:nth-child(2), - &.right > div:nth-child(2) { - margin: 0; - } + grid-template-columns: 100%; + grid-gap: 0; } } + +.spanColumns { + grid-column: span 2; +}