Skip to content

Instantly share code, notes, and snippets.

@jepser
Created May 28, 2019 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepser/43ee3a54d2d8ccc8cc8c2cdff11abab6 to your computer and use it in GitHub Desktop.
Save jepser/43ee3a54d2d8ccc8cc8c2cdff11abab6 to your computer and use it in GitHub Desktop.
/**
* 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