Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardise around Yoga CSS and styled-system API #22

Open
macintoshhelper opened this issue Mar 30, 2020 · 0 comments
Open

Standardise around Yoga CSS and styled-system API #22

macintoshhelper opened this issue Mar 30, 2020 · 0 comments

Comments

@macintoshhelper
Copy link
Member

Support 100% of Yoga CSS API.

Support styled-system aliases with proposed system:

const useStyles = (styles) => {
  return useMemo(() => {
    const parsedStyles = parseStyles(styles);
    return StyleSheet.create(parsedStyles);
  }, styles);
};

const filterStyles = props => props.filter(prop => ['backgroundColor, 'color, 'fontSize'].includes(prop);
const filterProps = props => props.filter(prop => !['backgroundColor, 'color, 'fontSize'].includes(prop);

const Box = (props) => {
  const styles = filterStyles(props);
  const style = useStyles(styles);

  return <View style={style} {...filterProps(props)} />;
};
const mapping = {
  bg: backgroundColor,
  p: padding,
  pt: paddingTop,
  pb: paddingBottom,
  pl: paddingLeft,
  pr: paddingRight,
  m: margin,
  mt: marginTop,
  mb: marginBottom
// ...
};

const parseStylesheet = (styles) => {
  const parsedStyles = {};

  Object.entries(styles).forEach(([att, val]) => {
    if (mapping[att]) {
      parsedStyles[mapping[att]] = val;
    }
  });

  return parsedStyles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant