Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align handling colors with RN #5825

Merged
merged 3 commits into from
May 15, 2024

Conversation

maciekstosio
Copy link
Contributor

@maciekstosio maciekstosio commented Mar 22, 2024

Summary

Fixes #5746. Not all color formats and syntax supported by RN is supported by reanimated. This PR should align with RN implementation. An example for rgb,rgba,hsl,hsl with comma and without (for view and Animated.View):

Before After
Color.Berfore.mov
Color.After.mov

Test plan

Reused RN tests.

Example from the video
import { StyleSheet, View, Text } from 'react-native';

import Animated, {
  useAnimatedStyle,
  useSharedValue,
  withRepeat,
  withTiming,
} from 'react-native-reanimated';
import React, { useEffect } from 'react';

const CompareColumnAnimated = ({
  title,
  withComma,
}: {
  title: string;
  withComma?: boolean;
}) => {
  const sv = useSharedValue(0);

  const toRgb = (value: number) => {
    'worklet';
    return Math.floor(Math.abs(value) * 255);
  };

  useEffect(() => {
    sv.value = withRepeat(withTiming(1, { duration: 1000 }), -1, true);
    return () => {
      sv.value = 0;
    };
  }, [sv]);

  return (
    <View style={{}}>
      <View style={styles.header}>
        <Text>{title}</Text>
      </View>
      <Animated.View
        style={[
          styles.box,
          useAnimatedStyle(() => ({
            backgroundColor: `rgb(${toRgb(sv.value)} 255 255)`
              .split(' ')
              .join(withComma ? ', ' : ' '),
          })),
        ]}
      />
      <Animated.View
        style={[
          styles.box,
          useAnimatedStyle(() => ({
            backgroundColor: `rgba(${toRgb(sv.value)} 255 255 0.5)`
              .split(' ')
              .join(withComma ? ', ' : ' '),
          })),
        ]}
      />
      <Animated.View
        style={[
          styles.box,
          useAnimatedStyle(() => ({
            backgroundColor: `hsl(${sv.value * 180} 100% 50%)`
              .split(' ')
              .join(withComma ? ', ' : ' '),
          })),
        ]}
      />
      <Animated.View
        style={[
          styles.box,
          useAnimatedStyle(() => ({
            backgroundColor: `hsla(${sv.value * 180} 100% 50% 0.5)`
              .split(' ')
              .join(withComma ? ', ' : ' '),
          })),
        ]}
      />
    </View>
  );
};

const CompareColumn = ({
  title,
  withComma,
}: {
  title: string;
  withComma?: boolean;
}) => (
  <View style={{}}>
    <View style={styles.header}>
      <Text>{title}</Text>
    </View>
    <View
      style={[
        styles.box,
        {
          backgroundColor: 'rgb(100 255 255)'
            .split(' ')
            .join(withComma ? ', ' : ' '),
        },
      ]}
    />
    <View
      style={[
        styles.box,
        {
          backgroundColor: 'rgba(100 255 255 0.5)'
            .split(' ')
            .join(withComma ? ', ' : ' '),
        },
      ]}
    />
    <View
      style={[
        styles.box,
        {
          backgroundColor: 'hsl(100 100% 50%)'
            .split(' ')
            .join(withComma ? ', ' : ' '),
        },
      ]}
    />
    <View
      style={[
        styles.box,
        {
          backgroundColor: 'hsla(100 100% 50% 0.5)'
            .split(' ')
            .join(withComma ? ', ' : ' '),
        },
      ]}
    />
  </View>
);

export default function EmptyExample() {
  return (
    <View style={styles.container}>
      <CompareColumnAnimated title="anim comma" withComma />
      <CompareColumn title="view comma" withComma />
      <CompareColumnAnimated title="anim no comma" />
      <CompareColumn title="view no comma" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    flexDirection: 'row',
  },
  box: {
    width: 100,
    height: 100,
  },
  header: { justifyContent: 'center', alignSelf: 'center' },
});

@maciekstosio maciekstosio marked this pull request as ready for review March 22, 2024 11:20
src/reanimated2/Colors.ts Show resolved Hide resolved
src/reanimated2/__tests__/Colors.test.ts Outdated Show resolved Hide resolved
@maciekstosio maciekstosio added this pull request to the merge queue May 15, 2024
Merged via the queue into main with commit 597b63e May 15, 2024
7 checks passed
@maciekstosio maciekstosio deleted the @maciekstosio/make-comma-separator-optional branch May 15, 2024 09:19
renovate bot added a commit to GSTJ/react-native-magic-modal that referenced this pull request Jun 3, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[react-native-reanimated](https://togithub.com/software-mansion/react-native-reanimated)
| [`~3.11.0` ->
`~3.12.0`](https://renovatebot.com/diffs/npm/react-native-reanimated/3.11.0/3.12.0)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/react-native-reanimated/3.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/react-native-reanimated/3.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/react-native-reanimated/3.11.0/3.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/react-native-reanimated/3.11.0/3.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>software-mansion/react-native-reanimated
(react-native-reanimated)</summary>

###
[`v3.12.0`](https://togithub.com/software-mansion/react-native-reanimated/releases/tag/3.12.0)

[Compare
Source](https://togithub.com/software-mansion/react-native-reanimated/compare/3.11.0...3.12.0)

#### What's Changed

- Give more meaningful warning when modifying a Shareable by
[@&#8203;tjzel](https://togithub.com/tjzel) in
[software-mansion/react-native-reanimated#5548
- Make animated components use different tags for events by
[@&#8203;szydlovsky](https://togithub.com/szydlovsky) in
[software-mansion/react-native-reanimated#5960
- Add `warning` and `failing` test decorators by
[@&#8203;Latropos](https://togithub.com/Latropos) in
[software-mansion/react-native-reanimated#5929
- Update useAnimatedKeyboard docs by
[@&#8203;maciekstosio](https://togithub.com/maciekstosio) in
[software-mansion/react-native-reanimated#5866
- Change the docs to mention, that `.springify()` works with
`.duration()` by [@&#8203;Latropos](https://togithub.com/Latropos) in
[software-mansion/react-native-reanimated#5990
- \[Android]\[Keyboard] More consistent inequality check to compute
keyboard state by [@&#8203;antFrancon](https://togithub.com/antFrancon)
in
[software-mansion/react-native-reanimated#5874
- fix typo on object key for `targetValues` in custom-animations.mdx by
[@&#8203;JDMathew](https://togithub.com/JDMathew) in
[software-mansion/react-native-reanimated#5994
- Remove outdated code for unsupported React Native versions by
[@&#8203;tomekzaw](https://togithub.com/tomekzaw) in
[software-mansion/react-native-reanimated#5979
- Remove REAInitializer by
[@&#8203;tomekzaw](https://togithub.com/tomekzaw) in
[software-mansion/react-native-reanimated#5681
- Prevent crash on non-existent view updates in Android by
[@&#8203;thomas-rx](https://togithub.com/thomas-rx) in
[software-mansion/react-native-reanimated#5767
- Fix location after shared element transition by
[@&#8203;piaskowyk](https://togithub.com/piaskowyk) in
[software-mansion/react-native-reanimated#6010
- Make `useScrollviewOffset` ref nullable and simplify its code by
[@&#8203;szydlovsky](https://togithub.com/szydlovsky) in
[software-mansion/react-native-reanimated#6009
- fix: use proper classes for bridgeless by
[@&#8203;WoLewicki](https://togithub.com/WoLewicki) in
[software-mansion/react-native-reanimated#5997
- docs: add
[@&#8203;swmansion/t-rex-ui](https://togithub.com/swmansion/t-rex-ui) by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[software-mansion/react-native-reanimated#6015
- Remove `CellRendererComponent` from Animated.FlatList props by
[@&#8203;Latropos](https://togithub.com/Latropos) in
[software-mansion/react-native-reanimated#5951
- Add `useComposedEventHandler` hook by
[@&#8203;szydlovsky](https://togithub.com/szydlovsky) in
[software-mansion/react-native-reanimated#5890
- Align handling colors with RN by
[@&#8203;maciekstosio](https://togithub.com/maciekstosio) in
[software-mansion/react-native-reanimated#5825
- Add more tests - useSharedValue, useAnimatedStyle , useDerivedValue by
[@&#8203;Latropos](https://togithub.com/Latropos) in
[software-mansion/react-native-reanimated#5981
- Make animation RollInLeft work with modifers by
[@&#8203;Latropos](https://togithub.com/Latropos) in
[software-mansion/react-native-reanimated#6039
- Test predefined entering animation by
[@&#8203;Latropos](https://togithub.com/Latropos) in
[software-mansion/react-native-reanimated#5995
- Tests: cancelAnimation, useFrameCallback, measure, withDecay by
[@&#8203;Latropos](https://togithub.com/Latropos) in
[software-mansion/react-native-reanimated#6016
- docs: fix useAnimatedKeyboard page crash by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[software-mansion/react-native-reanimated#6056
- \[Web LA] Remove `existingTransform` by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[software-mansion/react-native-reanimated#6060

#### New Contributors

[@&#8203;antFrancon](https://togithub.com/antFrancon),
[@&#8203;JDMathew](https://togithub.com/JDMathew),
[@&#8203;thomas-rx](https://togithub.com/thomas-rx),
[@&#8203;exploIF](https://togithub.com/exploIF)

#### 🙌 Thank you for your contributions!

**Full Changelog**:
software-mansion/react-native-reanimated@3.11.0...3.12.0

Package build:
https://github.com/software-mansion/react-native-reanimated/actions/runs/9287839734

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/GSTJ/react-native-magic-modal).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zNzcuOCIsInVwZGF0ZWRJblZlciI6IjM3LjM3Ny44IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useAnimatedStyle does not support hsl(x y% z%) syntax for colors
2 participants