react-native-signature-canvas

repository·master·Indexed 19 days ago

https://github.com/yanyuanfe/react-native-signature-canvas

A performant, customizable React Native signature canvas for iOS, Android, and Expo. Version 5.1.0 provides TypeScript support, WebView optimization, and advanced error handling. It allows users to capture signatures or draw on a canvas with support for background and overlay images, custom pen configurations, and imperative control via refs for actions like clearing, undoing, and redoing strokes.

Tokens
10K
Snippets
25
Records
31
Agent score
67%

What's inside react-native-signature-canvas

  1. Understand the limitations of webviewProps

    master

    While webviewProps allows for extensive customization, it cannot override the Core Props required for the signature pad to function. These protected props take precedence over anything defined in webviewProps:

    • ref: Internal WebView reference
    • source: HTML content for signature pad
    • onMessage: Message handler for signature events
    • onError: Error handler
    • onLoadEnd: Load completion handler
    • onLoadStart: Load start handler
    • onLoadProgress: Load progress handler
    • javaScriptEnabled: Must be true for the signature pad to work
    • useWebKit: Uses modern WebKit engine
  2. Migrate WebView props to webviewProps

    master

    In newer versions, individual WebView-related props should be consolidated into the webviewProps object. While old individual props are supported for backward compatibility, the new pattern is preferred.

    Before:

    <SignatureCanvas
      androidLayerType="hardware"
      androidHardwareAccelerationDisabled={false}
    />

    After:

    <SignatureCanvas
      webviewProps={{
        androidLayerType: "hardware",
        androidHardwareAccelerationDisabled: false,
      }}
    />
  3. Install react-native-signature-canvas

    master

    To use the signature canvas, install the main package. If you are using a React Native CLI project (not Expo), you must also install react-native-webview and run pod install for iOS.

    # Install the package
    npm install react-native-signature-canvas
    
    # For React Native CLI projects, also install WebView
    npm install react-native-webview
    cd ios && pod install # iOS only
  4. Migrate from v4.x to v5.x

    master

    Version 5.x is fully backward compatible with v4.x. It introduces two key new features:

    1. Enhanced error handling: Use the onError callback to catch and handle errors.
    2. WebView customization: Use the webviewProps prop to pass configuration directly to the underlying WebView.
    // NEW: Enhanced error handling
    <SignatureCanvas
      onError={(error) => console.error(error)} // New callback
    />
    
    // NEW: WebView customization
    <SignatureCanvas
      webviewProps={{ // New prop
        cacheEnabled: false,
        androidLayerType: "software"
      }}
    />
  5. Optimize SignatureCanvas performance

    master

    Depending on your device requirements, you can use different webviewProps configurations to balance performance and memory usage.

    // High-performance mode
    <SignatureCanvas
      webviewProps={{
        cacheEnabled: true,
        androidLayerType: "hardware",
        androidHardwareAccelerationDisabled: false,
      }}
    />
    
    // Low-memory mode
    <SignatureCanvas
      webviewProps={{
        cacheEnabled: false,
        androidLayerType: "software",
        androidHardwareAccelerationDisabled: true,
      }}
    />
  6. Handle SignatureCanvas errors

    master

    Use the onError prop to catch errors. While the component automatically attempts to recover from recoverable errors, you can use this callback to log errors or update your UI.

    const [error, setError] = useState(null);
    
    const handleError = (error) => {
      console.error('Signature error:', error);
      setError(error.message);
      // Error recovery is automatic, but you can handle it here
    };
    
    <SignatureCanvas
      onError={handleError}
      // Component automatically retries on recoverable errors
    />
    
    {error && (
      <Text style={{ color: 'red' }}>Error: {error}</Text>
    )}
  7. Configure WebView via webviewProps

    master

    You can pass configuration directly to the underlying WebView using the webviewProps prop. This is useful for optimizing performance or security.

    ### High Performance Setup
    ```jsx
    <SignatureCanvas
      webviewProps={{
        cacheEnabled: true,
        androidLayerType: "hardware",
        androidHardwareAccelerationDisabled: false,
      }}
    />

    Low Memory Setup

    <SignatureCanvas
      webviewProps={{
        cacheEnabled: false,
        androidLayerType: "software", 
        androidHardwareAccelerationDisabled: true,
      }}
    />

    Security Focused Setup

    <SignatureCanvas
      webviewProps={{
        allowFileAccess: false,
        allowFileAccessFromFileURLs: false,
        mixedContentMode: "never",
        allowsLinkPreview: false,
      }}
    />