-
Notifications
You must be signed in to change notification settings - Fork 255
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 way to extract props & variants types #264
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit c26cf18:
|
@hadihallak cool, nice workaround. A couple of questions:
// type helper
type StitchesComponent<C> = TStyledComponentProps<C> & TExtractVariantProps<C>;
// usage
function PageContainer(props: StitchesComponent<typeof Container>) {} We could sill expose them individually, but I imagine most people would need both types, most of the time, no? |
@hadihallak Something like this should get export interface IStyledComponent<ComponentOrTag extends React.ElementType, Variants, Config extends TConfig> extends React.FC<MergeElementProps<React.ElementType, VariantASProps<Config, Variants>> & {
as?: React.ElementType;
css?: TCssWithBreakpoints<Config>;
className?: string;
children?: any;
}> {
/**
* Props of a styled component
*/
<As extends React.ElementType = ComponentOrTag>(
// Merge native props with variant props to prevent props clashing.
// e.g. some HTML elements have `size` attribute. And when you combine
// both types (native and variant props) the common props become
// unusable (in typing-wise)
props: MergeElementProps<As, VariantASProps<Config, Variants>> & {
as?: As;
css?: TCssWithBreakpoints<Config>;
className?: string;
children?: any;
}
): any;
/**
* Compound Variant typing:
*/
compoundVariant: (
compoundVariants: VariantASProps<Config, Variants>,
possibleValues: TCssWithBreakpoints<Config>
) => IStyledComponent<ComponentOrTag, Variants, Config>;
/**
* @deprecated
*/
defaultProps?: never;
} I deprecated the |
That's Awesome @jjenzz 💯 I did two changes: After playing with this more I found out that the thing with the components getting a more broad typing happened also in styled-components and they have type util that allows the user to get the proper types. not sure why they haven't used something similar to what you suggested or if there are gotchas we're not aware of 🤔 I removed the old utils but part of me is thinking that |
Thanks @hadihallak Some thoughts on the naming, how about:
Usage examples: // extract all props of `Button`, inc `as` and `variants`
function MyButton(props: StitchesComponent<Button>) {}
// only extract `variants`
function MyComponent(props: StitchesVariants<Button>) {} @jonathantneal @jjenzz what do you think? |
For exposing types, I like these a lot. They look immediately clear in purpose, quickly writable, and structured well for IntelliSense completion. |
@peduarte As far as I understand it, we don't need the export type StitchesComponentProps<C = React.ElementType> = React.ComponentProps<C> |
@jjenzz I think its a nicer API, a nice to have. But yeah, that should work too 👍 |
Updated the PR with a new name for the variantsprops util and a StitchesComponentProps util that deals directly with the internal types instead of React.ComponentProps |
Oops, merged too soon and ended up committing to Commit here: Renamed |
@jjenzz After defaultProps deprecated, how default variant should be applied? |
Adds ExtractVariantProps generic util that allows extracting the variant props out of stitches components
Allows React.ComponentProps to work with stitches components - Fixes #248