Skip to content

Commit

Permalink
Merge pull request #18 from miguelcobain/master
Browse files Browse the repository at this point in the history
add support for color property
  • Loading branch information
k-fish authored Dec 6, 2017
2 parents a5f9b38 + d8a43f3 commit b31f0bf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ your different initials avatars:
}}
```

You can use a background-color directly you can pass in a `color` property.
This is useful if you want to generate colors from unique strings like emails or ids.
This background-color style will only be applied if `image` is a "falsy" value:

```hbs
{{initials-avatar
firstName=userGivenName
lastName=userFamilyName
company=userCompany
email=userEmail
color="#f6f8fa"
}}
```

## Installation

* `git clone` this repository
Expand Down
4 changes: 3 additions & 1 deletion addon/components/initials-avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ export default Component.extend({
/**
* Display the image using a background-image inline style
*/
style: computed('hasImage', function() {
style: computed('hasImage', 'color', function() {
if (this.get('hasImage')) {
return htmlSafe(`background-image: url(${this.get('image')}); background-size: cover`);
} else if (this.get('color')) {
return htmlSafe(`background-color: ${this.get('color')};`);
}
}),

Expand Down
33 changes: 33 additions & 0 deletions tests/integration/components/initials-avatar-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,36 @@ test('displays image if one is provided', function(assert) {
assert.equal(this.$('#initials-avatar').attr('style'), 'background-image: url(http://example.com/potus.jpg); background-size: cover');
assert.equal(this.$('#initials-avatar').text().trim(), '');
});

test('uses color if no image', function(assert) {
assert.expect(2);

this.render(hbs`
{{initials-avatar
id="initials-avatar"
color="#ffffff"
firstName="Barack"
lastName="Obama"
}}
`);

assert.equal(this.$('#initials-avatar').attr('style'), 'background-color: #ffffff;');
assert.equal(this.$('#initials-avatar').text().trim(), 'BO');
});

test('image supercedes color', function(assert) {
assert.expect(2);

this.render(hbs`
{{initials-avatar
id="initials-avatar"
color="#ffffff"
image="http://example.com/potus.jpg"
firstName="Barack"
lastName="Obama"
}}
`);

assert.equal(this.$('#initials-avatar').attr('style'), 'background-image: url(http://example.com/potus.jpg); background-size: cover');
assert.equal(this.$('#initials-avatar').text().trim(), '');
});

0 comments on commit b31f0bf

Please sign in to comment.