emoji-picker-element

repository·master·Indexed 23 days ago

https://github.com/nolanlawson/emoji-picker-element

A lightweight, memory-efficient emoji picker distributed as a web component. It uses IndexedDB to minimize memory consumption and supports custom emoji fonts, accessibility, and i18n by default. The library provides a <emoji-picker> custom element and a Database API for querying emoji data independently of the UI.

Tokens
8.3K
Snippets
22
Records
31
Agent score
83%

What's inside emoji-picker-element

  1. Understand the emoji rendering model

    master

    Native Emoji

    By default, emoji-picker-element renders native emoji. It does not download large sprite sheets or fonts, which avoids IP issues and ensures emojis look consistent with the user's operating system. It is limited to the emoji font installed on the user's device.

    Automatic Support Detection

    The picker automatically detects emoji support. It will only render characters that the browser/OS can display to avoid showing empty boxes or broken characters. If no color emoji are supported, an error message is displayed.

    IndexedDB Usage

    To avoid keeping large JSON files (which can be ~850kB) in memory, the picker uses IndexedDB. This allows:

    1. Avoiding constant re-parsing of JSON on every load.
    2. Keeping the main thread free by allowing data to be loaded in a web worker if desired.
  2. Optimize cache performance with ETag

    master
    For optimal performance, ensure your server exposes an ETag header for the emoji JSON file. emoji-picker-element will perform a HEAD request to check the ETag and avoid re-downloading the entire file if it hasn't changed. If ETag is not available, the picker falls back to a full GET request on every page load.
  3. Use emoji-picker-element in SSR frameworks (Next.js, SvelteKit)

    master

    The emoji-picker-element is designed for client-side rendering only and does not support Server-Side Rendering (SSR). Attempting to import it on the server will result in a requestAnimationFrame is not defined error.

    To use it in meta-frameworks, you must use client-side-only import techniques:

    • Next.js: Use next/dynamic for dynamic imports.
    • SvelteKit: Use onMount() to ensure code runs only on the client.
    • General: Use dynamic import() to lazy-load the picker when a user interacts with a button.
  4. Use a custom emoji font

    master

    By default, emoji-picker-element hides emojis that are not supported by the user's operating system or browser. To use a custom font and prevent the picker from hiding emojis, follow these steps:

    1. Set the --emoji-font-family CSS property on the emoji-picker element.
    2. Specify the maximum emoji version supported by your font using the emoji-version attribute (in HTML) or emojiVersion option (in JavaScript).

    When emoji-version is set, the picker will not attempt to detect or hide unsupported emojis.

    CSS Setup

    emoji-picker {
      --emoji-font-family: MyCustomFont;
    }

    HTML Usage

    <emoji-picker emoji-version="15.0"></emoji-picker>

    JavaScript Usage

    const picker = new Picker({
      emojiVersion: 15.0
    });
  5. Force light or dark mode

    master

    By default, the picker follows the user's system preference via prefers-color-scheme. You can override this behavior by adding the dark or light class directly to the <emoji-picker> element.

    <emoji-picker class="dark"></emoji-picker>
    <emoji-picker class="light"></emoji-picker>
  6. Optimize bundle size with tree-shaking

    master

    To avoid side effects (like the Picker automatically registering itself as a custom element) and to allow bundlers like Webpack or Rollup to tree-shake effectively, import the modules from their specific entry points:

    import Picker from 'emoji-picker-element/picker';
    import Database from 'emoji-picker-element/database';
    import Picker from 'emoji-picker-element/picker';
    import Database from 'emoji-picker-element/database';
  7. Customize emoji-picker-element with CSS variables

    master

    Since the component uses Shadow DOM, you cannot easily change its internal styles with arbitrary CSS. Instead, use the provided CSS variables to customize colors, sizes, and layout properties.

    emoji-picker {
      --num-columns: 6;
      --emoji-size: 3rem;
      --background: gray;
    }
  8. Initialize and use the Picker

    master

    You can use the Picker class to create an emoji picker instance. You can instantiate it via JavaScript and append it to the DOM, or use it as a declarative custom element in HTML.

    JavaScript usage:

    import { Picker } from 'emoji-picker-element';
    const picker = new Picker();
    document.body.appendChild(picker);

    HTML usage:

    <emoji-picker
      locale="fr"
      data-source="/fr-emoji.json"
      skin-tone-emoji="✌"
    ></emoji-picker>

    Note: Complex properties like i18n or customEmoji cannot be set via HTML attributes because they require object types.

    import { Picker } from 'emoji-picker-element';
    const picker = new Picker();
    document.body.appendChild(picker);
  9. Support focus-visible polyfill for accessibility

    master

    The component uses the :focus-visible pseudo-class for its accessibility focus ring. If you need to support browsers that do not natively support :focus-visible, you can use the focus-visible polyfill and apply it to the picker's shadowRoot.

    import 'focus-visible';
    
    const picker = new Picker();
    applyFocusVisiblePolyfill(picker.shadowRoot);
  10. Polyfill flag emojis on Windows

    master

    Windows (specifically in Chromium-based browsers) lacks support for country flag emojis. To fix this, you can use the country-flag-emoji-polyfill package. This allows the picker to use a specific font for flags while falling back to native fonts for other emojis.

    import { polyfillCountryFlagEmojis } from 'country-flag-emoji-polyfill';
    
    // emoji-picker-element will use "Twemoji Mozilla" and fall back to other fonts for non-flag emoji
    polyfillCountryFlagEmojis('Twemoji Mozilla');
  11. Host emoji data yourself using dataSource

    master

    To reduce external dependencies or host data on your own server, you can provide a custom URL to the dataSource option when initializing the Picker.

    1. Install the data package: npm install emoji-picker-element-data@^1.
    2. Host the JSON file (e.g., node_modules/emoji-picker-element-data/en/emojibase/data.json) on your web server.
    3. Pass the path to the Picker constructor.

    Note on CORS: If the JSON file is hosted on a different domain than the picker, the server must expose Access-Control-Allow-Origin: * and Access-Control-Expose-Headers: ETag (or *) to allow the picker to function correctly and use caching.

    const picker = new Picker({
      dataSource: '/path/to/my/webserver/data.json'
    });