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(Avatar): Support overriding the Avatar img element. Fixes #482 #483

Merged
merged 1 commit into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
60 changes: 60 additions & 0 deletions src/docs/pages/AvatarPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,66 @@ const AvatarPage: FC = () => {
</>
),
},
{
title: 'Override Image Element',
code: (
<div className="flex flex-wrap gap-2">
<Avatar
img={(props) => (
<img
referrerPolicy="no-referrer"
src="https://flowbite.com/docs/images/people/profile-picture-5.jpg"
{...props}
/>
)}
/>
<Avatar
img={(props) => (
<picture>
<source
media="(min-width: 900px)"
srcSet="https://flowbite.com/docs/images/people/profile-picture-3.jpg"
/>
<source
media="(min-width: 480px)"
srcSet="https://flowbite.com/docs/images/people/profile-picture-4.jpg"
/>
<img src="https://flowbite.com/docs/images/people/profile-picture-5.jpg" {...props} />
</picture>
)}
/>
</div>
),
rawCode: `<div className="flex flex-wrap gap-2">
<Avatar
img={(props) => (
<img
referrerPolicy="no-referrer"
src="https://flowbite.com/docs/images/people/profile-picture-5.jpg"
{...props}
/>
)}
/>
<Avatar
img={(props) => (
<picture>
<source
media="(min-width: 900px)"
srcSet="https://flowbite.com/docs/images/people/profile-picture-3.jpg"
/>
<source
media="(min-width: 480px)"
srcSet="https://flowbite.com/docs/images/people/profile-picture-4.jpg"
/>
<img
src="https://flowbite.com/docs/images/people/profile-picture-5.jpg"
{...props}
/>
</picture>
)}
/>
</div>`,
},
{
title: 'Placeholder',
code: (
Expand Down
44 changes: 24 additions & 20 deletions src/docs/pages/DemoPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type CodeExample = {
title: string;
content?: ReactNode;
code: ReactNode;
rawCode?: string;
showCode?: boolean;
codeClassName?: string;
codeStringifierOptions?: Options;
Expand All @@ -24,27 +25,30 @@ export type DemoPageProps = {
export const DemoPage: FC<DemoPageProps> = ({ examples }) => {
return (
<div className="mx-auto flex max-w-4xl flex-col gap-8 dark:text-white">
{examples.map(({ title, content, code, showCode = true, codeClassName, codeStringifierOptions }, index) => (
<div key={index} className="flex flex-col gap-2">
<span className="text-2xl font-bold">{title}</span>
{content && <div className="py-4">{content}</div>}
<div className={codeClassName}>
<Card>
{showCode && code}
<SyntaxHighlighter language="tsx" style={dracula}>
{reactElementToJSXString(code, {
showFunctions: true,
functionValue: (fn) => fn.name,
sortProps: false,
useBooleanShorthandSyntax: false,
useFragmentShortSyntax: false,
...codeStringifierOptions,
})}
</SyntaxHighlighter>
</Card>
{examples.map(
({ title, content, code, rawCode, showCode = true, codeClassName, codeStringifierOptions }, index) => (
<div key={index} className="flex flex-col gap-2">
<span className="text-2xl font-bold">{title}</span>
{content && <div className="py-4">{content}</div>}
<div className={codeClassName}>
<Card>
{showCode && code}
<SyntaxHighlighter language="tsx" style={dracula}>
{rawCode ||
reactElementToJSXString(code, {
showFunctions: true,
functionValue: (fn) => fn.name,
sortProps: false,
useBooleanShorthandSyntax: false,
useFragmentShortSyntax: false,
...codeStringifierOptions,
})}
</SyntaxHighlighter>
</Card>
</div>
</div>
</div>
))}
),
)}
</div>
);
};
11 changes: 11 additions & 0 deletions src/lib/components/Avatar/Avatar.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ describe('Components / Avatar', () => {
expect(initialsPlaceholder()).toHaveTextContent('RR');
});
});
describe('Image', () => {
it('should support custom image elements', () => {
render(
<Flowbite>
<Avatar img={(props) => <img referrerPolicy="no-referrer" {...props} />} />
</Flowbite>,
);

expect(img()).toHaveAttribute('referrerpolicy', 'no-referrer');
});
});
});

const img = () => screen.getByTestId('flowbite-avatar-img');
Expand Down
18 changes: 18 additions & 0 deletions src/lib/components/Avatar/Avatar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ DefaultAvatar.args = {
alt: 'Your avatar',
img: 'https://flowbite.com/docs/images/people/profile-picture-5.jpg',
};

export const CustomImage: Story<AvatarProps> = (props) => (
<>
<Avatar
{...props}
img={(props) => (
<picture>
<source media="(min-width: 900px)" srcSet="https://flowbite.com/docs/images/people/profile-picture-3.jpg" />
<source media="(min-width: 480px)" srcSet="https://flowbite.com/docs/images/people/profile-picture-4.jpg" />
<img src="https://flowbite.com/docs/images/people/profile-picture-5.jpg" {...props} />
</picture>
)}
/>
<small className="block text-center text-gray-500">Hint: Resize the viewport</small>
</>
);

CustomImage.storyName = 'Custom Image Element';
22 changes: 14 additions & 8 deletions src/lib/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import type { ComponentProps, FC, PropsWithChildren } from 'react';
import type { ComponentProps, FC, PropsWithChildren, ReactElement } from 'react';
import type { FlowbiteColors, FlowbitePositions, FlowbiteSizes } from '../Flowbite/FlowbiteTheme';
import { useTheme } from '../Flowbite/ThemeContext';
import AvatarGroup from './AvatarGroup';
Expand Down Expand Up @@ -40,10 +40,16 @@ export interface AvatarSizes extends Pick<FlowbiteSizes, 'xs' | 'sm' | 'md' | 'l
[key: string]: string;
}

export interface AvatarImageProps {
alt?: string;
className: string;
'data-testid': string;
}

export interface AvatarProps extends PropsWithChildren<Omit<ComponentProps<'div'>, 'color'>> {
alt?: string;
bordered?: boolean;
img?: string;
img?: string | ((props: AvatarImageProps) => ReactElement);
color?: keyof AvatarColors;
rounded?: boolean;
size?: keyof AvatarSizes;
Expand Down Expand Up @@ -78,16 +84,16 @@ const AvatarComponent: FC<AvatarProps> = ({
theme.size[size],
);

const imgProps = { alt, className: classNames(imgClassName, theme.img.on), 'data-testid': 'flowbite-avatar-img' };
return (
<div className={classNames(theme.base, className)} data-testid="flowbite-avatar" {...props}>
<div className="relative">
{img ? (
<img
alt={alt}
className={classNames(imgClassName, theme.img.on)}
data-testid="flowbite-avatar-img"
src={img}
/>
typeof img === 'string' ? (
<img {...imgProps} src={img} />
) : (
img(imgProps)
)
) : placeholderInitials ? (
<div
className={classNames(
Expand Down