Replies: 5 comments
-
another question is, I can define some variants in my project theme, But I want one component in my library to use it. It won't work right now because the component in my library can only recognize the variants I defined in the library theme. |
Beta Was this translation helpful? Give feedback.
-
Hey @chj-damon 1. A Shared theme Provider // src/components/Providers/ThemeProvider.tsx
import React from "react";
import { ApplicationProvider as KittenProvider } from "@ui-kitten/components";
import * as eva from "@eva-design/eva";
import { ThemeProvider as RestyleProvider } from "@shopify/restyle";
import { ProvidersProps } from "./Providers";
import { default as kittenTheme } from "~/constants/app-theme.json";
import restyleTheme from "~/constants/theme";
const ThemeProvider = ({ children }: ProvidersProps) => {
return (
<KittenProvider {...eva} theme={{ ...eva, ...kittenTheme }}>
<RestyleProvider theme={restyleTheme}>{children}</RestyleProvider>
</KittenProvider>
);
};
export default ThemeProvider; 2. import colors from KittenUI // src/constants/theme.ts
import { createTheme } from "@shopify/restyle";
import { default as palette } from "./app-theme.json"; <----- KittenUI theme
const restyleTheme = createTheme({
colors: {
...palette, <------ inject the palette from KittenUI
},
spacing: {
xs: 4,
s: 8,
m: 16,
l: 24,
xl: 32,
"2xl": 48,
"3xl": 96,
"4xl": 192,
"5xl": 256,
},
breakpoints: {
phone: 0,
tablet: 768,
},
borderRadii: {
xs: 4,
s: 16,
m: 24,
l: 64,
},
});
export type Theme = typeof restyleTheme;
export default restyleTheme; I have drawbacks, it's not perfect. Does my answer helped ? |
Beta Was this translation helpful? Give feedback.
-
@flexbox thanks for you reply, But I think I have different scenario: I use import { theme as libraryTheme } from 'ui-library';
export const projectTheme = {
...libraryTheme,
colors: {
...libraryTheme.colors,
// custom colors
},
textVariants: {
...libraryTheme.textVariants,
// custom text variants
}
} in this way, I can override the pre-defined variables in the library, it works fine. But, If I want the library to accept some extended variants or colors, it won't work, because the components in the library is not aware of these variables which are not defined in the library theme. for example, If I want to define another textVariants and use import { createText } from '@shopify/restyle';
import { Theme } from '../theme';
const Text = createText<Theme>();
export default Text; you see, I wonder if I'm using restyle the wrong way.... |
Beta Was this translation helpful? Give feedback.
-
any updates nowadays? |
Beta Was this translation helpful? Give feedback.
-
One possible solution can be passing a generic theme type to your component: import React from 'react';
import {TextProps as ReactNativeTextProps} from 'react-native';
import {BaseTheme, createText, TextProps} from '@shopify/restyle';
const Text = <Theme extends BaseTheme>(
props: TextProps<Theme> & ReactNativeTextProps,
) => {
const Comp = createText<Theme>();
return <Comp {...props} />;
};
export default Text; To consume it u can do something like: <Text<Theme> variant={'button2'} >I am a text</Text> To avoid passing Also, if your theme is in a specific format, u can force the passed generic Theme to extend your theme instead of Keep in mind that this can lead to a performance issue so u can consider memoizing const Comp = useMemo(() => createText<Theme>(), []); Or move it outside the component with any as generic. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I use restyle to build my react-native component library, there's a theme file exists in the library.
I now start to build a project and I want to use restyle too.
then the question is, How can I inject the project theme into the library so the components can follow the same theme with the app?
Or, if I misunderstood the scenario, what is the best practice about it?
Beta Was this translation helpful? Give feedback.
All reactions