-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from bamlab/feat/add-buttons
Example app: add buttons
- Loading branch information
Showing
12 changed files
with
144 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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 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 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,43 @@ | ||
import { forwardRef } from 'react'; | ||
import { Animated, View } from 'react-native'; | ||
import { SpatialNavigationNode } from 'react-tv-space-navigation/src'; | ||
import { Typography } from './Typography'; | ||
import styled from '@emotion/native'; | ||
import { useFocusAnimation } from '../helpers/useFocusAnimation'; | ||
import { scaledPixels } from '../helpers/scaledPixels'; | ||
|
||
type ButtonProps = { | ||
label: string; | ||
onSelect?: () => void; | ||
}; | ||
|
||
const ButtonContent = forwardRef<View, { label: string; isFocused: boolean }>((props, ref) => { | ||
const { isFocused, label } = props; | ||
const anim = useFocusAnimation(isFocused); | ||
return ( | ||
<Container style={anim} isFocused={isFocused} ref={ref}> | ||
<ColoredTypography isFocused={isFocused}>{label}</ColoredTypography> | ||
</Container> | ||
); | ||
}); | ||
|
||
ButtonContent.displayName = 'ButtonContent'; | ||
|
||
export const Button = ({ label }: ButtonProps) => { | ||
return ( | ||
<SpatialNavigationNode isFocusable> | ||
{({ isFocused }) => <ButtonContent label={label} isFocused={isFocused} />} | ||
</SpatialNavigationNode> | ||
); | ||
}; | ||
|
||
const Container = styled(Animated.View)<{ isFocused: boolean }>(({ isFocused, theme }) => ({ | ||
alignSelf: 'baseline', | ||
backgroundColor: isFocused ? 'white' : 'black', | ||
padding: theme.spacings.$4, | ||
borderRadius: scaledPixels(12), | ||
})); | ||
|
||
const ColoredTypography = styled(Typography)<{ isFocused: boolean }>(({ isFocused }) => ({ | ||
color: isFocused ? 'black' : 'white', | ||
})); |
13 changes: 13 additions & 0 deletions
13
packages/example/src/design-system/helpers/scaledPixels.ts
This file contains 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,13 @@ | ||
import { Dimensions } from 'react-native'; | ||
|
||
export const screen = Dimensions.get('window'); | ||
const scale = (screen.width || 1920) / 1920; | ||
|
||
/** | ||
* Unfortunately, AndroidTV handles pixels in a strange manner | ||
* PixelRatio does not seem to solve the problem properly on web. | ||
* So we just scale the pixels manually. | ||
* | ||
* https://github.com/react-native-tvos/react-native-tvos/issues/57 | ||
*/ | ||
export const scaledPixels = (pixels: number) => pixels * scale; |
17 changes: 17 additions & 0 deletions
17
packages/example/src/design-system/helpers/useFocusAnimation.ts
This file contains 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,17 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { Animated } from 'react-native'; | ||
|
||
export const useFocusAnimation = (isFocused: boolean) => { | ||
const scaleAnimation = useRef(new Animated.Value(0)).current; | ||
|
||
useEffect(() => { | ||
Animated.spring(scaleAnimation, { | ||
toValue: isFocused ? 1.1 : 1, | ||
useNativeDriver: true, | ||
damping: 10, | ||
stiffness: 100, | ||
}).start(); | ||
}, [isFocused, scaleAnimation]); | ||
|
||
return { transform: [{ scale: scaleAnimation }] }; | ||
}; |
This file contains 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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { scaledPixels } from '../helpers/scaledPixels'; | ||
|
||
export const sizes = { | ||
program: { | ||
landscape: { width: 250, height: 200 }, | ||
portrait: { width: 200, height: 250 }, | ||
landscape: { width: scaledPixels(250), height: scaledPixels(200) }, | ||
portrait: { width: scaledPixels(200), height: scaledPixels(250) }, | ||
}, | ||
}; |
This file contains 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 |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import { scaledPixels } from '../helpers/scaledPixels'; | ||
|
||
export const spacings = { | ||
0: 0, | ||
'$0.5': 2, | ||
$1: 4, | ||
$2: 8, | ||
$3: 12, | ||
$4: 16, | ||
$5: 20, | ||
$6: 24, | ||
$7: 28, | ||
$8: 32, | ||
$9: 36, | ||
$10: 40, | ||
$12: 48, | ||
$15: 60, | ||
$20: 80, | ||
0: scaledPixels(0), | ||
'$0.5': scaledPixels(2), | ||
$1: scaledPixels(4), | ||
$2: scaledPixels(8), | ||
$3: scaledPixels(12), | ||
$4: scaledPixels(16), | ||
$5: scaledPixels(20), | ||
$6: scaledPixels(24), | ||
$7: scaledPixels(28), | ||
$8: scaledPixels(32), | ||
$9: scaledPixels(36), | ||
$10: scaledPixels(40), | ||
$12: scaledPixels(48), | ||
$15: scaledPixels(60), | ||
$20: scaledPixels(80), | ||
}; |
This file contains 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 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 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 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 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