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

refactor(docs): avatar dx #3951

Merged
merged 3 commits into from
Nov 7, 2024
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
14 changes: 14 additions & 0 deletions apps/docs/content/components/avatar/bordered.raw.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Avatar} from "@nextui-org/react";

export default function App() {
return (
<div className="flex gap-4 items-center">
<Avatar isBordered src="https://i.pravatar.cc/150?u=a042581f4e29026024d" />
<Avatar isBordered src="https://i.pravatar.cc/150?u=a04258a2462d826712d" />
<Avatar isBordered src="https://i.pravatar.cc/150?u=a042581f4e29026704d" />
<Avatar isBordered src="https://i.pravatar.cc/150?u=a04258114e29026302d" />
<Avatar isBordered src="https://i.pravatar.cc/150?u=a04258114e29026702d" />
<Avatar isBordered src="https://i.pravatar.cc/150?u=a04258114e29026708c" />
</div>
);
}
15 changes: 1 addition & 14 deletions apps/docs/content/components/avatar/bordered.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
const App = `import {Avatar} from "@nextui-org/react";

export default function App() {
return (
<div className="flex gap-4 items-center">
<Avatar isBordered src="https://i.pravatar.cc/150?u=a042581f4e29026024d" />
<Avatar isBordered src="https://i.pravatar.cc/150?u=a04258a2462d826712d" />
<Avatar isBordered src="https://i.pravatar.cc/150?u=a042581f4e29026704d" />
<Avatar isBordered src="https://i.pravatar.cc/150?u=a04258114e29026302d" />
<Avatar isBordered src="https://i.pravatar.cc/150?u=a04258114e29026702d" />
<Avatar isBordered src="https://i.pravatar.cc/150?u=a04258114e29026708c" />
</div>
);
}`;
import App from "./bordered.raw.jsx?raw";

const react = {
"/App.jsx": App,
Expand Down
14 changes: 14 additions & 0 deletions apps/docs/content/components/avatar/colors.raw.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Avatar} from "@nextui-org/react";

export default function App() {
return (
<div className="flex gap-4 items-center">
<Avatar isBordered color="default" src="https://i.pravatar.cc/150?u=a042581f4e29026024d" />
<Avatar isBordered color="primary" src="https://i.pravatar.cc/150?u=a04258a2462d826712d" />
<Avatar isBordered color="secondary" src="https://i.pravatar.cc/150?u=a042581f4e29026704d" />
<Avatar isBordered color="success" src="https://i.pravatar.cc/150?u=a04258114e29026302d" />
<Avatar isBordered color="warning" src="https://i.pravatar.cc/150?u=a04258114e29026702d" />
<Avatar isBordered color="danger" src="https://i.pravatar.cc/150?u=a04258114e29026708c" />
</div>
);
}
15 changes: 1 addition & 14 deletions apps/docs/content/components/avatar/colors.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
const App = `import {Avatar} from "@nextui-org/react";

export default function App() {
return (
<div className="flex gap-4 items-center">
<Avatar isBordered color="default" src="https://i.pravatar.cc/150?u=a042581f4e29026024d" />
<Avatar isBordered color="primary" src="https://i.pravatar.cc/150?u=a04258a2462d826712d" />
<Avatar isBordered color="secondary" src="https://i.pravatar.cc/150?u=a042581f4e29026704d" />
<Avatar isBordered color="success" src="https://i.pravatar.cc/150?u=a04258114e29026302d" />
<Avatar isBordered color="warning" src="https://i.pravatar.cc/150?u=a04258114e29026702d" />
<Avatar isBordered color="danger" src="https://i.pravatar.cc/150?u=a04258114e29026708c" />
</div>
);
}`;
import App from "./colors.raw.jsx?raw";

const react = {
"/App.jsx": App,
Expand Down
41 changes: 41 additions & 0 deletions apps/docs/content/components/avatar/custom-fallback.raw.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {Avatar} from "@nextui-org/react";

export const CameraIcon = ({fill, size, height, width, ...props}) => {
return (
<svg
fill="none"
height={size || height || 24}
viewBox="0 0 24 24"
width={size || width || 24}
xmlns="http://www.w3.org/2000/svg"
{...props}
>
<path
clipRule="evenodd"
d="M17.44 6.236c.04.07.11.12.2.12 2.4 0 4.36 1.958 4.36 4.355v5.934A4.368 4.368 0 0117.64 21H6.36A4.361 4.361 0 012 16.645V10.71a4.361 4.361 0 014.36-4.355c.08 0 .16-.04.19-.12l.06-.12.106-.222a97.79 97.79 0 01.714-1.486C7.89 3.51 8.67 3.01 9.64 3h4.71c.97.01 1.76.51 2.22 1.408.157.315.397.822.629 1.31l.141.299.1.22zm-.73 3.836c0 .5.4.9.9.9s.91-.4.91-.9-.41-.909-.91-.909-.9.41-.9.91zm-6.44 1.548c.47-.47 1.08-.719 1.73-.719.65 0 1.26.25 1.72.71.46.459.71 1.068.71 1.717A2.438 2.438 0 0112 15.756c-.65 0-1.26-.25-1.72-.71a2.408 2.408 0 01-.71-1.717v-.01c-.01-.63.24-1.24.7-1.699zm4.5 4.485a3.91 3.91 0 01-2.77 1.15 3.921 3.921 0 01-3.93-3.926 3.865 3.865 0 011.14-2.767A3.921 3.921 0 0112 9.402c1.05 0 2.04.41 2.78 1.15.74.749 1.15 1.738 1.15 2.777a3.958 3.958 0 01-1.16 2.776z"
fill={fill}
fillRule="evenodd"
/>
</svg>
);
};

export default function App() {
return (
<div className="flex gap-4 items-center">
<Avatar
showFallback
fallback={
<CameraIcon
className="animate-pulse w-6 h-6 text-default-500"
fill="currentColor"
size={20}
/>
}
src="https://images.unsplash.com/broken"
/>
<Avatar showFallback name="Jane" src="https://images.unsplash.com/broken" />
<Avatar name="Joe" src="https://images.unsplash.com/broken" />
</div>
);
}
36 changes: 1 addition & 35 deletions apps/docs/content/components/avatar/custom-fallback.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,7 @@
const CameraIcon = `export const CameraIcon = ({fill, size, height, width, ...props}) => {
return (
<svg
fill="none"
height={size || height || 24}
viewBox="0 0 24 24"
width={size || width || 24}
xmlns="http://www.w3.org/2000/svg"
{...props}
>
<path
clipRule="evenodd"
d="M17.44 6.236c.04.07.11.12.2.12 2.4 0 4.36 1.958 4.36 4.355v5.934A4.368 4.368 0 0117.64 21H6.36A4.361 4.361 0 012 16.645V10.71a4.361 4.361 0 014.36-4.355c.08 0 .16-.04.19-.12l.06-.12.106-.222a97.79 97.79 0 01.714-1.486C7.89 3.51 8.67 3.01 9.64 3h4.71c.97.01 1.76.51 2.22 1.408.157.315.397.822.629 1.31l.141.299.1.22zm-.73 3.836c0 .5.4.9.9.9s.91-.4.91-.9-.41-.909-.91-.909-.9.41-.9.91zm-6.44 1.548c.47-.47 1.08-.719 1.73-.719.65 0 1.26.25 1.72.71.46.459.71 1.068.71 1.717A2.438 2.438 0 0112 15.756c-.65 0-1.26-.25-1.72-.71a2.408 2.408 0 01-.71-1.717v-.01c-.01-.63.24-1.24.7-1.699zm4.5 4.485a3.91 3.91 0 01-2.77 1.15 3.921 3.921 0 01-3.93-3.926 3.865 3.865 0 011.14-2.767A3.921 3.921 0 0112 9.402c1.05 0 2.04.41 2.78 1.15.74.749 1.15 1.738 1.15 2.777a3.958 3.958 0 01-1.16 2.776z"
fill='currentColor'
fillRule="evenodd"
/>
</svg>
);
};`;

const App = `import {Avatar} from "@nextui-org/react";
import {CameraIcon} from './CameraIcon';

export default function App() {
return (
<div className="flex gap-4 items-center">
<Avatar showFallback src='https://images.unsplash.com/broken' fallback={
<CameraIcon className="animate-pulse w-6 h-6 text-default-500" fill="currentColor" size={20} />
} />
<Avatar showFallback name='Jane' src='https://images.unsplash.com/broken' />
<Avatar name='Joe' src='https://images.unsplash.com/broken' />
</div>
);
}`;
import App from "./custom-fallback.raw.jsx?raw";

const react = {
"/App.jsx": App,
"/CameraIcon.jsx": CameraIcon,
};

export default {
Expand Down
60 changes: 60 additions & 0 deletions apps/docs/content/components/avatar/custom-impl.raw.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {forwardRef, useMemo} from "react";
import {AvatarIcon, useAvatar} from "@nextui-org/react";

const MyAvatar = forwardRef((props, ref) => {
const {
src,
icon = <AvatarIcon />,
alt,
classNames,
slots,
name,
showFallback,
fallback: fallbackComponent,
getInitials,
getAvatarProps,
getImageProps,
} = useAvatar({
ref,
...props,
});

const fallback = useMemo(() => {
if (!showFallback && src) return null;

const ariaLabel = alt || name || "avatar";

if (fallbackComponent) {
return (
<div
aria-label={ariaLabel}
className={slots.fallback({class: classNames?.fallback})}
role="img"
>
{fallbackComponent}
</div>
);
}

return name ? (
<span aria-label={ariaLabel} className={slots.name({class: classNames?.name})} role="img">
{getInitials(name)}
</span>
) : (
<span aria-label={ariaLabel} className={slots.icon({class: classNames?.icon})} role="img">
{icon}
</span>
);
}, [showFallback, src, fallbackComponent, name, classNames]);

return (
<div {...getAvatarProps()}>
{src && <img {...getImageProps()} alt={alt} />}
{fallback}
</div>
);
});

MyAvatar.displayName = "MyAvatar";

export default MyAvatar;
62 changes: 62 additions & 0 deletions apps/docs/content/components/avatar/custom-impl.raw.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {forwardRef, useMemo} from "react";
import {AvatarIcon, useAvatar, AvatarProps as BaseAvatarProps} from "@nextui-org/react";

export interface AvatarProps extends BaseAvatarProps {}

const MyAvatar = forwardRef<HTMLSpanElement, AvatarProps>((props, ref) => {
const {
src,
icon = <AvatarIcon />,
alt,
classNames,
slots,
name,
showFallback,
fallback: fallbackComponent,
getInitials,
getAvatarProps,
getImageProps,
} = useAvatar({
ref,
...props,
});

const fallback = useMemo(() => {
if (!showFallback && src) return null;

const ariaLabel = alt || name || "avatar";

if (fallbackComponent) {
return (
<div
aria-label={ariaLabel}
className={slots.fallback({class: classNames?.fallback})}
role="img"
>
{fallbackComponent}
</div>
);
}

return name ? (
<span aria-label={ariaLabel} className={slots.name({class: classNames?.name})} role="img">
{getInitials(name)}
</span>
) : (
<span aria-label={ariaLabel} className={slots.icon({class: classNames?.icon})} role="img">
{icon}
</span>
);
}, [showFallback, src, fallbackComponent, name, classNames]);

return (
<div {...getAvatarProps()}>
{src && <img {...getImageProps()} alt={alt} />}
{fallback}
</div>
);
});

MyAvatar.displayName = "MyAvatar";

export default MyAvatar;
Loading