Emoji Picker

repository·master·Indexed 43 days ago

https://github.com/ferrucc-io/emoji-picker

A highly composable and customizable React emoji picker component styled with Tailwind CSS. It features a virtualized list for performance, accessibility support, and integration for custom image-based emojis. The library provides a set of sub-components including Header, Input, Group, List, Preview, and SkinTone to build a flexible UI, with support for custom sections and frequently used emojis.

Tokens
4.2K
Snippets
15
Records
20
Agent score
54%

What's inside Emoji Picker

  1. Add Custom Emojis and Frequently Used Emojis

    master

    You can extend the picker with image-based emojis (similar to Slack) using customSections and pin specific emojis to the top using frequentlyUsedEmojis.

    Note on selection values:

    • Standard emojis return the emoji character (e.g., "👍").
    • Custom emojis return the name wrapped in colons (e.g., ":mic-drop:").
    import { EmojiPicker } from '@ferrucc-io/emoji-picker';
    import type { CustomEmoji, CustomSection } from '@ferrucc-io/emoji-picker';
    
    const customSections: CustomSection[] = [
      {
        id: 'custom',
        name: 'Custom',
        priority: 1, // Lower values appear first (defaults to 999)
        emojis: [
          { id: 'mic-drop', name: 'mic-drop', imageUrl: '/custom-emojis/mic-drop.gif' },
          { id: 'shipitparrot', name: 'shipitparrot', imageUrl: '/custom-emojis/shipitparrot.gif' },
        ],
      },
    ];
    
    const frequentlyUsedEmojis: (string | CustomEmoji)[] = [
      '👍',
      '❤️',
      { id: 'thisisfine', name: 'thisisfine', imageUrl: '/custom-emojis/thisisfine.gif' },
    ];
    
    <EmojiPicker
      customSections={customSections}
      frequentlyUsedEmojis={frequentlyUsedEmojis}
      onEmojiSelect={(emoji) => {
        console.log(emoji);
      }}
    >
      {/* ... */}
    </EmojiPicker>
  2. Install @ferrucc-io/emoji-picker

    master

    The emoji picker requires React and Tailwind CSS to function. Install the package using your preferred package manager:

    bun add @ferrucc-io/emoji-picker
    # or
    yarn add @ferrucc-io/emoji-picker
    # or
    npm i @ferrucc-io/emoji-picker
    # or
    pnpm add @ferrucc-io/emoji-picker
  3. Configure Tailwind CSS for Emoji Picker

    master

    Because the component uses Tailwind CSS, you must ensure your Tailwind configuration includes the component's styles.

    For Tailwind v4.x: Add the @source directive to your CSS file containing your Tailwind configuration.

    For Tailwind v3.x: Add the component's distribution path to the content array in your tailwind.config.ts.

    // If you're using Tailwind v4.x
    // In your CSS file containing your tailwind configuration
    @source "../node_modules/@ferrucc-io/emoji-picker";
    
    // If you're using Tailwind v3.x
    // In your tailwind.config.ts
    "content": [
      // Keep the existing content array
      // Add this line:
      "./node_modules/@ferrucc-io/emoji-picker/dist/**/*.{js,jsx,ts,tsx}",
    ],
  4. Implement Slack Style Emoji Picker

    master

    To achieve a Slack-like appearance, use the EmojiPicker.Preview component to show the selected emoji and the EmojiPicker.SkinTone component for skin tone selection.

    <EmojiPicker 
      className="font-['Lato'] w-[380px] border-none"
      emojisPerRow={9}
      emojiSize={36}
    >
      <EmojiPicker.Header>
        <EmojiPicker.Input 
          placeholder="Search all emoji" 
          className="h-[36px] bg-white dark:bg-zinc-800 border border-zinc-300 dark:border-zinc-600 w-full rounded-[8px] text-[15px] focus:shadow-[0_0_0_1px_#1d9bd1,0_0_0_6px_rgba(29,155,209,0.3)] dark:focus:shadow-[0_0_0_1px_#1d9bd1,0_0_0_6px_rgba(29,155,209,0.3)] focus:border-transparent focus:outline-none mb-1"
          hideIcon
        />
      </EmojiPicker.Header>
      <EmojiPicker.Group>
        <EmojiPicker.List containerHeight={320} />          
      </EmojiPicker.Group>
      <EmojiPicker.Preview>
        {({ previewedEmoji }) => (
          <>
            {previewedEmoji ? 
              <EmojiPicker.Content />
              : 
              <button>Add Emoji</button>
            }
            <EmojiPicker.SkinTone />
          </>
        )}
      </EmojiPicker.Preview>
    </EmojiPicker>
  5. Basic Usage of EmojiPicker

    master

    The EmojiPicker is a composable component. You build the UI by nesting sub-components like Header, Input, Group, and List.

    <EmojiPicker onEmojiSelect={handleEmojiSelect}>
      <EmojiPicker.Header>
        <EmojiPicker.Input placeholder="Search emoji" />
      </EmojiPicker.Header>
      <EmojiPicker.Group>
        <EmojiPicker.List />
      </EmojiPicker.Group>
    </EmojiPicker>
  6. Customize Section Headers with renderHeader

    master

    The renderHeader prop allows you to take full control over how section headers are rendered. It provides content (the header text/element) and isSticky (boolean indicating if the header is currently stuck to the top).

    import type { HeaderRendererProps } from '@ferrucc-io/emoji-picker';
    
    function MyHeader({ content, isSticky }: HeaderRendererProps) {
      return (
        <div className={isSticky ? 'bg-white/95 backdrop-blur-sm' : 'bg-transparent'}>
          {content}
        </div>
      );
    }
    
    <EmojiPicker renderHeader={MyHeader}>
      {/* ... */}
    </EmojiPicker>
  7. Custom Types: CustomEmoji and CustomSection

    master

    Use these interfaces to define your custom emoji data structures.

    interface CustomEmoji {
      id: string;
      name: string;
      imageUrl: string;
      category?: string;
    }
    
    interface CustomSection {
      id: string;
      name: string;
      emojis: (EmojiMetadata | CustomEmoji)[];
      priority?: number; // Lower values appear first (defaults to 999)
    }
  8. EmojiPickerProps Reference

    master

    The base EmojiPicker component accepts the following props for customization:

    interface EmojiPickerProps {
      emojisPerRow?: number;      // Number of emojis per row
      emojiSize?: number;         // Size of each emoji in pixels
      containerHeight?: number;    // Height of the emoji container
      maxUnicodeVersion?: number; // Maximum Unicode version to include (defaults to 16.0). Hides unsupported emojis.
      onEmojiSelect?: (emoji: string) => void; // Callback when emoji is selected
      customSections?: CustomSection[]; // Custom image-based emoji sections
      frequentlyUsedEmojis?: (string | CustomEmoji)[]; // Emojis to show in a "Frequently Used" section at the top
      renderHeader?: (props: HeaderRendererProps) => React.ReactNode; // Custom renderer for section headers
    }
  9. Configure EmojiPickerPreview props

    master

    The EmojiPickerPreview component accepts the following props:

    PropTypeDescription
    children(props: { previewedEmoji: EmojiMetadata | null }) => React.ReactNodeA render function that receives the currently hovered emoji metadata (with skin tone applied) or null if none is hovered.
    classNamestring (optional)Additional CSS classes to apply to the preview container.
  10. Use the EmojiPicker component

    master

    The EmojiPicker component is the primary entrypoint for the library. It provides a context-wrapped container that manages emoji state, selection, and configuration. It uses jotai internally for state management.

    To use it, wrap your desired sub-components (like EmojiPicker.Input, EmojiPicker.List, etc.) inside the EmojiPicker component. You can customize the picker's behavior via props such as onEmojiSelect, emojisPerRow, and emojiSize.

    import { EmojiPicker } from '@ferrucc-io/emoji-picker';
    
    function MyComponent() {
      return (
        <EmojiPicker 
          onEmojiSelect={(emoji) => console.log('Selected:', emoji)}
          emojisPerRow={6}
          emojiSize={24}
        >
          <EmojiPicker.Input />
          <EmojiPicker.List />
        </EmojiPicker>
      );
    }
  11. Use EmojiPicker sub-components

    master

    The EmojiPicker component exposes several static sub-components that must be used within the EmojiPicker provider to access the shared state. These include:

    • EmojiPicker.Input: The search input field.
    • EmojiPicker.Group: A container for grouping emojis.
    • EmojiPicker.List: The main list of emojis.
    • EmojiPicker.Preview: A preview area for the currently selected or hovered emoji.
    • EmojiPicker.Content: Uses EmojiPickerSmartContent for intelligent emoji rendering.
    • EmojiPicker.SkinTone: Controls or displays skin tone variations.
    • EmojiPicker.Header: A specialized header component for the picker.
  12. Configure the EmojiPickerList component

    master

    The EmojiPickerList component is a memoized component used to render the emoji selection interface, handling both category browsing and search results. It supports the following props for customization:

    • hideStickyHeader (boolean, optional): If set to true, the sticky header in the list will be hidden. Defaults to false.
    • containerHeight (number, optional): Sets the height of the emoji container. Defaults to 364.

    Note: The component automatically switches between displaying EmojiCategories when the search input is empty and EmojiSearchResults when a search term is present.

    import { EmojiPickerList } from '@ferrucc-io/emoji-picker';
    
    // Example usage with custom height and no sticky header
    <EmojiPickerList hideStickyHeader={true} containerHeight={400} />