Comments (4)
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.
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.
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.
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)
- support px to rem? HOT 1
- Support Style Props for Svelte HOT 1
- Wrong "missing token" warning when using nested tokens
- Semantic tokens break tokens completely HOT 4
- Jest encountered an unexpected token
- Code completion issues in Webstorm
- cva not extracting css in 0.33.0 HOT 1
- CVA and SVA not pre-generating CSS HOT 1
- backgroundImage doesn't work with template string as value HOT 1
- Pattern styles not compiled in monorepo
- Pattern styles not compiled in monorepo HOT 4
- Panda config polyfill & preflight options bug HOT 2
- Styles not generated on windows
- Color opacity modifier syntax (blue.300/70) in token references doesn't work with conditions HOT 1
- Recipe invalid when I change the name of the component HOT 1
- Config Recipe are not extracted properly using `raw` function HOT 2
- Using color opacity modifier syntax in semantic tokens with conditions will throw an error in validation
- Duplicated classes in extended components HOT 2
- Allow multi themes on pandacss/studio
- Unclear what CSS values are valid for `border` when `strictPropertyValues` is enabled HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from panda.