React Native Material

repository·main·Indexed 42 days ago

https://github.com/yamankatby/react-native-material

A set of modular, customizable, and cross-platform UI components for React Native applications following Material Design principles. Version 1.4.1 provides core components such as Appbar, Button, FAB, Avatar, and Typography, along with a ThemeProvider for managing palettes, shapes, and elevations.

Tokens
8.7K
Snippets
29
Records
33
Agent score
53%

What's inside React Native Material

  1. Available Material UI Components

    main

    The @react-native-material/core package provides a variety of pre-built components following Material Design principles. Key components include:

    • <Appbar />: Displays information and actions for the current screen (supports prominent and bottom app bars).
    • <Avatar />: Used for user profiles or icons in lists and menus.
    • <Backdrop />: A surface that appears behind other content to show contextual information.
    • <Badge />: Displays dynamic information (e.g., notification counts) on navigation elements.
    • <Banner />: Shows prominent messages with optional actions.
    • <Button />: Triggers actions or choices via single taps.
    • <Divider />: A thin line used to group content in layouts.
    • <FAB />: Floating Action Button representing the primary screen action.
    • <Surface />: Displays content on different shapes to communicate state or brand.
    • <Typography />: Used for presenting text content clearly and efficiently.
  2. Quick Start with Button

    main

    To get started immediately, import the Button component from @react-native-material/core. This example demonstrates a basic button that triggers an alert.

    import React from "react";
    import { Button } from "@react-native-material/core";
    
    export default function App() {
      return <Button title="Button" onPress={() => alert("hi!")} />;
    }
  3. Handle Button loading states

    main

    You can show a loading state using the loading prop. You can control where the indicator appears using loadingIndicatorPosition:

    • leading: Replaces the leading element.
    • trailing: Replaces the trailing element.
    • overlay: Adds the indicator as an absolute overlay over the entire button (hiding the title/icons).

    Custom indicators can be passed via loadingIndicator as a string, a React node, or a function receiving { color: string }.

    <Button 
      loading={true} 
      loadingIndicatorPosition="overlay" 
      loadingIndicator={<ActivityIndicator color="white" />} 
      title="Loading..."
    />
  4. Configure the application with ThemeProvider

    main

    Wrap your application (or a specific component tree) with the ThemeProvider to provide theme data to all child components.

    By default, ThemeProvider detects the device's color scheme using React Native's useColorScheme hook and automatically switches between darkTheme and defaultTheme. You can also provide a custom theme object to override this behavior.

    import { ThemeProvider } from '@react-native-material/core';
    
    function App() {
      return (
        <ThemeProvider>
          <YourAppComponents />
        </ThemeProvider>
      );
    }
  5. Integrate custom icons using IconComponentProvider

    main

    To use custom icon components with the library's <Icon /> component, you must wrap your application (or a specific subtree) with the IconComponentProvider. This provider allows the library's internal <Icon /> component to resolve which icon library (like Material Icons, FontAwesome, etc.) to use.

    1. Create or identify your icon component that accepts name, size, and color props.
    2. Wrap your app with IconComponentProvider and pass your component to the IconComponent prop.
    3. Use the <Icon /> component anywhere inside that provider.
    import { IconComponentProvider, Icon } from '@react-native-material/core';
    // Example: Using a hypothetical MyIconLibrary
    import { MyIconLibrary } from 'my-icon-library';
    
    const App = () => (
      <IconComponentProvider IconComponent={MyIconLibrary}>
        <Icon name="home" size={24} color="primary" />
      </IconComponentProvider>
    );
  6. Troubleshoot undefined IconComponent error

    main

    If you encounter the error: 'IconComponent' is undefined. Seems like you forgot to wrap your component with 'Provider'.'

    This occurs because the <Icon /> component was used outside of an <IconComponentProvider />. Ensure that your component tree is wrapped with the provider and that you have passed a valid component to the IconComponent prop.

  7. Use the TextInput component

    main

    The TextInput component is a Material Design-inspired text input for React Native. It supports multiple visual variants, labels, helper text, and icons (leading/trailing elements). It extends the standard react-native TextInputProps.

    import TextInput from './TextInput';
    
    // Basic usage
    <TextInput 
      label="Username" 
      placeholder="Enter your username" 
    />
    
    // Usage with variant and icons
    <TextInput
      variant="outlined"
      label="Email"
      leading={({ color, size }) => (
        <Icon color={color} size={size} name="email" />
      )}
      trailing={<ClearIcon />}
      helperText="We'll never share your email."
    />
  8. Use the Icon component

    main

    The <Icon /> component renders an icon based on the icon library provided via IconComponentProvider. It automatically handles color resolution using the theme's palette.

    Props:

    • name (string): The identifier for the icon (e.g., 'home', 'settings').
    • size (number, optional): The size of the icon.
    • color (Color, optional): A theme color key (e.g., 'primary', 'on-background'). Defaults to 'on-background'.
    • ...rest: Any other props passed to the underlying icon component.
    import { Icon } from '@react-native-material/core';
    
    // Usage inside an IconComponentProvider
    <Icon name="favorite" color="error" size={32} />
  9. Use window size and breakpoints for responsive layouts

    main

    The library re-exports utilities from react-native-window-size to help manage responsive layouts. You can use the WindowSizeProvider to wrap your application and access window dimensions and breakpoints via hooks like useWindowSize and useActiveBreakpoint.

    import {
      WindowSizeProvider,
      useWindowSize,
      useActiveBreakpoint,
      Breakpoint
    } from '@react-native-material/core';
    
    // Wrap your app
    <WindowSizeProvider>
      <MyComponent />
    </WindowSizeProvider>
    
    // Inside a component
    const MyComponent = () => {
      const { width, height } = useWindowSize();
      const activeBreakpoint = useActiveBreakpoint();
    
      return <Text>Width: {width}, Breakpoint: {activeBreakpoint}</Text>;
    };
  10. Use the AppBar component

    main

    The AppBar component is a Material UI-inspired bar used for navigation or actions, typically placed at the top or bottom of a screen. It supports titles, subtitles, leading/trailing elements (like icons), and customizable color schemes based on the theme palette.

    Key features:

    • Variants: Position the bar at the 'top' (default) or 'bottom'.
    • Dynamic Content: title, subtitle, leading, and trailing props can accept strings, React nodes, or render functions that receive theme-aware colors and sizes.
    • Theming: Use color to set the background palette and tintColor to override the content color. The enableColorOnDark prop controls whether the background color is applied when the device is in dark mode.
    • Layout: Supports centering the title via centerTitle.
    import AppBar from '@react-native-material/core/AppBar';
    import { Icon } from '@react-native-material/core'; // Assuming Icon exists
    
    const MyScreen = () => (
      <AppBar
        variant="top"
        title="My App"
        subtitle="Subtitle text"
        leading={({ color, size }) => (
          <Icon name="menu" color={color} size={size} />
        )}
        trailing={({ color, size }) => (
          <Icon name="search" color={color} size={size} />
        )}
        color="primary"
        centerTitle={true}
      />
    );
  11. Use the Chip component

    main

    The Chip component is a compact element used to represent an attribute, filter, or action. It supports different variants, custom labels, and optional leading or trailing elements (like icons).

    Props

    PropTypeDescription
    labelstring | React.ReactNode | ((props: { color: string }) => React.ReactNode | null) | nullThe text or element to display in the chip.
    leadingReact.ReactNode | ((props: { color: string; size: number }) => React.ReactNode | null) | nullAn element placed before the label. If a function, it receives color and size (default 24).
    trailingReact.ReactNode | ((props: { color: string; size: number }) => React.ReactNode | null) | nullAn element placed after the label. If a function, it receives color and size (default 18).
    variant'filled' | 'outlined'The visual style of the chip. Defaults to 'filled'.
    colorColorThe color theme for the chip. Defaults to 'on-surface'.
    labelStyleStyleProp<TextStyle>Style for the label text.
    contentContainerStylePressableProps['style']Style for the internal pressable container.
    leadingContainerStyleStyleProp<ViewStyle>Style for the leading element's container.
    trailingContainerStyleStyleProp<ViewStyle>Style for the trailing element's container.
    onPressPressableProps['onPress']Callback for press events.
    disabledbooleanWhether the chip is interactive.

    Usage Example

    import Chip from '@react-native-material/core/Chip';
    
    // Basic filled chip
    <Chip label="Tag" />
    
    // Outlined chip with an icon
    <Chip 
      label="Filter" 
      variant="outlined" 
      leading={() => <Icon name="filter" color="#000" size={24} />} 
    />
    
    // Chip with dynamic label color
    <Chip 
      label={({ color }) => <Text style={{ color }}>Dynamic Color</Text>} 
    />