-
Notifications
You must be signed in to change notification settings - Fork 136
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
Using styles within custom components #55
Comments
There's unfortunately no good way to do this right now, but I'll keep this shortcoming in mind and see if we can solve this in a nice way in future versions. |
Okay thank you! |
I also had issues figuring out a way to make my own button. type Props = VariantProps<Theme, "buttonVariants"> & {
label: string;
onPress?: () => void;
}; So, for example, you can create a ButtonContainer component, const ButtonContainer = createRestyleComponent<
VariantProps<Theme, "buttonVariants"> & React.ComponentProps<typeof Box>,
Theme
>([createVariant({ themeKey: "buttonVariants" })], Box); and use it in your Instead of just passing the type Props = React.ComponentProps<typeof ButtonContainer> & {
label: string;
onPress?: () => void;
};
const Button = ({ label, variant, onPress, ...rest }: Props) => (
<ButtonContainer variant={variant} {...rest}>
<Touchableopacity onPress={onPress}>
<Text variant={variant}>
{label}
</Text>
</Touchableopacity>
</ButtonContainer>
) The only downside i see is having to manually maintain the same variant names in both a Button container (just a box with variants) and the Text component, but it works... |
@Charly6596 can you please add your theme, I have the same problem, having hard time understanding how did you define your variants |
@minawalphonce sure thing, here's a simplified version of my theme const theme = createTheme({
colors: {
buttonPrimary: palette.blue,
buttonPrimaryForeground: palette.white,
buttonSecondary: palette.white,
buttonSecondaryForeground: palette.darkBlue,
},
spacing: {}, // omitted
breakpoints: {}, // omitted
textVariants: {
buttonPrimary: {
color: "buttonPrimaryForeground",
fontSize: 16,
},
buttonSecondary: {
color: "buttonSecondaryForeground",
fontSize: 16,
},
},
buttonVariants: {
buttonPrimary: {
backgroundColor: "buttonPrimary",
padding: "m",
},
buttonSecondary: {
backgroundColor: "buttonSecondary",
padding: "m",
},
},
}); |
The ability to define styles for subcomponents when using variants would be a great addition to the library. Not being able to do so feels limitating when trying to build component like : These kind of components, where you don't have to thing too much about the moving parts, are a good practice (IMO), resulting in a low overhead for developers when building screens, and overall more consistent styles. @Charly6596 workaround works but it breaks the colocation of styles, which sounds like hard to maintain in the long run with hundreds of components. |
I had this problem too and solved it by accessing the variant prop from import {
SpacingProps,
createRestyleComponent,
VariantProps,
createVariant,
useTheme,
spacing,
} from "@shopify/restyle";
import React from "react";
import { TouchableOpacity } from "react-native";
import { Text } from "./Text";
import { Theme } from "./theme";
export const BaseButton = createRestyleComponent<
VariantProps<Theme, "buttonVariants"> &
SpacingProps<Theme> &
React.ComponentProps<typeof TouchableOpacity>,
Theme
>([createVariant({ themeKey: "buttonVariants" }), spacing], TouchableOpacity);
type ButtonProps = React.ComponentProps<typeof BaseButton> & {
label: string;
};
export const Button = ({ label, ...rest }: ButtonProps) => {
const theme = useTheme<Theme>();
return (
<BaseButton {...rest}>
<Text
variant="buttonLabel"
color={theme.buttonVariants[rest.variant].color}
>
{label}
</Text>
</BaseButton>
);
}; Theme: export const theme = createTheme({
...
buttonVariants: {
primary: {
backgroundColor: "primary",
borderColor: "primary",
color: "white",
},
outlined: {
borderColor: "primary",
color: "secondary",
},
},
... That |
@vonkanehoffen // not sure if it will work but something like that 😅
color={theme.buttonVariants[rest.variant].color as ColorProps<Theme>} |
Even if we add as there is a typescript issue with color
I have tried with another property (fontSize) and it works fine with it. It seems to be specific to color |
Here is how I fix the TS issue
|
I had some trouble with the
|
Below is my custom button component.
I am trying to change the Text color based on the chosen variant, how can I access the restyle 'color' property directly from the props rather than using the style attribute. So for example I want to use
<Text variant="body" color= {props.color}>
which maps to thecolor
specified in the variant e.gprimrayDark
. Thanks!The text was updated successfully, but these errors were encountered: