Files
Nishan 1a49892d57 fix: linear gradient color stop fix up spec (#45969)
Summary:
- Color stops needs to follow [fix up spec](https://drafts.csswg.org/css-images-4/#color-stop-fixup)
- Adds multiple stops syntax support. e.g. linear-gradient(red 30% 50%, green).
- Rename `position` to `positions` in object style API. Optional string array here makes more sense. We'll add number array support once `px` support is added. Will do it as a follow up to this PR.

TODOs: transition hint syntax support `linear-gradient(red, 50%, green)` (Done locally, dependent on this PR). `px` support.

## Changelog:
[GENERAL] [FIXED] - Linear gradient color stop spec.

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: https://github.com/facebook/react-native/pull/45969

Test Plan: - Added testcases in processBackgroundImage-test.js

Reviewed By: javache

Differential Revision: D61309203

Pulled By: NickGerleman

fbshipit-source-id: 884052c6841320048933361f38e6478ff4192736
2024-08-19 18:23:13 -07:00

161 lines
3.7 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet';
import React from 'react';
import {StyleSheet, Text, View} from 'react-native';
type Props = $ReadOnly<{
style: ViewStyleProp,
testID?: string,
}>;
function GradientBox(props: Props): React.Node {
return (
<View style={[styles.box, props.style]} testID={props.testID}>
<Text style={styles.text}>Linear Gradient</Text>
</View>
);
}
const styles = StyleSheet.create({
box: {
width: 200,
height: 100,
justifyContent: 'center',
alignItems: 'center',
marginVertical: 10,
},
text: {
color: 'white',
fontWeight: 'bold',
},
});
exports.title = 'Linear Gradient';
exports.category = 'UI';
exports.description = 'Examples of linear gradients applied to views.';
exports.examples = [
{
title: 'Basic Linear Gradient',
description: 'Linear gradient from top to bottom',
render(): React.Node {
return (
<GradientBox
style={{
experimental_backgroundImage: 'linear-gradient(#e66465, #9198e5);',
}}
testID="linear-gradient-basic"
/>
);
},
},
{
title: 'Diagonal Gradient',
description: 'Linear gradient from top-left to bottom-right',
render(): React.Node {
return (
<GradientBox
testID="linear-gradient-diagonal"
style={{
experimental_backgroundImage:
'linear-gradient(to bottom right, yellow, green)',
}}
/>
);
},
},
{
title: 'Gradient with angle',
description: 'Linear gradient with angle',
render(): React.Node {
return (
<GradientBox
testID="linear-gradient-angle"
style={{
experimental_backgroundImage:
'linear-gradient(135deg, gray, brown)',
}}
/>
);
},
},
{
title: 'Multiple Color Stops',
render(): React.Node {
return (
<GradientBox
testID="linear-gradient-color-stops"
style={{
experimental_backgroundImage:
'linear-gradient(to right, red 0%, yellow 30%, green 60%, blue 100%)',
}}
/>
);
},
},
{
title: 'Linear gradient with object style syntax',
render(): React.Node {
return (
<GradientBox
testID="linear-gradient-object-style-syntax"
style={{
experimental_backgroundImage: [
{
type: 'linearGradient',
direction: 'to bottom',
colorStops: [
{color: 'purple', positions: ['0%']},
{color: 'orange', positions: ['100%']},
],
},
],
}}
/>
);
},
},
{
title: 'Gradient with uniform border style',
render(): React.Node {
return (
<GradientBox
testID="linear-gradient-with-uniform-borders"
style={{
experimental_backgroundImage:
'linear-gradient(to bottom right, yellow, green);',
borderRadius: 16,
}}
/>
);
},
},
{
title: 'Gradient with non-uniform border style',
render(): React.Node {
return (
<GradientBox
testID="linear-gradient-with-non-uniform-borders"
style={{
experimental_backgroundImage:
'linear-gradient(to bottom right, yellow, green);',
borderTopRightRadius: 8,
borderTopLeftRadius: 80,
}}
/>
);
},
},
];