Question: How can I provide multiple variants a props to a custom component that uses multiple RestyleComponents? #196
Replies: 5 comments 3 replies
-
I've the exact same problem.. hopefully there will be an answer soon. By the way in my case const Profile = ({ onPress, imgUrl, label, ...rest }: ProfileProps) => {
const props = useRestyle(restyleFunctions, rest);
return (
<Box marginHorizontal="s">
<TouchableOpacity onPress={onPress}>
<Box {...props}>
<Avatar rounded size="large" source={{ uri: imgUrl }} />
</Box>
</TouchableOpacity>
<Text variant="body" textAlign="center" marginTop="s">
{label}
</Text>
</Box>
);
}; For now, I only can do 'restyle' in the component that is wrapped inside the |
Beta Was this translation helpful? Give feedback.
-
Can I see your Avatar component, @erpriliano? Do you use variants to determine the size? |
Beta Was this translation helpful? Give feedback.
-
Why are you using So, passing something like: useRestyle([spacing], {margin: 'm'}) will return something like: { style: { margin: value of m in the theme } } Instead, you can have something like: import React from 'react';
import {
createBox,
createRestyleComponent,
createText,
createVariant,
ThemeProvider,
VariantProps,
} from '@shopify/restyle';
import {TouchableOpacity} from 'react-native';
import theme, {Theme} from './styles/theme';
const Box = createBox<Theme>();
const Text = createText<Theme>();
type Props = VariantProps<Theme, 'cardVariants'> &
VariantProps<Theme, 'textVariants', 'textVariants'> & {
label: string;
onPress: () => void;
};
const cardVariant = createVariant<Theme, 'cardVariants'>({
themeKey: 'cardVariants',
});
const Card = createRestyleComponent<
VariantProps<Theme, 'cardVariants'> & React.ComponentProps<typeof Box>,
Theme
>([cardVariant], Box);
const Button = ({onPress, label, textVariants, variant}: Props) => {
return (
<TouchableOpacity onPress={onPress}>
<Card variant={variant} justifyContent="center" alignItems="center">
<Text variant={textVariants}>{label}</Text>
</Card>
</TouchableOpacity>
);
};
export default () => (
<ThemeProvider theme={theme}>
<Box flex={1} alignItems={'center'} justifyContent={'center'}>
<Button
label={'Press Me'}
variant={'primary'}
textVariants={'button2'}
onPress={() => {}}
/>
</Box>
</ThemeProvider>
); If you want to change the name of the prop from the default (which is VariantProps<Theme, 'cardVariants', 'buttonVariant'> And if you want to add more props to your button to behave like a box, you can just do something like: type Props = BoxProps<Theme> &
VariantProps<Theme, 'cardVariants'> &
VariantProps<Theme, 'textVariants', 'textVariants'> & {
label: string;
onPress: () => void;
};
...
const Button = ({onPress, label, textVariants, variant, ...rest}: Props) => {
return (
<TouchableOpacity onPress={onPress}>
<Card
variant={variant}
justifyContent="center"
alignItems="center"
{...rest}>
<Text variant={textVariants}>{label}</Text>
</Card>
</TouchableOpacity>
);
};
export default () => (
<ThemeProvider theme={theme}>
<Box flex={1} alignItems={'center'} justifyContent={'center'}>
<Button
borderRadius={18}
margin={'m'}
label={'Press Me'}
variant={'primary'}
textVariants={'button2'}
onPress={() => {}}
/>
</Box>
</ThemeProvider>
); The same with any restyle function you want. |
Beta Was this translation helpful? Give feedback.
-
Hi, I need some help. I don't understand this library at all. I would like to achieve something like this
I know how I create theme is wrong. Is it anything like that possible?? Thank you very much in advance |
Beta Was this translation helpful? Give feedback.
-
Hi there! 👋 I'm currently working on a React Native project and using the createVariant and createRestyleComponent functions from the restyle library for styling. I've encountered an issue when trying to apply multiple variants to my Input component. Here's a simplified version of my code:
I'm trying to use multiple variants in my Input component, for example:
Below input styles which we are using
However, it seems that only one variant is applied at a time. Is there a specific way to correctly apply multiple variants using the createVariant and createRestyleComponent functions? Any guidance or examples would be greatly appreciated! Thanks in advance for your help! 🙏 |
Beta Was this translation helpful? Give feedback.
-
I've created a custom Button component that looks like the following:
How can I make it so that when I call
<Button variant="primary" ... />
I can do:<Button variant="primary" textVariant="button2" ..../>
I know I could do it manually on the ButtonProps interface, but I'd like a more intelligent way of doing this dynamically.
Beta Was this translation helpful? Give feedback.
All reactions