-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
[Avatar] Add avatar component #1210
Merged
Merged
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a54a77f
[Avatar] Add avatar component
acomanescu e2dd254
[Avatar] Remove image src value from usage example
acomanescu 6abe27a
Merge branch 'master' into feat/avatar
mnajdova edb9578
review comments
mnajdova 34dad43
convert to rem
mnajdova c8a6a51
proptypes & docs:api
mnajdova 1a8f87b
Merge remote-tracking branch 'upstream/master' into feat/avatar
mj12albert ad48d1e
Add a test
mj12albert 98457eb
Test the delay prop
mj12albert 7a06444
Fix TS
mj12albert 416d69b
Use noop util
mj12albert cc939fc
Fix referrerPolicy
mj12albert 8c0e720
Simplify root and context
mj12albert df65f19
Fix onLoadingStatusChange missing from docs
mj12albert 33a3ab0
Set font-size on avatar fallback demo
mj12albert c6eb5e2
Fix eslint
mj12albert 83870fe
docs
mj12albert 2d75eb6
Merge branch 'master' into feat/avatar
mj12albert 8e23e1f
Merge branch 'master' into feat/avatar
mj12albert 5be8d7f
Set width and height attrs in demo
mj12albert 9bdba31
Fix style hooks
mj12albert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "AvatarFallback", | ||
"description": "Rendered when the image fails to load or when no image is provided.\nRenders a `<span>` element.", | ||
"props": { | ||
"delay": { | ||
"type": "number", | ||
"description": "How long to wait before showing the fallback. Specified in milliseconds." | ||
}, | ||
"className": { | ||
"type": "string | (state) => string", | ||
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state." | ||
}, | ||
"render": { | ||
"type": "React.ReactElement | (props, state) => React.ReactElement", | ||
"description": "Allows you to replace the component’s HTML element\nwith a different tag, or compose it with another component.\n\nAccepts a `ReactElement` or a function that returns the element to render." | ||
} | ||
}, | ||
"dataAttributes": {}, | ||
"cssVariables": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "AvatarImage", | ||
"description": "The image to be displayed in the avatar.\nRenders an `<img>` element.", | ||
"props": { | ||
"onLoadingStatusChange": { | ||
"type": "function", | ||
"description": "Callback fired when the loading status changes." | ||
}, | ||
"className": { | ||
"type": "string | (state) => string", | ||
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state." | ||
}, | ||
"render": { | ||
"type": "React.ReactElement | (props, state) => React.ReactElement", | ||
"description": "Allows you to replace the component’s HTML element\nwith a different tag, or compose it with another component.\n\nAccepts a `ReactElement` or a function that returns the element to render." | ||
} | ||
}, | ||
"dataAttributes": {}, | ||
"cssVariables": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "AvatarRoot", | ||
"description": "Displays a user's profile picture, initials, or fallback icon.\nRenders a `<span>` element.", | ||
"props": { | ||
"className": { | ||
"type": "string | (state) => string", | ||
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state." | ||
}, | ||
"render": { | ||
"type": "React.ReactElement | (props, state) => React.ReactElement", | ||
"description": "Allows you to replace the component’s HTML element\nwith a different tag, or compose it with another component.\n\nAccepts a `ReactElement` or a function that returns the element to render." | ||
} | ||
}, | ||
"dataAttributes": {}, | ||
"cssVariables": {} | ||
} |
31 changes: 31 additions & 0 deletions
31
...rc/app/(public)/(content)/react/components/avatar/demos/hero/css-modules/index.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
.Root { | ||
display: inline-flex; | ||
justify-content: center; | ||
align-items: center; | ||
vertical-align: middle; | ||
border-radius: 100%; | ||
user-select: none; | ||
font-weight: 500; | ||
color: var(--color-gray-900); | ||
background-color: var(--color-gray-100); | ||
font-size: 1rem; | ||
line-height: 1; | ||
overflow: hidden; | ||
height: 3rem; | ||
width: 3rem; | ||
} | ||
|
||
.Image { | ||
object-fit: cover; | ||
height: 100%; | ||
width: 100%; | ||
} | ||
|
||
.Fallback { | ||
align-items: center; | ||
display: flex; | ||
justify-content: center; | ||
height: 100%; | ||
width: 100%; | ||
font-size: 1rem; | ||
} |
18 changes: 18 additions & 0 deletions
18
docs/src/app/(public)/(content)/react/components/avatar/demos/hero/css-modules/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as React from 'react'; | ||
import { Avatar } from '@base-ui-components/react/avatar'; | ||
import styles from './index.module.css'; | ||
|
||
export default function ExampleAvatar() { | ||
return ( | ||
<div style={{ display: 'flex', gap: 20 }}> | ||
<Avatar.Root className={styles.Root}> | ||
<Avatar.Image | ||
src="https://images.unsplash.com/photo-1543610892-0b1f7e6d8ac1?w=128&h=128&dpr=2&q=80" | ||
className={styles.Image} | ||
/> | ||
<Avatar.Fallback className={styles.Fallback}>LT</Avatar.Fallback> | ||
</Avatar.Root> | ||
<Avatar.Root className={styles.Root}>LT</Avatar.Root> | ||
</div> | ||
); | ||
} |
3 changes: 3 additions & 0 deletions
3
docs/src/app/(public)/(content)/react/components/avatar/demos/hero/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use client'; | ||
export { default as CssModules } from './css-modules'; | ||
export { default as Tailwind } from './tailwind'; |
21 changes: 21 additions & 0 deletions
21
docs/src/app/(public)/(content)/react/components/avatar/demos/hero/tailwind/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as React from 'react'; | ||
import { Avatar } from '@base-ui-components/react/avatar'; | ||
|
||
export default function ExampleAvatar() { | ||
return ( | ||
<div style={{ display: 'flex', gap: 20 }}> | ||
<Avatar.Root className="inline-flex size-12 items-center justify-center overflow-hidden rounded-full bg-gray-100 align-middle text-base font-medium text-black select-none"> | ||
<Avatar.Image | ||
src="https://images.unsplash.com/photo-1543610892-0b1f7e6d8ac1?w=128&h=128&dpr=2&q=80" | ||
className="size-full object-cover" | ||
/> | ||
<Avatar.Fallback className="flex size-full items-center justify-center text-base"> | ||
LT | ||
</Avatar.Fallback> | ||
</Avatar.Root> | ||
<Avatar.Root className="inline-flex size-12 items-center justify-center overflow-hidden rounded-full bg-gray-100 align-middle text-base font-medium text-black select-none"> | ||
LT | ||
</Avatar.Root> | ||
</div> | ||
); | ||
} |
24 changes: 24 additions & 0 deletions
24
docs/src/app/(public)/(content)/react/components/avatar/page.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Avatar | ||
|
||
<Subtitle>An easily stylable avatar component.</Subtitle> | ||
<Meta | ||
name="description" | ||
content="A high-quality, unstyled React avatar component that is easy to customize." | ||
/> | ||
|
||
<Demo path="./demos/hero" /> | ||
|
||
## API reference | ||
|
||
Import the component and assemble its parts: | ||
|
||
```jsx title="Anatomy" | ||
import { Avatar } from '@base-ui-components/react/avatar'; | ||
|
||
<Avatar.Root> | ||
<Avatar.Image src="" /> | ||
<Avatar.Fallback>LT</Avatar.Fallback> | ||
</Avatar.Root>; | ||
``` | ||
|
||
<Reference component="Avatar" parts="Root, Image, Fallback" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/react/src/avatar/fallback/AvatarFallback.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as React from 'react'; | ||
import { Mock } from 'vitest'; | ||
import { Avatar } from '@base-ui-components/react/avatar'; | ||
import { describeConformance, createRenderer } from '#test-utils'; | ||
import { useImageLoadingStatus } from '../image/useImageLoadingStatus'; | ||
|
||
vi.mock('../image/useImageLoadingStatus'); | ||
|
||
describe('<Avatar.Fallback />', () => { | ||
const { render } = createRenderer(); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describeConformance(<Avatar.Fallback />, () => ({ | ||
render: (node) => { | ||
return render(<Avatar.Root>{node}</Avatar.Root>); | ||
}, | ||
refInstanceof: window.HTMLSpanElement, | ||
})); | ||
|
||
it('should not render the children if the image loaded', async () => { | ||
(useImageLoadingStatus as Mock).mockReturnValue('loaded'); | ||
|
||
const { queryByTestId } = await render( | ||
<Avatar.Root> | ||
<Avatar.Image /> | ||
<Avatar.Fallback data-testid="fallback" /> | ||
</Avatar.Root>, | ||
); | ||
|
||
expect(queryByTestId('fallback')).to.equal(null); | ||
}); | ||
|
||
it('should render the fallback if the image fails to load', async () => { | ||
(useImageLoadingStatus as Mock).mockReturnValue('error'); | ||
|
||
const { queryByText } = await render( | ||
<Avatar.Root> | ||
<Avatar.Image /> | ||
<Avatar.Fallback>AC</Avatar.Fallback> | ||
</Avatar.Root>, | ||
); | ||
|
||
expect(queryByText('AC')).to.not.equal(null); | ||
}); | ||
|
||
describe('prop: delay', () => { | ||
const { clock, render: renderFakeTimers } = createRenderer(); | ||
|
||
clock.withFakeTimers(); | ||
|
||
it('shows the fallback when the delay has elapsed', async () => { | ||
const { queryByText } = await renderFakeTimers( | ||
<Avatar.Root> | ||
<Avatar.Image /> | ||
<Avatar.Fallback delay={100}>AC</Avatar.Fallback> | ||
</Avatar.Root>, | ||
); | ||
|
||
expect(queryByText('AC')).to.equal(null); | ||
|
||
clock.tick(100); | ||
|
||
expect(queryByText('AC')).to.not.equal(null); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { BaseUIComponentProps } from '../../utils/types'; | ||
import { useComponentRenderer } from '../../utils/useComponentRenderer'; | ||
import { useAvatarRootContext } from '../root/AvatarRootContext'; | ||
import type { AvatarRoot } from '../root/AvatarRoot'; | ||
|
||
/** | ||
* Rendered when the image fails to load or when no image is provided. | ||
* Renders a `<span>` element. | ||
* | ||
* Documentation: [Base UI Avatar](https://base-ui.com/react/components/avatar) | ||
*/ | ||
const AvatarFallback = React.forwardRef<HTMLSpanElement, AvatarFallback.Props>( | ||
function AvatarFallback(props: AvatarFallback.Props, forwardedRef) { | ||
const { className, render, delay, ...otherProps } = props; | ||
|
||
const { imageLoadingStatus } = useAvatarRootContext(); | ||
const [delayPassed, setDelayPassed] = React.useState(delay === undefined); | ||
|
||
React.useEffect(() => { | ||
let timerId: number | undefined; | ||
|
||
if (delay !== undefined) { | ||
timerId = window.setTimeout(() => setDelayPassed(true), delay); | ||
} | ||
|
||
return () => { | ||
window.clearTimeout(timerId); | ||
}; | ||
}, [delay]); | ||
|
||
const state: AvatarRoot.State = React.useMemo( | ||
() => ({ | ||
imageLoadingStatus, | ||
}), | ||
[imageLoadingStatus], | ||
); | ||
|
||
const { renderElement } = useComponentRenderer({ | ||
render: render ?? 'span', | ||
state, | ||
className, | ||
ref: forwardedRef, | ||
extraProps: otherProps, | ||
}); | ||
|
||
const shouldRender = imageLoadingStatus !== 'loaded' && delayPassed; | ||
|
||
return shouldRender ? renderElement() : null; | ||
}, | ||
); | ||
|
||
export namespace AvatarFallback { | ||
export interface Props extends BaseUIComponentProps<'span', AvatarRoot.State> { | ||
/** | ||
* How long to wait before showing the fallback. Specified in milliseconds. | ||
*/ | ||
delay?: number; | ||
} | ||
} | ||
|
||
export { AvatarFallback }; | ||
|
||
AvatarFallback.propTypes /* remove-proptypes */ = { | ||
// ┌────────────────────────────── Warning ──────────────────────────────┐ | ||
// │ These PropTypes are generated from the TypeScript type definitions. │ | ||
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ | ||
// └─────────────────────────────────────────────────────────────────────┘ | ||
/** | ||
* @ignore | ||
*/ | ||
children: PropTypes.node, | ||
/** | ||
* CSS class applied to the element, or a function that | ||
* returns a class based on the component’s state. | ||
*/ | ||
className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]), | ||
/** | ||
* How long to wait before showing the fallback. Specified in milliseconds. | ||
*/ | ||
delay: PropTypes.number, | ||
/** | ||
* Allows you to replace the component’s HTML element | ||
* with a different tag, or compose it with another component. | ||
* | ||
* Accepts a `ReactElement` or a function that returns the element to render. | ||
*/ | ||
render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]), | ||
} as any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as React from 'react'; | ||
import { Avatar } from '@base-ui-components/react/avatar'; | ||
import { describeConformance, createRenderer } from '#test-utils'; | ||
|
||
describe('<Avatar.Image />', () => { | ||
const { render } = createRenderer(); | ||
vi.mock('./useImageLoadingStatus', () => ({ | ||
useImageLoadingStatus: () => 'loaded', | ||
})); | ||
|
||
describeConformance(<Avatar.Image />, () => ({ | ||
render: (node) => { | ||
return render(<Avatar.Root>{node}</Avatar.Root>); | ||
}, | ||
refInstanceof: window.HTMLImageElement, | ||
})); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use a test that verifies if the fallback is displayed when the image fails to load and a one testing the delay prop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 98457eb