0 && !single) {
31 | return text.split(" ")[0].charAt(0) + text.split(" ")[1].charAt(0);
32 | } else {
33 | return text.charAt(0);
34 | }
35 | }
36 |
37 | var defaultProps = {
38 | size: 75,
39 | saturation: "80%",
40 | brightness: "40%",
41 | color: false,
42 | single: false
43 | };
44 |
45 | if (typeof exports != 'undefined') {
46 | exports.getBackgroundColor = getBackgroundColor;
47 | exports.getFontSize = getFontSize;
48 | exports.getInitials = getInitials;
49 | exports.defaultProps = defaultProps;
50 | }
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | if (typeof require != 'undefined') {
2 | var React = require('react')
3 |
4 | var {
5 | getBackgroundColor,
6 | getFontSize,
7 | getInitials,
8 | defaultProps
9 | } = require('./shared')
10 | var PropTypes = require('prop-types');
11 | }
12 |
13 | class Initicon extends React.Component {
14 | render() {
15 | let {props} = this
16 | return (
17 |
26 | {getInitials(props)}
27 |
28 | )
29 | }
30 | }
31 |
32 | Initicon.propTypes = {
33 | text: PropTypes.any.isRequired,
34 | size: PropTypes.any.isRequired,
35 | seed: PropTypes.number,
36 | color: PropTypes.any,
37 | single: PropTypes.bool,
38 | saturation: PropTypes.string,
39 | brightness: PropTypes.string
40 | }
41 |
42 | Initicon.defaultProps = defaultProps
43 |
44 | if (typeof module != 'undefined') {
45 | module.exports = Initicon;
46 | }
47 |
--------------------------------------------------------------------------------
/src/native.js:
--------------------------------------------------------------------------------
1 | import React, {Component} from 'react';
2 | import {
3 | StyleSheet,
4 | Text,
5 | View,
6 | } from 'react-native';
7 | import PropTypes from 'prop-types';
8 |
9 | import {
10 | getBackgroundColor,
11 | getFontSize,
12 | getInitials,
13 | defaultProps,
14 | } from './shared';
15 |
16 | class NativeIniticon extends Component {
17 | render() {
18 | let {props} = this
19 | return (
20 |
28 | {getInitials(props)}
29 |
30 | );
31 | }
32 | };
33 |
34 | NativeIniticon.propTypes = {
35 | text: PropTypes.string.isRequired,
36 | size: PropTypes.any.isRequired,
37 | seed: PropTypes.number,
38 | color: PropTypes.any,
39 | single: PropTypes.bool,
40 | saturation: PropTypes.string,
41 | brightness: PropTypes.string,
42 | }
43 |
44 | NativeIniticon.defaultProps = defaultProps;
45 |
46 | const styles = StyleSheet.create({
47 | icon: {
48 | flex: 1,
49 | justifyContent: 'center',
50 | alignItems: 'center',
51 | },
52 | text: {
53 | color: '#fff',
54 | },
55 | })
56 |
57 | module.exports = NativeIniticon;
58 |
--------------------------------------------------------------------------------
/src/shared.js:
--------------------------------------------------------------------------------
1 | function getBackgroundColor(props) {
2 | let {seed, saturation, brightness, color} = props
3 | if (color) return color
4 | var hue = Math.sin(seed)
5 | hue = hue < 0 ? -hue : hue
6 | hue = Math.round(hue * 359) + 1
7 | return "hsl(" + hue + "," + saturation + "," + brightness + ")"
8 | }
9 |
10 | function getFontSize(props) {
11 | let {single} = props
12 | return single ? (props.size)/1.7 : (props.size-5)/2
13 | }
14 |
15 | function getInitials(props) {
16 | let {text, single} = props
17 | if (text !== null && typeof text === 'object') {
18 | return text;
19 | } else if (text.indexOf(" ") > 0 && !single) {
20 | return text.split(" ")[0].charAt(0) + text.split(" ")[1].charAt(0)
21 | } else {
22 | return text.charAt(0)
23 | }
24 | }
25 |
26 | var defaultProps = {
27 | size: 75,
28 | saturation: "80%",
29 | brightness: "40%",
30 | color: false,
31 | single: false
32 | }
33 |
34 | if (typeof exports != 'undefined') {
35 | exports.getBackgroundColor = getBackgroundColor;
36 | exports.getFontSize = getFontSize;
37 | exports.getInitials = getInitials;
38 | exports.defaultProps = defaultProps;
39 | }
40 |
--------------------------------------------------------------------------------