Skip to content

Instantly share code, notes, and snippets.

@petekp
Created July 19, 2019 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petekp/2b096d9fc33a90c73c19cdd305d97a4d to your computer and use it in GitHub Desktop.
Save petekp/2b096d9fc33a90c73c19cdd305d97a4d to your computer and use it in GitHub Desktop.
Substrate Themed Button Example
import * as React from 'react'
import {
TouchableOpacity,
StyleProp,
TouchableOpacityProps,
TextStyle,
Text,
} from 'react-native'
import { ThemedComponentLocked } from '../../typings'
import { withTheme } from '../../ThemeContext/withTheme'
export interface ButtonProps {
children: string | React.ReactNode
textStyle?: StyleProp<TextStyle>
}
type Props = ButtonProps & TouchableOpacityProps
const defaultButtonTextStyle: StyleProp<TextStyle> = {
textAlign: 'center',
}
export const Button: React.FC<Props> = React.memo(
({
children,
style: inlineButtonStyle,
textStyle: inlineTextStyle,
...props
}) => (
<TouchableOpacity {...props} style={inlineButtonStyle}>
{typeof children === 'string' ? (
<Text style={[inlineTextStyle, defaultButtonTextStyle]}>
{children}
</Text>
) : (
children
)}
</TouchableOpacity>
)
)
const themedButton = (theme = 'base') => ({
setTheme,
getThemeStyle,
...props
}: ThemedComponentLocked<Props>) => {
const [viewStyle, textStyle] = getThemeStyle('button', theme)
return <Button style={viewStyle} textStyle={textStyle} {...props} />
}
export const Base = withTheme(themedButton('base'))
export const Primary = withTheme(themedButton('primary'))
export const Secondary = withTheme(themedButton('secondary'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment