A way to access stitches component props type? #213
-
First of all thanks for this amazing lib, I'm absolutely loving it so far!! I was thinking of a way of getting the type of the props a stitches component receives, to be able to compose other components on top of it without limiting the typings. So far, I've been playing around with something like: const StitchesButton = styled("button", {});
type StitchesButtonProps = React.ComponentProps<typeof StitchesButton>
type Props = { isLoading?: boolean } & StitchesButtonProps
const LoadingButton = ({ isLoading, ...buttonProps }: Props) => (
<StitchesButton {...buttonProps}>
{isLoading ? "loading..." : children}
</StitchesButton>
) but always get some error (sandbox here). Anybody have an idea of how to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 13 comments 29 replies
-
I think that's a great idea and the example you provided is a perfect use case. Right now, Im not sure what is the best way to achieve this, but I do agree it's something that need to be able to do. Will have a think while other people chip in. |
Beta Was this translation helpful? Give feedback.
-
I actually want this too. @julianbenegas did you get your technique to work by any chance? |
Beta Was this translation helpful? Give feedback.
-
This is exactly the use case I was thinking about in my discord question about getting prop types. Additionally I think it would help in working with dev tools like storybook or typedoc since those infer a lot based on typings. Using stitches with storybook yesterday I had to go in and manually set argTypes in the story because the inferred ones were missing the variants. |
Beta Was this translation helpful? Give feedback.
-
That's the usual I'm not sure how |
Beta Was this translation helpful? Give feedback.
-
I've used a hack in my own DS to solve this problem. I would basically expose a private type property It works perfectly except in some rare cases. For example, const Button = styled('button', {
variants: {
variant: {
primary: {...}
}
}
})
type Props = typeof Button.__props & { customProp:boolean }
const NewButton = (props:Props) => (<Button {...props} />)
// the as prop of `NewButton` prop can only be `button`
// you can use the variant and other dynamic props as you'd use them normally
<NewButton variant="primary" /> I am going to take a closer look at this |
Beta Was this translation helpful? Give feedback.
-
Hi there :) I believe we can just expose a https://codesandbox.io/s/stitches-ts-issue-forked-0ik04?file=/src/Button.tsx |
Beta Was this translation helpful? Give feedback.
-
I would expect to be able to use |
Beta Was this translation helpful? Give feedback.
-
We have a PR proposal here: #264 It essentially works like this. Stitches will expose two types:
Can be used like so: const Container = styled("div", {
marginX: "auto",
paddingX: "$3",
variants: {
size: {
"1": { maxWidth: "300px" },
"2": { maxWidth: "585px" },
"3": { maxWidth: "865px" }
}
}
});
function PageContainer(props: TStyledComponentProps<typeof Container> & TExtractVariantProps<typeof Container>) {
return <Container {...props} />;
} For a better DX, we could also: // type helper
type StitchesComponent<C> = TStyledComponentProps<C> & TExtractVariantProps<C>;
// usage
function PageContainer(props: StitchesComponent<typeof Container>) {} |
Beta Was this translation helpful? Give feedback.
-
I know this is a bit old, but this is how I'm doing it: import type { FC, InputHTMLAttributes } from 'react'
import type { $$StyledComponentProps } from '@stitches/react/types/styled-component'
import { styled } from 'stitches'
const StyledInput = styled('input', {
// ...
})
type InputProps = InputHTMLAttributes<HTMLInputElement> &
typeof StyledInput[$$StyledComponentProps] & { error: boolean }
const Input: FC<InputProps> = props => {
// your logic here
return <StyledInput {...props} />
}
export default Input |
Beta Was this translation helpful? Give feedback.
-
As of v1.0.0, you can use |
Beta Was this translation helpful? Give feedback.
-
Hi! I am getting a TypeScript error that seems to indicate that something might be a bit broken? I expected this to work fine based on the previous working examples: import * as React from "react";
import { styled, theme } from "@config/stitches.config";
const StyledButton = styled("button", {
// ...styleconfig
});
export type ButtonProps = React.ComponentProps<typeof StyledButton> & {
label: string;
};
const Button: React.FC<ButtonProps> = ({ label, ...rest }) => (
<StyledButton {...rest}>{label}</StyledButton>
);
export default Button; However, the This works: import * as React from "react";
import { styled, theme } from "@config/stitches.config";
const StyledButton = styled("button", {
// ...styleconfig
});
type StyleButtonProps = React.ComponentProps<typeof StyledButton>;
export type ButtonProps = Omit<StyleButtonProps, "css"> & {
label: string;
};
const Button: React.FC<ButtonProps> = ({ label, ...rest }) => (
<StyledButton {...rest}>{label}</StyledButton>
);
export default Button; Using |
Beta Was this translation helpful? Give feedback.
-
This way it worked for me, would it be correct? @peduarte |
Beta Was this translation helpful? Give feedback.
-
I think I've found a case when I guess it also relates to this issue: #427, but I decided to mention it here as that one is closed. |
Beta Was this translation helpful? Give feedback.
As of v1.0.0, you can use
React.ComponentProps
to get the component prop types. This does not include theas
prop.