Coder Social home page Coder Social logo

Typescript support for `as` prop about panda HOT 4 CLOSED

WDeenik avatar WDeenik commented on June 13, 2024 1
Typescript support for `as` prop

from panda.

Comments (4)

segunadebayo avatar segunadebayo commented on June 13, 2024 2

Hi @WDeenik,

This is by design; the as prop is notoriously known for being very challenging to type, and we don't intend to add types for this prop.

The as prop might be deprecated in the next major release. You can implement this in their userland.

const Text = ({ as, ...rest }) => {
   const Element = useMemo(() => styled(as, textRecipe), [as])
   return <Element { ...rest }/>
}

Thanks for understanding

from panda.

segunadebayo avatar segunadebayo commented on June 13, 2024 1

Just a heads up, we intend to remove the as prop implementation in the styled factory before v1. Would recommend using the approach I suggested above.

from panda.

subvertallchris avatar subvertallchris commented on June 13, 2024

Would it be any easier to implement this if the user was required to define a list of approved elements, perhaps via recipe? For instance, I don't need my Typography to act like a table or a button, so I could just list the header, span, div, h1, h2, etc,...

from panda.

subvertallchris avatar subvertallchris commented on June 13, 2024

Here's another example implementation that uses forwardRef and avoids hooks useMemo. It connects some simple variants to defined text layers. It's not all necessary just to get the as, of course, but here it is for completeness.

import * as React from 'react';

import { cva } from '@/styled-system/css';
import { styled } from '@/styled-system/jsx';

const typographyRecipe = cva({
  variants: {
    size: {
      body: {
        textStyle: 'ampBody',
      },
      h1: {
        textStyle: 'ampH1',
      },
      h2: {
        textStyle: 'ampH2',
      },
      h3: {
        textStyle: 'ampH3',
      },
      h4: {
        textStyle: 'ampH4',
      },
      h5: {
        textStyle: 'ampH5',
      },
      h6: {
        textStyle: 'ampH6',
      },
      caption: {
        textStyle: 'ampCaption',
      },
      overline: {
        textStyle: 'ampOverline',
      },
      link: {
        textStyle: 'inlineLink',
      },
    },
  },
  defaultVariants: {
    size: 'body',
  },
});

type TypographyHtmlElement = 'p' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'span' | 'a' | 'div';

interface ComponentProps {
  as?: TypographyHtmlElement;
}

const AsableTypography = <T extends TypographyHtmlElement>(
  { as = 'div', ...rest }: ComponentProps & React.JSX.IntrinsicElements[T],
  ref: React.ForwardedRef<T>,
) => {
  const testing = true;
  if (testing) {
    return React.createElement(as || 'div', { ref, ...rest });
  }
};

const ForwardableTypography = React.forwardRef(
  AsableTypography as <T extends TypographyHtmlElement>(
    props: ComponentProps & React.JSX.IntrinsicElements[T],
    ref: React.ForwardedRef<T>,
  ) => ReturnType<typeof AsableTypography>,
);

const Typography = styled(ForwardableTypography, typographyRecipe);

export default Typography;

from panda.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.