Skip to content

Instantly share code, notes, and snippets.

@smhutch
Created May 27, 2021 20:24
Show Gist options
  • Save smhutch/8fa1daa6587c7d1f99c51ca657a54a9d to your computer and use it in GitHub Desktop.
Save smhutch/8fa1daa6587c7d1f99c51ca657a54a9d to your computer and use it in GitHub Desktop.
Restricting the `as` prop with Stitches
import type * as Polymorphic from "@radix-ui/react-polymorphic";
import type { ComponentProps } from "react";
import React, { forwardRef } from "react";
import { styled } from "../../theme";
const DEFAULT_TAG = "p";
const StyledText = styled(DEFAULT_TAG, {
// your styles
});
export interface TextProps extends ComponentProps<typeof StyledText> {
as?: "h1" | "h2" | "h3" | "h4" | "h5" | "p" | "li" | "pre" | "span" | "label";
}
type PolymorphicText = Polymorphic.ForwardRefComponent<
typeof DEFAULT_TAG,
TextProps
>;
export const Text = forwardRef((props, ref) => (
<StyledText ref={ref} {...props} />
)) as PolymorphicText;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment