-
Notifications
You must be signed in to change notification settings - Fork 738
Incubator.Gradient - new component #3795
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
Open
M-i-k-e-l
wants to merge
3
commits into
master
Choose a base branch
from
feat/incubator-gradient
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
demo/src/screens/incubatorScreens/IncubatorGradientScreen.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import React, {useEffect, useMemo, useState} from 'react'; | ||
import {Assets, View, Text, Incubator, Icon, Colors} from 'react-native-ui-lib'; | ||
import {renderRadioGroup, renderSliderOption} from '../ExampleScreenPresenter'; | ||
|
||
const {Gradient} = Incubator; | ||
|
||
const COLORS = [Colors.$backgroundPrimaryHeavy, Colors.$backgroundPrimaryHeavy, Colors.$backgroundPrimaryMedium]; | ||
|
||
const GradientScreen = () => { | ||
const [type, setType] = useState('rectangle'); | ||
const [children, setChildren] = useState('none'); | ||
const [alignment, setAlignment] = useState('none'); | ||
const [size, setSize] = useState('fixed'); | ||
const [error, setError] = useState(''); | ||
const [angle, setAngle] = useState(0); | ||
|
||
const gradientProps = useMemo(() => { | ||
switch (type) { | ||
case 'rectangle': | ||
return size === 'fixed' ? {type: 'rectangle', width: 100, height: 100} : {type: 'rectangle'}; | ||
case 'circle': | ||
return size === 'fixed' ? {type: 'circle', radius: 50} : {type: 'circle'}; | ||
case 'border': | ||
return size === 'fixed' ? {type: 'border', width: 100, height: 100} : {type: 'border'}; | ||
} | ||
}, [type, size]); | ||
|
||
const childrenProps = useMemo(() => { | ||
switch (children) { | ||
case 'shortText': | ||
return <Text>Lorem ipsum dolor sit amet.</Text>; | ||
case 'text': | ||
return ( | ||
<Text> | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et | ||
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex | ||
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat | ||
nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit | ||
anim id est laborum. | ||
</Text> | ||
); | ||
case 'icon': | ||
return <Icon source={Assets.icons.demo.search}/>; | ||
} | ||
}, [children]); | ||
|
||
const alignmentProp = useMemo(() => { | ||
switch (alignment) { | ||
case 'none': | ||
return undefined; | ||
case 'center': | ||
return {center: true}; | ||
case 'centerH': | ||
return {centerH: true}; | ||
case 'centerV': | ||
return {centerV: true}; | ||
} | ||
}, [alignment]); | ||
|
||
useEffect(() => { | ||
if (children === 'none' && size === 'flex' && type !== 'border') { | ||
setError('No children + flex gives no gradient'); | ||
} else if (size === 'flex' && type === 'circle') { | ||
setError('flex size will result with an ellipse instead of a circle'); | ||
} else { | ||
setError(''); | ||
} | ||
}, [children, size, type]); | ||
|
||
return ( | ||
<View> | ||
{renderRadioGroup('Select type', | ||
'type', | ||
{Rectangle: 'rectangle', Circle: 'circle', Border: 'border'}, | ||
{isRow: true, state: type, setState: setType})} | ||
{renderRadioGroup('Select children', | ||
'children', | ||
{No: 'none', 'Short text': 'shortText', Text: 'text', Icon: 'icon'}, | ||
{isRow: true, state: children, setState: setChildren})} | ||
{renderRadioGroup('Select alignment', | ||
'alignment', | ||
{None: 'none', Center: 'center', CenterH: 'centerH', CenterV: 'centerV'}, | ||
{isRow: true, state: alignment, setState: setAlignment})} | ||
{renderRadioGroup('Select size', | ||
'size', | ||
{Fixed: 'fixed', Flex: 'flex'}, | ||
{isRow: true, state: size, setState: setSize})} | ||
<View marginH-s10> | ||
{renderSliderOption('Angle', 'angle', { | ||
min: 0, | ||
max: 360, | ||
step: 1, | ||
state: angle, | ||
setState: setAngle | ||
})} | ||
</View> | ||
<Gradient | ||
colors={COLORS} | ||
// @ts-expect-error | ||
type={type} | ||
{...gradientProps} | ||
{...alignmentProp} | ||
angle={angle} | ||
> | ||
{childrenProps} | ||
</Gradient> | ||
<Text marginT-s10 center $textDangerLight> | ||
{error} | ||
</Text> | ||
</View> | ||
); | ||
}; | ||
|
||
export default GradientScreen; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
import {LinearGradientPackage} from 'optionalDeps'; | ||
const LinearGradient = LinearGradientPackage?.default; | ||
import {BorderGradientProps} from './types'; | ||
import View from '../../components/view'; | ||
import Spacings from '../../style/spacings'; | ||
import Colors from '../../style/colors'; | ||
import useAngleTransform from './useAngleTransform'; | ||
|
||
const BorderGradient = (props: BorderGradientProps) => { | ||
const {colors, borderWidth = Spacings.s1, borderRadius, children, width, height, angle, ...others} = props; | ||
|
||
const innerWidth = width ? width - borderWidth * 2 : undefined; | ||
const innerHeight = height ? height - borderWidth * 2 : undefined; | ||
const {start, end} = useAngleTransform({angle}); | ||
|
||
if (!LinearGradient) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<View width={width} height={height}> | ||
<LinearGradient colors={colors} start={start} end={end} style={{borderRadius}}> | ||
<View | ||
bg-white | ||
width={innerWidth} | ||
height={innerHeight} | ||
style={{margin: borderWidth, borderRadius, borderWidth: 0, borderColor: Colors.transparent}} | ||
{...others} | ||
> | ||
{children} | ||
</View> | ||
</LinearGradient> | ||
</View> | ||
); | ||
}; | ||
|
||
export default BorderGradient; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import {LinearGradientPackage} from 'optionalDeps'; | ||
const LinearGradient = LinearGradientPackage?.default; | ||
import {CircleGradientProps} from './types'; | ||
import View from '../../components/view'; | ||
import useAngleTransform from './useAngleTransform'; | ||
|
||
const CircleGradient = (props: CircleGradientProps) => { | ||
const {colors, radius, angle, children, ...others} = props; | ||
|
||
const internalDiameter = radius ? radius * 2 : undefined; | ||
const {start, end} = useAngleTransform({angle}); | ||
|
||
if (!LinearGradient) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<View width={internalDiameter} height={internalDiameter} style={{borderRadius: 999, overflow: 'hidden'}}> | ||
<LinearGradient colors={colors} start={start} end={end}> | ||
<View width={internalDiameter} height={internalDiameter} {...others}> | ||
{children} | ||
</View> | ||
</LinearGradient> | ||
</View> | ||
); | ||
}; | ||
|
||
export default CircleGradient; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import {LinearGradientPackage} from 'optionalDeps'; | ||
const LinearGradient = LinearGradientPackage?.default; | ||
import {RectangleGradientProps} from './types'; | ||
import View from '../../components/view'; | ||
import useAngleTransform from './useAngleTransform'; | ||
|
||
const RectangleGradient = (props: RectangleGradientProps) => { | ||
const {colors, width, height, angle, children, ...others} = props; | ||
|
||
const {start, end} = useAngleTransform({angle}); | ||
|
||
if (!LinearGradient) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<View width={width} height={height}> | ||
<LinearGradient colors={colors} start={start} end={end}> | ||
<View width={width} height={height} {...others}> | ||
{children} | ||
</View> | ||
</LinearGradient> | ||
</View> | ||
); | ||
}; | ||
|
||
export default RectangleGradient; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file includes changes that belong in a different PR.
Have you verified that these changes don't break other screens ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes are required for this PR, if I made these changed in another (clean) PR, how would we test them?
Tested a few screens