react-nice-avatar

repository·main·Indexed 23 days ago

https://github.com/dapi-labs/react-nice-avatar

A React component library for generating customizable, stylized avatar illustrations. It features the Avatar component and a genConfig function that supports seed-based deterministic generation, manual customization, or random configuration. Users can control attributes such as face color, hair style, clothing, and container shapes (circle, rounded, or square).

Tokens
2K
Snippets
2
Records
11
Agent score
80%

What's inside react-nice-avatar

  1. Use react-nice-avatar

    main

    To use the avatar component, import Avatar and genConfig. You can generate a configuration object using genConfig and then spread that configuration into the Avatar component.

    genConfig supports three modes:

    1. Seed-based: Pass a string (like a name or email) to generate a consistent configuration.
    2. Customized: Pass an object with specific attributes to override defaults.
    3. Random: Pass no arguments to generate a random configuration.
  2. Use a seed string for deterministic avatars

    main

    To ensure a specific user or entity always gets the same avatar without storing the entire configuration object, pass a unique string to genConfig().

    The function converts the string into a hash code and uses it to pick attributes (sex, face color, hair style, etc.) from the available options. This results in a consistent, repeatable avatar for any given input string.

  3. Avatar component options

    main

    Options can be passed directly to the Avatar component as React props or provided to the genConfig function.

    Note on React Props: The following keys are exclusive to React props and cannot be used inside genConfig: id, className, style, and shape.

    | key | type | default | accept | tips |
    | -------------- | ------ | ------- | ---------------------------------------- | -------------------- |
    | `id` | string | | | Only for React Props |
    | `className` | string | | | Only for React Props |
    | `style` | object | | | Only for React Props |
    | `shape` | string | circle | circle, rounded, square | Only for React Props |
    | `sex` | string | | man, woman | |
    | `faceColor` | string | | | |
    | `earSize` | string | | small, big | |
    | `hairColor` | string | | | |
    | `hairStyle` | string | | normal, thick, mohawk, womanLong, womanShort | |
    | `hairColorRandom` | boolean | false | | thick,mohawk default only be black |
    | `hatColor` | string | | | |
    | `hatStyle` | string | | none, beanie, turban | Usually is none |
    | `eyeStyle` | string | | circle, oval, smile | |
    | `glassesStyle` | string | | none, round, square | Usually is none |
    | `noseStyle` | string | | short, long, round | |
    | `mouthStyle` | string | | laugh, smile, peace | |
    | `shirtStyle` | string | | hoody, short, polo | |
    | `shirtColor` | string | | | |
    | `bgColor` | string | | | |
    | `isGradient` | boolean | false | | |
  4. Configure the NiceAvatar component

    main

    The NiceAvatar component accepts a configuration object via its props to customize the avatar's appearance. You can specify features like sex, hairStyle, eyeStyle, and colors for various elements.

    Available configuration options include:

    • sex: 'man' | 'woman'
    • earSize: 'small' | 'big'
    • hairStyle: 'normal' | 'thick' | 'mohawk' | 'womanLong' | 'womanShort'
    • hatStyle: 'beanie' | 'turban' | 'none'
    • eyeStyle: 'circle' | 'oval' | 'smile'
    • glassesStyle: 'round' | 'square' | 'none'
    • noseStyle: 'short' | 'long' | 'round'
    • mouthStyle: 'laugh' | 'smile' | 'peace'
    • shirtStyle: 'hoody' | 'short' | 'polo'
    • eyeBrowStyle: 'up' | 'upWoman'
    • Colors: faceColor, hairColor, hatColor, shirtColor, bgColor (strings)
    • hairColorRandom: boolean
    • isGradient: boolean
    • shape: 'circle' | 'rounded' | 'square'
  5. Generate avatar configurations with genConfig()

    main

    The genConfig function is used to generate a complete configuration object for an avatar. It supports two modes of operation:

    1. Random/Manual Mode: Pass a partial configuration object. The function will fill in the missing properties by picking random values from the defaultOptions list. If a property is provided in the object, it will be used instead of a random one.
    2. Seed Mode: Pass a string instead of an object. The function will use the string as a seed to deterministically generate a configuration using a hash code. This allows you to generate the same avatar every time you use the same string.

    Note that genConfig includes internal logic to ensure visual harmony, such as avoiding certain hair colors that clash with face colors or ensuring shirt colors don't match hair/hat colors.

  6. Generate a random avatar configuration with genConfig

    main
    The genConfig function can be used to generate a complete, randomized AvatarFullConfig object. This is useful for creating diverse avatars without manually specifying every property. You can pass an existing configuration or a string to influence the generation, but it returns a Required<AvatarFullConfig> object containing all possible avatar properties.
  7. Configure ReactNiceAvatar props

    main

    The ReactNiceAvatar component accepts the following props to customize the avatar's appearance:

    PropTypeDescription
    idstringHTML ID for the container div
    classNamestringCSS class for the container div
    styleobjectInline styles for the container div
    shape"circle" | "rounded" | "square"The shape of the avatar container (default: "circle")
    sexstringThe sex of the avatar (uses defaultOptions.sex)
    faceColorstringColor of the face
    earSizestringSize of the ears (uses defaultOptions.earSize)
    hairColorstringColor of the hair
    hairStylestringStyle of the hair (uses defaultOptions.hairStyleMan or defaultOptions.hairStyleWoman)
    hairColorRandombooleanIf true, hair color is randomized (default: false)
    hatColorstringColor of the hat
    hatStylestringStyle of the hat (uses defaultOptions.hatStyle)
    eyeStylestringStyle of the eyes (uses defaultOptions.eyeStyle)
    glassesStylestringStyle of the glasses (uses defaultOptions.glassesStyle)
    noseStylestringStyle of the nose (uses defaultOptions.noseStyle)
    mouthStylestringStyle of the mouth (uses defaultOptions.mouthStyle)
    shirtStylestringStyle of the shirt (uses defaultOptions.shirtStyle)
    shirtColorstringColor of the shirt
    bgColorstringBackground color of the container
    isGradientbooleanWhether to use a gradient (internal logic)
  8. Use the ReactNiceAvatar component

    main

    The ReactNiceAvatar component is the primary entry point for generating customizable avatars. It accepts a variety of props to control the appearance of the face, hair, clothing, and background.

    Supported shape options for the container are:

    • circle (default)
    • rounded (6px border radius)
    • square (0 border radius)

    You can also enable hairColorRandom to randomize the hair color independently of the hairColor prop.