An accordion allows to show and hide a piece of content with a smooth animation. Commonly used in "FAQ" sections.
Loading...
The following implementation of an accordion relies on shared values. Leveraging shared values helps to prevent unnecessary re-renders. We define shared values using the useSharedValue hook.
const height =useSharedValue(0);
The AccordionItem component encapsulates each item in the accordion. A height shared value manages the height of the item. The height dynamically adjusts based on the isExpanded prop, resulting in smooth expansion and collapse animations. The duration prop controls the duration of the animation.
Inside the AccordionItem, the children represent the content section. It can accommodate various types of content.
Bottom sheets are surfaces containing supplementary content, anchored to the bottom of the screen. They can provide users with quick access to contextual information, actions, or settings without interrupting their current workflow.
The BottomSheet component accepts props such as isOpen - a shared value indicating whether the bottom sheet is open or closed, toggleSheet - a function to toggle the visibility of the bottom sheet, and an optional duration for animation.
The height shared value is used to track the height of the bottom sheet, while the progress derived value interpolates between 0 and 1 based on the state of isOpen, controlling the animation of the bottom sheet.
The useAnimatedStyle hook helps in creating animated styles based on shared values. These styles are then applied to BottomSheet to make it visually dynamic by adding backdrop and translating bottom sheet to the top.
Flip card allows you to display different content depending on whether the card is flipped or not. It can be especially useful when you do not want to display some data immediately after entering the screen (e.g. secure data) and only after fulfilling a certain condition or performing an action.
Loading...
For storing information about whether the card is flipped or not we use shared value with the useSharedValue hook. Using shared values helps to prevent unnecessary re-renders.
const isFlipped =useSharedValue(false);
This allows us to interpolate values between 0-180 and 180-360 degrees, depending on whether the card is flipped or not. In addition, we use withTiming util which makes our animation smooth.
The FlipCard component accepts several props: duration allows you to change the duration of the animation, setting direction to the value x allows you to change the direction of our animation, RegularContent and FlippedContent give ability to display different content for flipped and non flipped variants.
The FloatingActionButton is a reusable component that manages button styles, content and animations. For this we use props: buttonLetter, index and isExpanded.
We define the animated styles for the buttons within the FloatingActionButton component, passing the necessary values as props. The delay in their appearance on the screen is calculated based on the button's index. Buttons with a higher index will appear later and be positioned higher in the "column" of buttons.
A marquee is an element used to display scrolling content horizontally within a confined space. It's commonly seen in applications to information such as news tickers, advertisements, or any content that needs continuous display within a limited area.
It is located inside ChildrenScroller component that manages the scrolling animation by updating the offset value. It determines the horizontal translation of the child components, creates clones of the children and animates them horizontally based on the specified duration.
The Marquee component serves as the main orchestrator of the marquee effect. It calculates necessary dimensions, renders child components within a container, and coordinates the animation by utilizing the ChildrenScroller component.
Section lists allow you to organize long lists of content by dividing them with headings.
Loading...
The primary component, SectionList, acts as the main orchestrator of the entire Section List interface. It coordinates the rendering of the table of contents and individual content sections.
Within SectionList, there are two key components: TableOfContents and SectionCards.
TableOfContents is responsible for rendering the list of section names as a table of contents. It receives props such as data, visibleIndex, sectionCardsRef, and tableOfContentsRef to manage navigation and synchronization between the table of contents and section content.
SectionCards, on the other hand, manages the rendering of individual sections and their corresponding content. It receives props: sections, visibleIndex, sectionCardsRef, and tableOfContentsRef to render the content sections and handle scrolling interactions.
The onScroll in SectionCards calculates the offset as the user scrolls through the content and determines which section is currently most visible on the screen. It is done by comparing the distance of each section from the top of the screen - it identifies the section closest to the viewport's top edge.
We use the useSharedValue hook to create mutable shared values across different components. For instance, selectedItem and visibleIndex are shared values used to manage the currently selected section and its visibility index.
sectionCardsRef={sectionCardsRef} />
Additionally, we use useAnimatedStyle hook to define animated styles based on the shared values. Then, we apply these animated styles to components to create dynamic visual effects, such as changing font weights and adding bottom borders.
To enable interaction with the FlashList component - such as scrolling to specific sections, the code utilizes variables created using useRef such as sectionCardsRef and tableContentsRef
data={data} estimatedItemSize={52}
Here, the debounce function throttles the invocations of onScroll event handler which improves the performance.
Slider allows users to adjust a value or control a setting by sliding a handle along a track. It is commonly used to adjust settings such as volume, brightness, or in this case, the width of a box.
Loading...
We use the useSharedValue hook to store the offset of the slider handle, allowing for smooth animation during sliding.
const offset =useSharedValue(0);
This example is done using Pan gesture from react-native-gesture-handler library. It adjusts the handle's position and width of the box accordingly to the current offset. The offset is a shared value and is updated during the onChange event of the pan gesture.
The useAnimatedStyle hook is used to create animated styles for both the box and the slider handle. This ensures that changes to the offset value result in smooth animations for both components.
Leveraging animated props allows us to run them on the UI thread instead of the JS thread. To prevent unnecessary re-renders when the text displaying the current width of the box changes, we used the useAnimatedProps hook.
Additionally, we opted for TextInput instead of Text because TextInput has a text property that can be animated, whereas Text only has children.
This approach also enabled us to animate TextInput using shared values.
A switch element is a user interface component that allows users to toggle between two or more states. It is commonly used to turn on/off a setting, enable/disable a feature, or select between options.
Loading...
The following implementation of a switch relies on animatable values. Leveraging animatable values of color and position enables smooth transition between the two states.
We use the useSharedValue hook to store the dimensions of the element, which allows for precise calculation of position changes during the animation. The hook is there to prevent unnecessary re-renders.
The Switch component can represent any boolean value passed as a prop. The state dynamically adjusts based on the value prop resulting in smooth transition animations. It enables passing any function using the onPress prop. The duration prop controls the duration of the animation. The style and trackColors props enable personalization.