@atlaskit/avatar
Version:
An avatar is a visual representation of a user or entity.
44 lines (42 loc) • 1.34 kB
JavaScript
import { createContext, useContext } from 'react';
/**
* __Avatar context__
*
* This allows setting the size of all avatars under a context provider.
*
* ```tsx
* <AvatarContext.Provider value={{ size: 'small' }}>
* <Avatar
* // ...props
* />
* </AvatarContext.Provider>
* ```
*/
export const AvatarContext = /*#__PURE__*/createContext(undefined);
export const useAvatarContext = () => useContext(AvatarContext);
const defaultAvatarContentProps = {
as: 'span',
appearance: 'circle',
avatarImage: null,
ref: null,
size: 'medium'
};
/**
* __Avatar content context__
*
* This context provides the props for the AvatarContent component, enabling
* consumers to compose the AvatarContent with the Avatar component.
*/
export const AvatarContentContext = /*#__PURE__*/createContext(defaultAvatarContentProps);
export const useAvatarContent = () => useContext(AvatarContentContext);
/**
* Used to ensure Avatar sub-components are used within a Avatar component,
* and provide a useful error message if not.
*/
export const EnsureIsInsideAvatarContext = /*#__PURE__*/createContext(false);
export const useEnsureIsInsideAvatar = () => {
const context = useContext(EnsureIsInsideAvatarContext);
if (!context) {
throw new Error('Avatar sub-components must be used within a Avatar component.');
}
};