From a5a380a8862443869cb0ec2528475d27e9cd2ee7 Mon Sep 17 00:00:00 2001 From: J Smith Date: Sun, 12 Jan 2020 13:24:10 -0800 Subject: [PATCH] allow custom props on ParallaxBanner layers #81 --- __tests__/ParallaxBanner.test.js | 27 ++++++++ .../__snapshots__/ParallaxBanner.test.js.snap | 55 ++++++++++++++++ src/components/ParallaxBanner.js | 63 ++++++++++++------- 3 files changed, 122 insertions(+), 23 deletions(-) diff --git a/__tests__/ParallaxBanner.test.js b/__tests__/ParallaxBanner.test.js index 028b8bfaf..20f69adf7 100644 --- a/__tests__/ParallaxBanner.test.js +++ b/__tests__/ParallaxBanner.test.js @@ -134,4 +134,31 @@ describe('Expect the component', () => { expect(childFn).toBeCalled(); }); + + it('to render layers with custom props', () => { + const tree = renderer + .create( + + + , + { + createNodeMock, + } + ) + .toJSON(); + expect(tree).toMatchSnapshot(); + }); }); diff --git a/__tests__/__snapshots__/ParallaxBanner.test.js.snap b/__tests__/__snapshots__/ParallaxBanner.test.js.snap index 9d2114099..fd52e5553 100644 --- a/__tests__/__snapshots__/ParallaxBanner.test.js.snap +++ b/__tests__/__snapshots__/ParallaxBanner.test.js.snap @@ -122,6 +122,61 @@ exports[`Expect the component to render image banners correctly `; +exports[`Expect the component to render layers with custom props 1`] = ` +
+
+
+
+
+
+
+`; + exports[`Expect the component to render without expanded margins 1`] = `
{ > {layers.map( ( - { image, amount, children: layerChildren, expanded = true }, + { + amount, + children: layerChildren, + expanded = true, + image, + props = {}, + }, i ) => { + // save props to be merged + const layerStyle = props.style || {}; + const layerClass = props.className || ''; + + // remove from pass through props + delete props.style; + delete props.className; + + const layerClassMerged = `parallax-banner-layer-${i}${ + layerClass ? ` ${layerClass}` : '' + }`; + // if this is an expanded layer overwrite the top/bottom styles with negative margins const expandedStyle = expanded ? { @@ -35,6 +53,14 @@ const ParallaxBanner = ({ children, className, layers, style, disabled }) => { bottom: Math.abs(amount) * 100 * -1 + '%', } : {}; + // optional image styles + const imageStyle = image + ? { + backgroundImage: `url(${image})`, + backgroundPosition: 'center', + backgroundSize: 'cover', + } + : {}; return ( { styleOuter={absoluteStyle} disabled={disabled} > - {image ? ( -
- ) : ( -
- {layerChildren} -
- )} +
+ {layerChildren} +
); } @@ -89,6 +105,7 @@ ParallaxBanner.propTypes = { children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]), expanded: PropTypes.bool, image: PropTypes.string, + props: PropTypes.object, }) ), style: PropTypes.object,