Enable as
specification at declaration for composed components
#507
Replies: 4 comments 1 reply
-
Alternative API: const Box = styled('div', {})
cont Text = styled(Box.as('p'), {}) Benefit of this is when we make |
Beta Was this translation helpful? Give feedback.
-
I was able to come up with a pattern to solve this in the userland. Posting below for those who seek solution for the same const BoxAs = <T extends Parameters<typeof styled>[0]>(tag: T) => styled(tag, {});
const Box = BoxAs("div");
const TextAs = <T extends Parameters<typeof styled>[0]>(tag: T) => styled(BoxAs(tag), {});
const Text = TextAs("p");
const TextLink = styled(TextAs("a"), {}); Full example: https://codesandbox.io/s/composition-with-default-as-mf3x9 |
Beta Was this translation helpful? Give feedback.
-
@dylanklohr @afzalsayed96 check out #609 for some ideas related to this. |
Beta Was this translation helpful? Give feedback.
-
This feature was previously proposed, but was rejected for several reasons. |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
The existing polymorphic
as
prop from@stitches/react
works incredibly well for allowing control over the rendered element at time of consumption. One thing that has been a frequent pain point however is that there is no way to specify a value foras
when composing components.Example
This leads to forcing instances of
<Text />
to provide a value foras
every time it is consumed.Current Workaround
but this forces the onus of handling polymorphism for any other
props
that may be provided to<Text />
to consumers.Desired Solution
If one is able to provide a value for the
as
prop when declaring a newstyled
component that composes another component, like the following, consumers would be able to have access to the full power and awesome DX that@stitches/react
offers, with greater flexibility for component compositionAlternative APIs
Optional third Argument
Array as first Argument
Special
@as
PropertyPotentially Related Issues/Discussions
as
prop onstyled(Component)
components #448Beta Was this translation helpful? Give feedback.
All reactions