Welcome to the gluestack-ui component library! This guide outlines the structure, best practices, and expectations when contributing new components, specifically using the example of a Button component.
Component Structure:
Component Scope:
SCOPE
for each component. This helps manage context, and ensures proper theming/scoping of styles.const SCOPE = 'BUTTON';
Primitive Components:
Pressable
, Text
, View
, etc., and wrap them with utilities like withStyleContext
to manage shared props and context properly.
Component Context:
Use useStyleContext
to inherit parent styles and states in child components like ButtonText
, ButtonIcon
, and ButtonGroup
.
Example:
const { variant: parentVariant } = useStyleContext(SCOPE);
Example:
withStyleContext(Pressable, SCOPE)
Not need if there is no creator API Just use simple context api with scope.
Reusability & Variants:
Components should be highly configurable. Use the tva
(Tailwind Variants API) to define base styles, variants, and compound variants for different sizes, actions, and states.
Use Tokens only color token defined by us like:- primary, secondary
, avoid using hard coded color tokens like bg-red-500
If you don’t know how to design don’t worry create basic component design we will improve design according to our design guidelines.
Example:
const buttonStyle = tva({
base: 'base-styles',
variants: {
size: {
sm: 'small',
lg: 'large',
},
action: {
primary: 'bg-primary',
secondary: 'bg-secondary',
},
},
});
Composability:
CSS Interoperability:
cssInterop
to ensure styles are handled across React Native and web.
Styling:
Accessibility:
Testing:
Performance:
useMemo
, React.forwardRef
, and React.memo
to optimize component rendering and prevent unnecessary re-renders.Type Safety:
VariantProps
and appropriate interfaces.Display Names:
displayName
for better debugging and readability in React Developer Tools.Example:
Button.displayName = 'Button';
ButtonText.displayName = 'ButtonText';