-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(colorpicker): add colorpicker component
- Loading branch information
Artur Bien
committed
Jan 1, 2021
1 parent
179a7c6
commit e1fe103
Showing
9 changed files
with
306 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* eslint-disable no-console */ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import { | ||
ColorButton, | ||
Menu, | ||
ColorPicker, | ||
Divider, | ||
Button, | ||
} from 'react95-native'; | ||
|
||
import Container from '../util/Container'; | ||
|
||
const colors = [ | ||
'white', | ||
'black', | ||
'#bdbebd', | ||
'#7a7d7b', | ||
'#ff0102', | ||
'#7c0000', | ||
'#feff00', | ||
'#7c7d04', | ||
'#00ff00', | ||
'#077d04', | ||
'#0afeff', | ||
'#067d7b', | ||
'#1402ff', | ||
'#05007b', | ||
'#ff01ff', | ||
'#7a037b', | ||
'#cdaeb4', | ||
'#9b5d6a', | ||
'#e5d6de', | ||
'#9c9d9d', | ||
]; | ||
|
||
const ColorPickerExample = () => { | ||
const [colorMenuOpen, setColorMenuOpen] = React.useState(true); | ||
const [color, setColor] = React.useState(colors[3]); | ||
|
||
const handleColorChange = (value: string) => { | ||
setColor(value); | ||
setColorMenuOpen(false); | ||
}; | ||
return ( | ||
<Container> | ||
<Container.Section title='Default'> | ||
<View | ||
style={{ | ||
width: '100%', | ||
flexDirection: 'row', | ||
justifyContent: 'space-between', | ||
}} | ||
> | ||
<Menu | ||
open={colorMenuOpen} | ||
anchor={ | ||
<ColorButton | ||
onPress={() => setColorMenuOpen(state => !state)} | ||
color={color} | ||
/> | ||
} | ||
> | ||
<ColorPicker | ||
onChange={handleColorChange} | ||
value={color} | ||
colors={colors} | ||
colorsPerRow={5} | ||
/> | ||
<Divider style={{ marginVertical: 4 }} /> | ||
<Button | ||
disabled | ||
style={{ | ||
margin: 2, | ||
}} | ||
> | ||
Other... | ||
</Button> | ||
</Menu> | ||
<ColorButton color='red' disabled /> | ||
</View> | ||
</Container.Section> | ||
</Container> | ||
); | ||
}; | ||
|
||
export default ColorPickerExample; |
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,77 @@ | ||
import React, { useContext } from 'react'; | ||
import { StyleSheet, View, Image } from 'react-native'; | ||
import type { $RemoveChildren, Color } from '../types'; | ||
|
||
import { Button, Divider } from '..'; | ||
import { ThemeContext } from '../common/theming/Theme'; | ||
|
||
const dropdownSymbol = { | ||
default: | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAYAAADgkQYQAAAALElEQVQoU2NkIAIwEqGGgWRF/7GYCjYE3SRkhXA5bNaBFKKIk+wmnB4lyiQAAsgDCqkPxTcAAAAASUVORK5CYII=', | ||
disabled: | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAYAAADgkQYQAAAANklEQVQoU2NkIAIwEqGGgTRFLa0t/9FNramuARuCYhKyQpgCDEUgAZBCZAVYFWHzCGkOxxcUANHnDAplQ9G1AAAAAElFTkSuQmCC', | ||
}; | ||
|
||
type Props = $RemoveChildren<typeof Button> & { | ||
color?: Color; | ||
}; | ||
|
||
const previewHeight = 20; | ||
|
||
const ColorButton = ({ disabled, color, ...rest }: Props) => { | ||
const theme = useContext(ThemeContext); | ||
|
||
return ( | ||
<Button variant='outside' disabled={disabled} {...rest}> | ||
<View style={styles.row}> | ||
<View | ||
style={[ | ||
styles.colorPreview, | ||
{ | ||
backgroundColor: color || 'transparent', | ||
borderWidth: 2, | ||
borderColor: disabled | ||
? theme.materialTextDisabled | ||
: theme.materialText, | ||
shadowColor: disabled | ||
? theme.materialTextDisabledShadow | ||
: 'transparent', | ||
shadowOffset: { | ||
width: 1, | ||
height: 1, | ||
}, | ||
shadowOpacity: 1, | ||
shadowRadius: 0, | ||
}, | ||
]} | ||
/> | ||
<Divider orientation='vertical' size={previewHeight} /> | ||
<Image | ||
style={styles.dropdownIcon} | ||
source={{ | ||
uri: dropdownSymbol[disabled ? 'disabled' : 'default'], | ||
}} | ||
/> | ||
</View> | ||
</Button> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
row: { | ||
flexDirection: 'row', | ||
}, | ||
colorPreview: { | ||
width: 36, | ||
marginRight: 6, | ||
}, | ||
dropdownIcon: { | ||
width: 20, | ||
height: 20, | ||
alignSelf: 'center', | ||
marginRight: -4, | ||
marginLeft: 1, | ||
}, | ||
}); | ||
|
||
export default ColorButton; |
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 @@ | ||
export { default } from './ColorButton'; |
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,105 @@ | ||
import React, { useContext } from 'react'; | ||
import { | ||
StyleSheet, | ||
TouchableHighlight, | ||
View, | ||
StyleProp, | ||
ViewStyle, | ||
} from 'react-native'; | ||
import type { Color } from '../types'; | ||
|
||
import { Border } from '../common/styleElements'; | ||
import { ThemeContext } from '../common/theming/Theme'; | ||
|
||
const colorPreviewSize = 34; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function chunk(arr: any[], chunkSize: number) { | ||
const R = []; | ||
for (let i = 0, len = arr.length; i < len; i += chunkSize) | ||
R.push(arr.slice(i, i + chunkSize)); | ||
return R; | ||
} | ||
|
||
type Props = { | ||
colors: Color[]; | ||
colorsPerRow?: number; | ||
onChange?: (value: Color) => void; | ||
style?: StyleProp<ViewStyle>; | ||
value?: Color; | ||
}; | ||
|
||
const ColorPicker = ({ | ||
colors, | ||
colorsPerRow = 5, | ||
onChange, | ||
style, | ||
value, | ||
...rest | ||
}: Props) => { | ||
const theme = useContext(ThemeContext); | ||
|
||
const handleColorPress = (v: Color) => { | ||
onChange?.(v); | ||
}; | ||
const rows = chunk(colors, colorsPerRow); | ||
|
||
return ( | ||
<View style={style} {...rest}> | ||
{rows.map((rowColors, i) => ( | ||
<View style={styles.row} key={i}> | ||
{rowColors.map(color => { | ||
// TODO: pick more visible cborders olors for selected item | ||
const isSelected = color === value; | ||
return ( | ||
<TouchableHighlight | ||
key={color} | ||
onPress={() => handleColorPress(color)} | ||
style={[ | ||
styles.color, | ||
{ | ||
borderWidth: 2, | ||
borderColor: isSelected | ||
? theme.materialText | ||
: 'transparent', | ||
}, | ||
]} | ||
> | ||
<View | ||
style={{ | ||
flex: 1, | ||
backgroundColor: color, | ||
borderWidth: isSelected ? 2 : 0, | ||
borderColor: theme.canvas, | ||
}} | ||
> | ||
<View | ||
style={{ | ||
flex: 1, | ||
borderWidth: isSelected ? 2 : 0, | ||
borderColor: theme.materialText, | ||
}} | ||
> | ||
{!isSelected && <Border variant='cutout' />} | ||
</View> | ||
</View> | ||
</TouchableHighlight> | ||
); | ||
})} | ||
</View> | ||
))} | ||
</View> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
row: { | ||
flexDirection: 'row', | ||
}, | ||
color: { | ||
width: colorPreviewSize, | ||
height: colorPreviewSize, | ||
}, | ||
}); | ||
|
||
export default ColorPicker; |
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 @@ | ||
export { default } from './ColorPicker'; |
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