![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
anoa-react-native-theme
Advanced tools
Theme and stylesheet manager for ReactNative
npm i anoa-react-native-theme
import { createTheme } from 'anoa-react-native-theme'
export const BlueTheme = createTheme(
// define all theme props here
// like color, thickness, radius, etc.
{
color: {
primary: 'blue',
secondary: 'yellow'
},
border: {
thick: 1,
bold: 10
}
},
// define named styles using defined theme props
props => ({
container: {
padding: 40,
backgroundColor: props.color.primary
},
button: {
borderWidth: props.border.thick,
backgroundColor: props.color.secondary
}
})
)
export const RedTheme = BlueTheme.extend(
// override default theme props value
{
color: {
primary: 'red',
secondary: 'green'
}
},
// override default theme style
props => ({
container: {
padding: 80
},
button: {
borderWidth: props.border.bold
}
})
)
import { ThemeContext } from 'anoa-react-native-theme'
export const AppTheme = new ThemeContext(BlueTheme, {
red: RedTheme
// define the rest themes you have, eg:
// green: GreenTheme,
// yellow: YellowTheme
// etc ...
// Notice: all themes defined here must created/extended
// from default theme or child of default theme.
})
We are using React Context to provide themes accessibility.
The AppTheme
you've created above contains Provider
and Consumer
property.
You have to wrap your root component with Provider
to make themes
available in every component inside.
export default class App extends Component {
// ...
render() {
return (
<AppTheme.Provider
// optional props to get default theme
getDefault={async () => {
// do async operation to get selected theme
// should returns 'default' or the object key of themes
// you've defined in the context (eg: 'red' for RedTheme)
return await MyFancyStorage.getTheme()
}}
// optional props, the event that listen when theme get changed
onChange={async key => {
// key returns the 'default' or the object key of themes
await MyFancyStorage.setTheme(key)
}}
>
<View style={styles.container}>
<AwesomeComponent />
</View>
</AppTheme.Provider>
)
}
// ...
}
To consume theme you need to wrap the component with Consumer
. Wrapping with Consumer
can also be done by using withTheme
HOC or withThemeClass
class decorator.
The Consumer
resulting function component
with one parameter called theme
consists of:
props
-- represents current theme variables.
styles
-- represents current theme named stylesheets.
change
- a function to change theme.
For more detail, have a look this example:
export class AwesomeComponent extends React.Component {
// ...
public render() {
return (
<AppTheme.Consumer>
{({ theme: { props, styles, change } }) => (
<View style={styles.container}>
<Button
style={styles.button}
color={props.color.primary}
title="Switch to Red Theme"
onPress={async () => {
change('red')
}}
/>
<Button
style={theme.styles.button}
title="Switch to Default Theme"
onPress={async () => {
change('default')
}}
/>
</View>
)}
</AppTheme.Consumer>
)
}
// ...
}
Use AppTheme.withTheme
as HOC (Higher Order Component) that injects theme
into your component.
export interface ProfileProps {
firstName: string
lastName: string
}
export const Profile = AppTheme.withTheme<ProfileProps>(
({ theme, firstName, lastName }) => (
<View style={theme.styles.container}>
<Text>{firstName}</Text>
<Text>{lastName}</Text>
</View>
)
)
If you are fans of class decorator, you can use AppTheme.withThemeClass
class decorator to inject theme
into your component class.
// Theme props injected into AppThemeProps (see explanation bellow).
// We make it Partial as we don't wont this props to be required when
// using this component.
export interface LoginProps extends Partial<AppThemeProps> {
// some props
}
@AppTheme.withThemeClass()
export class Login extends React.Component<LoginProps> {
constructor(props: LoginProps) {
super(props)
}
public render() {
// consume theme props, cast it as Required props
// as it always injected here.
const { theme } = this.props as Required<LoginProps>
return (
<View style={theme.styles.button}>
<Text style={{ color: theme.props.color.primary }}>Login</Text>
</View>
)
}
}
AppThemeProps
For typing convenience we need AppThemeProps
interface that provide app theme props accessibility. Creating this interface is easily done by:
import { ThemeContextProps } from 'anoa-react-native-theme'
// Use the default theme type (BlueTheme) to presents props values.
export type AppThemeProps = ThemeContextProps<typeof BlueTheme>
The AppTheme
also comes with createStyleSheet
function that let you create custom local styles for your component using theme props values.
@AppTheme.withThemeClass()
export class About extends React.Component<AboutProps> {
constructor(props: AboutProps) {
super(props)
}
public render() {
return (
<View style={styles.container}>
<Text>About</Text>
</View>
)
}
}
// create stylesheet based on theme props value
const styles = AppTheme.createStyleSheet(props => ({
container: {
backgroundColor: props.color.primary,
padding: 25
}
}))
MIT
FAQs
Theme and stylesheet manager for ReactNative
We found that anoa-react-native-theme demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.