Skip to content

Commit 781a246

Browse files
cweeiimergify[bot]
authored andcommitted
feat(avatarinitials): move backgroundColor into scss (#1585)
* feat(avatar): allow style override for avatarinitials * feat(avatarintials): move backgroundcolor of initials into scss * feat(avatarintials): use data attribute to determine backgroundColor * feat(avatarinitials): moved avatar colors into _theme.scss * feat(avatarinitials): parse sass lists when compiling constants files * feat(avatarinitials): remove unused sass variable * build(buildstylevars): readded line * style(avatar): readded removed line
1 parent d16b0da commit 781a246

File tree

8 files changed

+41
-32
lines changed

8 files changed

+41
-32
lines changed

scripts/build-style-vars.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,20 @@ async function main() {
2424
const jsData = parse(scssData, {
2525
camelCase: false,
2626
});
27-
const moduleString = Object.entries(jsData)
27+
28+
// Parse Sass lists
29+
const formattedData = Object.entries(jsData).map(([k, v]) => (v.indexOf(', ') >= 0 ? [k, v.split(', ')] : [k, v]));
30+
31+
const moduleString = formattedData
2832
.map(([k, v]) => `export const ${camelCase(k)} = ${JSON.stringify(v)}; // ${k}`)
2933
.join('\n');
30-
const jsonString = JSON.stringify(jsData, null, 2);
34+
35+
// Recreate literal object before stringifying
36+
const newJson = formattedData.reduce((prev, [k, v]) => {
37+
prev[k] = v;
38+
return prev;
39+
}, {});
40+
const jsonString = JSON.stringify(newJson, null, 2);
3141

3242
// Don't write the files again if nothing has changed
3343
let priorJson;
@@ -37,7 +47,7 @@ async function main() {
3747
priorJson = {};
3848
}
3949

40-
if (!isEqual(priorJson, jsData)) {
50+
if (!isEqual(priorJson, newJson)) {
4151
await writeFile(outputJs, `${moduleHeader}${moduleString}`);
4252
await writeFile(outputJson, jsonString);
4353
}

src/components/avatar/Avatar.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
line-height: 1;
3939
}
4040

41+
@for $i from 1 through length($avatar-colors) {
42+
.avatar-initials[data-bg-idx='#{$i - 1}'] {
43+
background-color: nth($avatar-colors, $i);
44+
}
45+
}
46+
4147
&.avatar--large {
4248
width: 44px;
4349
height: 44px;

src/components/avatar/AvatarInitials.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
11
// @flow
22
import React from 'react';
3-
import {
4-
bdlLightBlue,
5-
bdlBoxBlue,
6-
bdlDarkBlue,
7-
bdlGray62,
8-
bdlGreenLight,
9-
bdlYellorange,
10-
bdlGrimace,
11-
bdlPurpleRain,
12-
bdlWatermelonRed,
13-
} from '../../styles/variables';
14-
15-
const AVATAR_COLORS = [
16-
bdlLightBlue,
17-
bdlBoxBlue,
18-
bdlDarkBlue,
19-
bdlGray62,
20-
bdlGreenLight,
21-
bdlYellorange,
22-
bdlGrimace,
23-
bdlPurpleRain,
24-
bdlWatermelonRed,
25-
];
3+
import { avatarColors } from '../../styles/variables';
264

275
const getInitials = name => {
286
const firstInitial = name.slice(0, 1);
@@ -38,9 +16,10 @@ type Props = {
3816

3917
const AvatarInitials = ({ className = '', id, name }: Props) => {
4018
const avatarColorSelector = parseInt(id, 10) || 0;
41-
const backgroundColor = AVATAR_COLORS[avatarColorSelector % AVATAR_COLORS.length];
19+
const backgroundColorIndex = avatarColorSelector % avatarColors.length;
20+
4221
return (
43-
<span className={`avatar-initials ${className}`} style={{ backgroundColor }}>
22+
<span className={`avatar-initials ${className}`} data-bg-idx={backgroundColorIndex}>
4423
{getInitials(name)}
4524
</span>
4625
);

src/components/avatar/__tests__/AvatarInitials-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ describe('components/avatar/AvatarInitials', () => {
1313
expect(wrapper.text()).toEqual('HW');
1414
});
1515

16-
test('should set a backgroundColor based on id', () => {
16+
test('should set data-bg-idx attribute based on id', () => {
1717
const wrapper = shallow(<AvatarInitials id="10" name="hello world" />);
18-
expect(wrapper.prop('style').backgroundColor).toEqual('#0061d5');
18+
expect(wrapper.prop('data-bg-idx')).toEqual(1);
1919
});
2020

21-
test('should set a default backgroundColor if no id is passed in', () => {
21+
test('should set a default data-bg-idx attribute if no id is passed in', () => {
2222
const wrapper = shallow(<AvatarInitials name="hello world" />);
23-
expect(wrapper.prop('style').backgroundColor).toEqual('#2486fc');
23+
expect(wrapper.prop('data-bg-idx')).toEqual(0);
2424
});
2525
});

src/styles/_variables.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@import './constants/layout';
44
@import './constants/animation';
55
@import './constants/typography';
6+
@import './constants/themes';
67
@import './deprecated'; // <<< Deprecated
78

89
// Mixins

src/styles/constants/_themes.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$avatar-colors: $bdl-light-blue, $bdl-box-blue, $bdl-dark-blue, $bdl-gray-62, $bdl-green-light, $bdl-yellorange, $bdl-grimace, $bdl-purple-rain, $bdl-watermelon-red;

src/styles/variables.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export const bdlWatermelonRed10 = "#fdebee"; // bdl-watermelon-red-10
6464
export const bdlWatermelonRed05 = "#fef5f6"; // bdl-watermelon-red-05
6565
export const bdlWatermelonRed02 = "#fffbfc"; // bdl-watermelon-red-02
6666
export const bdlLineHeight = "20px"; // bdl-line-height
67+
export const avatarColors = ["#2486fc","#0061d5","#003c84","#767676","#26c281","#f5b31b","#4826c2","#9f3fed","#ed3757"]; // avatar-colors
6768
export const modalDialogZIndex = "160"; // modal-dialog-z-index
6869
export const menuZIndex = "170"; // menu-z-index
6970
export const notificationsWrapperZIndex = "180"; // notifications-wrapper-z-index

src/styles/variables.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@
6161
"bdl-watermelon-red-05": "#fef5f6",
6262
"bdl-watermelon-red-02": "#fffbfc",
6363
"bdl-line-height": "20px",
64+
"avatar-colors": [
65+
"#2486fc",
66+
"#0061d5",
67+
"#003c84",
68+
"#767676",
69+
"#26c281",
70+
"#f5b31b",
71+
"#4826c2",
72+
"#9f3fed",
73+
"#ed3757"
74+
],
6475
"modal-dialog-z-index": "160",
6576
"menu-z-index": "170",
6677
"notifications-wrapper-z-index": "180",

0 commit comments

Comments
 (0)