Skip to content

Commit

Permalink
feat(chip): enable displaying pictures instead of icons
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiarokh authored and Befkadu1 committed May 16, 2024
1 parent 106d4a8 commit cfc0c1d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
3 changes: 3 additions & 0 deletions etc/lime-elements.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface Chip<T = any> {
// @deprecated
iconTitle?: string;
id: number | string;
image?: Image_2;
removable?: boolean;
selected?: boolean;
text: string;
Expand Down Expand Up @@ -203,6 +204,7 @@ export namespace Components {
"disabled": boolean;
"icon"?: string | Icon;
"identifier"?: number | string;
"image"?: Image_2;
"invalid": boolean;
"language": Languages;
"link"?: Omit<Link, 'text'>;
Expand Down Expand Up @@ -1095,6 +1097,7 @@ namespace JSX_2 {
"disabled"?: boolean;
"icon"?: string | Icon;
"identifier"?: number | string;
"image"?: Image_2;
"invalid"?: boolean;
"language"?: Languages;
"link"?: Omit<Link, 'text'>;
Expand Down
6 changes: 6 additions & 0 deletions src/components/chip-set/chip.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Image } from '../../global/shared-types/image.types';
import { Icon } from '../../global/shared-types/icon.types';

/**
Expand All @@ -19,6 +20,11 @@ export interface Chip<T = any> {
*/
icon?: string | Icon;

/**
* A picture to be displayed instead of the icon on the chip.
*/
image?: Image;

/**
* Color of the icon. Overrides `--icon-color`.
*
Expand Down
13 changes: 11 additions & 2 deletions src/components/chip/chip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
font-size: 0.875rem;
padding: 0 0.125rem;

&:has(limel-icon) {
&:has(limel-icon),
&:has(img) {
.text {
padding-left: 0.25rem;
padding-left: 0;
}
}

Expand Down Expand Up @@ -146,10 +147,18 @@
}
}

img,
limel-icon {
flex-shrink: 0;
width: calc(var(--limel-chip-height) - 0.25rem);
height: calc(var(--limel-chip-height) - 0.25rem);
}

img {
border-radius: 50%;
}

limel-icon {
padding: 0.0625rem;
}

Expand Down
23 changes: 19 additions & 4 deletions src/components/chip/chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
DELETE_KEY_CODE,
} from '../../util/keycodes';
import { ChipType, Chip as OldChipInterface } from '../chip-set/chip.types';
import { Image } from '../../global/shared-types/image.types';
import { isEmpty } from 'lodash-es';

interface ChipInterface extends Omit<OldChipInterface, 'id' | 'badge'> {
/**
Expand Down Expand Up @@ -72,6 +74,7 @@ interface ChipInterface extends Omit<OldChipInterface, 'id' | 'badge'> {
* @exampleComponent limel-example-chip-button
* @exampleComponent limel-example-chip-link
* @exampleComponent limel-example-chip-icon-colors
* @exampleComponent limel-example-chip-image
* @exampleComponent limel-example-chip-badge
* @exampleComponent limel-example-chip-filter
* @exampleComponent limel-example-chip-removable
Expand Down Expand Up @@ -104,6 +107,12 @@ export class Chip implements ChipInterface {
@Prop()
public icon?: string | Icon;

/**
* A picture to be displayed instead of the icon on the chip.
*/
@Prop()
public image?: Image;

/**
* If supplied, the chip will become a clickable link.
*/
Expand Down Expand Up @@ -216,7 +225,7 @@ export class Chip implements ChipInterface {
onKeyDown={this.handleDeleteKeyDown}
>
{this.renderSpinner()}
{this.renderIcon()}
{this.renderPicture()}
{this.renderLabel()}
{this.renderBadge()}
{this.renderProgressBar()}
Expand All @@ -238,7 +247,7 @@ export class Chip implements ChipInterface {
onKeyDown={this.handleDeleteKeyDown}
>
{this.renderSpinner()}
{this.renderIcon()}
{this.renderPicture()}
{this.renderLabel()}
{this.renderBadge()}
{this.renderProgressBar()}
Expand All @@ -251,13 +260,19 @@ export class Chip implements ChipInterface {
return <span class="text">{this.text}</span>;
};

private renderIcon() {
private renderPicture() {
const icon = getIconName(this.icon);

if (!icon) {
if (!icon && !this.image) {
return;
}

if (!isEmpty(this.image)) {
return (
<img src={this.image.src} alt={this.image.alt} loading="lazy" />
);
}

return (
<limel-icon
badge={true}
Expand Down
29 changes: 29 additions & 0 deletions src/components/chip/examples/chip-image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Component, h } from '@stencil/core';

/**
* Picture instead of icon
* Using the `Img` interface, you can specify an image to be displayed on the chip.
*
* :::note
* The specified image will be displayed instead of the icon, if both are provided.
* :::
*/
@Component({
tag: 'limel-example-chip-image',
shadow: true,
})
export class ChipImageExample {
public render() {
const icon = {
name: 'filled_star',
color: 'rgb(var(--color-yellow-default))',
};

const image = {
src: 'https://lundalogik.github.io/lime-elements/780af2a6-d3d1-4593-8642-f03210d09271.png',
alt: 'A picture of Lucy Chyzhova, UX designer at Lime Technologies',
};

return <limel-chip text="Lucy Chyzhova" icon={icon} image={image} />;
}
}

0 comments on commit cfc0c1d

Please sign in to comment.