1.1.1 • Published 9 months ago

react-native-fast-collapsible v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

React Native Fast Collapsible

Pure Javascript library for React Native with super-fast collapsible component using Reanimated v3 API. It works with Expo.

Motivation

There are currently many libraries available for Collapsible components, but they are either no longer supported or do not implement the latest innovations in the React Native architecture. Hence the need for a library that works with the new architecture based on the React Native Reanimated API V3 and allow to have always native performance.

Prerequisites

⚠️ Peer Dependencies

This library has a peer dependency on react-native-reanimated has to be installed, linked and configured into your project. Follow react-native-reanimated to install the dependency.

Installation

yarn install react-native-fast-collapsible

Usage / API

Collapsible component

A simple component that enables an animated action to hide and show content.

Collapsible component

Example

import React, { useState } from 'react';

import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { Collapsible } from 'react-native-fast-collapsible';

export default function SimpleCollapsible() {
  const [isVisible, setVisibility] = useState(false);

  const toggleVisibility = () => {
    setVisibility((previous) => !previous);
  };

  return (
    <View style={styles.container}>
      <TouchableOpacity onPress={toggleVisibility}>
        <Text>Expand / Collapse</Text>
      </TouchableOpacity>

      <Collapsible isVisible={isVisible}>
        <Text>Lorem ipsum....</Text>
      </Collapsible>
    </View>
  );
}

Properties

PropertyTypeDescriptionDefault
isVisiblebooleanWhether to show the child components or nottop
heightOffsetnumberOffset of collapsed children visible0
durationnumberTime in ms of animation300
easingEasingType of animation from ReanimatedEasing.linear
childrenReactNodeChildren to show or hide

useCollapsible hook

Headless hook to create your own collapsible components.

Customized collapsible component

Example

import React, { useState } from 'react';

import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { useCollapsible } from 'react-native-fast-collapsible';
import Animated, {
  Easing,
  interpolate,
  useAnimatedStyle,
} from 'react-native-reanimated';

export default function HeadlessCollapsible() {
  const [isVisible, setVisibility] = useState(false);

  const { animatedStyles, onLayout, height, maxHeight } = useCollapsible({
    isVisible,
    easing: Easing.bounce,
    duration: 1000,
  });

  const toggleVisibility = () => {
    setVisibility((previous) => !previous);
  };

  const arrowStyles = useAnimatedStyle(() => {
    const degree = interpolate(height.value, [0, maxHeight], [0, 180]);

    return {
      transform: [{ rotate: `${degree}deg` }],
    };
  });

  return (
    <View style={styles.container}>
      <TouchableOpacity
        onPress={toggleVisibility}
        style={styles.buttonContainer}
      >
        <Text>Expand / Collapse</Text>
        <Animated.Text style={arrowStyles}>↓</Animated.Text>
      </TouchableOpacity>

      <Animated.View
        style={[animatedStyles, styles.overflowHidden]}
        pointerEvents={isVisible ? 'auto' : 'none'}
      >
        <View onLayout={onLayout} style={styles.collapsibleContainer}>
          <Text>Lorem ipsum...</Text>
        </View>
      </Animated.View>
    </View>
  );
}

Config object options

PropertyTypeDescriptionDefault
isVisiblebooleanWhether to show the child components or nottop
heightOffsetnumberOffset of collapsed children visible0
durationnumberTime in ms of animation300
easingEasingType of animation from ReanimatedEasing.linear

Hook returned object

PropertyTypeDescription
onLayout(event: LayoutChangeEvent) => voidFunction need to be used on children container
heightSharedValue<number>Reanimated shared value with current height
animatedStylesnumberStyles of children container
maxHeightnumberChildren max height

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

Maintainers

License

MIT License. © Lukasz Kurant 2023

1.1.1

9 months ago

1.1.0

9 months ago