Created
May 28, 2019 21:29
-
-
Save jepser/43ee3a54d2d8ccc8cc8c2cdff11abab6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generate a valid structure for responsive configuration for a component | |
* @param {object} props props received from the component | |
* @param {array} baseProps list of props to be validated | |
* | |
* @returns a structured object with the props for each media query | |
*/ | |
export const generateResponsiveProps = (props, baseProps) => { | |
// from the breakpoints registered check which props exists | |
const shapedPropsWithMq = Object.keys(BREAKPOINTS).reduce( | |
(responsiveProps, mqName) => { | |
const propsForMq = props[mqName]; | |
if (!propsForMq && typeof propsForMq !== 'object') return responsiveProps; | |
// for the props that exists, prepare them with the correct shape | |
const shapedProps = baseProps.reduce( | |
(propList, prop) => ({ | |
...propList, | |
[prop]: propsForMq[prop], | |
}), | |
{} | |
); | |
return { | |
...responsiveProps, | |
[mqName]: shapedProps, | |
}; | |
}, | |
{} | |
); | |
return shapedPropsWithMq; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment