Skip to content

Commit

Permalink
Style prop for Avatar (#3117)
Browse files Browse the repository at this point in the history
  • Loading branch information
phamduylong authored Jan 23, 2025
1 parent 38044f0 commit d57f7fa
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/twelve-pets-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@skeletonlabs/skeleton-svelte': patch
'@skeletonlabs/skeleton-react': patch
---

Implement `style` prop for Avatar component.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface AccordionItemProps extends React.PropsWithChildren, accordion.I

export interface AccordionControlProps extends React.PropsWithChildren {
/** The heading element. */
headingElement?: keyof JSX.IntrinsicElements;
headingElement?: keyof React.JSX.IntrinsicElements;
/** Set a disabled state for the item. */
disabled?: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ describe('Avatar', () => {
expect(component.getByTestId(testId)).toHaveClass(value);
});
}

it('Correctly applies the `style` prop', () => {
const value = { 'background-color': 'rgb(0, 128, 0)', opacity: 0.5 };
const component = render(<Avatar name="name" src="src" style={value}></Avatar>);
expect(component.getByTestId(testId)).toHaveStyle('background-color: rgb(0, 128, 0); opacity: 0.5;');
});
});

describe('Fallback', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/skeleton-react/src/lib/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const Avatar: React.FC<AvatarProps> = ({
// Image
imageBase = 'w-full object-cover',
imageClasses = '',
style,
// Fallback
fallbackBase = 'w-full h-full flex justify-center items-center',
fallbackClasses = '',
Expand Down Expand Up @@ -53,7 +54,7 @@ export const Avatar: React.FC<AvatarProps> = ({
srcSet={srcSet}
alt={name}
className={`${imageBase} ${imageClasses}`}
style={{ filter: filter ? `url(${filter})` : undefined }}
style={{ ...style, filter: filter ? `url(${filter})` : undefined }}
data-testid="avatar-image"
/>
)}
Expand Down
2 changes: 2 additions & 0 deletions packages/skeleton-react/src/lib/components/Avatar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface AvatarProps extends React.PropsWithChildren {
imageBase?: string;
/** Provide avatar image arbitrary CSS classes. */
imageClasses?: string;
/** Set avatar image styles. */
style?: React.CSSProperties;

// Fallback ---
/** Set base classes for the fallback element. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// Image
imageBase = 'w-full object-cover',
imageClasses = '',
style = '',
// Fallback
fallbackBase = 'w-full h-full flex justify-center items-center',
fallbackClasses = '',
Expand Down Expand Up @@ -54,6 +55,7 @@
class="{imageBase} {imageClasses}"
style:filter={filter && `url(${filter})`}
data-testid="avatar-image"
{style}
/>
{/if}
<!-- Fallback -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ describe('Avatar', () => {
expect(component).toHaveClass(value);
});
}

it('Correctly applies the `style` prop', () => {
const value = 'background-color: rgb(0, 128, 0); opacity: 0.5;';
render(Avatar, { src: test.src, name: test.name, style: value });
const component = screen.getByTestId(testIds.image);
expect(component).toHaveStyle(value);
});
});

describe('Fallback', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/skeleton-svelte/src/lib/components/Avatar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface AvatarProps {
imageBase?: string;
/** Provide avatar image arbitrary CSS classes. */
imageClasses?: string;
/** Set avatar image styles. */
style?: string;

// Fallback ---
/** Set base classes for the fallback element. */
Expand Down

0 comments on commit d57f7fa

Please sign in to comment.