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

Add color={...} shorthand for all shapes #17

Open
2 tasks
seveibar opened this issue Jul 13, 2024 · 2 comments
Open
2 tasks

Add color={...} shorthand for all shapes #17

seveibar opened this issue Jul 13, 2024 · 2 comments

Comments

@seveibar
Copy link
Contributor

e.g. you should be able to do <Cube color="red" size={[1,0,0]} />

  • If color is provided, wrap the shape in a Colorize (NOTE: probably should be implemented as a wrapper to prevent redundant code)
  • Accept/convert colors from any standard web format, e.g. "red", "#f00", "#ff0000", "ff0000", "rgba(255,0,0,1)" (note: there are libraries that convert all of these)
@Slaviiiii
Copy link
Contributor

Hey Seve could you explain the Colorize wrapper

@seveibar
Copy link
Contributor Author

seveibar commented Jul 18, 2024

@Slaviiiii yes the idea behind the React Colorize wrapper is to make it so a component accepts the color prop without repeating code across components.

interface ColorProps {
  color?: string;
}

// Define the type for a React component that can accept any props
type ReactComponent<P = any> = React.ComponentType<P>;

export function withColorProps<P extends object>(
  WrappedComponent: ReactComponent<P>
): ReactComponent<P & ColorProps> {
  // Create a new component that includes the color prop
  const WithColor: React.FC<P & ColorProps> = ({ color, ...props }) => {
    return (
      <Colorize color={color}
        <WrappedComponent {...props as P} />;
      </Colorize>
    )
  };

  // Set the display name for easier debugging
  WithColor.displayName = `WithColor(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;

  return WithColor;
}

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

2 participants