From 6adfead16fb339d82b90c5e7d9a8680889e5c7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateo=20Guzm=C3=A1n?= Date: Sun, 21 Aug 2022 10:34:37 +0200 Subject: [PATCH] feat(hearts): adding colors and theming support for hearts --- docs/docs/components/hearts.mdx | 7 ++++--- example/src/App.tsx | 8 +++++++- src/components/Heart.tsx | 5 +++-- src/components/Hearts.tsx | 29 ++++++++++++++++++++++------- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/docs/docs/components/hearts.mdx b/docs/docs/components/hearts.mdx index 472ad93..4bb4899 100644 --- a/docs/docs/components/hearts.mdx +++ b/docs/docs/components/hearts.mdx @@ -12,9 +12,9 @@ A set of animated hearts. ### Set of hearts -| Name | Type | Description | -| :-------------- | :--------- | :---------------------------------------------------------------------------------------- | -| theme? | `string[]` | An array of colors used to add theming to the animation. Fiesta theme is used by default. | +| Name | Type | Description | +| :----- | :--------- | :---------------------------------------------------------------------------------------- | +| theme? | `string[]` | An array of colors used to add theming to the animation. Fiesta theme is used by default. | ```js import { StyleSheet, View } from 'react-native'; @@ -44,6 +44,7 @@ You can also use a single heart. Notice that you need to wrap the heart with a ` | x | `number` | Initial x position of the heart. | | y | `number` | Initial y position of the heart. | | autoplay? | `boolean` | Whether the heart should animate automatically or not (default is `true`). | +| color? | `boolean` | Heart color (default is `rgba(238, 17, 131, 1)`). | ```js import { StyleSheet } from 'react-native'; diff --git a/example/src/App.tsx b/example/src/App.tsx index 1925ace..31fbabc 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -97,7 +97,13 @@ function App() { setComponentToRender()} + onPress={() => + setComponentToRender( + + ) + } style={styles.pressable} > diff --git a/src/components/Heart.tsx b/src/components/Heart.tsx index b18fe1f..718dca0 100644 --- a/src/components/Heart.tsx +++ b/src/components/Heart.tsx @@ -7,9 +7,10 @@ export interface HeartProps { x: number; y: number; autoplay?: boolean; + color?: string; } -function Heart({ x, y, autoplay = true }: HeartProps) { +function Heart({ x, y, autoplay = true, color = palette.red }: HeartProps) { const opacity = useValue(1); const changeOpacity = useCallback( @@ -30,7 +31,7 @@ function Heart({ x, y, autoplay = true }: HeartProps) { diff --git a/src/components/Hearts.tsx b/src/components/Hearts.tsx index 81f86cf..50cf838 100644 --- a/src/components/Hearts.tsx +++ b/src/components/Hearts.tsx @@ -1,4 +1,4 @@ -import React, { memo, useCallback, useEffect } from 'react'; +import React, { memo, useCallback, useEffect, useMemo } from 'react'; import { StyleSheet } from 'react-native'; import { Canvas, @@ -11,13 +11,23 @@ import { screenHeight } from '../constants/dimensions'; import { screenWidth } from '../constants/dimensions'; import Heart from './Heart'; import { shuffleArray } from '../utils/array'; +import { colorsFromTheme } from '../utils/colors'; +import { FiestaThemes } from '../constants/theming'; const xGap = 40; -const optimalNumberOfStars = Math.floor(screenWidth / xGap); -const starsToRenderArray = [...Array(optimalNumberOfStars)]; -const yPositions = shuffleArray(starsToRenderArray.map((_, i) => i * xGap)); +const optimalNumberOfHearts = Math.floor(screenWidth / xGap); +const heartsToRenderArray = [...Array(optimalNumberOfHearts)]; +const yPositions = shuffleArray(heartsToRenderArray.map((_, i) => i * xGap)); -function Hearts() { +interface HeartsProps { + theme?: string[]; +} + +function Hearts({ theme = FiestaThemes.default }: HeartsProps) { + const colors = useMemo( + () => colorsFromTheme(theme, optimalNumberOfHearts), + [theme] + ); const yPosition = useValue(screenHeight); const changeItemPosition = useCallback( @@ -44,8 +54,13 @@ function Hearts() { return ( - {starsToRenderArray.map((_, index) => ( - + {heartsToRenderArray.map((_, index) => ( + ))}