${props.url} How do I do this without Styled Components? #1063
Answered
by
predaytor
BuchananQuantum
asked this question in
Help
-
Transitioning over from styled components. How do I inject props into stitches components? const Header = styled.div<{ url?: string }> @media(min-width: 1536px) { |
Beta Was this translation helpful? Give feedback.
Answered by
predaytor
Jul 17, 2022
Replies: 1 comment 2 replies
-
You can create a wrapper with Using import { styled } from 'stitches.config';
import type { CSS, VariantProps } from 'stitches.config';
import React from 'react';
const Component = styled('div', {
height: '310px',
// theme.colors.placeholder in config file
backgroundColor: '$placeholder',
backgroundPosition: 'center',
backgroundZize: 'cover',
variants: {},
compoundVariants: [],
defaultVariants: {},
});
type HeaderVariants = VariantProps<typeof Component>;
type HeaderProps = React.ComponentPropsWithRef<'div'> &
HeaderVariants & { css?: CSS, url?: string };
export const Header = React.forwardRef<HTMLDivElement, HeaderProps>(
({ url, css, ...props }, ref) => {
return <Component {...props} ref={ref} css={{ backgroundImage: `url(${url})`, ...css }} />;
}
); Using import { css } from 'stitches.config';
import type { CSS, VariantProps } from 'stitches.config';
import React from 'react';
import clsx from 'clsx';
const styles = css({
height: '310px',
backgroundColor: '$placeholder',
backgroundPosition: 'center',
backgroundZize: 'cover',
variants: {
variant: {
gray: {},
},
},
});
type HeaderVariants = VariantProps<typeof component>;
type HeaderProps = React.ComponentPropsWithRef<'div'> &
HeaderVariants & { css?: CSS, url?: string };
export const Header = React.forwardRef<HTMLDivElement, HeaderProps>(
({ url, variant, css, ...props }, ref) => {
const className = clsx(
styles({
variant,
css: {
...css,
backgroundImage: `url(${url})`,
},
}).className,
props.className
);
return <div {...props} ref={ref} className={className} />;
}
); |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
BuchananQuantum
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can create a wrapper with
React.forwardRef
.Using
styled
: