-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write docs for shouldForwardProp
#655
Comments
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. |
Hey @Andarist, I'm using I was thinking about using const Button = ({ children, /*more props here */ data-test }) => {
// some logic based on props
return <StyledButton data-test={data-test}>{children}</StyledButton>
} This doesn't work because I would like to allow |
U cant quite override the default behaviour, u could create a custom "proxy" I would encourage you to rethink if you need to pass data-test around as prop, seems like an anti-pattern to me (unless some specific use case needs it). Most commonly it should be just used as a simple marker, used plain in sight, not as a configurable prop. |
I would like to keep using I'll try custom proxy when it becomes more painful. Right now I have just one place in codebase where I want to use Thank you for clarification 👍 |
Why don't make shouldForwardProp === '@emotion/is-prop-valid' by default? I often use props in css and it's really non-convenient to write this construction every time. I think it's rare case when you need to render invalid prop and it would be more convenient to sometimes write something like |
It's a default for DOM tags - it's not for compose components though. |
@Andarist what is the signature for shouldForwardProp in the case of something like |
Not sure what do u mean, this example doesnt use showcase what props are being passed to that styled component. |
@Andarist I mean, how do I pass in |
you cant, you have to use factory syntax: styled('div', { shouldForwardProp: prop => true })({ color: 'green' }); |
Hello guys, This is what I have: import styled, { CreateStyled } from '@emotion/styled';
import isPropValid from '@emotion/is-prop-valid';
interface ITheme = {
//myThemeStructure
}
const WHITE_LIST_PROPS = ['props', 'that', 'will', 'be', 'passed'];
const shouldForwardProp = (prop: string): boolean =>
isPropValid(prop) || WHITE_LIST_PROPS.includes(prop);
const customStyled = (cmp: any) => styled(cmp, { shouldForwardProp });
export default customStyled as CreateStyled<ITheme>; And then I'm receiving an error saying:
This is happening when I'm writing Could you please point me out what am I doing wrong? Also, if there is any way of doing this, I can add it to the docs. This could be helpful to other people also. Thanks in advance! |
Did you ever manage to get this working properly? |
If anyone is interested, I am using this: /* helpers.ts */
import { ReactElement, PropsWithChildren } from 'react';
import emotionStyled from '@emotion/styled';
import isPropValid from '@emotion/is-prop-valid';
import { css, SerializedStyles } from '@emotion/react';
type StyleGetter<T> = {
(props: T): SerializedStyles;
};
type ProxiedStyled = {
[key in keyof JSX.IntrinsicElements]: <T>(
styleGetter?: StyleGetter<T>,
whitelist?: string[],
) => (props: PropsWithChildren<T>) => ReactElement<T>;
};
export const styled = new Proxy({} as ProxiedStyled, {
get(_, name: keyof JSX.IntrinsicElements) {
return <T extends object>(
styleGetter: StyleGetter<T> = () => css``,
whitelist: (keyof T)[] = [],
) => emotionStyled(name, {
shouldForwardProp: (prop: keyof T) => isPropValid(prop) || whitelist.includes(prop),
})(styleGetter);
},
}); then use it: import { css } from '@emotion/react';
import { styled } from './helpers';
interface Props {
textColor: string;
}
const Container = styled.div<Props>(({ textColor }) => css`
position: relative;
color: ${textColor};
`); When you use const Container = styled.div<Props>(({ textColor }) => css`
position: relative;
color: ${textColor};
`, ['textColor']); |
Would be great to have this one documented, it might be used like this:
Another example can be found in this test case.
The text was updated successfully, but these errors were encountered: