Repository navigation
feat(react): add forwardedAs prop to styled components - #1540
Open
officialasishkumar wants to merge 1 commit into
Open
officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
The `as` prop on a styled component replaces the component it wraps. When the wrapped component is not a styled component (for example a router link) there was no way to change the element rendered inside it without replacing the component itself. Add a `forwardedAs` prop, matching styled-components. It is removed from the props passed down and handed to the wrapped component as its `as` prop. It is ignored when `as` is set (the wrapped component is replaced or already receives `as`) and when the styled component is created from an HTML tag, so it never reaches the DOM. The prop is added to the `as` overloads of StyledComponentWithAs, with type tests, runtime tests and documentation in docs/API.md. Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Closes #713
ason a styled component replaces the component it wraps. For a wrapped component that is not itself a styled component (e.g. a routerLink), there is no way to change what that component renders without replacing it.forwardedAs, as in styled-components, covers that case.Summary
forwardedAsis stripped from the props and passed to the wrapped component as itsasprop.asis set, and when the styled component is built from an HTML tag, so it never ends up on the DOM.forwardedAs?: React.ElementTypeto theasoverloads ofStyledComponentWithAs, so it is typed for components created from HTML tags and for wrapped styled components. Wrapping a plain React component still returns that component's own props type, as it does forastoday.docs/API.mdand a changeset (minor) for@linaria/react.Test plan
On Node 22 in
packages/react:pnpm test— 26 tests pass, 17 snapshots unchanged; includes new tests for a wrapped custom component, a wrapped styled component,ascombined withforwardedAs, and HTML tags.pnpm test:dts— type tests pass, including newforwardedAscases and a@ts-expect-errorfor an invalid value.pnpm typecheckand eslint on the changed files are clean.