UNPKG

2.89 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5exports.__esModule = true;
6exports["default"] = exports.avatarSizes = void 0;
7
8var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
10var _ThemeProvider = require("../ThemeProvider");
11
12var _ColorModeProvider = require("../ColorModeProvider");
13
14var _colorsUtils = require("../theme/colors-utils");
15
16function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
17
18function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
19
20// Found this on StackOverflow :)
21function string2Hex(str) {
22 var hash = 0;
23 if (str.length === 0) return hash;
24
25 for (var i = 0; i < str.length; i++) {
26 hash = str.charCodeAt(i) + ((hash << 5) - hash);
27 hash = hash & hash;
28 }
29
30 var color = "#";
31
32 for (var j = 0; j < 3; j++) {
33 var value = hash >> j * 8 & 255;
34 color += ("00" + value.toString(16)).substr(-2);
35 }
36
37 return color;
38}
39
40var avatarSizes = {
41 "2xs": 4,
42 xs: 6,
43 sm: 8,
44 md: 12,
45 lg: 16,
46 xl: 24,
47 "2xl": 32,
48 full: "full"
49};
50exports.avatarSizes = avatarSizes;
51
52var useAvatarStyle = function useAvatarStyle(_ref) {
53 var size = _ref.size,
54 name = _ref.name,
55 showBorder = _ref.showBorder,
56 borderColor = _ref.borderColor;
57
58 var _useTheme = (0, _ThemeProvider.useTheme)(),
59 colors = _useTheme.colors;
60
61 var _useColorMode = (0, _ColorModeProvider.useColorMode)(),
62 colorMode = _useColorMode.colorMode;
63
64 var bg = name ? string2Hex(name) : colors.gray[400];
65 var color = name ? (0, _colorsUtils.isDarkColor)(bg) ? "#fff" : "gray.800" : "#fff";
66 var _borderColor = {
67 light: "#fff",
68 dark: "gray.800"
69 };
70 var baseProps = {
71 display: "inline-flex",
72 rounded: "full",
73 alignItems: "center",
74 flexShrink: "0",
75 justifyContent: "center",
76 position: "relative"
77 };
78 return _objectSpread({}, baseProps, {
79 size: avatarSizes[size],
80 bg: bg,
81 color: color
82 }, showBorder && {
83 border: "2px",
84 borderColor: borderColor || _borderColor[colorMode]
85 });
86};
87
88var _default = useAvatarStyle;
89exports["default"] = _default;
\No newline at end of file