avataaars-generator
repository·master·Indexed 25 days ago
https://github.com/fangpenlin/avataaars-generatorA web-based application for creating custom avatars based on designs by Pablo Stanley. Version 0.1.0 provides a standalone web interface and React components, including AvatarForm for customization, ComponentCode for generating img tags via the avataaars.io API, and a Renderer component for displaying avatars. It supports both client-side and server-side rendering modes and utilizes react-url-query to synchronize avatar configurations with URL query parameters.
What's inside avataaars-generator
- If you want to integrate Avataaars into a React project instead of using the web generator, use the dedicated React component available in a separate repository: https://github.com/fangpenlin/avataaars
Run the Avataaars generator server locally
masterTo run the web-based avatar generator application locally for development purposes, use the following command in your terminal:
yarn startUse the Avataaars Generator in Server Rendering Mode
masterThe Avataaars generator supports a server-side rendering (SSR) mode. When the URL contains the query parameter__render__=1, the application renders theRenderercomponent directly into thedocument.bodyinstead of the standard#rootelement. This mode is intended for environments where the fullAppshell is not required and only the avatar rendering logic is needed.Use ComponentCode to generate img tag code
masterTheComponentCodecomponent generates an HTML<img>tag string that points to theavataaars.ioAPI. It uses the currentoptionContextto collect all selected avatar options and appends them as query parameters to the URL. This allows you to embed the generated avatar as an SVG image in any web application.Use the Renderer component to display an Avatar
masterThe
Renderercomponent is a high-level React component used to render anAvatarbased on provided props. It is wrapped withaddUrlPropsto automatically sync avatar configuration with URL query parameters.To use it, provide the following props:
avatarStyle: AnAvatarStyleenum value (defaults toAvatarStyle.Circle).onChangeUrlQueryParams: A callback function triggered when URL parameters change. Signature:(params: any, updateType: string) => void.onChangeAvatarStyle: A callback function triggered when the avatar style changes. Signature:(avatarStyle: AvatarStyle) => void.
Note: The component uses
OptionContextinternally to manage avatar options.Use addUrlProps to sync component props with URL query parameters
masterTheaddUrlPropsfunction is a Higher-Order Component (HOC) from thereact-url-querymodule. It allows you to wrap a React component so that its props are automatically synchronized with the current URL query parameters. This is useful for creating components that react to changes in the URL.Use the Avataaars Generator in Client Rendering Mode
masterIn standard client-side mode (when__render__=1is absent from the URL), the application renders theAppcomponent into the HTML element with the IDroot. It also configuresreact-url-queryusing the internalhistoryobject to ensure that URL state updates are synchronized with the application state.Configure the AvatarForm component
masterTheAvatarFormcomponent provides a UI for customizing avatars. It requiresavatarStyleandoptionContextprops to manage the state of avatar attributes. You can also provide callback functions to handle downloads, style changes, and visibility toggles for code or image previews.Configure URL query behavior with configureUrlQuery
masterUseconfigureUrlQueryto set global configuration options for how URL query parameters are handled within the application.Reference UrlUpdateTypes for URL update strategies
masterWhen updating the URL viareact-url-query, you can specify different update strategies using the constants defined inUrlUpdateTypes.AvatarForm Props Reference
masterThe following props are available for the
AvatarFormcomponent:avatarStyle: The currentAvatarStyle(e.g.,AvatarStyle.CircleorAvatarStyle.Transparent).optionContext: AnOptionContextobject used to manage and retrieve the state of avatar options.displayingCode: Boolean indicating if the React code snippet is currently visible.displayingImg: Boolean indicating if the<img>tag preview is currently visible.onDownloadPNG: Callback function triggered when the PNG download button is clicked.onDownloadSVG: Callback function triggered when the SVG download button is clicked.onAvatarStyleChange: Callback function triggered when the avatar style (Circle vs Transparent) is changed. Receives the newAvatarStyle.onToggleCode: Callback function triggered when the code visibility is toggled.onToggleImg: Callback function triggered when the image visibility is toggled.
Props for ComponentCode
masterThe
ComponentCodecomponent accepts the following prop:Prop Type Description avatarStyleAvatarStyleThe style configuration for the avatar.