Skip to content

Commit

Permalink
feat: avatar component
Browse files Browse the repository at this point in the history
  • Loading branch information
mogusbi-motech committed Feb 17, 2023
1 parent 2a90a64 commit bf00f3e
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 4 deletions.
14 changes: 13 additions & 1 deletion packages/project-tailwind/src/components/Alert.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Transition } from '@headlessui/react';
import { XMarkIcon } from '@heroicons/react/24/solid';
import { Fragment, ReactNode, useCallback, useEffect, useState } from 'react';
import {
Fragment,
ReactNode,
Ref,
useCallback,
useEffect,
useState,
} from 'react';
import {
Sizing,
Themes,
Expand All @@ -11,6 +18,9 @@ import {

/** Alert component properties */
export interface IAlertProps {
/** Dismiss button reference */
buttonRef?: Ref<HTMLButtonElement>;

/** Sets whether alert can be dismissed or a timer, in milliseconds, to automatically dismiss */
dismissable?: boolean | number;

Expand Down Expand Up @@ -45,6 +55,7 @@ const DISMISS_TEXT = 'Dismiss';
*/
export function Alert(props: IAlertProps) {
const {
buttonRef,
dismissable = false,
dismissText = DISMISS_TEXT,
icon,
Expand Down Expand Up @@ -167,6 +178,7 @@ export function Alert(props: IAlertProps) {
{dismissable && (
<button
className={buttonStyles}
ref={buttonRef}
type="button"
onClick={dismiss}
>
Expand Down
23 changes: 23 additions & 0 deletions packages/project-tailwind/src/components/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentPropsWithRef } from 'react';

/** Avatar component properties */
export interface IAvatarProps extends ComponentPropsWithRef<'img'> {
/** Alternative information text */
alt: string;

/** Image path */
src: string;
}

/**
* Display user avatar photo
*
* @param props - Component props
*
* @returns Avatar component
*/
export function Avatar(props: IAvatarProps) {
const { alt, className = 'h-8 w-8', src, ...rest } = props;

return <img {...{ alt, className, src }} {...rest} />;
}
4 changes: 2 additions & 2 deletions packages/project-tailwind/src/components/Logo.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentPropsWithoutRef } from 'react';
import { ComponentPropsWithRef } from 'react';

/** Logo component properties */
export interface ILogoProps extends ComponentPropsWithoutRef<'svg'> {
export interface ILogoProps extends ComponentPropsWithRef<'svg'> {
/** Alternative information text */
alt: string;
}
Expand Down
27 changes: 27 additions & 0 deletions packages/project-tailwind/src/components/__tests__/Avatar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { render } from '@testing-library/react';
import { Avatar } from '../Avatar';

describe('Avatar', () => {
it('should correctly render component when no styles are set', () => {
const { asFragment } = render(
<Avatar
alt="Alt text"
src="https://www.gravatar.com/avatar/8801091e665fdac669daa63d32167b7b"
/>,
);

expect(asFragment()).toMatchSnapshot();
});

it('should correctly render component when custom styles are set', () => {
const { asFragment } = render(
<Avatar
className="h-auto w-auto"
alt="Alt text"
src="https://www.gravatar.com/avatar/8801091e665fdac669daa63d32167b7b"
/>,
);

expect(asFragment()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Avatar should correctly render component when custom styles are set 1`] = `
<DocumentFragment>
<img
alt="Alt text"
class="h-auto w-auto"
src="https://www.gravatar.com/avatar/8801091e665fdac669daa63d32167b7b"
/>
</DocumentFragment>
`;

exports[`Avatar should correctly render component when no styles are set 1`] = `
<DocumentFragment>
<img
alt="Alt text"
class="h-8 w-8"
src="https://www.gravatar.com/avatar/8801091e665fdac669daa63d32167b7b"
/>
</DocumentFragment>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Logo should apply native svg attributes 1`] = `
<DocumentFragment>
<svg
aria-labelledby="logo-alt-text"
class="w-40 h-40 text-blue-600"
class="h-40 w-40 text-blue-600"
viewBox="0 0 345.21 398.61"
xmlns="http://www.w3.org/2000/svg"
>
Expand Down
2 changes: 2 additions & 0 deletions packages/project-tailwind/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import './styles/tailwind.css';

/** Components */
export * from './components/Alert';
export * from './components/Avatar';
export * from './components/Logo';
17 changes: 17 additions & 0 deletions packages/project-tailwind/src/stories/Avatar.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ComponentMeta, ComponentStory } from '@storybook/react';
import { Avatar } from '../components/Avatar';

export default {
args: {
alt: 'My avatar',
className: 'h-40 w-40',
src: 'https://www.gravatar.com/avatar/8801091e665fdac669daa63d32167b7b',
},
component: Avatar,
} as ComponentMeta<typeof Avatar>;

const Template: ComponentStory<typeof Avatar> = (props) => (
<Avatar {...props} />
);

export const AvatarComponent = Template.bind({});

0 comments on commit bf00f3e

Please sign in to comment.