Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Tag): allow custom tag color #724

Merged
merged 8 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/beeq/src/components/tag/_storybook/bq-tag.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const meta: Meta = {
disabled: { control: 'boolean' },
hidden: { control: 'boolean' },
removable: { control: 'boolean' },
'custom-color': { control: 'text' },
selected: { control: 'boolean' },
size: { control: 'select', options: [...TAG_SIZE] },
variant: { control: 'select', options: [...TAG_VARIANT] },
Expand All @@ -37,6 +38,7 @@ const meta: Meta = {
border: undefined,
clickable: false,
color: undefined,
'custom-color': undefined,
disabled: false,
hidden: false,
removable: false,
Expand All @@ -54,6 +56,7 @@ const Template = (args: Args) => html`
border=${ifDefined(args.border)}
?clickable=${args.clickable}
color=${ifDefined(args.color)}
custom-color=${ifDefined(args['custom-color'])}
Cata1989 marked this conversation as resolved.
Show resolved Hide resolved
?disabled=${args.disabled}
?hidden=${args.hidden}
?removable=${args.removable}
Expand Down Expand Up @@ -144,6 +147,23 @@ export const ColorOutline: Story = {
},
};

export const CustomColor: Story = {
name: 'Color - custom',
render: (args: Args) => html`
<div class="flex gap-s">
<!-- Hex custom color -->
${Template({ ...args, color: '#FF5733', text: 'HEX', icon: 'yin-yang' })}
<!-- Rgba custom color -->
${Template({ ...args, color: 'rgba(255, 87, 51, 0.7)', text: 'RGBA', icon: 'rss' })}
<!-- Theme custom color palette -->
${Template({ ...args, color: 'data--sky', text: 'Theme Palette', icon: 'palette' })}
</div>
`,
args: {
variant: 'outline',
},
};

export const RemovableFilled: Story = {
name: 'Removable - filled',
render: (args: Args) => html`
Expand Down
17 changes: 12 additions & 5 deletions packages/beeq/src/components/tag/bq-tag.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Component, Element, Event, EventEmitter, h, Host, Method, Prop, State, Watch } from '@stencil/core';

import { TAG_SIZE, TAG_VARIANT, TTagBorderRadius, TTagColor, TTagSize, TTagVariant } from './bq-tag.types';
import { TAG_COLOR, TAG_SIZE, TAG_VARIANT, TTagBorderRadius, TTagColor, TTagSize, TTagVariant } from './bq-tag.types';
import { iconSize, textColor } from './helper';
import { hasSlotContent, validatePropValue } from '../../shared/utils';
import { getColorCSSVariable, hasSlotContent, validatePropValue } from '../../shared/utils';

/**
* @part wrapper - The wrapper container `<div>` of the element inside the shadow DOM.
Expand Down Expand Up @@ -174,7 +174,7 @@ export class BqTag {
};

private get isClickable(): boolean {
return this.clickable && !this.color && !this.removable;
return this.clickable && !this.color && !this.hasCustomColor && !this.removable;
}

private get isRemovable(): boolean {
Expand All @@ -185,6 +185,10 @@ export class BqTag {
return this.isRemovable && this.hidden;
}

private get hasCustomColor(): boolean {
return this.color !== undefined ? !TAG_COLOR.includes(this.color) : false;
}

// render() function
// Always the last one in the class.
// ===================================
Expand All @@ -193,13 +197,16 @@ export class BqTag {
const style = {
'--bq-tag--icon-prefix-size': `${iconSize(this.size)}px`,
...(this.border && { '--bq-tag--border-radius': `var(--bq-radius--${this.border})` }),
...(this.color && { '--bq-tag--background-color': getColorCSSVariable(this.color) ?? this.color }),
...(this.hasCustomColor && { '--bq-text--primary': `var(--bq-text--primary-alt)` }),
};

return (
<Host style={style} aria-hidden={this.isHidden ? 'true' : 'false'} hidden={this.isHidden ? 'true' : 'false'}>
<button
class={{
[`bq-tag bq-tag__${this.size} bq-tag__${this.color || 'default'} bq-tag__${this.variant}`]: true,
[`bq-tag bq-tag__${this.size}`]: true,
[`bq-tag__${this.color || 'default'} bq-tag__${this.variant}`]: !this.hasCustomColor,
'is-clickable': this.isClickable,
'is-removable': this.removable,
// Active/Selected state when clickable
Expand Down Expand Up @@ -236,7 +243,7 @@ export class BqTag {
<bq-icon
size={iconSize(this.size)}
name="x-circle"
color={this.color ? textColor(this.color)[this.variant] : 'text--primary'}
color={this.color && !this.hasCustomColor ? textColor(this.color)[this.variant] : 'text--primary'}
/>
</bq-button>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/beeq/src/components/tag/bq-tag.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const TAG_SIZE = ['xsmall', 'small', 'medium'] as const;
export type TTagSize = (typeof TAG_SIZE)[number];

export const TAG_COLOR = ['error', 'gray', 'info', 'success', 'warning'] as const;
export type TTagColor = (typeof TAG_COLOR)[number];
export type TTagColor = (typeof TAG_COLOR)[number] | (string & object);

export const TAG_VARIANT = ['outline', 'filled'] as const;
export type TTagVariant = (typeof TAG_VARIANT)[number];
Expand Down
6 changes: 5 additions & 1 deletion packages/beeq/src/shared/utils/cssVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const CSSVALUEPREFIX = '--bq-';
* @return {string} The correspongin CSS custom property variable
Cata1989 marked this conversation as resolved.
Show resolved Hide resolved
*/
export function getColorCSSVariable(colorName: string): string {
return `var(${CSSVALUEPREFIX}${colorName})`;
const token = `${CSSVALUEPREFIX}${colorName}`;
const value = getComputedStyle(document.documentElement).getPropertyValue(token);
if (!value) return undefined;

return `var(${token})`;
}

/**
Expand Down