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

Allow to use css color syntax #5846

Draft
wants to merge 2 commits into
base: @maciekstosio/make-comma-separator-optional
Choose a base branch
from

Conversation

maciekstosio
Copy link
Contributor

@maciekstosio maciekstosio commented Mar 28, 2024

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 maciekstosio changed the title @maciekstosio/allow to use css color syntax Allow to use css color syntax Mar 28, 2024
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.

None yet

1 participant