-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
566 additions
and
8 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions
92
packages/k2-alpine/src/components/Avatar/Avatar.stories.tsx
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,92 @@ | ||
import React, { useState } from 'react' | ||
import { Switch } from 'react-native' | ||
import { ScrollView, Text, View } from '../Primitives' | ||
import { useTheme } from '../..' | ||
import AvatarSelector from './AvatarSelector' | ||
import { Avatar } from './Avatar' | ||
|
||
export default { | ||
title: 'Avatar' | ||
} | ||
|
||
export const All = (): JSX.Element => { | ||
const { theme } = useTheme() | ||
|
||
const [hasBlur, setHasBlur] = useState(true) | ||
|
||
const AVATARS = [ | ||
require('../../assets/avatars/avatar-1.jpeg'), | ||
require('../../assets/avatars/avatar-2.jpeg'), | ||
require('../../assets/avatars/avatar-3.jpeg'), | ||
require('../../assets/avatars/avatar-4.jpeg'), | ||
require('../../assets/avatars/avatar-5.jpeg'), | ||
require('../../assets/avatars/avatar-6.png'), | ||
require('../../assets/avatars/avatar-7.png'), | ||
require('../../assets/avatars/avatar-8.png'), | ||
require('../../assets/avatars/avatar-9.jpeg'), | ||
{ | ||
uri: 'https://miro.medium.com/v2/resize:fit:1256/format:webp/1*xm2-adeU3YD4MsZikpc5UQ.png' | ||
}, | ||
{ | ||
uri: 'https://www.cnet.com/a/img/resize/7589227193923c006f9a7fd904b77bc898e105ff/hub/2021/11/29/f566750f-79b6-4be9-9c32-8402f58ba0ef/richerd.png?auto=webp&width=768' | ||
}, | ||
{ | ||
uri: 'https://i.seadn.io/s/raw/files/a9cb8c2298a64819a3036083818d0447.jpg?auto=format&dpr=1&w=1000' | ||
}, | ||
{ | ||
uri: 'https://i.seadn.io/gcs/files/441e674e79460fc975d976465bb3634d.png?auto=format&dpr=1&w=1000' | ||
}, | ||
{ | ||
uri: 'https://www.svgrepo.com/show/19461/url-link.svg' | ||
} | ||
].map((avatar, index) => { | ||
return { id: index.toString(), source: avatar } | ||
}) | ||
|
||
const [selectedAvatarId, setSelectedAvatarId] = useState<string | undefined>( | ||
AVATARS[0]?.id | ||
) | ||
|
||
const handleSelect = (id: string): void => { | ||
setSelectedAvatarId(id) | ||
} | ||
|
||
const backgroundColor = theme.colors.$surfacePrimary | ||
|
||
return ( | ||
<ScrollView | ||
style={{ | ||
width: '100%', | ||
backgroundColor | ||
}}> | ||
<View sx={{ alignItems: 'flex-end', padding: 12 }}> | ||
<View | ||
sx={{ | ||
gap: 8, | ||
flexDirection: 'row', | ||
alignItems: 'center' | ||
}}> | ||
<Text>Blur on</Text> | ||
<Switch value={hasBlur} onValueChange={setHasBlur} /> | ||
</View> | ||
</View> | ||
<View sx={{ alignItems: 'center', padding: 100 }}> | ||
<Avatar | ||
backgroundColor={backgroundColor} | ||
source={ | ||
AVATARS.find(avatar => avatar.id === selectedAvatarId)?.source | ||
} | ||
size="large" | ||
hasBlur={hasBlur} | ||
/> | ||
</View> | ||
<AvatarSelector | ||
backgroundColor={backgroundColor} | ||
selectedId={selectedAvatarId} | ||
avatars={AVATARS} | ||
onSelect={handleSelect} | ||
/> | ||
<View sx={{ height: 200 }} /> | ||
</ScrollView> | ||
) | ||
} |
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,115 @@ | ||
import React, { useEffect } from 'react' | ||
import { ImageSourcePropType, Platform, ViewStyle } from 'react-native' | ||
import Animated, { | ||
Easing, | ||
useAnimatedStyle, | ||
useSharedValue, | ||
withTiming | ||
} from 'react-native-reanimated' | ||
import { BlurView } from 'expo-blur' | ||
import { View } from '../Primitives' | ||
import { useTheme } from '../..' | ||
import { HexagonImageView, HexagonBorder } from './HexagonImageView' | ||
|
||
export const Avatar = ({ | ||
source, | ||
size, | ||
isSelected, | ||
isPressed, | ||
hasBlur, | ||
style, | ||
backgroundColor | ||
}: { | ||
source: ImageSourcePropType | ||
size: number | 'small' | 'large' | ||
backgroundColor: string | ||
isSelected?: boolean | ||
isPressed?: boolean | ||
hasBlur?: boolean | ||
style?: ViewStyle | ||
}): JSX.Element => { | ||
const { theme } = useTheme() | ||
|
||
const height = typeof size === 'number' ? size : size === 'small' ? 90 : 150 | ||
|
||
const pressedAnimation = useSharedValue(1) | ||
const pressedAnimatedStyle = useAnimatedStyle(() => ({ | ||
transform: [{ scale: pressedAnimation.value }] | ||
})) | ||
// to cancel out the blur effect on the backgroundColor, we need to use a darker background color for the blur view | ||
const surfacePrimaryBlurBgMap = theme.isDark | ||
? { | ||
[theme.colors.$surfacePrimary]: | ||
Platform.OS === 'ios' ? '#050506' : '#0a0a0b', | ||
[theme.colors.$surfaceSecondary]: | ||
Platform.OS === 'ios' ? '#37373f' : '#373743', | ||
[theme.colors.$surfaceTertiary]: | ||
Platform.OS === 'ios' ? '#1A1A1C' : '#1C1C1F' | ||
} | ||
: { | ||
[theme.colors.$surfacePrimary]: undefined, | ||
[theme.colors.$surfaceSecondary]: undefined, | ||
[theme.colors.$surfaceTertiary]: | ||
Platform.OS === 'ios' ? '#8b8b8c' : '#79797c' | ||
} | ||
|
||
useEffect(() => { | ||
pressedAnimation.value = withTiming(isPressed ? 0.95 : 1, { | ||
duration: 150, | ||
easing: Easing.inOut(Easing.ease) | ||
}) | ||
}, [isPressed, pressedAnimation]) | ||
|
||
return ( | ||
<Animated.View | ||
style={[ | ||
{ width: height, height: height, overflow: 'visible' }, | ||
pressedAnimatedStyle, | ||
style | ||
]}> | ||
{hasBlur === true && ( | ||
<View | ||
sx={{ | ||
backgroundColor: surfacePrimaryBlurBgMap[backgroundColor], | ||
position: 'absolute', | ||
top: -BLURAREA_INSET + 10, | ||
left: -BLURAREA_INSET, | ||
right: 0, | ||
bottom: 0, | ||
width: height + BLURAREA_INSET * 2, | ||
height: height + BLURAREA_INSET * 2, | ||
alignItems: 'center', | ||
justifyContent: 'center' | ||
}}> | ||
<HexagonImageView | ||
backgroundColor={backgroundColor} | ||
source={source} | ||
height={height} | ||
/> | ||
<BlurView | ||
style={{ | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
right: 0, | ||
bottom: 0 | ||
}} | ||
tint={theme.isDark ? 'dark' : undefined} | ||
intensity={75} | ||
experimentalBlurMethod="dimezisBlurView" | ||
/> | ||
</View> | ||
)} | ||
<HexagonImageView | ||
source={source} | ||
height={height} | ||
backgroundColor={backgroundColor} | ||
isSelected={isSelected} | ||
hasLoading={true} | ||
/> | ||
<HexagonBorder height={height} /> | ||
</Animated.View> | ||
) | ||
} | ||
|
||
const BLURAREA_INSET = 50 |
96 changes: 96 additions & 0 deletions
96
packages/k2-alpine/src/components/Avatar/AvatarSelector.tsx
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,96 @@ | ||
import { Dimensions, ImageSourcePropType } from 'react-native' | ||
import React, { useMemo, useState } from 'react' | ||
import Carousel from 'react-native-reanimated-carousel' | ||
import { Pressable } from '../Primitives' | ||
import { Avatar } from './Avatar' | ||
|
||
const AvatarSelector = ({ | ||
avatars, | ||
selectedId, | ||
onSelect, | ||
backgroundColor | ||
}: { | ||
avatars: { id: string; source: ImageSourcePropType }[] | ||
selectedId?: string | ||
onSelect?: (id: string) => void | ||
backgroundColor: string | ||
}): JSX.Element => { | ||
const data = useMemo(() => { | ||
// we should always have an even number of avatars, due to infinite scrolling + two avatars per column | ||
if (avatars.length % 2 === 0) { | ||
return avatars | ||
} else { | ||
return [...avatars, ...avatars] | ||
} | ||
}, [avatars]) | ||
const [pressedIndex, setPressedIndex] = useState<number>() | ||
|
||
const handlePressIn = (index: number): void => { | ||
setPressedIndex(index) | ||
} | ||
|
||
const handlePressOut = (index: number): void => { | ||
if (pressedIndex === index) { | ||
setPressedIndex(undefined) | ||
} | ||
} | ||
|
||
const handleSelect = (index: number): void => { | ||
if (data[index]?.id === undefined) { | ||
return | ||
} | ||
|
||
onSelect?.(data[index].id) | ||
} | ||
|
||
const renderItem = ({ | ||
item, | ||
index | ||
}: { | ||
item: { id: string; source: ImageSourcePropType } | ||
index: number | ||
}): JSX.Element => { | ||
return ( | ||
<Pressable | ||
key={index} | ||
style={{ marginTop: index % 2 === 0 ? configuration.avatarWidth : 0 }} | ||
onPressIn={() => handlePressIn(index)} | ||
onPressOut={() => handlePressOut(index)} | ||
onPress={() => handleSelect(index)}> | ||
<Avatar | ||
source={item.source} | ||
size={configuration.avatarWidth} | ||
isSelected={data[index]?.id === selectedId} | ||
isPressed={pressedIndex === index} | ||
backgroundColor={backgroundColor} | ||
/> | ||
</Pressable> | ||
) | ||
} | ||
|
||
return ( | ||
<Carousel | ||
width={configuration.avatarWidth / 2 + configuration.spacing} | ||
height={configuration.avatarWidth * 2} | ||
data={data} | ||
renderItem={renderItem} | ||
pagingEnabled={false} | ||
snapEnabled={false} | ||
style={{ | ||
width: '100%', | ||
overflow: 'visible', | ||
paddingVertical: configuration.spacing * 2, | ||
marginLeft: SCREEN_WIDTH / 2 - configuration.avatarWidth / 2 | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
const configuration = { | ||
avatarWidth: 90, | ||
spacing: 6 | ||
} | ||
|
||
const SCREEN_WIDTH = Dimensions.get('window').width | ||
|
||
export default AvatarSelector |
Oops, something went wrong.