react-native-reanimated icon indicating copy to clipboard operation
react-native-reanimated copied to clipboard

Allow to use css color syntax

Open maciekstosio opened this issue 1 year ago • 0 comments

Summary

The PR should introduced basic CSS4 support for color() and display-p3 color space. This PR should align with those PRs from RN: PR, PR

Test plan

Tests based on PR from RN + EmptyExample app. Currently color support is not in react-native release so the example below does not work and this PR is Draft. Should be tested again when the RN changes are released.

EmptyExample for showcase
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,
  colorSpace,
}: {
  title: string;
  colorSpace?: string;
}) => {
  const sv = useSharedValue(0);

  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: `color(${colorSpace} ${sv} 1 1)`,
          })),
        ]}
      />
      <Animated.View
        style={[
          styles.box,
          useAnimatedStyle(() => ({
            backgroundColor: `color(${colorSpace} ${sv.value} 1 1 0.5)`,
          })),
        ]}
      />
      <Animated.View
        style={[
          styles.box,
          useAnimatedStyle(() => ({
            backgroundColor: `color(${colorSpace} 1 1 / 0.5)`,
          })),
        ]}
      />
    </View>
  );
};

const CompareColumn = ({
  title,
  colorSpace,
}: {
  title: string;
  colorSpace?: string;
}) => (
  <View style={{}}>
    <View style={styles.header}>
      <Text>{title}</Text>
    </View>
    <View
      style={[
        styles.box,
        {
          backgroundColor: `color(${colorSpace} 0.5 1 1)`,
        },
      ]}
    />
    <View
      style={[
        styles.box,
        {
          backgroundColor: `color(${colorSpace} 0.5 1 1 0.5)`,
        },
      ]}
    />
    <View
      style={[
        styles.box,
        {
          backgroundColor: `color(${colorSpace} 0.5 1 1 / 0.5)`,
        },
      ]}
    />
  </View>
);

export default function EmptyExample() {
  return (
    <View style={styles.container}>
      <CompareColumnAnimated title="anim srgb" colorSpace="srgb" />
      <CompareColumnAnimated title="anim p3" colorSpace="display-p3" />
      <CompareColumn title="view srgb" colorSpace="srgb" />
      <CompareColumn title="view p3" colorSpace="display-p3" />
    </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 avatar Mar 28 '24 07:03 maciekstosio